<html><body><p><tt><font size="2">Serge Pavlov <sepavloff@gmail.com> wrote on 05.03.2020 06:57:08:<br><br>> Actually it is hard to rely on default FP environment in many cases.<br>> We know that a program starts with default FP state installed. But <br>> in other cases we generally cannot assume this. For example, can we <br>> assume default FP environment in this case?<br>></font></tt><br><tt><font size="2">> float qqq(float x) {</font></tt><br><tt><font size="2">>   return nearbyint(x);</font></tt><br><tt><font size="2">> }</font></tt><br><br><tt><font size="2">I'm not sure what problem you see here.  In default mode, i.e.</font></tt><br><tt><font size="2">when there is no "#pragma STDC FENV_ACCESS on" in effect,</font></tt><br><tt><font size="2">then the compiler can always assume that the default rounding</font></tt><br><tt><font size="2">mode is in effect.</font></tt><br><tt><font size="2"><br>> Depending on the answer compiler either generates non-constrained <br>> intrinsic or constrained. Result of `nearbyint` depends on current <br>> rounding mode, so this function accesses FP environment - it <br>> implicitly reads rounding mode. Shall user use `#pragma STDC <br>> FENV_ACCESS on` here? Actually no.<br></font></tt><br><tt><font size="2">Well, if #pragma STDC FENV_ACCESS on is not in effect, that means</font></tt><br><tt><font size="2">that the user has promised that at this point during execution,</font></tt><br><tt><font size="2">we will *always* have the default FP environment.  As you quote:</font></tt><br><tt><font size="2"> <br>> C standard (n2454):<br></font></tt><br><tt><font size="2">> 7.6.1p2</font></tt><br><tt><font size="2">> The FENV_ACCESS pragma provides a means to inform the implementation<br>> when a program might access the floating-point environment to test</font></tt><br><tt><font size="2">> floating-point status flags or run under non-default</font></tt><br><tt><font size="2">> floating-point control modes.</font></tt><br><br><tt><font size="2">Note the last clause "*run under* non-default floating-point</font></tt><br><tt><font size="2">control modes".  Every bit of code that can possibly ever run</font></tt><br><tt><font size="2">while non-default FP modes are in effect *must* be compiled</font></tt><br><tt><font size="2">with #pragma STDC FENV_ACCESS in effect, or else the whole</font></tt><br><tt><font size="2">program has undefined behavior.</font></tt><br><br><tt><font size="2">> 7.6p1</font></tt><br><tt><font size="2">> A floating-point status flag is a</font></tt><br><tt><font size="2">> system variable whose value is set (but never cleared) when a <br>> floating-point exception is raised, which</font></tt><br><tt><font size="2">> occurs as a side effect of exceptional floating-point arithmetic to <br>> provide auxiliary information.</font></tt><br><br><tt><font size="2">True but irrelevant, since 7.6.1p2 also talks about floating-point</font></tt><br><tt><font size="2">*control modes*, which are collectively defined as:</font></tt><br><br><tt><font size="2">7.6p1</font></tt><br><tt><font size="2">A floating-point control mode is a system variable whose value may</font></tt><br><tt><font size="2">be set by the user to affect the subsequent behavior of</font></tt><br><tt><font size="2">floating-point arithmetic.</font></tt><br><br><tt><font size="2">And this includes the rounding-mode controls.</font></tt><br><tt><font size="2"> <br>> Not every access to FP environment requires `#pragma STDC <br>> FENV_ACCESS on`, only that which reads FP exception status or sets <br>> control modes. None occurs in the example above.<br>> <br>> So, even if `#pragma STDC FENV_ACCESS on` is absent we should not <br>> assume default FP environment in the case of functions that read <br>> control modes, including nearbyint and rint. They cannot assume <br>> default rounding mode and must be ordered relative to other <br>> instructions that may access FP environment. The scope of non-<br>> constrained intrinsics would be only initialization code, which <br>> seems to be marginal case.</font></tt><br><br><tt><font size="2">This all seems to be based on a misreading of the standard.</font></tt><br><tt><font size="2"><br>Bye,</font></tt><br><tt><font size="2">Ulrich</font></tt><BR>
</body></html>