Zilog Z8F0130 User Manual
Page 113

UM013037-1212
Project Menu
Zilog Developer Studio II – Z8 Encore!
User Manual
89
depend on the old behavior and might have to expend additional effort now to keep their
code working without the deprecated feature.
When the
Disable ANSI Promotions
checkbox is deselected, the compiler performs inte-
ger-type promotions when necessary so that the program’s observed behavior is as defined
by the ANSI C Standard. Integer-type promotions are conversions that occur automati-
cally when a smaller (for example, 8 bits) variable is used in an expression involving
larger (for example, 16 bits) variables. For example, when mixing chars and ints in an
expression, the compiler casts the chars into ints. Conversions of this kind are always
done, regardless of the setting of the
Disable ANSI Promotions
checkbox.
The ANSI Standard has special rules for the handling of chars (and shorts), and it is the
application of these special rules that is disabled when the checkbox is selected. The spe-
cial rules dictate that chars (both signed and unsigned) must always be promoted to ints
before being used in arithmetic or relational (such as < and ==) operations. By selecting
the
ANSI Promotions
checkbox, these rules are disregarded, and the compiler can operate
on char entities without promoting them. This can make for smaller code because the com-
piler does not have to create extra code to do the promotions and then to operate on larger
values. In making this a deprecated feature, Zilog has worked to make the compiler more
efficient at avoiding truly needless promotions so that the code size penalty for observing
the standard is negligible.
Disabling the promotions can often be a safe optimization to invoke, but this is subject to
several exceptions. One exception is when an arithmetic overflow of the smaller variable
is possible. For example, the result of adding
(char)10
to
(char) 126
does not fit
within an 8-bit
char
variable, so the result is
(char) -120
.) In such cases, you get dif-
ferent results depending on whether ANSI promotions are enabled or disabled.
If you write:
char a = 126;
char b = 10;
int i = a + b:
with ANSI promotions enabled, you get the right answer: 136. With ANSI promotions dis-
abled, you get the wrong answer:
-
120. The reason for the different result is that while in
both cases there is a conversion from
char
to
int
, the conversion is applied earlier or
later depending on this setting. With ANSI promotions enabled, the conversion is done as
soon as possible, so it occurs before the addition, and the result is correct even though it is
too large to fit into a
char
. With ANSI promotions disabled, the conversion is not done
until a larger type is explicitly called for in the code. Therefore, the addition is done with
char
s, the overflow occurs, and only after that is the result converted to
int
.
By the ANSI Standard, these special promotions are only applied to
char
s and
short
s. If
you have the analogous code with the sum of two
int
s being assigned into a
long
, the
compiler does not automatically promote the ints to longs before adding them, and if the
sum overflows the
int
size, then the result is always wrong whether ANSI promotions are
- Z8F0131 Z8F0230 Z8F0231 Z8F0430 Z8F0431 Z8F043A Z8F0830 Z8F0831 Z8F083A Z8F1232 Z8F1233 Z8F0113 Z8F011A Z8F0123 Z8F012A Z8F0213 Z8F021A Z8F0223 Z8F022A Z8F0411 Z8F0412 Z8F0413 Z8F041A Z8F0421 Z8F0422 Z8F0423 Z8F042A Z8F0811 Z8F0812 Z8F0813 Z8F081A Z8F0821 Z8F0822 Z8F0823 Z8F082A Z8F0880 Z8F1621 Z8F1622 Z8F1680 Z8F1681 Z8F1682 Z8F2421 Z8F2422 Z8F2480 Z8F3221 Z8F3222 Z8F3281 Z8F3282 Z8F4821 Z8F4822 Z8F4823 Z8F6081 Z8F6082 Z8F6421 Z8F6422 Z8F6423 Z8F6481 Z8F6482 Z8FS021A ZMOT1AHH Z8FS040B ZMOT0BHH ZMOT0BSB Z8FMC04 Z8FMC08 Z8FMC16