__attribute__((optimize)) and fast-math related oddities

tbp tbptbp@gmail.com
Sat Oct 17 22:47:00 GMT 2009


Hang on while i put on my flame-proof suit. There.
Merrily trying to make a test-case showing how unmanageable it is to
try to override *math* flags per function, i soon had to stop
because...
$ cat amusing.cc
#include <cmath>
static __attribute__((optimize("-fno-associative-math"))) double
foo1(double x) { return (x + pow(2, 52)) - pow(2, 52); }
static __attribute__((noinline)) double bar1(double x) { return foo1(x); }
#ifdef HUH
static __attribute__((optimize("-fno-associative-math"))) double
foo2(double x) { return (x + pow(2, 52)) - pow(2, 52); }
static __attribute__((noinline, optimize("-ffast-math"))) double
bar2(double x) { return foo2(x); }
#endif
int main() {
	double x = 1.1;
	if (bar1(x) == x) return 1;
#ifdef HUH
	if (bar2(x) == x) return 2;
#endif
	return 0;
}
$ g++-4.4 -O2 amusing.cc -ffast-math && ./a.out; echo $?
0
$ g++-4.4 -O2 amusing.cc -ffast-math -DHUH && ./a.out; echo $?
1
$ g++-4.4 -O2 amusing.cc && ./a.out; echo $?
0
$ g++-4.4 -O2 amusing.cc -DHUH && ./a.out; echo $?
1
... made even less sense than expected.
I got that like for other 'incompatible' flags, conflicting math flags
should prevent inlining, only they don't. And it's all weird. But that
one takes the cake. Could someone tell me what the fuss is about?

$ g++-4.4 -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian
4.4.1-6' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--enable-shared --enable-multiarch --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4
--program-suffix=-4.4 --enable-nls --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --enable-objc-gc
--with-arch-32=i486 --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.1 (Debian 4.4.1-6)



More information about the Gcc mailing list