about summary refs log tree commit diff
diff options
context:
space:
mode:
authornsz <nsz@port70.net>2012-05-06 21:24:28 +0200
committernsz <nsz@port70.net>2012-05-06 21:24:28 +0200
commit6ab8136b4477fd75381c06fa0e7fa93c89c712a1 (patch)
treefea8ff7bf34e2a41e797528571d62097413b3685
parent4e597feef0595caefa39ab43c813734a1244fa84 (diff)
downloadmusl-6ab8136b4477fd75381c06fa0e7fa93c89c712a1.tar.gz
musl-6ab8136b4477fd75381c06fa0e7fa93c89c712a1.tar.xz
musl-6ab8136b4477fd75381c06fa0e7fa93c89c712a1.zip
add FORCE_EVAL macro to evaluate float expr for their side effect
updated nextafter* to use FORCE_EVAL, it can be used in many other
places in the math code to improve readability.
-rw-r--r--src/internal/libm.h13
-rw-r--r--src/math/nextafter.c6
-rw-r--r--src/math/nextafterf.c6
-rw-r--r--src/math/nextafterl.c12
-rw-r--r--src/math/nexttoward.c6
-rw-r--r--src/math/nexttowardf.c6
6 files changed, 25 insertions, 24 deletions
diff --git a/src/internal/libm.h b/src/internal/libm.h
index 8c5474a8..a71c4c05 100644
--- a/src/internal/libm.h
+++ b/src/internal/libm.h
@@ -32,6 +32,19 @@ union dshape {
 	uint64_t bits;
 };
 
+#define FORCE_EVAL(x) do {                          \
+	if (sizeof(x) == sizeof(float)) {           \
+		volatile float __x;                 \
+		__x = (x);                          \
+	} else if (sizeof(x) == sizeof(double)) {   \
+		volatile double __x;                \
+		__x = (x);                          \
+	} else {                                    \
+		volatile long double __x;           \
+		__x = (x);                          \
+	}                                           \
+} while(0)
+
 /* Get two 32 bit ints from a double.  */
 #define EXTRACT_WORDS(hi,lo,d)                                  \
 do {                                                            \
diff --git a/src/math/nextafter.c b/src/math/nextafter.c
index e4bfb022..a3b42c99 100644
--- a/src/math/nextafter.c
+++ b/src/math/nextafter.c
@@ -29,9 +29,7 @@ double nextafter(double x, double y)
 	if (e == 0x7ff)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (e == 0) {
-		volatile double z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (e == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }
diff --git a/src/math/nextafterf.c b/src/math/nextafterf.c
index 47775b92..b703487b 100644
--- a/src/math/nextafterf.c
+++ b/src/math/nextafterf.c
@@ -28,9 +28,7 @@ float nextafterf(float x, float y)
 	if (e == 0x7f800000)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (e == 0) {
-		volatile float z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (e == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }
diff --git a/src/math/nextafterl.c b/src/math/nextafterl.c
index c09d9dd0..edc3cc9c 100644
--- a/src/math/nextafterl.c
+++ b/src/math/nextafterl.c
@@ -38,10 +38,8 @@ long double nextafterl(long double x, long double y)
 	if (ux.bits.exp == 0x7fff)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (ux.bits.exp == 0) {
-		volatile float z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (ux.bits.exp == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }
 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
@@ -77,10 +75,8 @@ long double nextafterl(long double x, long double y)
 	if (ux.bits.exp == 0x7fff)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (ux.bits.exp == 0) {
-		volatile float z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (ux.bits.exp == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }
 #endif
diff --git a/src/math/nexttoward.c b/src/math/nexttoward.c
index 43f8fee8..7355f2f1 100644
--- a/src/math/nexttoward.c
+++ b/src/math/nexttoward.c
@@ -38,10 +38,8 @@ double nexttoward(double x, long double y)
 	if (e == 0x7ff)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (e == 0) {
-		volatile float z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (e == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }
 #endif
diff --git a/src/math/nexttowardf.c b/src/math/nexttowardf.c
index e8e6f676..8648be6a 100644
--- a/src/math/nexttowardf.c
+++ b/src/math/nexttowardf.c
@@ -30,9 +30,7 @@ float nexttowardf(float x, long double y)
 	if (e == 0x7f800000)
 		return x + x;
 	/* raise underflow if ux.value is subnormal or zero */
-	if (e == 0) {
-		volatile float z;
-		z = x*x + ux.value*ux.value;
-	}
+	if (e == 0)
+		FORCE_EVAL(x*x + ux.value*ux.value);
 	return ux.value;
 }