我对 C 标准的解释有疑问,最新草案取自http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2454.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2454.pdf.
标准评价
该标准定义了pragma STD FENV_ACCESS
并指出(7.6.1p2):
The FENV_ACCESS pragma provides a means to inform the implementation when a program might
access the floating-point environment to test floating-point status flags or run under non-default
floating-point control modes.
目前尚不清楚为什么该编译指示需要在非默认浮点控制模式下运行。是不是因为
- 设置这些非默认模式需要写入控制模式寄存器,或者
- 即使已经设置了非默认当前模式,编译指示始终是必需的?
在标准的这一段后面,我们发现:
If part of a program tests floating-point status flags or establishes non-default floating-point
mode settings using any means other than the FENV_ROUND pragmas, but was translated with the
state for the FENV_ACCESS pragma "off", the behavior is undefined.
看起来测试当前模式而不改变它不是未定义的行为。但同一段的脚注指出:
In general, if the state of FENV_ACCESS is "off", the translator can assume that the flags are
not tested, and that default modes are in effect, except where specified otherwise by an
FENV_ROUND pragma.
问题
因此,如果未指定 pragma FENV_ACCESS,是否意味着默认舍入模式有效?
假设 pragma FENV_ROUND 也不存在,并且编译器假定 FENV_ACCESS 默认情况下处于关闭状态,这是向后兼容所必需的。
Example
考虑以下源代码:
#include <math.h>
float func_01(float x) {
return nearbyint(x);
}
功能nearbyint
被描述为(7.12.9.3)使用当前舍入模式进行舍入。但代码中没有pragma FENV_ACCESS
。这是否意味着当前的舍入模式可以被忽略并且nearbyint
与roundeven
?