about summary refs log tree commit diff
path: root/src/math/nextafterf.c
diff options
context:
space:
mode:
authornsz <nsz@port70.net>2012-05-06 13:08:59 +0200
committernsz <nsz@port70.net>2012-05-06 13:08:59 +0200
commit6cf865dba69bab6346dc268d9173609af36b984e (patch)
treed30e0262f1114a707bb1bc9fa03fb381e6dba324 /src/math/nextafterf.c
parent98c9af500125df41fdb46d7e384b00982d72493a (diff)
downloadmusl-6cf865dba69bab6346dc268d9173609af36b984e.tar.gz
musl-6cf865dba69bab6346dc268d9173609af36b984e.tar.xz
musl-6cf865dba69bab6346dc268d9173609af36b984e.zip
math: nextafter and nexttoward cleanup
make nexttoward, nexttowardf independent of long double representation.
fix nextafterl: it did not raise underflow flag when the result was 0.
Diffstat (limited to 'src/math/nextafterf.c')
-rw-r--r--src/math/nextafterf.c84
1 files changed, 26 insertions, 58 deletions
diff --git a/src/math/nextafterf.c b/src/math/nextafterf.c
index bdc88cab..4727f7b0 100644
--- a/src/math/nextafterf.c
+++ b/src/math/nextafterf.c
@@ -1,67 +1,35 @@
-/* origin: FreeBSD /usr/src/lib/msun/src/s_nextafterf.c */
-/*
- * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
- */
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
-
 #include "libm.h"
 
+#define SIGN 0x80000000
+
 float nextafterf(float x, float y)
 {
-	volatile float t;
-	int32_t hx,hy,ix,iy;
-
-	GET_FLOAT_WORD(hx, x);
-	GET_FLOAT_WORD(hy, y);
-	ix = hx & 0x7fffffff;  /* |x| */
-	iy = hy & 0x7fffffff;  /* |y| */
+	union fshape ux, uy;
+	uint32_t ax, ay, e;
 
-	if (ix > 0x7f800000 || /* x is nan */
-	    iy > 0x7f800000)   /* y is nan */
-		return x+y;
-	if (x == y)            /* x == y */
+	if (isnan(x) || isnan(y))
+		return x + y;
+	ux.value = x;
+	uy.value = y;
+	if (ux.bits == uy.bits)
 		return y;
-	if (ix == 0) {         /* x == 0 */
-		SET_FLOAT_WORD(x, (hy&0x80000000)|1); /* return +-minsubnormal */
-		/* raise underflow flag */
-		t = x*x;
-		if (t == x)
-			return t;
-		return x;
-	}
-	if (hx >= 0) {         /* x > 0 */
-		if (hx > hy) {             /* x > y, x -= ulp */
-			hx--;
-		} else {                   /* x < y, x += ulp */
-			hx++;
-		}
-	} else {               /* x < 0 */
-		if (hy >= 0 || hx > hy) {  /* x < y, x -= ulp */
-			hx--;
-		} else {                   /* x > y, x += ulp */
-			hx++;
-		}
-	}
-	hy = hx & 0x7f800000;
-	if (hy >= 0x7f800000)  /* overflow */
-		return x+x;
-	if (hy < 0x00800000) { /* underflow */
-		/* raise underflow flag */
-		t = x*x;
-		if (t != x) {
-			SET_FLOAT_WORD(y, hx);
+	ax = ux.bits & ~SIGN;
+	ay = uy.bits & ~SIGN;
+	if (ax == 0) {
+		if (ay == 0)
 			return y;
-		}
+		ux.bits = (uy.bits & SIGN) | 1;
+	} else if (ax > ay || ((ux.bits ^ uy.bits) & SIGN))
+		ux.bits--;
+	else
+		ux.bits++;
+	e = ux.bits & 0x7f800000;
+	/* raise overflow if ux.value is infinite and x is finite */
+	if (e == 0x7f800000)
+		return x + x;
+	/* raise underflow if ux.value is subnormal or zero */
+	if (e == 0) {
+		volatile float z = x*x + ux.value*ux.value;
 	}
-	SET_FLOAT_WORD(x, hx);
-	return x;
+	return ux.value;
 }