除法为乘法和LUT? /快浮分相互 [英] division as multiply and LUT ? / fast float division reciprocal

查看:184
本文介绍了除法为乘法和LUT? /快浮分相互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在查找表(如1 / f-> 1 * inv [f])的形式
中进行浮点除法的倒数?如何做到这一点?
我认为一些和掩码和移位应该应用浮动以使
它的一种形式的索引?

Is it possible to make a reciprocal of float division in form of look up table (such like 1/f -> 1*inv[f] ) ? How it could be done? I think some and mask and shift should be appled to float to make it a form of index? How would be it exectly?

推荐答案

你可以猜到这样的一个近似倒数:

You can guess an approximate inverse like this:

int x = reinterpret_cast<int>(f);
x = 0x7EEEEEEE - x;
float inv = reinterpret_cast<float>(x);



在我的测试中,0x7EF19D07稍微好一点(包括2个N​​ewton-Raphson改进的效果测试) 。

In my tests, 0x7EF19D07 was slightly better (tested with the effects of 2 Newton-Raphson refinements included).

您可以用Newton-Raphson改善:

Which you can then improve with Newton-Raphson:

inv = inv * (2 - inv * f);

根据需要随意迭代。

为了最小化相对误差:


  • 0x7EF311C2(无细化)

  • 0x7EF311C3(1细化)

  • 0x7EF312AC(2项细分)

  • 0x7EEEEBB3(3项细分)

  • 0x7EF311C2 (without refinement)
  • 0x7EF311C3 (1 refinement)
  • 0x7EF312AC (2 refinements)
  • 0x7EEEEBB3 (3 refinements)

对于1和2之间的输入(它们在该范围之外工作良好,但它们可能不是最好的):

To minimize the absolute error for inputs between 1 and 2 (they work well enough outside that range, but they may not be the best):


  • 0x7EF504F3 )

  • 0x7EF40D2F(1细化)

  • 0x7EF39252(2项细分)

对于三个细化步骤,初始近似几乎不影响最大相对误差。 0x7EEEEEEE工作得很好,我找不到更好的东西。

For three refinement steps, the initial approximation barely affects the maximum relative error. 0x7EEEEEEE works great, and I couldn't find anything better.

这篇关于除法为乘法和LUT? /快浮分相互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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