xref: /openbsd-src/lib/libm/src/s_fma.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_fma.c,v 1.7 2016/09/12 19:47:02 guenther Exp $	*/
249393c00Smartynas 
349393c00Smartynas /*-
449393c00Smartynas  * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
549393c00Smartynas  * All rights reserved.
649393c00Smartynas  *
749393c00Smartynas  * Redistribution and use in source and binary forms, with or without
849393c00Smartynas  * modification, are permitted provided that the following conditions
949393c00Smartynas  * are met:
1049393c00Smartynas  * 1. Redistributions of source code must retain the above copyright
1149393c00Smartynas  *    notice, this list of conditions and the following disclaimer.
1249393c00Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
1349393c00Smartynas  *    notice, this list of conditions and the following disclaimer in the
1449393c00Smartynas  *    documentation and/or other materials provided with the distribution.
1549393c00Smartynas  *
1649393c00Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1749393c00Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1849393c00Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1949393c00Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2049393c00Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2149393c00Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2249393c00Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2349393c00Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2449393c00Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2549393c00Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2649393c00Smartynas  * SUCH DAMAGE.
2749393c00Smartynas  */
2849393c00Smartynas 
2949393c00Smartynas #include <fenv.h>
3049393c00Smartynas #include <float.h>
3149393c00Smartynas #include <math.h>
3249393c00Smartynas 
3349393c00Smartynas /*
3449393c00Smartynas  * Fused multiply-add: Compute x * y + z with a single rounding error.
3549393c00Smartynas  *
3649393c00Smartynas  * We use scaling to avoid overflow/underflow, along with the
3749393c00Smartynas  * canonical precision-doubling technique adapted from:
3849393c00Smartynas  *
3949393c00Smartynas  *	Dekker, T.  A Floating-Point Technique for Extending the
4049393c00Smartynas  *	Available Precision.  Numer. Math. 18, 224-242 (1971).
4149393c00Smartynas  *
4249393c00Smartynas  * This algorithm is sensitive to the rounding precision.  FPUs such
4349393c00Smartynas  * as the i387 must be set in double-precision mode if variables are
4449393c00Smartynas  * to be stored in FP registers in order to avoid incorrect results.
4549393c00Smartynas  * This is the default on FreeBSD, but not on many other systems.
4649393c00Smartynas  *
4749393c00Smartynas  * Hardware instructions should be used on architectures that support it,
4849393c00Smartynas  * since this implementation will likely be several times slower.
4949393c00Smartynas  */
5049393c00Smartynas #if LDBL_MANT_DIG != 113
5149393c00Smartynas double
fma(double x,double y,double z)5249393c00Smartynas fma(double x, double y, double z)
5349393c00Smartynas {
5449393c00Smartynas 	static const double split = 0x1p27 + 1.0;
5549393c00Smartynas 	double xs, ys, zs;
5649393c00Smartynas 	double c, cc, hx, hy, p, q, tx, ty;
5749393c00Smartynas 	double r, rr, s;
5849393c00Smartynas 	int oround;
5949393c00Smartynas 	int ex, ey, ez;
6049393c00Smartynas 	int spread;
6149393c00Smartynas 
6249393c00Smartynas 	/*
6349393c00Smartynas 	 * Handle special cases. The order of operations and the particular
6449393c00Smartynas 	 * return values here are crucial in handling special cases involving
6549393c00Smartynas 	 * infinities, NaNs, overflows, and signed zeroes correctly.
6649393c00Smartynas 	 */
6749393c00Smartynas 	if (x == 0.0 || y == 0.0)
6849393c00Smartynas 		return (x * y + z);
6949393c00Smartynas 	if (z == 0.0)
7049393c00Smartynas 		return (x * y);
7149393c00Smartynas 	if (!isfinite(x) || !isfinite(y))
7249393c00Smartynas 		return (x * y + z);
7349393c00Smartynas 	if (!isfinite(z))
7449393c00Smartynas 		return (z);
7549393c00Smartynas 
7649393c00Smartynas 	xs = frexp(x, &ex);
7749393c00Smartynas 	ys = frexp(y, &ey);
7849393c00Smartynas 	zs = frexp(z, &ez);
7949393c00Smartynas 	oround = fegetround();
8049393c00Smartynas 	spread = ex + ey - ez;
8149393c00Smartynas 
8249393c00Smartynas 	/*
8349393c00Smartynas 	 * If x * y and z are many orders of magnitude apart, the scaling
8449393c00Smartynas 	 * will overflow, so we handle these cases specially.  Rounding
8549393c00Smartynas 	 * modes other than FE_TONEAREST are painful.
8649393c00Smartynas 	 */
8749393c00Smartynas 	if (spread > DBL_MANT_DIG * 2) {
8849393c00Smartynas 		fenv_t env;
8949393c00Smartynas 		feraiseexcept(FE_INEXACT);
9049393c00Smartynas 		switch(oround) {
9149393c00Smartynas 		case FE_TONEAREST:
9249393c00Smartynas 			return (x * y);
9349393c00Smartynas 		case FE_TOWARDZERO:
9463d6ab21Smartynas 			if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
9549393c00Smartynas 				return (x * y);
9649393c00Smartynas 			feholdexcept(&env);
9749393c00Smartynas 			r = x * y;
9849393c00Smartynas 			if (!fetestexcept(FE_INEXACT))
9949393c00Smartynas 				r = nextafter(r, 0);
10049393c00Smartynas 			feupdateenv(&env);
10149393c00Smartynas 			return (r);
10249393c00Smartynas 		case FE_DOWNWARD:
10349393c00Smartynas 			if (z > 0.0)
10449393c00Smartynas 				return (x * y);
10549393c00Smartynas 			feholdexcept(&env);
10649393c00Smartynas 			r = x * y;
10749393c00Smartynas 			if (!fetestexcept(FE_INEXACT))
10849393c00Smartynas 				r = nextafter(r, -INFINITY);
10949393c00Smartynas 			feupdateenv(&env);
11049393c00Smartynas 			return (r);
11149393c00Smartynas 		default:	/* FE_UPWARD */
11249393c00Smartynas 			if (z < 0.0)
11349393c00Smartynas 				return (x * y);
11449393c00Smartynas 			feholdexcept(&env);
11549393c00Smartynas 			r = x * y;
11649393c00Smartynas 			if (!fetestexcept(FE_INEXACT))
11749393c00Smartynas 				r = nextafter(r, INFINITY);
11849393c00Smartynas 			feupdateenv(&env);
11949393c00Smartynas 			return (r);
12049393c00Smartynas 		}
12149393c00Smartynas 	}
12249393c00Smartynas 	if (spread < -DBL_MANT_DIG) {
12349393c00Smartynas 		feraiseexcept(FE_INEXACT);
12449393c00Smartynas 		if (!isnormal(z))
12549393c00Smartynas 			feraiseexcept(FE_UNDERFLOW);
12649393c00Smartynas 		switch (oround) {
12749393c00Smartynas 		case FE_TONEAREST:
12849393c00Smartynas 			return (z);
12949393c00Smartynas 		case FE_TOWARDZERO:
13063d6ab21Smartynas 			if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
13149393c00Smartynas 				return (z);
13249393c00Smartynas 			else
13349393c00Smartynas 				return (nextafter(z, 0));
13449393c00Smartynas 		case FE_DOWNWARD:
13563d6ab21Smartynas 			if ((x > 0.0) ^ (y < 0.0))
13649393c00Smartynas 				return (z);
13749393c00Smartynas 			else
13849393c00Smartynas 				return (nextafter(z, -INFINITY));
13949393c00Smartynas 		default:	/* FE_UPWARD */
14063d6ab21Smartynas 			if ((x > 0.0) ^ (y < 0.0))
14149393c00Smartynas 				return (nextafter(z, INFINITY));
14249393c00Smartynas 			else
14349393c00Smartynas 				return (z);
14449393c00Smartynas 		}
14549393c00Smartynas 	}
14649393c00Smartynas 
14749393c00Smartynas 	/*
14849393c00Smartynas 	 * Use Dekker's algorithm to perform the multiplication and
14949393c00Smartynas 	 * subsequent addition in twice the machine precision.
15049393c00Smartynas 	 * Arrange so that x * y = c + cc, and x * y + z = r + rr.
15149393c00Smartynas 	 */
15249393c00Smartynas 	fesetround(FE_TONEAREST);
15349393c00Smartynas 
15449393c00Smartynas 	p = xs * split;
15549393c00Smartynas 	hx = xs - p;
15649393c00Smartynas 	hx += p;
15749393c00Smartynas 	tx = xs - hx;
15849393c00Smartynas 
15949393c00Smartynas 	p = ys * split;
16049393c00Smartynas 	hy = ys - p;
16149393c00Smartynas 	hy += p;
16249393c00Smartynas 	ty = ys - hy;
16349393c00Smartynas 
16449393c00Smartynas 	p = hx * hy;
16549393c00Smartynas 	q = hx * ty + tx * hy;
16649393c00Smartynas 	c = p + q;
16749393c00Smartynas 	cc = p - c + q + tx * ty;
16849393c00Smartynas 
16949393c00Smartynas 	zs = ldexp(zs, -spread);
17049393c00Smartynas 	r = c + zs;
17149393c00Smartynas 	s = r - c;
17249393c00Smartynas 	rr = (c - (r - s)) + (zs - s) + cc;
17349393c00Smartynas 
17449393c00Smartynas 	spread = ex + ey;
17549393c00Smartynas 	if (spread + ilogb(r) > -1023) {
17649393c00Smartynas 		fesetround(oround);
17749393c00Smartynas 		r = r + rr;
17849393c00Smartynas 	} else {
17949393c00Smartynas 		/*
18049393c00Smartynas 		 * The result is subnormal, so we round before scaling to
18149393c00Smartynas 		 * avoid double rounding.
18249393c00Smartynas 		 */
18349393c00Smartynas 		p = ldexp(copysign(0x1p-1022, r), -spread);
18449393c00Smartynas 		c = r + p;
18549393c00Smartynas 		s = c - r;
18649393c00Smartynas 		cc = (r - (c - s)) + (p - s) + rr;
18749393c00Smartynas 		fesetround(oround);
18849393c00Smartynas 		r = (c + cc) - p;
18949393c00Smartynas 	}
19049393c00Smartynas 	return (ldexp(r, spread));
19149393c00Smartynas }
19249393c00Smartynas #else	/* LDBL_MANT_DIG == 113 */
19349393c00Smartynas /*
19449393c00Smartynas  * 113 bits of precision is more than twice the precision of a double,
19549393c00Smartynas  * so it is enough to represent the intermediate product exactly.
19649393c00Smartynas  */
19749393c00Smartynas double
fma(double x,double y,double z)19849393c00Smartynas fma(double x, double y, double z)
19949393c00Smartynas {
20049393c00Smartynas 	return ((long double)x * y + z);
20149393c00Smartynas }
20249393c00Smartynas #endif	/* LDBL_MANT_DIG != 113 */
203*2f2c0062Sguenther DEF_STD(fma);
204*2f2c0062Sguenther LDBL_MAYBE_UNUSED_CLONE(fma);
205