简体   繁体   English

JIT是否使用/ F处理浮点运算:快速或/ F:精确

[英]Does the JIT process floating point arithmetic with /F:fast or /F:precise

In C++ optimizers you can specify the use of fast floating point arithmetic instead of precise floating point arithmetic. 在C ++优化器中,您可以指定使用快速浮点算法而不是精确浮点算法。 The difference is that in fast mode it will do optimizations that would apply to real world math, but may not produce the correct results when done on a computer. 不同之处在于,在快速模式下,它将执行适用于真实世界数学的优化,但在计算机上完成时可能无法产生正确的结果。

An example of this can be shown using GodBolt using the following code and the -O2 optimization. 可以使用GodBolt使用以下代码和-O2优化来显示此示例。 If you are using clang or gcc you can put in the -ffast-math parameter to view what the code looks like with these optimizations. 如果您使用的是clang或gcc,则可以输入-ffast-math参数来查看这些优化的代码。

double mulBy6DivBy12(double x)
{ return 6 * x / 12; }

double divBy2(double x)
{ return x / 2; }

Without the -ffast-math parameter it will generate a multiply and divide operation in the mulBy6DivBy12 function, with it on the two functions will look the same. 如果没有-ffast-math参数,它将在mulBy6DivBy12函数中生成乘法和除法运算,两个函数上的它看起来都是一样的。

What I want to know is at runtime, when being processed by the JIT does it do these potentially unsafe optimizations? 我想知道的是在运行时,当由JIT处理它是否会进行这些潜在的不安全优化?

There are two interesting statements in the Java Language Specification. Java语言规范中有两个有趣的语句。

The first is a broad one in JLS section 15.7 : 第一部分是JLS第15.7节中的第一部分

15.7. 15.7。 Evaluation Order 评估订单

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right. Java编程语言保证运算符的操作数似乎以特定的评估顺序进行评估,即从左到右。

This is already a fully guarantee; 这已经是一个完全保证; even though execution may be in any order, they have to appear to be in the left-to-right evaluation order. 即使执行可能是任何顺序,它们也必须呈现从左到右的评估顺序。 A re-ordering of the floating point operations would break that appearance, so it's not allowed. 重新排序浮点运算会破坏该外观,因此不允许这样做。

Secondly, there is a guarantee specifically for floating point operations in JLS section 4.2.4 : 其次, 在JLS第4.2.4节中专门针对浮点运算提供了保证:

The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Java编程语言要求浮点算法的行为就像每个浮点运算符将其浮点结果四舍五入到结果精度一样。 Inexact results must be rounded to the representable value nearest to the infinitely precise result; 不精确的结果必须四舍五入到最接近无限精确结果的可表示值; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. 如果两个最接近的可表示值相等,则选择具有最低有效位的值。 This is the IEEE 754 standard's default rounding mode known as round to nearest . 这是IEEE 754标准的默认舍入模式,称为舍入到最近

This clarifies exactly how each (left-to-right) operation should round its results. 这清楚地说明了每个(从左到右)操作应如何围绕其结果。 This also disallows any reordering that changes the result of the calculation from the result that you get if you perform the operations in left-to-right order. 这也禁止任何重新排序,如果您按从左到右的顺序执行操作,则会从您获得的结果更改计算结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM