Avoiding denormal values in C++

前端 未结 6 834
遇见更好的自我
遇见更好的自我 2020-11-30 04:34

After searching a long time for a performance bug, I read about denormal floating point values.

Apparently denormalized floating-point values can be a major performa

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 05:18

    You can test whether a float is denormal using

    #include 
    
    if ( std::fpclassify( flt ) == FP_SUBNORMAL )
    

    (Caveat: I'm not sure that this will execute at full speed in practice.)

    In C++03, and this code has worked for me in practice,

    #include 
    #include 
    
    if ( flt != 0 && std::fabsf( flt ) < std::numeric_limits::min() ) {
        // it's denormalized
    }
    

    To decide where to apply this, you may use a sample-based analyzer like Shark, VTune, or Zoom, to highlight the instructions slowed by denormal values. Micro-optimization, even more than other optimizations, is totally hopeless without analysis both before and after.

提交回复
热议问题