1*63d6ab21Smartynas /* $OpenBSD: s_fmal.c,v 1.3 2013/11/12 19:00:38 martynas 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 long double
fmal(long double x,long double y,long double z)4349393c00Smartynas fmal(long double x, long double y, long double z)
4449393c00Smartynas {
4549393c00Smartynas #if LDBL_MANT_DIG == 64
4649393c00Smartynas static const long double split = 0x1p32L + 1.0;
4749393c00Smartynas #elif LDBL_MANT_DIG == 113
4849393c00Smartynas static const long double split = 0x1p57L + 1.0;
4949393c00Smartynas #endif
5049393c00Smartynas long double xs, ys, zs;
5149393c00Smartynas long double c, cc, hx, hy, p, q, tx, ty;
5249393c00Smartynas long double r, rr, s;
5349393c00Smartynas int oround;
5449393c00Smartynas int ex, ey, ez;
5549393c00Smartynas int spread;
5649393c00Smartynas
5749393c00Smartynas /*
5849393c00Smartynas * Handle special cases. The order of operations and the particular
5949393c00Smartynas * return values here are crucial in handling special cases involving
6049393c00Smartynas * infinities, NaNs, overflows, and signed zeroes correctly.
6149393c00Smartynas */
6249393c00Smartynas if (x == 0.0 || y == 0.0)
6349393c00Smartynas return (x * y + z);
6449393c00Smartynas if (z == 0.0)
6549393c00Smartynas return (x * y);
6649393c00Smartynas if (!isfinite(x) || !isfinite(y))
6749393c00Smartynas return (x * y + z);
6849393c00Smartynas if (!isfinite(z))
6949393c00Smartynas return (z);
7049393c00Smartynas
7149393c00Smartynas xs = frexpl(x, &ex);
7249393c00Smartynas ys = frexpl(y, &ey);
7349393c00Smartynas zs = frexpl(z, &ez);
7449393c00Smartynas oround = fegetround();
7549393c00Smartynas spread = ex + ey - ez;
7649393c00Smartynas
7749393c00Smartynas /*
7849393c00Smartynas * If x * y and z are many orders of magnitude apart, the scaling
7949393c00Smartynas * will overflow, so we handle these cases specially. Rounding
8049393c00Smartynas * modes other than FE_TONEAREST are painful.
8149393c00Smartynas */
8249393c00Smartynas if (spread > LDBL_MANT_DIG * 2) {
8349393c00Smartynas fenv_t env;
8449393c00Smartynas feraiseexcept(FE_INEXACT);
8549393c00Smartynas switch(oround) {
8649393c00Smartynas case FE_TONEAREST:
8749393c00Smartynas return (x * y);
8849393c00Smartynas case FE_TOWARDZERO:
89*63d6ab21Smartynas if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
9049393c00Smartynas return (x * y);
9149393c00Smartynas feholdexcept(&env);
9249393c00Smartynas r = x * y;
9349393c00Smartynas if (!fetestexcept(FE_INEXACT))
9449393c00Smartynas r = nextafterl(r, 0);
9549393c00Smartynas feupdateenv(&env);
9649393c00Smartynas return (r);
9749393c00Smartynas case FE_DOWNWARD:
9849393c00Smartynas if (z > 0.0)
9949393c00Smartynas return (x * y);
10049393c00Smartynas feholdexcept(&env);
10149393c00Smartynas r = x * y;
10249393c00Smartynas if (!fetestexcept(FE_INEXACT))
10349393c00Smartynas r = nextafterl(r, -INFINITY);
10449393c00Smartynas feupdateenv(&env);
10549393c00Smartynas return (r);
10649393c00Smartynas default: /* FE_UPWARD */
10749393c00Smartynas if (z < 0.0)
10849393c00Smartynas return (x * y);
10949393c00Smartynas feholdexcept(&env);
11049393c00Smartynas r = x * y;
11149393c00Smartynas if (!fetestexcept(FE_INEXACT))
11249393c00Smartynas r = nextafterl(r, INFINITY);
11349393c00Smartynas feupdateenv(&env);
11449393c00Smartynas return (r);
11549393c00Smartynas }
11649393c00Smartynas }
11749393c00Smartynas if (spread < -LDBL_MANT_DIG) {
11849393c00Smartynas feraiseexcept(FE_INEXACT);
11949393c00Smartynas if (!isnormal(z))
12049393c00Smartynas feraiseexcept(FE_UNDERFLOW);
12149393c00Smartynas switch (oround) {
12249393c00Smartynas case FE_TONEAREST:
12349393c00Smartynas return (z);
12449393c00Smartynas case FE_TOWARDZERO:
125*63d6ab21Smartynas if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
12649393c00Smartynas return (z);
12749393c00Smartynas else
12849393c00Smartynas return (nextafterl(z, 0));
12949393c00Smartynas case FE_DOWNWARD:
130*63d6ab21Smartynas if ((x > 0.0) ^ (y < 0.0))
13149393c00Smartynas return (z);
13249393c00Smartynas else
13349393c00Smartynas return (nextafterl(z, -INFINITY));
13449393c00Smartynas default: /* FE_UPWARD */
135*63d6ab21Smartynas if ((x > 0.0) ^ (y < 0.0))
13649393c00Smartynas return (nextafterl(z, INFINITY));
13749393c00Smartynas else
13849393c00Smartynas return (z);
13949393c00Smartynas }
14049393c00Smartynas }
14149393c00Smartynas
14249393c00Smartynas /*
14349393c00Smartynas * Use Dekker's algorithm to perform the multiplication and
14449393c00Smartynas * subsequent addition in twice the machine precision.
14549393c00Smartynas * Arrange so that x * y = c + cc, and x * y + z = r + rr.
14649393c00Smartynas */
14749393c00Smartynas fesetround(FE_TONEAREST);
14849393c00Smartynas
14949393c00Smartynas p = xs * split;
15049393c00Smartynas hx = xs - p;
15149393c00Smartynas hx += p;
15249393c00Smartynas tx = xs - hx;
15349393c00Smartynas
15449393c00Smartynas p = ys * split;
15549393c00Smartynas hy = ys - p;
15649393c00Smartynas hy += p;
15749393c00Smartynas ty = ys - hy;
15849393c00Smartynas
15949393c00Smartynas p = hx * hy;
16049393c00Smartynas q = hx * ty + tx * hy;
16149393c00Smartynas c = p + q;
16249393c00Smartynas cc = p - c + q + tx * ty;
16349393c00Smartynas
16449393c00Smartynas zs = ldexpl(zs, -spread);
16549393c00Smartynas r = c + zs;
16649393c00Smartynas s = r - c;
16749393c00Smartynas rr = (c - (r - s)) + (zs - s) + cc;
16849393c00Smartynas
16949393c00Smartynas spread = ex + ey;
17049393c00Smartynas if (spread + ilogbl(r) > -16383) {
17149393c00Smartynas fesetround(oround);
17249393c00Smartynas r = r + rr;
17349393c00Smartynas } else {
17449393c00Smartynas /*
17549393c00Smartynas * The result is subnormal, so we round before scaling to
17649393c00Smartynas * avoid double rounding.
17749393c00Smartynas */
17849393c00Smartynas p = ldexpl(copysignl(0x1p-16382L, r), -spread);
17949393c00Smartynas c = r + p;
18049393c00Smartynas s = c - r;
18149393c00Smartynas cc = (r - (c - s)) + (p - s) + rr;
18249393c00Smartynas fesetround(oround);
18349393c00Smartynas r = (c + cc) - p;
18449393c00Smartynas }
18549393c00Smartynas return (ldexpl(r, spread));
18649393c00Smartynas }
187