铛倒数1的优化 [英] Clang reciprocal to 1 optimisations

查看:111
本文介绍了铛倒数1的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与同事讨论后,我结束了测试阉如果铛将优化两个部门,一个倒数到1,一个司。

After a discussion with colleagues, I ended up testing wether if clang would optimize two divisions, with a reciprocal to 1, to a single division.

const的浮动X = A / B; // X,它不会在其他地方使用
常量浮Y = 1 / X;

从理论上铛可以优化到常量浮动Y = B / A 如果 X 仅作为一个临时步骤值,不是吗?

Theoretically clang could optimize to const float y = b / a if x is used only as a temporary step value, no?

下面是输入和放大器;一个简单的测试案例输出: https://gist.github.com/Jiboo / d6e839084841d39e5ab6 (在你OUPUTS可以看到,它的执行,而不是优化的两个师,)

Here's the input&output of a simple test case: https://gist.github.com/Jiboo/d6e839084841d39e5ab6 (in both ouputs you can see that it's performing the two divisions, instead of optimizing)

此相关的问题,是我的背后COM prehension,似乎只注重为什么不使用特定指令,而在我的情况下,它未完成的优化:<一href=\"http://stackoverflow.com/questions/32002277/why-does-gcc-or-clang-not-optimise-reciprocal-to-1-instruction-when-using-fast-m\">Why使用快速数学

This related question, is behind my comprehension and seem to focus only on why a specific instruction isn't used, whereas in my case it's the optimisation that isn't done: Why does GCC or Clang not optimise reciprocal to 1 instruction when using fast-math

谢谢,
JB。

Thanks, JB.

推荐答案

没有,铛不能做到这一点。

No, clang can not do that.

但首先,你为什么要使用浮动?浮动有六位precision,双有15除非你有一个很好的理由,那你能解释,使用双。

But first, why are you using float? float has six digits precision, double has 15. Unless you have a good reason, that you can explain, use double.

1 /(A / B)在浮点运算是不一样的B / A。什么是编译器要做的,就是在第一种情况:

1 / (a / b) in floating-point arithmetic is not the same as b / a. What the compiler has to do, is in the first case:


  1. 将b除a

  2. 回合的结果最接近的浮点数

  3. 除以1的结果

  4. 圆形的结果到最近的浮点数。

在第二种情况:


  1. 将一个分水岭湾

  2. 圆形的结果到最近的浮点数。

编译器只能改变code如果结果是保证是相同的,并且,如果编译器作者不能产生一个数学证明的结果是相同的,编译器不能改变code。有在所述第一情况下为两个舍入操作,舍入不同的号码,所以这是不可能的,结果可以保证是相同的。

The compiler can only change the code if the result is guaranteed to be the same, and if the compiler writer cannot produce a mathematical proof that the result is the same, the compiler cannot change the code. There are two rounding operations in the first case, rounding different numbers, so it is unlikely that the result can be guaranteed to be the same.

这篇关于铛倒数1的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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