s_fma.c revision 1.2
1/*	$OpenBSD: s_fma.c,v 1.2 2012/12/05 23:20:04 deraadt Exp $	*/
2
3/*-
4 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29/* LINTLIBRARY */
30
31#include <fenv.h>
32#include <float.h>
33#include <math.h>
34
35/*
36 * Fused multiply-add: Compute x * y + z with a single rounding error.
37 *
38 * We use scaling to avoid overflow/underflow, along with the
39 * canonical precision-doubling technique adapted from:
40 *
41 *	Dekker, T.  A Floating-Point Technique for Extending the
42 *	Available Precision.  Numer. Math. 18, 224-242 (1971).
43 *
44 * This algorithm is sensitive to the rounding precision.  FPUs such
45 * as the i387 must be set in double-precision mode if variables are
46 * to be stored in FP registers in order to avoid incorrect results.
47 * This is the default on FreeBSD, but not on many other systems.
48 *
49 * Hardware instructions should be used on architectures that support it,
50 * since this implementation will likely be several times slower.
51 */
52#if LDBL_MANT_DIG != 113
53double
54fma(double x, double y, double z)
55{
56	static const double split = 0x1p27 + 1.0;
57	double xs, ys, zs;
58	double c, cc, hx, hy, p, q, tx, ty;
59	double r, rr, s;
60	int oround;
61	int ex, ey, ez;
62	int spread;
63
64	/*
65	 * Handle special cases. The order of operations and the particular
66	 * return values here are crucial in handling special cases involving
67	 * infinities, NaNs, overflows, and signed zeroes correctly.
68	 */
69	if (x == 0.0 || y == 0.0)
70		return (x * y + z);
71	if (z == 0.0)
72		return (x * y);
73	if (!isfinite(x) || !isfinite(y))
74		return (x * y + z);
75	if (!isfinite(z))
76		return (z);
77
78	xs = frexp(x, &ex);
79	ys = frexp(y, &ey);
80	zs = frexp(z, &ez);
81	oround = fegetround();
82	spread = ex + ey - ez;
83
84	/*
85	 * If x * y and z are many orders of magnitude apart, the scaling
86	 * will overflow, so we handle these cases specially.  Rounding
87	 * modes other than FE_TONEAREST are painful.
88	 */
89	if (spread > DBL_MANT_DIG * 2) {
90		fenv_t env;
91		feraiseexcept(FE_INEXACT);
92		switch(oround) {
93		case FE_TONEAREST:
94			return (x * y);
95		case FE_TOWARDZERO:
96			if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
97				return (x * y);
98			feholdexcept(&env);
99			r = x * y;
100			if (!fetestexcept(FE_INEXACT))
101				r = nextafter(r, 0);
102			feupdateenv(&env);
103			return (r);
104		case FE_DOWNWARD:
105			if (z > 0.0)
106				return (x * y);
107			feholdexcept(&env);
108			r = x * y;
109			if (!fetestexcept(FE_INEXACT))
110				r = nextafter(r, -INFINITY);
111			feupdateenv(&env);
112			return (r);
113		default:	/* FE_UPWARD */
114			if (z < 0.0)
115				return (x * y);
116			feholdexcept(&env);
117			r = x * y;
118			if (!fetestexcept(FE_INEXACT))
119				r = nextafter(r, INFINITY);
120			feupdateenv(&env);
121			return (r);
122		}
123	}
124	if (spread < -DBL_MANT_DIG) {
125		feraiseexcept(FE_INEXACT);
126		if (!isnormal(z))
127			feraiseexcept(FE_UNDERFLOW);
128		switch (oround) {
129		case FE_TONEAREST:
130			return (z);
131		case FE_TOWARDZERO:
132			if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
133				return (z);
134			else
135				return (nextafter(z, 0));
136		case FE_DOWNWARD:
137			if (x > 0.0 ^ y < 0.0)
138				return (z);
139			else
140				return (nextafter(z, -INFINITY));
141		default:	/* FE_UPWARD */
142			if (x > 0.0 ^ y < 0.0)
143				return (nextafter(z, INFINITY));
144			else
145				return (z);
146		}
147	}
148
149	/*
150	 * Use Dekker's algorithm to perform the multiplication and
151	 * subsequent addition in twice the machine precision.
152	 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
153	 */
154	fesetround(FE_TONEAREST);
155
156	p = xs * split;
157	hx = xs - p;
158	hx += p;
159	tx = xs - hx;
160
161	p = ys * split;
162	hy = ys - p;
163	hy += p;
164	ty = ys - hy;
165
166	p = hx * hy;
167	q = hx * ty + tx * hy;
168	c = p + q;
169	cc = p - c + q + tx * ty;
170
171	zs = ldexp(zs, -spread);
172	r = c + zs;
173	s = r - c;
174	rr = (c - (r - s)) + (zs - s) + cc;
175
176	spread = ex + ey;
177	if (spread + ilogb(r) > -1023) {
178		fesetround(oround);
179		r = r + rr;
180	} else {
181		/*
182		 * The result is subnormal, so we round before scaling to
183		 * avoid double rounding.
184		 */
185		p = ldexp(copysign(0x1p-1022, r), -spread);
186		c = r + p;
187		s = c - r;
188		cc = (r - (c - s)) + (p - s) + rr;
189		fesetround(oround);
190		r = (c + cc) - p;
191	}
192	return (ldexp(r, spread));
193}
194#else	/* LDBL_MANT_DIG == 113 */
195/*
196 * 113 bits of precision is more than twice the precision of a double,
197 * so it is enough to represent the intermediate product exactly.
198 */
199double
200fma(double x, double y, double z)
201{
202	return ((long double)x * y + z);
203}
204#endif	/* LDBL_MANT_DIG != 113 */
205
206#if	LDBL_MANT_DIG == 53
207#ifdef	lint
208/* PROTOLIB1 */
209long double fmal(long double, long double, long double);
210#else	/* lint */
211__weak_alias(fmal, fma);
212#endif	/* lint */
213#endif	/* LDBL_MANT_DIG == 53 */
214