[LLVMdev] Casting from float to unsigned char - incorrect output?
Eli Friedman
eli.friedman at gmail.com
Mon Aug 6 13:03:51 PDT 2012
On Mon, Aug 6, 2012 at 12:43 PM, ryan baird <ryanrbaird at gmail.com> wrote:
> I am compiling the following code for the MIPS architecture:
>
> unsigned char trunc(float f) {
> return (unsigned char) f;
> }
>
> and it produces the following assembly (directives removed for convenience:
> trunc:
> trunc.w.s $f0, $f12
> mfc1 $2, $f0
> jr $ra
> nop
>
> However, this does not seem to produce the correct output for negative
> numbers. When I run the following code, I get -1 instead of 255 (which is
> produced by compiling natively with gcc).
> int trunc(float c);
> int main() {
> printf("%d\n", trunc(-1.0));
> }
That code has undefined behavior; see 6.3.1.4p1 in C99
-Eli
More information about the llvm-dev
mailing list