s_fmal.c revision 143211
1/*-
2 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/msun/src/s_fmal.c 143211 2005-03-07 04:54:20Z das $");
29
30#include <fenv.h>
31#include <float.h>
32#include <math.h>
33
34/*
35 * Fused multiply-add: Compute x * y + z with a single rounding error.
36 *
37 * We use scaling to avoid overflow/underflow, along with the
38 * canonical precision-doubling technique adapted from:
39 *
40 *	Dekker, T.  A Floating-Point Technique for Extending the
41 *	Available Precision.  Numer. Math. 18, 224-242 (1971).
42 *
43 * XXX May incur a small error for subnormal results due to double
44 *     rounding induced by the final scaling operation.
45 */
46long double
47fmal(long double x, long double y, long double z)
48{
49#if LDBL_MANT_DIG == 64
50	static const long double split = 0x1p32L + 1.0;
51#elif LDBL_MANT_DIG == 113
52	static const long double split = 0x1p57L + 1.0;
53#endif
54	long double xs, ys, zs;
55	long double c, cc, hx, hy, p, q, tx, ty;
56	long double r, rr, s;
57	int oround;
58	int ex, ey, ez;
59	int spread;
60
61	if (z == 0.0)
62		return (x * y);
63	if (x == 0.0 || y == 0.0)
64		return (x * y + z);
65
66	/* Results of frexp() are undefined for these cases. */
67	if (!isfinite(x) || !isfinite(y) || !isfinite(z))
68		return (x * y + z);
69
70	xs = frexpl(x, &ex);
71	ys = frexpl(y, &ey);
72	zs = frexpl(z, &ez);
73	oround = fegetround();
74	spread = ex + ey - ez;
75
76	/*
77	 * If x * y and z are many orders of magnitude apart, the scaling
78	 * will overflow, so we handle these cases specially.  Rounding
79	 * modes other than FE_TONEAREST are painful.
80	 */
81	if (spread > LDBL_MANT_DIG * 2) {
82		fenv_t env;
83		feraiseexcept(FE_INEXACT);
84		switch(oround) {
85		case FE_TONEAREST:
86			return (x * y);
87		case FE_TOWARDZERO:
88			if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
89				return (x * y);
90			feholdexcept(&env);
91			r = x * y;
92			if (!fetestexcept(FE_INEXACT))
93				r = nextafterl(r, 0);
94			feupdateenv(&env);
95			return (r);
96		case FE_DOWNWARD:
97			if (z > 0.0)
98				return (x * y);
99			feholdexcept(&env);
100			r = x * y;
101			if (!fetestexcept(FE_INEXACT))
102				r = nextafterl(r, -INFINITY);
103			feupdateenv(&env);
104			return (r);
105		default:	/* FE_UPWARD */
106			if (z < 0.0)
107				return (x * y);
108			feholdexcept(&env);
109			r = x * y;
110			if (!fetestexcept(FE_INEXACT))
111				r = nextafterl(r, INFINITY);
112			feupdateenv(&env);
113			return (r);
114		}
115	}
116	if (spread < -LDBL_MANT_DIG) {
117		feraiseexcept(FE_INEXACT);
118		if (!isnormal(z))
119			feraiseexcept(FE_UNDERFLOW);
120		switch (oround) {
121		case FE_TONEAREST:
122			return (z);
123		case FE_TOWARDZERO:
124			if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
125				return (z);
126			else
127				return (nextafterl(z, 0));
128		case FE_DOWNWARD:
129			if (x > 0.0 ^ y < 0.0)
130				return (z);
131			else
132				return (nextafterl(z, -INFINITY));
133		default:	/* FE_UPWARD */
134			if (x > 0.0 ^ y < 0.0)
135				return (nextafterl(z, INFINITY));
136			else
137				return (z);
138		}
139	}
140
141	/*
142	 * Use Dekker's algorithm to perform the multiplication and
143	 * subsequent addition in twice the machine precision.
144	 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
145	 */
146	fesetround(FE_TONEAREST);
147
148	p = xs * split;
149	hx = xs - p;
150	hx += p;
151	tx = xs - hx;
152
153	p = ys * split;
154	hy = ys - p;
155	hy += p;
156	ty = ys - hy;
157
158	p = hx * hy;
159	q = hx * ty + tx * hy;
160	c = p + q;
161	cc = p - c + q + tx * ty;
162
163	zs = ldexpl(zs, -spread);
164	r = c + zs;
165	s = r - c;
166	rr = (c - (r - s)) + (zs - s) + cc;
167
168	fesetround(oround);
169	return (ldexpl(r + rr, ex + ey));
170}
171