From 59ccc04e14cee9c86f3488b2302cac7cf6bccbcd Mon Sep 17 00:00:00 2001 From: riastradh Date: Thu, 21 May 2020 05:56:31 +0000 Subject: [PATCH] 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 --- lib/libc/compat/gen/compat_ldexp_ieee754.c | 61 +++++++++++++--------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/libc/compat/gen/compat_ldexp_ieee754.c b/lib/libc/compat/gen/compat_ldexp_ieee754.c index 6e7fafc7659e..8c8cb49a935c 100644 --- a/lib/libc/compat/gen/compat_ldexp_ieee754.c +++ b/lib/libc/compat/gen/compat_ldexp_ieee754.c @@ -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.8 2020/05/21 05:56:31 riastradh Exp $ */ /*- * Copyright (c) 1999 The NetBSD Foundation, Inc. @@ -31,14 +31,34 @@ #include #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.8 2020/05/21 05:56:31 riastradh Exp $"); #endif /* LIBC_SCCS and not lint */ #include -#include -#include -double ldexp(double, int); +#include + +#include +#include +#include + +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) +{ + + 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