APM:Libraries
fenv.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include_next <fenv.h>
4 
5 #if defined(__APPLE__) && defined(__MACH__)
6 
7 // Public domain polyfill for feenableexcept on OS X
8 // http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c
9 
10 inline int feenableexcept(unsigned int excepts)
11 {
12  static fenv_t fenv;
13  unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
14  // previous masks
15  unsigned int old_excepts;
16 
17  if (fegetenv(&fenv)) {
18  return -1;
19  }
20  old_excepts = fenv.__control & FE_ALL_EXCEPT;
21 
22  // unmask
23  fenv.__control &= ~new_excepts;
24  fenv.__mxcsr &= ~(new_excepts << 7);
25 
26  return fesetenv(&fenv) ? -1 : old_excepts;
27 }
28 
29 inline int fedisableexcept(unsigned int excepts)
30 {
31  static fenv_t fenv;
32  unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
33  // all previous masks
34  unsigned int old_excepts;
35 
36  if (fegetenv(&fenv)) {
37  return -1;
38  }
39  old_excepts = fenv.__control & FE_ALL_EXCEPT;
40 
41  // mask
42  fenv.__control |= new_excepts;
43  fenv.__mxcsr |= new_excepts << 7;
44 
45  return fesetenv(&fenv) ? -1 : old_excepts;
46 }
47 
48 #endif