[Icc-avr] chars should be converted to ints in
arithmeticexpressions
Rodney Pearson
rodney at junipersys.com
Tue Mar 27 11:04:33 PST 2007
In ICCV7AVR Help -> Expressions and Type Promotions it states:
"Another point of note is that the C standard says that operands are
promoted to equivalent types before the operation is done. In particular, an
integer expression must be promoted to at least int type if its type is
smaller than an int type. However, the "as-if" rule says that the promotion
does not need to physically occur if the result is the same. Our compilers
will try to optimize the byte-sized operations whenever possible. Some
expressions are more difficult to optimize, especially if they produce an
intermediate value. For example,
char *p;
...
... *p++...
The compiler may not be as optimal, since *p is a temporary value that needs
to be preserved."
It may be possible that this "difficult intermediate optimization" is
somehow (not) occuring. Richard may have to weigh-in on this one.
Cheers!
Rodney Pearson
Software Engineer
Juniper Systems, Inc.
www.junipersys.com
_____
From: icc-avr-bounces at imagecraft.com [mailto:icc-avr-bounces at imagecraft.com]
On Behalf Of Glenn Greig
Sent: Tuesday, March 27, 2007 11:14 AM
To: icc-avr at imagecraft.com
Subject: [Icc-avr] chars should be converted to ints in
arithmeticexpressions
Icc-avr v7.12 does not appear to be promoting chars to ints in arithmetic
expressions.
The following code works correctly when compiled with v7.02:
char data[8];
int volts;
readPowerSupply(data);
volts = ((data[6] << 8) + data[5]) * 14;
With Icc-avr v7.12, data[6] is not converted to int,
so (data[6] << 8) is zero, and I get the wrong answer.
I can fix it by casting:
volts = (((int)data[6] << 8) + data[5]) * 14;
but I believe that the compiler should convert to int before shifting.
K&R (section 2.7, page 39, 1978) states:
"chars and ints may be freely intermixed in arithmetic expressions:
every char in an expression is automatically converted to an int."
Since people rely on this behavior, it needs to be there
(especially since it was there in an earlier version!).
Cheers,
Glenn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dragonsgate.net/pipermail/icc-avr/attachments/20070327/485b6195/attachment.html
More information about the Icc-avr
mailing list