C ++是否允许浮点表达式收缩? [英] Is floating point expression contraction allowed in C++?

查看:144
本文介绍了C ++是否允许浮点表达式收缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浮点表达式有时可以在处理硬件上收缩,例如使用融合的乘法和加法作为单个硬件操作.

Floating point expressions can sometimes be contracted on the processing hardware, e.g. using fused multiply-and-add as a single hardware operation.

显然,使用这些不仅仅是实现细节,还受编程语言规范的约束.具体而言,C89标准不允许此类收缩,而在C99中,只要定义了某些宏,则允许此类收缩.请参阅此SO答案中的详细信息.

Apparently, using these this isn't merely an implementation detail but governed by programming language specification. Specifically, the C89 standard does not allow such contractions, while in C99 they are allowed provided that some macro is defined. See details in this SO answer.

但是C ++呢?浮点收缩是不允许的吗?在某些标准中允许?普遍允许吗?

But what about C++? Are floating-point contractions not allowed? Allowed in some standards? Allowed universally?

推荐答案

摘要

允许进行收缩,但是为用户提供了一种禁用收缩的功能.标准中使用不清楚的语言会掩盖禁用它们是否会提供预期结果的问题.

Summary

Contractions are permitted, but a facility is provided for the user to disable them. Unclear language in the standard clouds the issue of whether disabling them will provide desired results.

我在官方C ++ 2003标准和2017 n4659草案中对此进行了调查.除非另有说明,否则C ++的引用来自2003年.

I investigated this in the official C++ 2003 standard and the 2017 n4659 draft. C++ citations are from 2003 unless otherwise indicated.

在任何一个文档中都没有出现合同"文本.但是,第5条表达式[expr]第10段(与2017年8 [expr] 13中的相同文字)说:

The text "contract" does not appear in either document. However, clause 5 Expressions [expr] paragraph 10 (same text in 2017’s 8 [expr] 13) says:

与类型所需的精度和范围相比,浮点操作数的值和浮点表达式的结果可以表示得更好;类型不会因此改变.

The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

我希望此声明明确声明是可以自由使用此额外精度和范围(实现可以在某些表达式中使用它,包括子表达式,而在其他表达式中不使用它)还是必须统一使用(如果实现使用额外的精度,必须在每个浮点表达式中使用它)或根据其他规则(例如,它可能对float使用一种精度,对double使用另一种精度).

I would prefer this statement explicitly stated whether this extra precision and range could be used freely (the implementation may use it in some expressions, including subexpressions, while not using it in others) or had to be used uniformly (if the implementation uses extra precision, it must use it in every floating-point expression) or according to some other rules (such as it may use one precision for float, another for double).

如果我们宽容地解释它,则意味着在a*b+c中,a*b可以以无限的精度和范围求值,然后可以以实现时正常的精度和范围来评估加法.从数学上讲,这等效于收缩,因为它的结果与使用融合的乘加指令计算a*b+c的结果相同.

If we interpret it permissively, it means that, in a*b+c, a*b could be evaluated with infinite precision and range, and then the addition could be evaluated with whatever precision and range is normal for the implementation. This is mathematically equivalent to contraction, as it has the same result as evaluating a*b+c with a fused multiply-add instruction.

因此,采用这种解释,实现可能会压缩表达式.

Hence, with this interpretation, implementations may contract expressions.

17.4.1.2 [lib.headers] 3(与2017年20.5.1.2 [headers] 3类似)说:

17.4.1.2 [lib.headers] 3 (similar text in 2017’s 20.5.1.2 [headers] 3) says:

标准C库的功能在其他18个标头中提供,如表12所示……

The facilities of the Standard C Library are provided in 18 additional headers, as shown in Table 12…

表12包括<cmath>,第4段指出这与math.h相对应.从技术上讲,C ++ 2003标准是指C 1990标准,但是我没有电子形式的文件,也不知道我的纸质副本在哪里,所以我将使用C 2011标准(但非官方草案N1570),该标准是C ++指的是2017年草案.

Table 12 includes <cmath>, and paragraph 4 indicates this corresponds to math.h. Technically, the C++ 2003 standard refers to the C 1990 standard, but I do not have it in electronic form and do not know where my paper copy is, so I will use the C 2011 standard (but unofficial draft N1570), which the C++ 2017 draft refers to.

C标准在<math.h>中定义了pragma FP_CONTRACT:

The C standard defines, in <math.h>, a pragma FP_CONTRACT:

#pragma STDC FP_CONTRACT on-off-switch

其中开关on允许收缩表达式,或者off禁止它们.它还说,编译指示的默认状态是实现定义的.

where on-off-switch is on to allow contraction of expressions or off to disallow them. It also says the default state for the pragma is implementation-defined.

C ++标准未定义设施"或设施".词典对设施"的定义是为特定目的而提供的场所,设施或设备"(新牛津美国词典,Apple词典应用程序版本2.2.2(203)).便利设施是建筑物或地方的理想或有用功能或设施".编译指示是为特定目的提供的有用功能,因此它似乎是一种功能,因此包含在<cmath>中.

The C++ standard does not define "facility" or "facilities." A dictionary definition of "facility" is "a place, amenity, or piece of equipment provided for a particular purpose" (New Oxford American Dictionary, Apple Dictionary application version 2.2.2 (203)). An amenity is "a desirable or useful feature or facility of a building or place." A pragma is a useful feature provided for a particular purpose, so it seems to be a facility, so it is included in <cmath>.

因此,使用该编译指示应允许或禁止收缩.

Hence, using this pragma should permit or disallow contractions.

  • 启用FP_CONTRACT时,可以执行收缩操作,并且默认情况下可以启用.

  • Contractions are permitted when FP_CONTRACT is on, and it may be on by default.

即使[c8>处于关闭状态,但对于明确的解释而言,即使是[c8>]处于关闭状态,也可以解释8 [expr] 13的文本以有效地允许收缩.

The text of 8 [expr] 13 can be interpreted to effectively allow contractions even if FP_CONTRACT is off but is insufficiently clear for definitive interpretation.

这篇关于C ++是否允许浮点表达式收缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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