2016-07-03 06:51:13 -03:00
|
|
|
#pragma once
|
2016-07-06 11:30:17 -03:00
|
|
|
|
2016-07-03 06:51:13 -03:00
|
|
|
#include_next <fenv.h>
|
|
|
|
|
2019-07-23 08:00:43 -03:00
|
|
|
#ifndef HAVE_FEENABLEEXCEPT
|
2016-07-03 06:51:13 -03:00
|
|
|
#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
|
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
inline int feenableexcept(unsigned int excepts)
|
2016-07-03 06:51:13 -03:00
|
|
|
{
|
2016-07-06 11:30:17 -03:00
|
|
|
static fenv_t fenv;
|
|
|
|
unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
|
|
|
|
// previous masks
|
|
|
|
unsigned int old_excepts;
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
if (fegetenv(&fenv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
old_excepts = fenv.__control & FE_ALL_EXCEPT;
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
// unmask
|
|
|
|
fenv.__control &= ~new_excepts;
|
|
|
|
fenv.__mxcsr &= ~(new_excepts << 7);
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
return fesetenv(&fenv) ? -1 : old_excepts;
|
2016-07-03 06:51:13 -03:00
|
|
|
}
|
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
inline int fedisableexcept(unsigned int excepts)
|
2016-07-03 06:51:13 -03:00
|
|
|
{
|
2016-07-06 11:30:17 -03:00
|
|
|
static fenv_t fenv;
|
|
|
|
unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
|
|
|
|
// all previous masks
|
|
|
|
unsigned int old_excepts;
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
if (fegetenv(&fenv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
old_excepts = fenv.__control & FE_ALL_EXCEPT;
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
// mask
|
|
|
|
fenv.__control |= new_excepts;
|
|
|
|
fenv.__mxcsr |= new_excepts << 7;
|
2016-07-03 06:51:13 -03:00
|
|
|
|
2016-07-06 11:30:17 -03:00
|
|
|
return fesetenv(&fenv) ? -1 : old_excepts;
|
2016-07-03 06:51:13 -03:00
|
|
|
}
|
2016-07-06 11:30:17 -03:00
|
|
|
|
2019-07-23 08:00:43 -03:00
|
|
|
#else
|
|
|
|
inline int feenableexcept(unsigned int excepts)
|
|
|
|
{
|
|
|
|
#pragma STDC FENV_ACCESS ON
|
|
|
|
fexcept_t flags;
|
|
|
|
/* Save current exception flags. */
|
|
|
|
fegetexceptflag(&flags, FE_ALL_EXCEPT);
|
|
|
|
|
|
|
|
feclearexcept(FE_ALL_EXCEPT); /* clear all fp exception conditions */
|
|
|
|
return fesetexceptflag(&flags, excepts) != 0 ? -1 : flags; /* set new flags */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int fedisableexcept(unsigned int excepts)
|
|
|
|
{
|
|
|
|
#pragma STDC FENV_ACCESS ON
|
|
|
|
fexcept_t flags;
|
|
|
|
/* Save current exception flags. */
|
|
|
|
fegetexceptflag(&flags, FE_ALL_EXCEPT);
|
|
|
|
|
|
|
|
feclearexcept(FE_ALL_EXCEPT); /* clear all fp exception conditions */
|
|
|
|
return fesetexceptflag(&flags, ~excepts) != 0 ? -1 : flags; /* set new flags */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2016-07-06 11:30:17 -03:00
|
|
|
#endif
|