[cfe-dev] [LLVMdev] LLVM frontend supporting arbitrary bit-width integral datatypes

Sebastian Redl sebastian.redl at getdesigned.at
Thu Jun 25 07:10:45 PDT 2009


Duncan Sands wrote:
> Hi Adam,
>
>   
>> One problem, I was trying to solve was, that I need to declare variables of let's say 5-bit width like  'i5 var',
>> the maximal bit-width may be limited to 64 bits. I need such variables to represent instruction's operands,
>> example is at the end this message.
>>     
>
> any standard compliant C compiler supports i5, believe it or not.
>
> Try this:
>
> #include <stdio.h>
>
> struct i3 { int i:3; };
>
> int main(void) {
>    struct i3 A, B, C;
>
>    A.i = 2;
>    B.i = 3;
>    C.i = A.i + B.i;
>    printf("%d + %d = %d\n", A.i, B.i, C.i);
>    return 0;
> }
>
> I get:
> 2 + 3 = -3
> which is correct for i3 arithmetic!
> There might be some problems with signed vs unsigned bitfields,
> depending on the compiler.
>   
You're producing a signed overflow, which is simply undefined behavior.
Unsigned overflow is well-defined, even for bitfields, but signed is not.

Sebastian



More information about the cfe-dev mailing list