浮点除法与浮点乘法 [英] Floating point division vs floating point multiplication

查看:26
本文介绍了浮点除法与浮点乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码是否有任何(非微优化)性能提升

Is there any (non-microoptimization) performance gain by coding

float f1 = 200f / 2

对比

float f2 = 200f * 0.5

几年前我的一位教授告诉我,浮点除法比浮点乘法慢,但没有详细说明原因.

A professor of mine told me a few years ago that floating point divisions were slower than floating point multiplications without elaborating the why.

这种说法是否适用于现代 PC 架构?

Does this statement hold for modern PC architecture?

更新1

关于评论,请同时考虑这种情况:

In respect to a comment, please do also consider this case:

float f1;
float f2 = 2
float f3 = 3;
for( i =0 ; i < 1e8; i++)
{
  f1 = (i * f2 + i / f3) * 0.5; //or divide by 2.0f, respectively
}

更新 2引用评论:

[我想]知道什么是算法/架构要求导致 > 除法在硬件上比乘法复杂得多

[I want] to know what are the algorithmic / architectural requirements that cause > division to be vastly more complicated in hardware than multiplication

推荐答案

是的,许多 CPU 可以在 1 或 2 个时钟周期内执行乘法运算,但除法总是需要更长的时间(尽管 FP 除法有时比整数除法更快).

Yes, many CPUs can perform multiplication in 1 or 2 clock cycles but division always takes longer (although FP division is sometimes faster than integer division).

如果您查看 这个答案你会看到除法可以超过24个周期.

If you look at this answer you will see that division can exceed 24 cycles.

为什么除法比乘法花这么长时间?如果您还记得小学时,您可能还记得乘法基本上可以通过许多同时加法来执行.除法需要迭代减法,不能同时执行,因此需要更长的时间.事实上,一些 FP 单元通过执行倒数近似并乘以它来加速除法.它不太准确,但速度更快.

Why does division take so much longer than multiplication? If you remember back to grade school, you may recall that multiplication can essentially be performed with many simultaneous additions. Division requires iterative subtraction that cannot be performed simultaneously so it takes longer. In fact, some FP units speed up division by performing a reciprocal approximation and multiplying by that. It isn't quite as accurate but is somewhat faster.

这篇关于浮点除法与浮点乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆