x11gfx.hidd: support 32 bit modes
[AROS.git] / compiler / stdc / math / s_fmal.c
blob82432e12d8b346c713131dcc2ddba3c7305a1a27
1 /*-
2 * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
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.
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.
27 __FBSDID("$FreeBSD: src/lib/msun/src/s_fmal.c,v 1.3 2007/01/07 07:54:21 das Exp $");
29 #include <fenv.h>
30 #include <float.h>
31 #include <math.h>
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).
42 long double
43 fmal(long double x, long double y, long double z)
45 #if LDBL_MANT_DIG == 64
46 static const long double split = 0x1p32L + 1.0;
47 #elif LDBL_MANT_DIG == 113
48 static const long double split = 0x1p57L + 1.0;
49 #endif
50 long double xs, ys, zs;
51 long double c, cc, hx, hy, p, q, tx, ty;
52 long double r, rr, s;
53 int oround;
54 int ex, ey, ez;
55 int spread;
57 if (z == 0.0)
58 return (x * y);
59 if (x == 0.0 || y == 0.0)
60 return (x * y + z);
62 /* Results of frexp() are undefined for these cases. */
63 if (!isfinite(x) || !isfinite(y) || !isfinite(z))
64 return (x * y + z);
66 xs = frexpl(x, &ex);
67 ys = frexpl(y, &ey);
68 zs = frexpl(z, &ez);
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) {
78 fenv_t env;
79 feraiseexcept(FE_INEXACT);
80 switch(oround) {
81 case FE_TONEAREST:
82 return (x * y);
83 case FE_TOWARDZERO:
84 if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
85 return (x * y);
86 feholdexcept(&env);
87 r = x * y;
88 if (!fetestexcept(FE_INEXACT))
89 r = nextafterl(r, 0);
90 feupdateenv(&env);
91 return (r);
92 case FE_DOWNWARD:
93 if (z > 0.0)
94 return (x * y);
95 feholdexcept(&env);
96 r = x * y;
97 if (!fetestexcept(FE_INEXACT))
98 r = nextafterl(r, -INFINITY);
99 feupdateenv(&env);
100 return (r);
101 default: /* FE_UPWARD */
102 if (z < 0.0)
103 return (x * y);
104 feholdexcept(&env);
105 r = x * y;
106 if (!fetestexcept(FE_INEXACT))
107 r = nextafterl(r, INFINITY);
108 feupdateenv(&env);
109 return (r);
112 if (spread < -LDBL_MANT_DIG) {
113 feraiseexcept(FE_INEXACT);
114 if (!isnormal(z))
115 feraiseexcept(FE_UNDERFLOW);
116 switch (oround) {
117 case FE_TONEAREST:
118 return (z);
119 case FE_TOWARDZERO:
120 if (x > 0.0 ^ y < 0.0 ^ z < 0.0)
121 return (z);
122 else
123 return (nextafterl(z, 0));
124 case FE_DOWNWARD:
125 if (x > 0.0 ^ y < 0.0)
126 return (z);
127 else
128 return (nextafterl(z, -INFINITY));
129 default: /* FE_UPWARD */
130 if (x > 0.0 ^ y < 0.0)
131 return (nextafterl(z, INFINITY));
132 else
133 return (z);
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);
144 p = xs * split;
145 hx = xs - p;
146 hx += p;
147 tx = xs - hx;
149 p = ys * split;
150 hy = ys - p;
151 hy += p;
152 ty = ys - hy;
154 p = hx * hy;
155 q = hx * ty + tx * hy;
156 c = p + q;
157 cc = p - c + q + tx * ty;
159 zs = ldexpl(zs, -spread);
160 r = c + zs;
161 s = r - c;
162 rr = (c - (r - s)) + (zs - s) + cc;
164 spread = ex + ey;
165 if (spread + ilogbl(r) > -16383) {
166 fesetround(oround);
167 r = r + rr;
168 } else {
170 * The result is subnormal, so we round before scaling to
171 * avoid double rounding.
173 p = ldexpl(copysignl(0x1p-16382L, r), -spread);
174 c = r + p;
175 s = c - r;
176 cc = (r - (c - s)) + (p - s) + rr;
177 fesetround(oround);
178 r = (c + cc) - p;
180 return (ldexpl(r, spread));