Why does compiler generate additional sqrts in the compiled assembly code

后端 未结 1 662
礼貌的吻别
礼貌的吻别 2020-12-11 15:34

I\'m trying to profile the time it takes to compute a sqrt using the following simple C code, where readTSC() is a function to read the CPU\'s cycle counter.



        
1条回答
  •  离开以前
    2020-12-11 16:07

    It's using the library sqrt function for error handling. See glibc's documentation: 20.5.4 Error Reporting by Mathematical Functions: math functions set errno for compatibility with systems that don't have IEEE754 exception flags. Related: glibc's math_error(7) man page.

    As an optimization, it first tries to perform the square root by the inlined sqrtsd instruction, then checks the result against itself using the ucomisd instruction which sets the flags as follows:

    CASE (RESULT) OF
       UNORDERED:    ZF,PF,CF  111;
       GREATER_THAN: ZF,PF,CF  000;
       LESS_THAN:    ZF,PF,CF  001;
       EQUAL:        ZF,PF,CF  100;
    ESAC;
    

    In particular, comparing a QNaN to itself will return UNORDERED, which is what you will get if you try to take the square root of a negative number. This is covered by the jp branch. The je check is just paranoia, checking for exact equality.


    Also note that gcc has a -fno-math-errno option which will sacrifice this error handling for speed. This option is part of -ffast-math, but can be used on its own without enabling any result-changing optimizations.

    sqrtsd on its own correctly produces NaN for negative and NaN inputs, and sets the IEEE754 Invalid flag. The check and branch is only to preserve the errno-setting semantics which most code doesn't rely on.

    -fno-math-errno is the default on Darwin (OS X), where the math library never sets errno, so functions can be inlined without this check.

    0 讨论(0)
提交回复
热议问题