[cfp-interest 3439] Re: [[reproducible]] EXAMPLE 3 in 6.7.13.8

Jim Thomas jaswthomas at sbcglobal.net
Mon Apr 14 14:53:17 PDT 2025



> On Apr 14, 2025, at 12:12 PM, JeanHeyd Meneide <phdofthehouse at gmail.com> wrote:
> 
> On Mon, Apr 14, 2025 Jim Thomas <jaswthomas at sbcglobal.net <mailto:jaswthomas at sbcglobal.net>> wrote:
>> EXAMPLE 3 in 6.7.13.8.3 (in N3467) seems wrong. I understand that if a function is declared with the [[reproducible]] attribute then the compiler can elide repeated calls with the same arguments. But two such calls to the distance function in the example could not be elided if status flags are cleared after the first call and tested after the second. The function can raise “overflow”, “underflow”, and “inexact” floating-point exceptions.
> 
>      Both asking and trying to find a solution, here. Does this mean in addition to the FP_CONTRACT pragma, it needs to also turn off FENV_ACCESS to prevent having access to floating point status flags?

That wouldn’t help.

> Does turning it off inside of a function mean a function cannot change those flags anymore, or does it just mean that the function itself won't access those flags?

The latter.

> The former, I think, would solve this problem, but the latter doesn't help for the example I don't believe.

Correct.

> (And, fi that is the case, do we then need to look into explicitly asserting that the floating point flags / exceptions are not active?)

> 
>      If we did want to make such a function reproducible, what would you change to make it that way?


The function could be written so that it doesn’t change the flags, e.g. with

#pragma STDC FP_CONTRACT OFF
#pragma STDC FENV_ACCESS ON
fenv_t save_env;
double d;
fegetenv(&save_env);
fesetenv(FE_DFL_ENV);
double d = sqrt(x[0]*x[0] + x[1]*x[1]);
fesetenv(&save_env);
return d;

Of course that would mean the function wouldn’t report floating-point exceptions.

(TS 18661-5 has #pragma STDC FENV_EXCEPT FE_ALL_EXCEPT NO_FLAG which means functions inside don’t set flags.)

- Jim Thomas



> 
> Sincerely,
> JeanHeyd

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.oakapple.net/pipermail/cfp-interest/attachments/20250414/cae19dec/attachment.htm>


More information about the cfp-interest mailing list