[MLton] power pc "port"
Filip Pizlo
pizlo@purdue.edu
Sun, 5 Sep 2004 11:12:49 -0500 (EST)
> Henry and/or Stephen can probably chime in with more info, but is GCC
> really right here? Word8_neg is specifying an 8-bit negation -- a
> perfectly well defined operation which given 8 bits returns the 8-bit
> negation. While it may be the case that the implementation needs to side
> step through 32-bit operations, why can the end result look any different
> than what would happen under an 8-bit machine?
Well, here are more details according to the C standard. Consider the
Word8_neg function, which is defined as follows:
unsigned char Word8_neg(unsigned char w) {
return (- w);
}
The steps that happen are:
1) w must be promoted for the negation to be performed
2) the negation is performed
3) the result of the negation must be converted for the return, which
requires an unsigned char type.
In step (1), the unsigned char is promoted to int. See sections 6.3.1.1
(defined what promotion means) and 6.5.3.3 (defines what negation means)
of the C standard.
In step (2), the negation happens; this is nothing special. Note that the
value produced by the negation is still an int.
In step (3), a conversion from a signed type (specifically, int) to an
unsigned type (specifically, unsigned char) occurs. In section 6.3.1.3 of
the C standard, in paragraph 2, it states:
"Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value that can
be represented in the new type until the value is in the range of the new
type."
Note that this is exactly the same as doing:
return (- w)&0xff;
Hence, the masking that GCC is doing is quite correct with respect to the
C standard.
--
Filip Pizlo
http://bocks.psych.purdue.edu/
pizlo@purdue.edu