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

Eli Friedman eli.friedman at gmail.com
Fri Jun 26 15:23:41 PDT 2009


On Thu, Jun 25, 2009 at 7:16 AM, Duncan Sands<baldrick at free.fr> wrote:
> Hi Sebastian,
>
>> You're producing a signed overflow, which is simply undefined behavior.
>> Unsigned overflow is well-defined, even for bitfields, but signed is not.
>
> that's true, here's an unsigned version (outputs 5 + 5 = 2):
>
> #include <stdio.h>
>
> struct i3 { unsigned i:3; };
>
> int main(void) {
>   struct i3 A, B, C;
>
>   A.i = 5;
>   B.i = 5;
>   C.i = A.i + B.i;
>   printf("%d + %d = %d\n", A.i, B.i, C.i);
>   return 0;
> }

Note that you're actually doing signed arithmetic here; it just
doesn't happen to matter because "int" is much larger than the
bitfield.

-Eli




More information about the llvm-dev mailing list