Well, what is it supposed to do? Technically, according to the C standard,
anyway, this operation has undefined effects, because you are converting a
negative number ((signed long) (y - z) * 10) into an unsigned number in
order to add to "x".
As the feeble joke in the compiler community goes, the compiler is free at
this point to sing the star-spangled banner.. (or abort with a sign overflow
error, or compute a bogus result, or ...)
What you could do in this case is something along the lines of the
following:
unsigned long test (unsigned long x, signed short y, signed short z)
{
/* return x + (signed long) (y - z) * 10; */
signed long v1 = x; // loses if x >= 0x80000000
signed long v2 = (y - z) * 10;
return (unsigned long)(v1 + v2);
}
Here you are consciously choosing a signed long as an intermediate type for
the expression, and the effects are undefined if "x" doesn't fit in a signed
long. (Doesn't apply for the particular example you have above, but could
fail for other values).
Alternatively, you could choose some other express form of whatever
intermediate calculation you expect the compiler to do (and in what type -
if you pick unsigned long as the intermediate type, you will lose if z > y),
and spell it out in a similar way.
-----Original Message-----
From: Stephen Best [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 15, 2000 6:28 PM
To: Palm Developer Forum
Subject: CW6 optimizer bug?
Given the logic below, what would you expect for result?
unsigned long test (unsigned long x, signed short y, signed short z)
{
return x + (signed long) (y - z) * 10;
}
result = test (10, 0, 1);
CW5, CW6 (Optimization level 0) and VC++ 5.0 all give result = 0.
CW6 (Optimization levels 1-4) give result = 655360 (0x000a0000).
The suspect code generated is:
move.w 12(a6),d0
sub.w 14(a6),d0
mulu.w #10,d0 <---- should be muls.w?
add.l 8(a6),d0
Stephen Best
Bitware Australia
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palm.com/devzone/mailinglists.html
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html