beautypg.com

Zilog Z8F0130 User Manual

Page 113

background image

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