tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] bitfield handling


From: Vicente Bergas
Subject: Re: [Tinycc-devel] bitfield handling
Date: Sun, 30 Apr 2017 12:05:01 +0200

>Hi,
>
>On Fri, 28 Apr 2017, Vicente Bergas wrote:
>
>>For example the following does not work:
>>struct{short x:12; char y:1;}s; s.y=1;
>
>Note that the above is tricky: char can be signed and unsigned if not 
>explicitely specified, and if it's signed then a 1-bit bitfield of char only 
>contains values -1 and 0, so storing a 1 into it actually makes the value be 
>-1. So depending on what you tested that might actually have been the problem. 
>(I.e. always specify signedness of char when using it to store numeric values 
>and not characters).

There is no problem with signedness. To factor out the sign, you can
also test this:
struct{unsigned short x:12; unsigned char y:2;}s; s.y=1;
which also fails. Both fail even after applying your patch.

The problem is with the size of the type. If you look at the generated
object code, it is treating s.y as a single byte and that byte has
only bits 0 to 7, so, the bit 12 (where s.y actually resides) is just
discarded.
The patch I proposed increased the size of the type, so, the actual
important data is not discarded. This can be safely done (I think)
because the actual size of it is already taken into account in the bit
shifting and masking.

>>The attached patch improves bitfield handling.
>
>Yeah, but the bitfield layout involving char fields was actually what was 
>broken. I've fixed that in mob making the above (with 'unsigned char') and ...
>
>>There are still corner cases which are still failing, like:
>>struct{int x:31; char y:2;}s; s.y=1;
>>in which the char overflows the 32-bit type.
>
>... this work.  Thanks for the report.

This was a different bug, thanks, for fixing.

>Ciao,
>Michael.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]