summaryrefslogtreecommitdiffstats
path: root/winsup/mingw/mingwex/math/llround.c
diff options
context:
space:
mode:
Diffstat (limited to 'winsup/mingw/mingwex/math/llround.c')
-rw-r--r--winsup/mingw/mingwex/math/llround.c35
1 files changed, 15 insertions, 20 deletions
diff --git a/winsup/mingw/mingwex/math/llround.c b/winsup/mingw/mingwex/math/llround.c
index f8fafc48d..45b754c75 100644
--- a/winsup/mingw/mingwex/math/llround.c
+++ b/winsup/mingw/mingwex/math/llround.c
@@ -1,24 +1,19 @@
-#include <fenv.h>
#include <math.h>
+#include <limits.h>
+#include <errno.h>
long long
-llround (double x) {
- long long retval;
- unsigned short saved_cw, _cw;
- __asm__ (
- "fnstcw %0;" : "=m" (saved_cw)
- ); /* save control word */
- _cw = ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)
- | (x > 0.0 ? FE_UPWARD : FE_DOWNWARD); /* round away from zero */
- __asm__ (
- "fldcw %0;" : : "m" (_cw)
- ); /* load the rounding control */
- __asm__ __volatile__ (
- "fistpll %0" : "=m" (retval) : "t" (x) : "st"
- );
- __asm__ (
- "fldcw %0;" : : "m" (saved_cw)
- ); /* restore control word */
- return retval;
+llround (double x)
+{
+ /* Add +/- 0.5, then round towards zero. */
+ double tmp = trunc (x + (x >= 0.0 ? 0.5 : -0.5));
+ if (!isfinite (tmp)
+ || tmp > (double)LONG_LONG_MAX
+ || tmp < (double)LONG_LONG_MIN)
+ {
+ errno = ERANGE;
+ /* Undefined behaviour, so we could return anything. */
+ /* return tmp > 0.0 ? LONG_LONG_MAX : LONG_LONG_MIN; */
+ }
+ return (long long)tmp;
}
-