fenv.h 1.1 KB

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