xref: /freebsd/lib/msun/src/s_fmal.c (revision 34f746cc7f8a8dd261027a8b392b76e70adc8438)
165e60ab1SDavid Schultz /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
477927540SDavid Schultz  * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
565e60ab1SDavid Schultz  * All rights reserved.
665e60ab1SDavid Schultz  *
765e60ab1SDavid Schultz  * Redistribution and use in source and binary forms, with or without
865e60ab1SDavid Schultz  * modification, are permitted provided that the following conditions
965e60ab1SDavid Schultz  * are met:
1065e60ab1SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
1165e60ab1SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
1265e60ab1SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
1365e60ab1SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
1465e60ab1SDavid Schultz  *    documentation and/or other materials provided with the distribution.
1565e60ab1SDavid Schultz  *
1665e60ab1SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1765e60ab1SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1865e60ab1SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1965e60ab1SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2065e60ab1SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2165e60ab1SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2265e60ab1SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2365e60ab1SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2465e60ab1SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2565e60ab1SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2665e60ab1SDavid Schultz  * SUCH DAMAGE.
2765e60ab1SDavid Schultz  */
2865e60ab1SDavid Schultz 
2965e60ab1SDavid Schultz #include <fenv.h>
3065e60ab1SDavid Schultz #include <float.h>
3165e60ab1SDavid Schultz #include <math.h>
3265e60ab1SDavid Schultz 
33ec950830SDavid Schultz #include "fpmath.h"
34ec950830SDavid Schultz 
3565e60ab1SDavid Schultz /*
3677927540SDavid Schultz  * A struct dd represents a floating-point number with twice the precision
3777927540SDavid Schultz  * of a long double.  We maintain the invariant that "hi" stores the high-order
3877927540SDavid Schultz  * bits of the result.
3977927540SDavid Schultz  */
4077927540SDavid Schultz struct dd {
4177927540SDavid Schultz 	long double hi;
4277927540SDavid Schultz 	long double lo;
4377927540SDavid Schultz };
4477927540SDavid Schultz 
4577927540SDavid Schultz /*
4677927540SDavid Schultz  * Compute a+b exactly, returning the exact result in a struct dd.  We assume
4777927540SDavid Schultz  * that both a and b are finite, but make no assumptions about their relative
4877927540SDavid Schultz  * magnitudes.
4977927540SDavid Schultz  */
5077927540SDavid Schultz static inline struct dd
5177927540SDavid Schultz dd_add(long double a, long double b)
5277927540SDavid Schultz {
5377927540SDavid Schultz 	struct dd ret;
5477927540SDavid Schultz 	long double s;
5577927540SDavid Schultz 
5677927540SDavid Schultz 	ret.hi = a + b;
5777927540SDavid Schultz 	s = ret.hi - a;
5877927540SDavid Schultz 	ret.lo = (a - (ret.hi - s)) + (b - s);
5977927540SDavid Schultz 	return (ret);
6077927540SDavid Schultz }
6177927540SDavid Schultz 
6277927540SDavid Schultz /*
63ec950830SDavid Schultz  * Compute a+b, with a small tweak:  The least significant bit of the
64ec950830SDavid Schultz  * result is adjusted into a sticky bit summarizing all the bits that
65ec950830SDavid Schultz  * were lost to rounding.  This adjustment negates the effects of double
66ec950830SDavid Schultz  * rounding when the result is added to another number with a higher
67ec950830SDavid Schultz  * exponent.  For an explanation of round and sticky bits, see any reference
68ec950830SDavid Schultz  * on FPU design, e.g.,
69ec950830SDavid Schultz  *
70ec950830SDavid Schultz  *     J. Coonen.  An Implementation Guide to a Proposed Standard for
71ec950830SDavid Schultz  *     Floating-Point Arithmetic.  Computer, vol. 13, no. 1, Jan 1980.
72ec950830SDavid Schultz  */
73ec950830SDavid Schultz static inline long double
74ec950830SDavid Schultz add_adjusted(long double a, long double b)
75ec950830SDavid Schultz {
76ec950830SDavid Schultz 	struct dd sum;
77ec950830SDavid Schultz 	union IEEEl2bits u;
78ec950830SDavid Schultz 
79ec950830SDavid Schultz 	sum = dd_add(a, b);
80ec950830SDavid Schultz 	if (sum.lo != 0) {
81ec950830SDavid Schultz 		u.e = sum.hi;
82ec950830SDavid Schultz 		if ((u.bits.manl & 1) == 0)
83ec950830SDavid Schultz 			sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
84ec950830SDavid Schultz 	}
85ec950830SDavid Schultz 	return (sum.hi);
86ec950830SDavid Schultz }
87ec950830SDavid Schultz 
88ec950830SDavid Schultz /*
89ec950830SDavid Schultz  * Compute ldexp(a+b, scale) with a single rounding error. It is assumed
90ec950830SDavid Schultz  * that the result will be subnormal, and care is taken to ensure that
91ec950830SDavid Schultz  * double rounding does not occur.
92ec950830SDavid Schultz  */
93ec950830SDavid Schultz static inline long double
94ec950830SDavid Schultz add_and_denormalize(long double a, long double b, int scale)
95ec950830SDavid Schultz {
96ec950830SDavid Schultz 	struct dd sum;
97ec950830SDavid Schultz 	int bits_lost;
98ec950830SDavid Schultz 	union IEEEl2bits u;
99ec950830SDavid Schultz 
100ec950830SDavid Schultz 	sum = dd_add(a, b);
101ec950830SDavid Schultz 
102ec950830SDavid Schultz 	/*
103ec950830SDavid Schultz 	 * If we are losing at least two bits of accuracy to denormalization,
104ec950830SDavid Schultz 	 * then the first lost bit becomes a round bit, and we adjust the
105ec950830SDavid Schultz 	 * lowest bit of sum.hi to make it a sticky bit summarizing all the
106ec950830SDavid Schultz 	 * bits in sum.lo. With the sticky bit adjusted, the hardware will
107ec950830SDavid Schultz 	 * break any ties in the correct direction.
108ec950830SDavid Schultz 	 *
109ec950830SDavid Schultz 	 * If we are losing only one bit to denormalization, however, we must
110ec950830SDavid Schultz 	 * break the ties manually.
111ec950830SDavid Schultz 	 */
112ec950830SDavid Schultz 	if (sum.lo != 0) {
113ec950830SDavid Schultz 		u.e = sum.hi;
114ec950830SDavid Schultz 		bits_lost = -u.bits.exp - scale + 1;
11519eb5fb0SEitan Adler 		if ((bits_lost != 1) ^ (int)(u.bits.manl & 1))
116ec950830SDavid Schultz 			sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
117ec950830SDavid Schultz 	}
118ec950830SDavid Schultz 	return (ldexp(sum.hi, scale));
119ec950830SDavid Schultz }
120ec950830SDavid Schultz 
121ec950830SDavid Schultz /*
12277927540SDavid Schultz  * Compute a*b exactly, returning the exact result in a struct dd.  We assume
12377927540SDavid Schultz  * that both a and b are normalized, so no underflow or overflow will occur.
12477927540SDavid Schultz  * The current rounding mode must be round-to-nearest.
12577927540SDavid Schultz  */
12677927540SDavid Schultz static inline struct dd
12777927540SDavid Schultz dd_mul(long double a, long double b)
12877927540SDavid Schultz {
12977927540SDavid Schultz #if LDBL_MANT_DIG == 64
13077927540SDavid Schultz 	static const long double split = 0x1p32L + 1.0;
13177927540SDavid Schultz #elif LDBL_MANT_DIG == 113
13277927540SDavid Schultz 	static const long double split = 0x1p57L + 1.0;
13377927540SDavid Schultz #endif
13477927540SDavid Schultz 	struct dd ret;
13577927540SDavid Schultz 	long double ha, hb, la, lb, p, q;
13677927540SDavid Schultz 
13777927540SDavid Schultz 	p = a * split;
13877927540SDavid Schultz 	ha = a - p;
13977927540SDavid Schultz 	ha += p;
14077927540SDavid Schultz 	la = a - ha;
14177927540SDavid Schultz 
14277927540SDavid Schultz 	p = b * split;
14377927540SDavid Schultz 	hb = b - p;
14477927540SDavid Schultz 	hb += p;
14577927540SDavid Schultz 	lb = b - hb;
14677927540SDavid Schultz 
14777927540SDavid Schultz 	p = ha * hb;
14877927540SDavid Schultz 	q = ha * lb + la * hb;
14977927540SDavid Schultz 
15077927540SDavid Schultz 	ret.hi = p + q;
15177927540SDavid Schultz 	ret.lo = p - ret.hi + q + la * lb;
15277927540SDavid Schultz 	return (ret);
15377927540SDavid Schultz }
15477927540SDavid Schultz 
15577927540SDavid Schultz /*
15665e60ab1SDavid Schultz  * Fused multiply-add: Compute x * y + z with a single rounding error.
15765e60ab1SDavid Schultz  *
15865e60ab1SDavid Schultz  * We use scaling to avoid overflow/underflow, along with the
15965e60ab1SDavid Schultz  * canonical precision-doubling technique adapted from:
16065e60ab1SDavid Schultz  *
16165e60ab1SDavid Schultz  *	Dekker, T.  A Floating-Point Technique for Extending the
16265e60ab1SDavid Schultz  *	Available Precision.  Numer. Math. 18, 224-242 (1971).
16365e60ab1SDavid Schultz  */
16465e60ab1SDavid Schultz long double
16565e60ab1SDavid Schultz fmal(long double x, long double y, long double z)
16665e60ab1SDavid Schultz {
167ec950830SDavid Schultz 	long double xs, ys, zs, adj;
168ec950830SDavid Schultz 	struct dd xy, r;
16965e60ab1SDavid Schultz 	int oround;
17065e60ab1SDavid Schultz 	int ex, ey, ez;
17165e60ab1SDavid Schultz 	int spread;
17265e60ab1SDavid Schultz 
17392a1a6e1SDavid Schultz 	/*
17492a1a6e1SDavid Schultz 	 * Handle special cases. The order of operations and the particular
17592a1a6e1SDavid Schultz 	 * return values here are crucial in handling special cases involving
17692a1a6e1SDavid Schultz 	 * infinities, NaNs, overflows, and signed zeroes correctly.
17792a1a6e1SDavid Schultz 	 */
17865e60ab1SDavid Schultz 	if (x == 0.0 || y == 0.0)
17965e60ab1SDavid Schultz 		return (x * y + z);
18092a1a6e1SDavid Schultz 	if (z == 0.0)
18192a1a6e1SDavid Schultz 		return (x * y);
18292a1a6e1SDavid Schultz 	if (!isfinite(x) || !isfinite(y))
18365e60ab1SDavid Schultz 		return (x * y + z);
18492a1a6e1SDavid Schultz 	if (!isfinite(z))
18592a1a6e1SDavid Schultz 		return (z);
18665e60ab1SDavid Schultz 
18765e60ab1SDavid Schultz 	xs = frexpl(x, &ex);
18865e60ab1SDavid Schultz 	ys = frexpl(y, &ey);
18965e60ab1SDavid Schultz 	zs = frexpl(z, &ez);
19065e60ab1SDavid Schultz 	oround = fegetround();
19165e60ab1SDavid Schultz 	spread = ex + ey - ez;
19265e60ab1SDavid Schultz 
19365e60ab1SDavid Schultz 	/*
19465e60ab1SDavid Schultz 	 * If x * y and z are many orders of magnitude apart, the scaling
19565e60ab1SDavid Schultz 	 * will overflow, so we handle these cases specially.  Rounding
19665e60ab1SDavid Schultz 	 * modes other than FE_TONEAREST are painful.
19765e60ab1SDavid Schultz 	 */
19865e60ab1SDavid Schultz 	if (spread < -LDBL_MANT_DIG) {
19965e60ab1SDavid Schultz 		feraiseexcept(FE_INEXACT);
20065e60ab1SDavid Schultz 		if (!isnormal(z))
20165e60ab1SDavid Schultz 			feraiseexcept(FE_UNDERFLOW);
20265e60ab1SDavid Schultz 		switch (oround) {
20365e60ab1SDavid Schultz 		case FE_TONEAREST:
20465e60ab1SDavid Schultz 			return (z);
20565e60ab1SDavid Schultz 		case FE_TOWARDZERO:
20600160652SEd Maste 			if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
20765e60ab1SDavid Schultz 				return (z);
20865e60ab1SDavid Schultz 			else
20965e60ab1SDavid Schultz 				return (nextafterl(z, 0));
21065e60ab1SDavid Schultz 		case FE_DOWNWARD:
21100160652SEd Maste 			if ((x > 0.0) ^ (y < 0.0))
21265e60ab1SDavid Schultz 				return (z);
21365e60ab1SDavid Schultz 			else
21465e60ab1SDavid Schultz 				return (nextafterl(z, -INFINITY));
21565e60ab1SDavid Schultz 		default:	/* FE_UPWARD */
21600160652SEd Maste 			if ((x > 0.0) ^ (y < 0.0))
21765e60ab1SDavid Schultz 				return (nextafterl(z, INFINITY));
21865e60ab1SDavid Schultz 			else
21965e60ab1SDavid Schultz 				return (z);
22065e60ab1SDavid Schultz 		}
22165e60ab1SDavid Schultz 	}
222ec950830SDavid Schultz 	if (spread <= LDBL_MANT_DIG * 2)
223ec950830SDavid Schultz 		zs = ldexpl(zs, -spread);
224ec950830SDavid Schultz 	else
225ec950830SDavid Schultz 		zs = copysignl(LDBL_MIN, zs);
22665e60ab1SDavid Schultz 
22765e60ab1SDavid Schultz 	fesetround(FE_TONEAREST);
22892927b8bSEd Maste 	/* work around clang issue #8472 */
2297dbbb6ddSDavid Schultz 	volatile long double vxs = xs;
23065e60ab1SDavid Schultz 
231ec950830SDavid Schultz 	/*
232ec950830SDavid Schultz 	 * Basic approach for round-to-nearest:
233ec950830SDavid Schultz 	 *
234ec950830SDavid Schultz 	 *     (xy.hi, xy.lo) = x * y		(exact)
235ec950830SDavid Schultz 	 *     (r.hi, r.lo)   = xy.hi + z	(exact)
236ec950830SDavid Schultz 	 *     adj = xy.lo + r.lo		(inexact; low bit is sticky)
237ec950830SDavid Schultz 	 *     result = r.hi + adj		(correctly rounded)
238ec950830SDavid Schultz 	 */
2397dbbb6ddSDavid Schultz 	xy = dd_mul(vxs, ys);
24077927540SDavid Schultz 	r = dd_add(xy.hi, zs);
241ec950830SDavid Schultz 
2420c7e4d5fSDavid Schultz 	spread = ex + ey;
2430c7e4d5fSDavid Schultz 
244*34f746ccSSteve Kargl 	if (r.hi == 0.0 && xy.lo == 0) {
245ec950830SDavid Schultz 		/*
246ec950830SDavid Schultz 		 * When the addends cancel to 0, ensure that the result has
247ec950830SDavid Schultz 		 * the correct sign.
248ec950830SDavid Schultz 		 */
249ec950830SDavid Schultz 		fesetround(oround);
250ec950830SDavid Schultz 		volatile long double vzs = zs; /* XXX gcc CSE bug workaround */
251*34f746ccSSteve Kargl 		return (xy.hi + vzs);
252ec950830SDavid Schultz 	}
25365e60ab1SDavid Schultz 
254ec950830SDavid Schultz 	if (oround != FE_TONEAREST) {
2552c243582SDavid Schultz 		/*
256ec950830SDavid Schultz 		 * There is no need to worry about double rounding in directed
257ec950830SDavid Schultz 		 * rounding modes.
2582c243582SDavid Schultz 		 */
2592c243582SDavid Schultz 		fesetround(oround);
26092927b8bSEd Maste 		/* work around clang issue #8472 */
2617dbbb6ddSDavid Schultz 		volatile long double vrlo = r.lo;
2627dbbb6ddSDavid Schultz 		adj = vrlo + xy.lo;
263ec950830SDavid Schultz 		return (ldexpl(r.hi + adj, spread));
2642c243582SDavid Schultz 	}
265ec950830SDavid Schultz 
266ec950830SDavid Schultz 	adj = add_adjusted(r.lo, xy.lo);
267ec950830SDavid Schultz 	if (spread + ilogbl(r.hi) > -16383)
268ec950830SDavid Schultz 		return (ldexpl(r.hi + adj, spread));
269ec950830SDavid Schultz 	else
270ec950830SDavid Schultz 		return (add_and_denormalize(r.hi, adj, spread));
27165e60ab1SDavid Schultz }
272