避免 C++ 中的非规范值
在搜索了很长时间的性能错误后,我读到了非正规浮点值.
After searching a long time for a performance bug, I read about denormal floating point values.
显然,非规范化浮点值可能是一个主要的性能问题,如以下问题所示:为什么将 0.1f 更改为 0 会变慢性能下降 10 倍?
Apparently denormalized floating-point values can be a major performance concern as is illustrated in this question: Why does changing 0.1f to 0 slow down performance by 10x?
我有一个 Intel Core 2 Duo,我正在使用 gcc 进行编译,使用 -O2
.
I have an Intel Core 2 Duo and I am compiling with gcc, using -O2
.
那我该怎么办?我可以以某种方式指示 g++ 避免异常值吗?如果不是,我可以以某种方式测试 float
是否异常?
So what do I do? Can I somehow instruct g++ to avoid denormal values?
If not, can I somehow test if a float
is denormal?
推荐答案
你可以使用测试一个浮点数是否异常
You can test whether a float is denormal using
#include <cmath>
if ( std::fpclassify( flt ) == FP_SUBNORMAL )
(警告:我不确定这是否会在实践中全速执行.)
(Caveat: I'm not sure that this will execute at full speed in practice.)
在 C++03 中,这段代码在实践中对我有用,
In C++03, and this code has worked for me in practice,
#include <cmath>
#include <limits>
if ( flt != 0 && std::fabsf( flt ) < std::numeric_limits<float>::min() ) {
// it's denormalized
}
要决定在哪里应用此功能,您可以使用基于样本的分析器,例如 Shark、VTune 或 Zoom,以突出显示因非规范值而减慢的指令.微优化,甚至比其他优化,如果不前后分析,是完全没有希望的.
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.
相关文章