Is there a document describing how Clang handles excess floating-point precision?

后端 未结 2 1243
名媛妹妹
名媛妹妹 2020-12-03 02:24

It is nearly impossible(*) to provide strict IEEE 754 semantics at reasonable cost when the only floating-point instructions one is allowed to used are the 387 ones. It is p

2条回答
  •  执笔经年
    2020-12-03 03:11

    For the record, below is what I found by experimentation. The following program shows various behaviors when compiled with Clang:

    #include 
    
    int r1, r2, r3, r4, r5, r6, r7;
    
    double ten = 10.0;
    
    int main(int c, char **v)
    {
      r1 = 0.1 == (1.0 / ten);
      r2 = 0.1 == (1.0 / 10.0);
      r3 = 0.1 == (double) (1.0 / ten);
      r4 = 0.1 == (double) (1.0 / 10.0);
      ten = 10.0;
      r5 = 0.1 == (1.0 / ten);
      r6 = 0.1 == (double) (1.0 / ten);
      r7 = ((double) 0.1) == (1.0 / 10.0);
      printf("r1=%d r2=%d r3=%d r4=%d r5=%d r6=%d r7=%d\n", r1, r2, r3, r4, r5, r6, r7);
    }
    

    The results vary with the optimization level:

    $ clang -v
    Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
    $ clang -mno-sse2 -std=c99  t.c && ./a.out
    r1=0 r2=1 r3=0 r4=1 r5=1 r6=0 r7=1
    $ clang -mno-sse2 -std=c99 -O2  t.c && ./a.out
    r1=0 r2=1 r3=0 r4=1 r5=1 r6=1 r7=1
    

    The cast (double) that differentiates r5 and r6 at -O2 has no effect at -O0 and for variables r3 and r4. The result r1 is different from r5 at all optimization levels, whereas r6 only differs from r3 at -O2.

提交回复
热议问题