about summary refs log tree commit diff
path: root/src/math/fma.c
diff options
context:
space:
mode:
authorSzabolcs Nagy <nsz@port70.net>2013-10-07 18:46:43 +0000
committerSzabolcs Nagy <nsz@port70.net>2013-10-07 18:46:43 +0000
commit8f438115f2c12b40fa7b1884f87db72857af67f6 (patch)
tree2c68fb9d043c1ffe011b209a69a7a85a6e504346 /src/math/fma.c
parent4b539a826b64d846d02a6cde9d6bcff22472af88 (diff)
downloadmusl-8f438115f2c12b40fa7b1884f87db72857af67f6.tar.gz
musl-8f438115f2c12b40fa7b1884f87db72857af67f6.tar.xz
musl-8f438115f2c12b40fa7b1884f87db72857af67f6.zip
math: fix rare underflow issue in fma
the issue is described in commits 1e5eb73545ca6cfe8b918798835aaf6e07af5beb
and ffd8ac2dd50f99c3c83d7d9d845df9874ec3e7d5
Diffstat (limited to 'src/math/fma.c')
-rw-r--r--src/math/fma.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/math/fma.c b/src/math/fma.c
index 84868be5..02f5c865 100644
--- a/src/math/fma.c
+++ b/src/math/fma.c
@@ -431,12 +431,24 @@ double fma(double x, double y, double z)
 		/*
 		 * There is no need to worry about double rounding in directed
 		 * rounding modes.
-		 * TODO: underflow is not raised properly, example in downward rounding:
+		 * But underflow may not be raised properly, example in downward rounding:
 		 * fma(0x1.000000001p-1000, 0x1.000000001p-30, -0x1p-1066)
 		 */
+		double ret;
+#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
+		int e = fetestexcept(FE_INEXACT);
+		feclearexcept(FE_INEXACT);
+#endif
 		fesetround(oround);
 		adj = r.lo + xy.lo;
-		return scalbn(r.hi + adj, spread);
+		ret = scalbn(r.hi + adj, spread);
+#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
+		if (ilogb(ret) < -1022 && fetestexcept(FE_INEXACT))
+			feraiseexcept(FE_UNDERFLOW);
+		else if (e)
+			feraiseexcept(FE_INEXACT);
+#endif
+		return ret;
 	}
 
 	adj = add_adjusted(r.lo, xy.lo);