[Top][All Lists]

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

Re: [avr-gcc-list] char to int promotion in bitwise operators

From: Joerg Wunsch
Subject: Re: [avr-gcc-list] char to int promotion in bitwise operators
Date: Fri, 21 Aug 2009 21:35:39 +0200 (MET DST)

Francisco Silva <address@hidden> wrote:

> Try the following spell:
>  ------------------
>  #define FLAG_A 0x01
>  #define FLAG_B 0x02
>  ...
> uint8_t t /* temporary dummy variable */
>  if ((t = flags & FLAG_A)) ...
>  if ((t = flags & FLAG_B)) ...
>  ------------------
> The double parenteses avoids a ggc warning.

Using a typecast looks a little better, I think.

Also, I noticed it can help to use inline functions to encapsulate
tests, as this helps the compiler finding out there are no side
effects of reducing the operation to 8 bits.

Andrew, maybe you can post some compilable code snippet to check.  I
tried creating a small piece of code myself, but GCC always compiled
that one using plain 8-bit instructions.  That all depends a little on
the context, it seems.  Technically, the compiler is required to
perform an implied promotion to 16 bits, and it is then only allowed
to reduce the operation to just 8 bits if the compiler can be entirely
sure this behaves the same as the operation with everything promoted
to `int'.  So in case of doubt, the compiler will always be on the
safe side to use 16 bits, because standard conformity must be the
primary goal, and optimization only comes next.

cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)

reply via email to

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