diff options
Diffstat (limited to 'src/math/round.c')
| -rw-r--r-- | src/math/round.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/math/round.c b/src/math/round.c index c154bdbf..5eedd578 100644 --- a/src/math/round.c +++ b/src/math/round.c @@ -1,10 +1,33 @@ # define TGSOURCE "round.c" #include "_tgmath.h" #include <math.h> +#include "fenv.h" + +#ifndef __GNUC__ +#pragma STDC FENV_ACCESS ON +#endif TYPE TGFN(round)(TYPE x) { - return x; + switch (fpclassify(x)) { + case FP_ZERO: return x; + case FP_INFINITE: return x; + default: break; + } + + fenv_t save_env; + feholdexcept(&save_env); + + TYPE ret = TGFN(rint)(x); + + if (fetestexcept(FE_INEXACT)) { + fesetround(FE_TOWARDZERO); + ret = TGFN(rint)(TGFN(copysign)(0.5 + TGFN(fabs)(x), x)); + } + + feupdateenv(&save_env); + + return ret; } /* |
