https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90248
Bug ID: 90248 Summary: larger than 0 compare fails with -ffinite-math-only -funsafe-math-optimizations Product: gcc Version: 8.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: kau at zurich dot ibm.com Target Milestone: --- Created attachment 46242 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46242&action=edit preprocessed minimal example file Hello, the following code produces unexpected/different results using gcc 8.2.1 when compared to gcc 7.3.1 test.cc: #include <iostream> int main() { float a = 0; std::cout << a << std::endl; a = (a > 0) ? 1.0 : -1.0; std::cout << a << std::endl; return 0; } I would expect this code to print 0 -1 with "gcc version 7.3.1 20180130 (Red Hat 7.3.1-2) (GCC)" it does. with "gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC)" it prints 0 1 This behavior depends on the combination of two compiler flags. If either one is missing, the code behaves as expected. # g++ -Wall -Wextra -ffinite-math-only -funsafe-math-optimizations test.cc -o test; ./test 0 1 # g++ -Wall -Wextra -funsafe-math-optimizations test.cc -o test; ./test 0 -1 # g++ -Wall -Wextra -ffinite-math-only test.cc -o test; ./test 0 -1 # g++ -Wall -Wextra test.cc -o test; ./test 0 -1 Note that if I replace a = (a > 0) ? 1.0 : -1.0; with if (a > 0) a = 1.0; else a = -1.0; it also works as expected regardless of the compiler version or flags. Compiler output # g++ -v -save-temps -Wall -Wextra -ffinite-math-only -funsafe-math-optimizations test.cc -o test Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-ffinite-math-only' '-funsafe-math-optimizations' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -E -quiet -v -D_GNU_SOURCE test.cc -mtune=generic -march=x86-64 -Wall -Wextra -ffinite-math-only -funsafe-math-optimizations -fpch-preprocess -o test.ii ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward /usr/lib/gcc/x86_64-redhat-linux/8/include /usr/local/include /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-ffinite-math-only' '-funsafe-math-optimizations' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -fpreprocessed test.ii -quiet -dumpbase test.cc -mtune=generic -march=x86-64 -auxbase test -Wall -Wextra -version -ffinite-math-only -funsafe-math-optimizations -o test.s GNU C++14 (GCC) version 8.2.1 20181215 (Red Hat 8.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.0.2, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C++14 (GCC) version 8.2.1 20181215 (Red Hat 8.2.1-6) (x86_64-redhat-linux) compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.0.2, isl version isl-0.16.1-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 2c6c0283460e534bb8025645f70a7cfb COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-ffinite-math-only' '-funsafe-math-optimizations' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' as -v --64 -o test.o test.s GNU assembler version 2.29.1 (x86_64-redhat-linux) using BFD version version 2.29.1-23.fc28 COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-ffinite-math-only' '-funsafe-math-optimizations' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/8/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/8/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper -plugin-opt=-fresolution=test.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/8 -L/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/8/../../.. test.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/8/crtfastmath.o /usr/lib/gcc/x86_64-redhat-linux/8/crtend.o /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crtn.o COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-ffinite-math-only' '-funsafe-math-optimizations' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' Compiler version with unexpected behavior: $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 8.3.1 20190223 (Red Hat 8.3.1-2) (GCC) Compiler version with expected behavior regardless of compiler flags or code changes: $ gcc -v Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 7.3.1 20180130 (Red Hat 7.3.1-2) (GCC) I'm aware that using -ffinite-math-only -funsafe-math-optimizations allows gcc to divert from the IEEE standard. I am surprised though, that this simple comparison produces different results depending on the speicifc gcc version and whe way I code it. Best regards, Michael