浮点除法与浮点乘法

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

问题描述

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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)