8

I have been excited by this post: https://stackoverflow.com/a/57674631/2492801 and I consider using -fno-math-errno. But I would like to be sure that I do not harm the behaviour of the software I am working on.

Therefore I have checked the (rather large) codebase to see where errno is being used and I wanted to decide whether these usages interfere with -fno-math-errno. But how to do that? The documentation says:

-fno-math-errno

Do not set errno after calling math functions that are executed with a single instruction, e.g., sqrt...

But how can I know which math functions are executed with a single instruction? Is this documented somewhere? Where?

It seems as if the codebase I use relies on errno especially when calling strtol and when working with streams. I guess that strtol is not executed with a single instruction. Is it considered to be a math function at all? How can I be sure?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Benjamin Bihler
  • 1,612
  • 11
  • 32
  • Your first goto for stuff like this should be [cppreference](https://en.cppreference.com/w/) – NathanOliver Aug 27 '19 at 14:20
  • @NathanOliver I have checked it. I could not find any hint whether `strtol` is executed with a single instruction or whether it is considered to be a math function. Also I could not find a list of math functions that are executed with a single instruction. Could you give me more hints what you mean? – Benjamin Bihler Aug 27 '19 at 14:25
  • We can't know the implementation generally so you actually have to check your implementation. What you can do though is use it to get a list of functions that do set `errorno`. Also, `strtol` uses integer math, not floating point math so it is exempt. – NathanOliver Aug 27 '19 at 14:27
  • The second part of your comment seems correct. The quotation from the gcc documentation in my post is below a line stating: "The following options control compiler behavior regarding floating-point arithmetic." Therefore `strtol` is safe. But the first two sentences I do not understand. For all functions from `` that I have checked, cppreference states: "Errors are reported as specified in math_errhandling." But still I do not know whether they are executed with a single instruction. So what is your recommendation? Read the implementation source code? – Benjamin Bihler Aug 27 '19 at 14:36
  • 1
    @BenjaminBihler -- this is non-conforming behavior, so documentation of what the standard requires (e.g., cppreference) won't help you. You're stuck with whatever documentation your compiler gives you. – Pete Becker Aug 27 '19 at 14:40
  • @PeteBecker And since the gcc documentation seems not to contain a list of affected methods, I cannot know? Hmmm... what a pity! – Benjamin Bihler Aug 27 '19 at 14:43
  • Note that (in C at least), math functions only set errno if `(math_errhandling & MATH_ERRNO) != 0` -- if that config bit is 0, they might or might not set errno in case of a range or domain error. So as long as gcc arranges for it not to be set, `-fno-math-errno` is still standards conforming. – Chris Dodd Aug 27 '19 at 20:55

1 Answers1

5

You can find list of functions affected by -fno-math-errno in GCC's builtins.def (search for "ERRNO"). It seems that only some functions from math.h header (cos, sin, exp, etc.) are affected. Treatment of other standard functions that use errno (strtol, etc.) will not change under this flag.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • 1
    The querent here seems not to know the standard meaning of "math function", and is asking if `strtol` is a math function. You're only answering that part by example, so maybe good to state more clearly that math functions are ones declared in `math.h` which take `float` or `double`, or `long double` args. i.e. they do floating point calculations and nothing else. Things like `nextafter`, `nearbyint`, and `lrint` are also math functions, not just stuff like `cos` and `exp` which are FP implementations of continuous mathematical functions. – Peter Cordes Dec 28 '21 at 11:59
  • @PeterCordes thank you, I've updated the answer. In case you think that more details may be helpful - feel free to update the answer. – yugr Dec 28 '21 at 12:02