2 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
35 * Fused multiply-add: Compute x * y + z with a single rounding error.
37 * We use scaling to avoid overflow/underflow, along with the
38 * canonical precision-doubling technique adapted from:
40 * Dekker, T. A Floating-Point Technique for Extending the
41 * Available Precision. Numer. Math. 18, 224-242 (1971).
43 * This algorithm is sensitive to the rounding precision. FPUs such
44 * as the i387 must be set in double-precision mode if variables are
45 * to be stored in FP registers in order to avoid incorrect results.
46 * This is the default on FreeBSD, but not on many other systems.
48 * Hardware instructions should be used on architectures that support it,
49 * since this implementation will likely be several times slower.
51 #if LDBL_MANT_DIG != 113
53 fma(double x, double y, double z)
55 static const double split = 0x1p27 + 1.0;
57 double c, cc, hx, hy, p, q, tx, ty;
65 if (x == 0.0 || y == 0.0)
68 /* Results of frexp() are undefined for these cases. */
69 if (!isfinite(x) || !isfinite(y) || !isfinite(z))
75 oround = fegetround();
76 spread = ex + ey - ez;
79 * If x * y and z are many orders of magnitude apart, the scaling
80 * will overflow, so we handle these cases specially. Rounding
81 * modes other than FE_TONEAREST are painful.
83 if (spread > DBL_MANT_DIG * 2) {
85 feraiseexcept(FE_INEXACT);
90 if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
94 if (!fetestexcept(FE_INEXACT))
103 if (!fetestexcept(FE_INEXACT))
104 r = nextafter(r, -INFINITY);
107 default: /* FE_UPWARD */
112 if (!fetestexcept(FE_INEXACT))
113 r = nextafter(r, INFINITY);
118 if (spread < -DBL_MANT_DIG) {
119 feraiseexcept(FE_INEXACT);
121 feraiseexcept(FE_UNDERFLOW);
126 if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
129 return (nextafter(z, 0));
131 if (x > 0.0 ^ y < 0.0)
134 return (nextafter(z, -INFINITY));
135 default: /* FE_UPWARD */
136 if (x > 0.0 ^ y < 0.0)
137 return (nextafter(z, INFINITY));
144 * Use Dekker's algorithm to perform the multiplication and
145 * subsequent addition in twice the machine precision.
146 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
148 fesetround(FE_TONEAREST);
161 q = hx * ty + tx * hy;
163 cc = p - c + q + tx * ty;
165 zs = ldexp(zs, -spread);
168 rr = (c - (r - s)) + (zs - s) + cc;
171 if (spread + ilogb(r) > -1023) {
176 * The result is subnormal, so we round before scaling to
177 * avoid double rounding.
179 p = ldexp(copysign(0x1p-1022, r), -spread);
182 cc = (r - (c - s)) + (p - s) + rr;
186 return (ldexp(r, spread));
188 #else /* LDBL_MANT_DIG == 113 */
190 * 113 bits of precision is more than twice the precision of a double,
191 * so it is enough to represent the intermediate product exactly.
194 fma(double x, double y, double z)
196 return ((long double)x * y + z);
198 #endif /* LDBL_MANT_DIG != 113 */
200 #if (LDBL_MANT_DIG == 53)
201 __weak_reference(fma, fmal);