https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84891

            Bug ID: 84891
           Summary: -fno-signed-zeros leads to optimization which should
                    be possible only if also -ffinite-math-only is on
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: no...@turm-lahnstein.de
  Target Milestone: ---

Please consider the following example

   #include <complex>
   #include <iostream>
   #include <cmath>

   std::complex<double> mult(std::complex<double> c, double im){
       std::complex<double> jomega(0.0, im);
       return c*jomega;
   }


   int main(){
     //(-nan,-nan) expected:
     std::cout<<"case INF:
"<<mult(std::complex<double>(INFINITY,0.0),0.0<<"\n";

     //(nan,nan) expected:
     std::cout<<"case NAN: "<<mult(std::complex<double>(NAN,0.0), 0.0)<<"\n";   
   }

when compiled with -fno-signed-zeros the compiler seems to make some
optimizations which should not be possible without -ffinite-math-only, because
the result of 0.0*nan and 0.0*inf isn't 0.0. Live here
http://coliru.stacked-crooked.com/a/bca026da888d5b5d


    echo "IEEE 754"; g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp &&
./a.out
    echo "Non IEEE 754"; g++ -std=c++17 -O2 -Wall -fno-signed-zeros -pedantic
-pthread main.cpp && ./a.out

gives us:

    IEEE 754
    case INF: (-nan,-nan)
    case NAN: (nan,nan)
    Non IEEE 754
    case INF: (nan,0)
    case NAN: (nan,0)


The resulting assembler is (see also https://godbolt.org/g/TSvcSp)

    mult(std::complex<double>, double):
        mulsd   %xmm2, %xmm1
        movapd  %xmm0, %xmm3
        mulsd   %xmm2, %xmm3
        movapd  %xmm1, %xmm0
        movapd  %xmm3, %xmm1
        xorpd   .LC0(%rip), %xmm0
        ret
  .LC0:
        .long   0
        .long   -2147483648
        .long   0
        .long   0

with only two multiplication instead of four. The clang-behavior is more
similar to gcc-version 4.6.

Reply via email to