From 6d012d2df00d742329a05aed46f657f439023fee Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sat, 23 Jan 2021 15:53:56 +0100 Subject: [PATCH] ape: apply infinite recursion in fmod() fix (thanks jxy and ality) Apply changeset 2880:cab2b9d13a73 to ape's fmod() implementation. Remove the unused math/fmod.c copy. --- sys/src/ape/lib/ap/math/fmod.c | 27 --------------------------- sys/src/ape/lib/ap/plan9/frexp.c | 13 +++++++++++-- 2 files changed, 11 insertions(+), 29 deletions(-) delete mode 100644 sys/src/ape/lib/ap/math/fmod.c diff --git a/sys/src/ape/lib/ap/math/fmod.c b/sys/src/ape/lib/ap/math/fmod.c deleted file mode 100644 index 876c64c39..000000000 --- a/sys/src/ape/lib/ap/math/fmod.c +++ /dev/null @@ -1,27 +0,0 @@ -/* floating-point mod function without infinity or NaN checking */ -#include -double -fmod (double x, double y) -{ - int sign = 0, yexp; - double r, yfr; - - if (y == 0) - return 0; - if (y < 0) - y = -y; - yfr = frexp (y, &yexp); - if (x < 0) { - sign = 1; - r = -x; - } else - r = x; - while (r >= y) { - int rexp; - double rfr = frexp (r, &rexp); - r -= ldexp (y, rexp - yexp - (rfr < yfr)); - } - if (sign) - r = -r; - return r; -} diff --git a/sys/src/ape/lib/ap/plan9/frexp.c b/sys/src/ape/lib/ap/plan9/frexp.c index 0d81f52c7..2e814dd19 100644 --- a/sys/src/ape/lib/ap/plan9/frexp.c +++ b/sys/src/ape/lib/ap/plan9/frexp.c @@ -73,6 +73,16 @@ modf(double d, double *ip) Cheat x; int e; + x.d = d; + e = (x.ms >> SHIFT) & MASK; + if(e == MASK){ + *ip = d; + if(x.ls != 0 || (x.ms & 0xfffffL) != 0) /* NaN */ + return d; + /* ±Inf */ + x.ms &= 0x80000000L; + return x.d; + } if(d < 1) { if(d < 0) { f = modf(-d, ip); @@ -82,8 +92,7 @@ modf(double d, double *ip) *ip = 0; return d; } - x.d = d; - e = ((x.ms >> SHIFT) & MASK) - BIAS; + e -= BIAS; if(e <= SHIFT+1) { x.ms &= ~(0x1fffffL >> e); x.ls = 0;