1 /* $OpenBSD: s_fma.c,v 1.6 2013/11/12 19:00:38 martynas Exp $ */
4 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Fused multiply-add: Compute x * y + z with a single rounding error.
36 * We use scaling to avoid overflow/underflow, along with the
37 * canonical precision-doubling technique adapted from:
39 * Dekker, T. A Floating-Point Technique for Extending the
40 * Available Precision. Numer. Math. 18, 224-242 (1971).
42 * This algorithm is sensitive to the rounding precision. FPUs such
43 * as the i387 must be set in double-precision mode if variables are
44 * to be stored in FP registers in order to avoid incorrect results.
45 * This is the default on FreeBSD, but not on many other systems.
47 * Hardware instructions should be used on architectures that support it,
48 * since this implementation will likely be several times slower.
50 #if LDBL_MANT_DIG != 113
52 fma(double x, double y, double z)
54 static const double split = 0x1p27 + 1.0;
56 double c, cc, hx, hy, p, q, tx, ty;
63 * Handle special cases. The order of operations and the particular
64 * return values here are crucial in handling special cases involving
65 * infinities, NaNs, overflows, and signed zeroes correctly.
67 if (x == 0.0 || y == 0.0)
71 if (!isfinite(x) || !isfinite(y))
79 oround = fegetround();
80 spread = ex + ey - ez;
83 * If x * y and z are many orders of magnitude apart, the scaling
84 * will overflow, so we handle these cases specially. Rounding
85 * modes other than FE_TONEAREST are painful.
87 if (spread > DBL_MANT_DIG * 2) {
89 feraiseexcept(FE_INEXACT);
94 if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
98 if (!fetestexcept(FE_INEXACT))
107 if (!fetestexcept(FE_INEXACT))
108 r = nextafter(r, -INFINITY);
111 default: /* FE_UPWARD */
116 if (!fetestexcept(FE_INEXACT))
117 r = nextafter(r, INFINITY);
122 if (spread < -DBL_MANT_DIG) {
123 feraiseexcept(FE_INEXACT);
125 feraiseexcept(FE_UNDERFLOW);
130 if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
133 return (nextafter(z, 0));
135 if ((x > 0.0) ^ (y < 0.0))
138 return (nextafter(z, -INFINITY));
139 default: /* FE_UPWARD */
140 if ((x > 0.0) ^ (y < 0.0))
141 return (nextafter(z, INFINITY));
148 * Use Dekker's algorithm to perform the multiplication and
149 * subsequent addition in twice the machine precision.
150 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
152 fesetround(FE_TONEAREST);
165 q = hx * ty + tx * hy;
167 cc = p - c + q + tx * ty;
169 zs = ldexp(zs, -spread);
172 rr = (c - (r - s)) + (zs - s) + cc;
175 if (spread + ilogb(r) > -1023) {
180 * The result is subnormal, so we round before scaling to
181 * avoid double rounding.
183 p = ldexp(copysign(0x1p-1022, r), -spread);
186 cc = (r - (c - s)) + (p - s) + rr;
190 return (ldexp(r, spread));
192 #else /* LDBL_MANT_DIG == 113 */
194 * 113 bits of precision is more than twice the precision of a double,
195 * so it is enough to represent the intermediate product exactly.
198 fma(double x, double y, double z)
200 return ((long double)x * y + z);
202 #endif /* LDBL_MANT_DIG != 113 */
204 #if LDBL_MANT_DIG == DBL_MANT_DIG
205 __strong_alias(fmal, fma);
206 #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */