tech-kern archive

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

Re: introducing flags_t



On Wed, Apr 22, 2009 at 08:20:22AM +1000, matthew green wrote:
>    
>    In many places we use 'int' for flags.
>    Also in most places, the flag is checked this way:
>    
>       if (flags & FLAGX)
>           do_something();
>    
>    This is problematic when the top-most bit is used.
> 
> 
> i'm curious what the actual problem is here.  afaict, the
> code generated for the above test is the same no matter
> the type of 'flags', on several platforms.

If FLAGX is the msb this might be (1 << 31), (1u << 31), 0x80000000 or
0x80000000u I think the compiler might object to the first - but the
other 3 are all unsigned (on a 32bit arch).

'int_var & unsigned_var' will implicitly cast the int to unsigned - which
is (at least for 2s compiliment systems) defined to copy the bit-pattern
over. So this will DTRT.

So the only plausible problem is if the compiler saturates the (1 << 31)
calculation generating ((1 << 31) - 1). I don't think gcc ever does this!

        David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index