3

Reading a bug report for clang not supporting FENV_ACCESS pragma I've come across a comment:

Setting the rounding mode without using #pragma STDC FENV_ACCESS ON invokes undefined behavior. See C11 7.6.1/2. (This pragma does not exist in C++, so <cfenv> is unusable, but that's not our fault...)

Does this pragma really not exist in C++, rendering <cfenv> unusable? I've tried to search for it in the C++11 standard, but it really isn't mentioned at all. Are pragmas inherited from C along with function prototypes? Or are they actually not needed to avoid UB, since the C++ standard doesn't say anything about behavior being undefined when the pragma is not used (due to not mentioning the pragma at all)?

Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • This is going to require rolling up your sleeves and write and submit a patch for LLVM. Nice when open source lets you fix these things. – Hans Passant Mar 25 '16 at 11:46
  • @HansPassant what do you mean? How can a patch for LLVM fix a problem in Standard? – Ruslan Mar 25 '16 at 11:51

1 Answers1

1

I searched the 2015 standard draft text and found no occurrences of FENV_ACCESS. http://cppreference.com also has nothing about it.

However, http://cplusplus.com does mention it (since it is not in the standard I think we must assume that this is advisory information at best):

http://www.cplusplus.com/reference/cfenv/FENV_ACCESS/

Quoting from cplusplus.com: (emphasis mine)

If set to on, the program informs the compiler that it might access the floating-point environment to test its status flags (exceptions) or run under control modes other than the one by default.

If set to off, the compiler may perform certain optimizations that can subvert these tests and mode changes, and thus accessing the floating-point environment in the cases described above, causes undefined behavior.

Whether the state of this pragma by default is on or off depends on the compiler settings and library implementation.

Given the unsettling lack of clarity, I would want to avoid its use if at all possible.

As ever, if the use was unavoidable I'd want to encapsulate it into one class that I can specialise and test for each architecture.

And then document the existence of this class and the trouble it may cause if the compiler, environment or library implementation is upgraded.

Update:

There is a very brief mention of the header in the c++ standard:

§ 26.3 The floating-point environment [cfenv]

...

2 The header defines all functions, types, and macros the same as Clause 7.6 of the C standard.

Update:

Further information here: http://en.cppreference.com/w/cpp/preprocessor/impl

My reading of this is that the pragma is defined by the C11 standard, not the C++11 standard. Therefore use in a c++ program is strictly implementation/un-defined.

Community
  • 1
  • 1
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • [cppreference](http://en.cppreference.com/w/cpp/preprocessor/impl) mentions it under the words `The following three pragmas are defined by the C language standard`. cplusplus.com seems to assert that they do work in C++, but this claim seems unfounded. – Ruslan Mar 25 '16 at 12:17
  • @Ruslan perhaps better wording would be "may or may not work depending on your implementation"? – Richard Hodges Mar 25 '16 at 12:18
  • Does this all mean that it's a language defect that should be reported and fixed? – Ruslan Mar 25 '16 at 15:37
  • It means that adding yet another bug report to the 11 that already exist is not useful. As I said, somebody has to roll up their sleeves to get this common issue resolved. – Hans Passant Mar 25 '16 at 16:22
  • @Ruslan Something being undefined doesn't mean it's necessarily a bug or defect. I guess the question is, without it are you still able to write coherent programs? – Richard Hodges Mar 25 '16 at 16:38
  • @RichardHodges well if one can't set non-default rounding mode without causing UB, this makes some floating-point computations at best very inefficient. This does seem to be a blocker from using standard C++ for such computations. – Ruslan Mar 25 '16 at 19:17
  • @HansPassant I guess by 11 bug reports you mean the ones filed against clang. But what does clang have to do with a language problem? clang is just one of many implementations of the language. – Ruslan Mar 25 '16 at 19:21
  • @Ruslan presumably you could call into a C module for these particular computations? But yes, meh! – Richard Hodges Mar 25 '16 at 19:23