From 484db9ff824b0aa496fddf8d2df4abd2e2406661 Mon Sep 17 00:00:00 2001 From: Siddharth Bharat Purohit Date: Sun, 3 Jul 2016 15:21:13 +0530 Subject: [PATCH] AP_Common: add replacements for fe control functions systems without them --- libraries/AP_Common/missing/fenv.h | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 libraries/AP_Common/missing/fenv.h diff --git a/libraries/AP_Common/missing/fenv.h b/libraries/AP_Common/missing/fenv.h new file mode 100644 index 0000000000..ce9f518d4a --- /dev/null +++ b/libraries/AP_Common/missing/fenv.h @@ -0,0 +1,42 @@ +#pragma once +#include_next + +#if defined(__APPLE__) && defined(__MACH__) + +// Public domain polyfill for feenableexcept on OS X +// http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c + +inline int +feenableexcept (unsigned int excepts) +{ + static fenv_t fenv; + unsigned int new_excepts = excepts & FE_ALL_EXCEPT, + old_excepts; // previous masks + + if ( fegetenv (&fenv) ) return -1; + old_excepts = fenv.__control & FE_ALL_EXCEPT; + + // unmask + fenv.__control &= ~new_excepts; + fenv.__mxcsr &= ~(new_excepts << 7); + + return ( fesetenv (&fenv) ? -1 : old_excepts ); +} + +inline int +fedisableexcept (unsigned int excepts) +{ + static fenv_t fenv; + unsigned int new_excepts = excepts & FE_ALL_EXCEPT, + old_excepts; // all previous masks + + if ( fegetenv (&fenv) ) return -1; + old_excepts = fenv.__control & FE_ALL_EXCEPT; + + // mask + fenv.__control |= new_excepts; + fenv.__mxcsr |= new_excepts << 7; + + return ( fesetenv (&fenv) ? -1 : old_excepts ); +} +#endif // APPLE