[llvm-dev] Is it a valid fp transformation?

Artur Pilipenko via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 20 03:25:00 PDT 2017


This C program produces different results with -O0 and -O3 optimization levels.

#include <stdio.h>
float test(unsigned int arg) {
    return (float)((int)(arg * 58)) + 1;
}
int main() {
    printf("%d\n", (int)test((unsigned int)-831710640));
}

O0 result is -994576896
O3 result is -994576832

It happens because LLVM (specifically instcombine) does the following transformation:
(float)x + 1.0 => (float)(x + 1)

For some values the expression before and after yield different results:
                  x = -994576864
         (float)x = -994576896.000000
(float)x + 1.0 = -994576896.000000
(float)(x + 1) = -994576832.000000

I’m curious if this is a correct transformation and why.

Artur



More information about the llvm-dev mailing list