Dear list,

Consider the following small program:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    double a;

    a = strtod(argv[0], NULL);
    printf("%f\n", floor(a));
    return(0);
}

When compiling this with a -march that supports the roundsd instruction, the floor() call seems to only be compiled to such an instruction if -funsafe-math-optimizations is specified.

Why is this? I notice the glibc's floor() implementation (for SSE4.1-enabled processors) consists of only this instruction, so barring a bug in glibc, that would seem to imply to me the roundsd is IEEE-compliant and safe. Why does GCC consider it unsafe?

For reference, these are the complete compile commands I'm using to test:

gcc -O -march=haswell -c -S -o test.s test.c
vs.
gcc -O -funsafe-math-optimizations -march=haswell -c -S -o test.s test.c


--
Fredrik Tolf

Reply via email to