code: plan9front

Download patch

ref: 6d012d2df00d742329a05aed46f657f439023fee
parent: b5c7158f39e448d70052076b83d04ef22d794e5a
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Jan 23 10:53:56 EST 2021

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.

--- a/sys/src/ape/lib/ap/math/fmod.c
+++ /dev/null
@@ -1,27 +1,0 @@
-/* floating-point mod function without infinity or NaN checking */
-#include <math.h>
-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;
-}
--- a/sys/src/ape/lib/ap/plan9/frexp.c
+++ b/sys/src/ape/lib/ap/plan9/frexp.c
@@ -73,6 +73,16 @@
 	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 @@
 		*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;