是否可以在运行时优化浮点乘以零的运算? [英] Can floating point multiplication by zero be optimised at runtime?
问题描述
我正在编写一种算法来查找nxn矩阵的逆矩阵.让我们以3x3矩阵的具体情况为例.
I am writing an algorithm to find the inverse of an nxn matrix. Let us take the specific case of a 3x3 matrix.
手动反转矩阵时,通常会查找包含一个或多个零的行/列,从而使行列式计算更快,因为它消除了您需要计算的项.
When you invert a matrix by hand, you typically look for rows/columns containing one or more zeros to make the determinant calculation faster as it eliminates terms you need to calculate.
在C/C ++中遵循此逻辑,如果您标识具有一个或多个零的行/列,则最终将得到以下代码:
Following this logic in C/C++, if you identify a row/column with one or more zeros, you will end up with the following code:
float term1 = currentElement * DetOf2x2(...);
// ^
// This is equal to 0.
//
// float term2 = ... and so on.
由于编译器无法知道currentElement
在编译时为零,因此无法将其优化为float term = 0;
之类的东西,因此浮点乘法将在运行时执行.
As the compiler cannot know currentElement
will be zero at compile time, it cannot be optimised to something like float term = 0;
and thus the floating point multiplication will be carried out at runtime.
我的问题是,这些零值会使浮点乘法运算更快,还是不管currentElement
的值如何,乘法运算花费相同的时间?如果无法在运行时优化乘法,则可以删除搜索包含零的行/列的逻辑.
My question is, will these zero values make the floating point multiplication faster, or will the multiplication take the same amount of time regardless of the value of currentElement
? If there is no way of optimising the multiplication at runtime, then I can remove the logic that searches for rows/columns containing zeros.
推荐答案
float term1 = currentElement * DetOf2x2(...);
即使currentElement为0,编译器也会调用DetOf2x2(...)
:这肯定比最终乘法的开销大得多,无论是否为0.造成这种情况的原因有多种:
The compiler will call DetOf2x2(...)
even if currentElement is 0: that's sure to be far more costly than the final multiplication, whether by 0 or not. There are multiple reasons for that:
-
DetOf2x2(...)
可能会有副作用(例如输出到日志文件),即使currentElement
为0
,并且 -
DetOf2x2(...)
可能会返回诸如Not-a-Number/NaN前哨值之类的值,无论如何都应传播到term1
(如Nils Pipenbrinck首先指出的那样)
DetOf2x2(...)
may have side effects (like output to a log file) that need to happen even whencurrentElement
is0
, andDetOf2x2(...)
may return values like the Not-a-Number / NaN sentinel that should propagate toterm1
anyway (as noted first by Nils Pipenbrinck)
鉴于DetOf2x2(...)
几乎可以肯定,它只能在运行时确定值,而在编译时不能排除后者的可能性.
Given DetOf2x2(...)
is almost certainly working on values that can only be determined at run-time, the latter possibility can't be ruled out at compile time.
如果要避免拨打Detof2x2(...)
的电话,请尝试:
If you want to avoid the call to Detof2x2(...)
, try:
float term1 = (currentElement != 0) ? currentElement * DetOf2x2(...) : 0;
这篇关于是否可以在运行时优化浮点乘以零的运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!