避免C ++中的异常值 [英] Avoiding denormal values in C++
问题描述
在长时间搜索性能错误后,我阅读了关于非正规浮点值。
很显然,反规范化的浮点值可能是一个主要的性能问题,说明在这个问题:
为什么要将0.1f改为0会使性能降低10倍?
我有一个英特尔酷睿2双核,我正在使用gcc编译,使用
-O2
。 那么我该怎么办?我可以以某种方式指示g ++避免反常值吗?
如果没有,我可以以某种方式测试一个 float
是否正常?
您可以使用
测试浮动是否为反常的。$ b
#include< cmath>
if(std :: fpclassify(flt)== FP_SUBNORMAL)
(Caveat:我不知道这将在实践中全速执行。)
在C ++ 03中,这个代码在我的实践中,
#include< cmath>
#include< limits>
if(flt!= 0& std :: fabsf(flt)< std :: numeric_limits< float> :: min()){
//它是非规范化的
}
您可以使用基于样本的分析器,VTune或缩放,以突出显示非正常值减慢的指令。微优化,甚至比其他优化,是完全希望没有分析前后。
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 performance concern as is illustrated in this question: Why does changing 0.1f to 0 slow down performance by 10x?
I have an Intel Core 2 Duo and I am compiling with gcc, using -O2
.
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.)
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
}
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.
这篇关于避免C ++中的异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!