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 __FBSDID("$FreeBSD: src/lib/msun/src/s_fmal.c,v 1.3 2007/01/07 07:54:21 das Exp $");
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).
43 fmal(long double x
, long double y
, long double z
)
45 #if LDBL_MANT_DIG == 64
46 static const long double split
= 0x1p
32L + 1.0;
47 #elif LDBL_MANT_DIG == 113
48 static const long double split
= 0x1p
57L + 1.0;
50 long double xs
, ys
, zs
;
51 long double c
, cc
, hx
, hy
, p
, q
, tx
, ty
;
59 if (x
== 0.0 || y
== 0.0)
62 /* Results of frexp() are undefined for these cases. */
63 if (!isfinite(x
) || !isfinite(y
) || !isfinite(z
))
69 oround
= fegetround();
70 spread
= ex
+ ey
- ez
;
73 * If x * y and z are many orders of magnitude apart, the scaling
74 * will overflow, so we handle these cases specially. Rounding
75 * modes other than FE_TONEAREST are painful.
77 if (spread
> LDBL_MANT_DIG
* 2) {
79 feraiseexcept(FE_INEXACT
);
84 if (x
> 0.0 ^ y
< 0.0 ^ z
< 0.0)
88 if (!fetestexcept(FE_INEXACT
))
97 if (!fetestexcept(FE_INEXACT
))
98 r
= nextafterl(r
, -INFINITY
);
101 default: /* FE_UPWARD */
106 if (!fetestexcept(FE_INEXACT
))
107 r
= nextafterl(r
, INFINITY
);
112 if (spread
< -LDBL_MANT_DIG
) {
113 feraiseexcept(FE_INEXACT
);
115 feraiseexcept(FE_UNDERFLOW
);
120 if (x
> 0.0 ^ y
< 0.0 ^ z
< 0.0)
123 return (nextafterl(z
, 0));
125 if (x
> 0.0 ^ y
< 0.0)
128 return (nextafterl(z
, -INFINITY
));
129 default: /* FE_UPWARD */
130 if (x
> 0.0 ^ y
< 0.0)
131 return (nextafterl(z
, INFINITY
));
138 * Use Dekker's algorithm to perform the multiplication and
139 * subsequent addition in twice the machine precision.
140 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
142 fesetround(FE_TONEAREST
);
155 q
= hx
* ty
+ tx
* hy
;
157 cc
= p
- c
+ q
+ tx
* ty
;
159 zs
= ldexpl(zs
, -spread
);
162 rr
= (c
- (r
- s
)) + (zs
- s
) + cc
;
165 if (spread
+ ilogbl(r
) > -16383) {
170 * The result is subnormal, so we round before scaling to
171 * avoid double rounding.
173 p
= ldexpl(copysignl(0x1p
-16382L, r
), -spread
);
176 cc
= (r
- (c
- s
)) + (p
- s
) + rr
;
180 return (ldexpl(r
, spread
));