Module Name:    src
Committed By:   martin
Date:           Mon May 25 15:26:05 UTC 2020

Modified Files:
        src/lib/libc/compat/gen [netbsd-9]: compat_ldexp_ieee754.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #920):

        lib/libc/compat/gen/compat_ldexp_ieee754.c: revision 1.8

Teach libc's compat ldexp stub to raise fp exceptions.

This ldexp stub will shadow the ldexp weak alias for scalbn in libm,
which is unfortunate but hard to fix properly without chasing the
mythical libc bump beast.  With the change here, we should raise all
the same exceptions that libm's scalbn does -- overflow, underflow,
inexact-result, and (for signalling NaN only) invalid-operation.

This in turn should correct the missing overflow/underflow exceptions
of our portable C fma, and perhaps other routines.

XXX pullup


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.7.16.1 src/lib/libc/compat/gen/compat_ldexp_ieee754.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/compat/gen/compat_ldexp_ieee754.c
diff -u src/lib/libc/compat/gen/compat_ldexp_ieee754.c:1.7 src/lib/libc/compat/gen/compat_ldexp_ieee754.c:1.7.16.1
--- src/lib/libc/compat/gen/compat_ldexp_ieee754.c:1.7	Sat Aug 27 09:35:13 2016
+++ src/lib/libc/compat/gen/compat_ldexp_ieee754.c	Mon May 25 15:26:05 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: compat_ldexp_ieee754.c,v 1.7 2016/08/27 09:35:13 christos Exp $ */
+/* $NetBSD: compat_ldexp_ieee754.c,v 1.7.16.1 2020/05/25 15:26:05 martin Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -31,14 +31,34 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.7 2016/08/27 09:35:13 christos Exp $");
+__RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.7.16.1 2020/05/25 15:26:05 martin Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
+
 #include <machine/ieee.h>
+
 #include <errno.h>
+#include <float.h>
+#include <math.h>
+
+static volatile const double tiny = DBL_MIN, huge = DBL_MAX;
+
+static double
+underflow(double val)
+{
+
+	errno = ERANGE;
+	return (val < 0 ? -tiny*tiny : tiny*tiny);
+}
+
+static double
+overflow(double val)
+{
 
-double ldexp(double, int);
+	errno = ERANGE;
+	return (val < 0 ? -huge*huge : huge*huge);
+}
 
 /*
  * Multiply the given value by 2^expon.
@@ -53,10 +73,13 @@ ldexp(double val, int expon)
 	oldexp = u.dblu_dbl.dbl_exp;
 
 	/*
-	 * If input is zero, Inf or NaN, just return it.
+	 * If input is zero, Inf or NaN, just return it, but raise
+	 * invalid exception if it is a signalling NaN: adding any of
+	 * these inputs to itself gives itself as output; arithmetic on
+	 * a signalling NaN additionally raises invalid-operation.
 	 */
 	if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN)
-		return (val);
+		return (val + val);
 
 	if (oldexp == 0) {
 		/*
@@ -68,17 +91,13 @@ ldexp(double val, int expon)
 			 * Optimization: if the scaling can be done in a single
 			 * multiply, or underflows, just do it now.
 			 */
-			if (expon <= -DBL_FRACBITS) {
-				errno = ERANGE;
-				return (val < 0.0 ? -0.0 : 0.0);
-			}
+			if (expon <= -DBL_FRACBITS)
+				return underflow(val);
 			mul.dblu_d = 0.0;
 			mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
 			u.dblu_d *= mul.dblu_d;
-			if (u.dblu_d == 0.0) {
-				errno = ERANGE;
-				return (val < 0.0 ? -0.0 : 0.0);
-			}
+			if (u.dblu_d == 0.0)
+				return underflow(val);
 			return (u.dblu_d);
 		} else {
 			/*
@@ -105,20 +124,14 @@ ldexp(double val, int expon)
 		/*
 		 * The result overflowed; return +/-Inf.
 		 */
-		u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN;
-		u.dblu_dbl.dbl_frach = 0;
-		u.dblu_dbl.dbl_fracl = 0;
-		errno = ERANGE;
-		return (u.dblu_d);
+		return overflow(val);
 	} else if (newexp <= 0) {
 		/*
 		 * The output number is either denormal or underflows (see
 		 * comments in machine/ieee.h).
 		 */
-		if (newexp <= -DBL_FRACBITS) {
-			errno = ERANGE;
-			return (val < 0.0 ? -0.0 : 0.0);
-		}
+		if (newexp <= -DBL_FRACBITS)
+			return underflow(val);
 		/*
 		 * Denormalize the result.  We do this with a multiply.  If
 		 * expon is very large, it won't fit in a double, so we have

Reply via email to