1 /* origin: FreeBSD /usr/src/lib/msun/src/s_fmal.c */
3 * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
31 long double fmal(long double x, long double y, long double z)
35 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
37 #if LDBL_MANT_DIG == 64
38 #define LASTBIT(u) (u.i.m & 1)
39 #define SPLIT (0x1p32L + 1)
40 #elif LDBL_MANT_DIG == 113
41 #define LASTBIT(u) (u.i.lo & 1)
42 #define SPLIT (0x1p57L + 1)
46 * A struct dd represents a floating-point number with twice the precision
47 * of a long double. We maintain the invariant that "hi" stores the high-order
56 * Compute a+b exactly, returning the exact result in a struct dd. We assume
57 * that both a and b are finite, but make no assumptions about their relative
60 static inline struct dd dd_add(long double a, long double b)
67 ret.lo = (a - (ret.hi - s)) + (b - s);
72 * Compute a+b, with a small tweak: The least significant bit of the
73 * result is adjusted into a sticky bit summarizing all the bits that
74 * were lost to rounding. This adjustment negates the effects of double
75 * rounding when the result is added to another number with a higher
76 * exponent. For an explanation of round and sticky bits, see any reference
77 * on FPU design, e.g.,
79 * J. Coonen. An Implementation Guide to a Proposed Standard for
80 * Floating-Point Arithmetic. Computer, vol. 13, no. 1, Jan 1980.
82 static inline long double add_adjusted(long double a, long double b)
91 sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
97 * Compute ldexp(a+b, scale) with a single rounding error. It is assumed
98 * that the result will be subnormal, and care is taken to ensure that
99 * double rounding does not occur.
101 static inline long double add_and_denormalize(long double a, long double b, int scale)
110 * If we are losing at least two bits of accuracy to denormalization,
111 * then the first lost bit becomes a round bit, and we adjust the
112 * lowest bit of sum.hi to make it a sticky bit summarizing all the
113 * bits in sum.lo. With the sticky bit adjusted, the hardware will
114 * break any ties in the correct direction.
116 * If we are losing only one bit to denormalization, however, we must
117 * break the ties manually.
121 bits_lost = -u.i.se - scale + 1;
122 if ((bits_lost != 1) ^ LASTBIT(u))
123 sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
125 return scalbnl(sum.hi, scale);
129 * Compute a*b exactly, returning the exact result in a struct dd. We assume
130 * that both a and b are normalized, so no underflow or overflow will occur.
131 * The current rounding mode must be round-to-nearest.
133 static inline struct dd dd_mul(long double a, long double b)
136 long double ha, hb, la, lb, p, q;
149 q = ha * lb + la * hb;
152 ret.lo = p - ret.hi + q + la * lb;
157 * Fused multiply-add: Compute x * y + z with a single rounding error.
159 * We use scaling to avoid overflow/underflow, along with the
160 * canonical precision-doubling technique adapted from:
162 * Dekker, T. A Floating-Point Technique for Extending the
163 * Available Precision. Numer. Math. 18, 224-242 (1971).
165 long double fmal(long double x, long double y, long double z)
167 #pragma STDC FENV_ACCESS ON
168 long double xs, ys, zs, adj;
175 * Handle special cases. The order of operations and the particular
176 * return values here are crucial in handling special cases involving
177 * infinities, NaNs, overflows, and signed zeroes correctly.
179 if (!isfinite(x) || !isfinite(y))
183 if (x == 0.0 || y == 0.0)
191 oround = fegetround();
192 spread = ex + ey - ez;
195 * If x * y and z are many orders of magnitude apart, the scaling
196 * will overflow, so we handle these cases specially. Rounding
197 * modes other than FE_TONEAREST are painful.
199 if (spread < -LDBL_MANT_DIG) {
201 feraiseexcept(FE_INEXACT);
205 feraiseexcept(FE_UNDERFLOW);
208 default: /* FE_TONEAREST */
212 if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
215 return (nextafterl(z, 0));
219 if (x > 0.0 ^ y < 0.0)
222 return (nextafterl(z, -INFINITY));
226 if (x > 0.0 ^ y < 0.0)
227 return (nextafterl(z, INFINITY));
233 if (spread <= LDBL_MANT_DIG * 2)
234 zs = scalbnl(zs, -spread);
236 zs = copysignl(LDBL_MIN, zs);
238 fesetround(FE_TONEAREST);
241 * Basic approach for round-to-nearest:
243 * (xy.hi, xy.lo) = x * y (exact)
244 * (r.hi, r.lo) = xy.hi + z (exact)
245 * adj = xy.lo + r.lo (inexact; low bit is sticky)
246 * result = r.hi + adj (correctly rounded)
249 r = dd_add(xy.hi, zs);
255 * When the addends cancel to 0, ensure that the result has
259 volatile long double vzs = zs; /* XXX gcc CSE bug workaround */
260 return xy.hi + vzs + scalbnl(xy.lo, spread);
263 if (oround != FE_TONEAREST) {
265 * There is no need to worry about double rounding in directed
267 * But underflow may not be raised correctly, example in downward rounding:
268 * fmal(0x1.0000000001p-16000L, 0x1.0000000001p-400L, -0x1p-16440L)
271 #if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
272 int e = fetestexcept(FE_INEXACT);
273 feclearexcept(FE_INEXACT);
277 ret = scalbnl(r.hi + adj, spread);
278 #if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
279 if (ilogbl(ret) < -16382 && fetestexcept(FE_INEXACT))
280 feraiseexcept(FE_UNDERFLOW);
282 feraiseexcept(FE_INEXACT);
287 adj = add_adjusted(r.lo, xy.lo);
288 if (spread + ilogbl(r.hi) > -16383)
289 return scalbnl(r.hi + adj, spread);
291 return add_and_denormalize(r.hi, adj, spread);