From 9d26bc6958f853de9a6239a9b4f548a64acc9e40 Mon Sep 17 00:00:00 2001 From: tcr3dr Date: Thu, 4 Jun 2015 12:05:42 -0700 Subject: [PATCH] HAL_SITL: Adds polyfill for feenableexcept on OS X. --- libraries/AP_HAL_SITL/Scheduler.cpp | 1 + libraries/AP_HAL_SITL/fenv_polyfill.h | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 libraries/AP_HAL_SITL/fenv_polyfill.h diff --git a/libraries/AP_HAL_SITL/Scheduler.cpp b/libraries/AP_HAL_SITL/Scheduler.cpp index b346c52ad6..6899bdb24f 100644 --- a/libraries/AP_HAL_SITL/Scheduler.cpp +++ b/libraries/AP_HAL_SITL/Scheduler.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "fenv_polyfill.h" using namespace HALSITL; diff --git a/libraries/AP_HAL_SITL/fenv_polyfill.h b/libraries/AP_HAL_SITL/fenv_polyfill.h new file mode 100644 index 0000000000..cfa8b9991a --- /dev/null +++ b/libraries/AP_HAL_SITL/fenv_polyfill.h @@ -0,0 +1,32 @@ + +#ifndef __FENV_POLYFILL_H__ +#define __FENV_POLYFILL_H__ + +#if (defined __APPLE__) && (defined(__i386__) || defined(__x86_64__)) + +#include + +// 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 ); +} + +#endif // APPLE + +#endif // __FENV_POLYFILL_H__ +