1/* Compute x * y + z as ternary operation.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2010.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <float.h>
21#include <math.h>
22#include <fenv.h>
23#include <ieee754.h>
24#include <math_private.h>
25#include <tininess.h>
26
27/* This implementation uses rounding to odd to avoid problems with
28 double rounding. See a paper by Boldo and Melquiond:
29 http://www.lri.fr/~melquion/doc/08-tc.pdf */
30
31double
32__fma (double x, double y, double z)
33{
34 union ieee754_double u, v, w;
35 int adjust = 0;
36 u.d = x;
37 v.d = y;
38 w.d = z;
39 if (__builtin_expect (u.ieee.exponent + v.ieee.exponent
40 >= 0x7ff + IEEE754_DOUBLE_BIAS - DBL_MANT_DIG, 0)
41 || __builtin_expect (u.ieee.exponent >= 0x7ff - DBL_MANT_DIG, 0)
42 || __builtin_expect (v.ieee.exponent >= 0x7ff - DBL_MANT_DIG, 0)
43 || __builtin_expect (w.ieee.exponent >= 0x7ff - DBL_MANT_DIG, 0)
44 || __builtin_expect (u.ieee.exponent + v.ieee.exponent
45 <= IEEE754_DOUBLE_BIAS + DBL_MANT_DIG, 0))
46 {
47 /* If z is Inf, but x and y are finite, the result should be
48 z rather than NaN. */
49 if (w.ieee.exponent == 0x7ff
50 && u.ieee.exponent != 0x7ff
51 && v.ieee.exponent != 0x7ff)
52 return (z + x) + y;
53 /* If z is zero and x are y are nonzero, compute the result
54 as x * y to avoid the wrong sign of a zero result if x * y
55 underflows to 0. */
56 if (z == 0 && x != 0 && y != 0)
57 return x * y;
58 /* If x or y or z is Inf/NaN, or if x * y is zero, compute as
59 x * y + z. */
60 if (u.ieee.exponent == 0x7ff
61 || v.ieee.exponent == 0x7ff
62 || w.ieee.exponent == 0x7ff
63 || x == 0
64 || y == 0)
65 return x * y + z;
66 /* If fma will certainly overflow, compute as x * y. */
67 if (u.ieee.exponent + v.ieee.exponent > 0x7ff + IEEE754_DOUBLE_BIAS)
68 return x * y;
69 /* If x * y is less than 1/4 of DBL_TRUE_MIN, neither the
70 result nor whether there is underflow depends on its exact
71 value, only on its sign. */
72 if (u.ieee.exponent + v.ieee.exponent
73 < IEEE754_DOUBLE_BIAS - DBL_MANT_DIG - 2)
74 {
75 int neg = u.ieee.negative ^ v.ieee.negative;
76 double tiny = neg ? -0x1p-1074 : 0x1p-1074;
77 if (w.ieee.exponent >= 3)
78 return tiny + z;
79 /* Scaling up, adding TINY and scaling down produces the
80 correct result, because in round-to-nearest mode adding
81 TINY has no effect and in other modes double rounding is
82 harmless. But it may not produce required underflow
83 exceptions. */
84 v.d = z * 0x1p54 + tiny;
85 if (TININESS_AFTER_ROUNDING
86 ? v.ieee.exponent < 55
87 : (w.ieee.exponent == 0
88 || (w.ieee.exponent == 1
89 && w.ieee.negative != neg
90 && w.ieee.mantissa1 == 0
91 && w.ieee.mantissa0 == 0)))
92 {
93 double force_underflow = x * y;
94 math_force_eval (force_underflow);
95 }
96 return v.d * 0x1p-54;
97 }
98 if (u.ieee.exponent + v.ieee.exponent
99 >= 0x7ff + IEEE754_DOUBLE_BIAS - DBL_MANT_DIG)
100 {
101 /* Compute 1p-53 times smaller result and multiply
102 at the end. */
103 if (u.ieee.exponent > v.ieee.exponent)
104 u.ieee.exponent -= DBL_MANT_DIG;
105 else
106 v.ieee.exponent -= DBL_MANT_DIG;
107 /* If x + y exponent is very large and z exponent is very small,
108 it doesn't matter if we don't adjust it. */
109 if (w.ieee.exponent > DBL_MANT_DIG)
110 w.ieee.exponent -= DBL_MANT_DIG;
111 adjust = 1;
112 }
113 else if (w.ieee.exponent >= 0x7ff - DBL_MANT_DIG)
114 {
115 /* Similarly.
116 If z exponent is very large and x and y exponents are
117 very small, adjust them up to avoid spurious underflows,
118 rather than down. */
119 if (u.ieee.exponent + v.ieee.exponent
120 <= IEEE754_DOUBLE_BIAS + 2 * DBL_MANT_DIG)
121 {
122 if (u.ieee.exponent > v.ieee.exponent)
123 u.ieee.exponent += 2 * DBL_MANT_DIG + 2;
124 else
125 v.ieee.exponent += 2 * DBL_MANT_DIG + 2;
126 }
127 else if (u.ieee.exponent > v.ieee.exponent)
128 {
129 if (u.ieee.exponent > DBL_MANT_DIG)
130 u.ieee.exponent -= DBL_MANT_DIG;
131 }
132 else if (v.ieee.exponent > DBL_MANT_DIG)
133 v.ieee.exponent -= DBL_MANT_DIG;
134 w.ieee.exponent -= DBL_MANT_DIG;
135 adjust = 1;
136 }
137 else if (u.ieee.exponent >= 0x7ff - DBL_MANT_DIG)
138 {
139 u.ieee.exponent -= DBL_MANT_DIG;
140 if (v.ieee.exponent)
141 v.ieee.exponent += DBL_MANT_DIG;
142 else
143 v.d *= 0x1p53;
144 }
145 else if (v.ieee.exponent >= 0x7ff - DBL_MANT_DIG)
146 {
147 v.ieee.exponent -= DBL_MANT_DIG;
148 if (u.ieee.exponent)
149 u.ieee.exponent += DBL_MANT_DIG;
150 else
151 u.d *= 0x1p53;
152 }
153 else /* if (u.ieee.exponent + v.ieee.exponent
154 <= IEEE754_DOUBLE_BIAS + DBL_MANT_DIG) */
155 {
156 if (u.ieee.exponent > v.ieee.exponent)
157 u.ieee.exponent += 2 * DBL_MANT_DIG + 2;
158 else
159 v.ieee.exponent += 2 * DBL_MANT_DIG + 2;
160 if (w.ieee.exponent <= 4 * DBL_MANT_DIG + 6)
161 {
162 if (w.ieee.exponent)
163 w.ieee.exponent += 2 * DBL_MANT_DIG + 2;
164 else
165 w.d *= 0x1p108;
166 adjust = -1;
167 }
168 /* Otherwise x * y should just affect inexact
169 and nothing else. */
170 }
171 x = u.d;
172 y = v.d;
173 z = w.d;
174 }
175
176 /* Ensure correct sign of exact 0 + 0. */
177 if (__glibc_unlikely ((x == 0 || y == 0) && z == 0))
178 {
179 x = math_opt_barrier (x);
180 return x * y + z;
181 }
182
183 fenv_t env;
184 libc_feholdexcept_setround (&env, FE_TONEAREST);
185
186 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
187#define C ((1 << (DBL_MANT_DIG + 1) / 2) + 1)
188 double x1 = x * C;
189 double y1 = y * C;
190 double m1 = x * y;
191 x1 = (x - x1) + x1;
192 y1 = (y - y1) + y1;
193 double x2 = x - x1;
194 double y2 = y - y1;
195 double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2;
196
197 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
198 double a1 = z + m1;
199 double t1 = a1 - z;
200 double t2 = a1 - t1;
201 t1 = m1 - t1;
202 t2 = z - t2;
203 double a2 = t1 + t2;
204 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
205 math_force_eval (m2);
206 math_force_eval (a2);
207 feclearexcept (FE_INEXACT);
208
209 /* If the result is an exact zero, ensure it has the correct sign. */
210 if (a1 == 0 && m2 == 0)
211 {
212 libc_feupdateenv (&env);
213 /* Ensure that round-to-nearest value of z + m1 is not reused. */
214 z = math_opt_barrier (z);
215 return z + m1;
216 }
217
218 libc_fesetround (FE_TOWARDZERO);
219
220 /* Perform m2 + a2 addition with round to odd. */
221 u.d = a2 + m2;
222
223 if (__glibc_unlikely (adjust < 0))
224 {
225 if ((u.ieee.mantissa1 & 1) == 0)
226 u.ieee.mantissa1 |= libc_fetestexcept (FE_INEXACT) != 0;
227 v.d = a1 + u.d;
228 /* Ensure the addition is not scheduled after fetestexcept call. */
229 math_force_eval (v.d);
230 }
231
232 /* Reset rounding mode and test for inexact simultaneously. */
233 int j = libc_feupdateenv_test (&env, FE_INEXACT) != 0;
234
235 if (__glibc_likely (adjust == 0))
236 {
237 if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7ff)
238 u.ieee.mantissa1 |= j;
239 /* Result is a1 + u.d. */
240 return a1 + u.d;
241 }
242 else if (__glibc_likely (adjust > 0))
243 {
244 if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7ff)
245 u.ieee.mantissa1 |= j;
246 /* Result is a1 + u.d, scaled up. */
247 return (a1 + u.d) * 0x1p53;
248 }
249 else
250 {
251 /* If a1 + u.d is exact, the only rounding happens during
252 scaling down. */
253 if (j == 0)
254 return v.d * 0x1p-108;
255 /* If result rounded to zero is not subnormal, no double
256 rounding will occur. */
257 if (v.ieee.exponent > 108)
258 return (a1 + u.d) * 0x1p-108;
259 /* If v.d * 0x1p-108 with round to zero is a subnormal above
260 or equal to DBL_MIN / 2, then v.d * 0x1p-108 shifts mantissa
261 down just by 1 bit, which means v.ieee.mantissa1 |= j would
262 change the round bit, not sticky or guard bit.
263 v.d * 0x1p-108 never normalizes by shifting up,
264 so round bit plus sticky bit should be already enough
265 for proper rounding. */
266 if (v.ieee.exponent == 108)
267 {
268 /* If the exponent would be in the normal range when
269 rounding to normal precision with unbounded exponent
270 range, the exact result is known and spurious underflows
271 must be avoided on systems detecting tininess after
272 rounding. */
273 if (TININESS_AFTER_ROUNDING)
274 {
275 w.d = a1 + u.d;
276 if (w.ieee.exponent == 109)
277 return w.d * 0x1p-108;
278 }
279 /* v.ieee.mantissa1 & 2 is LSB bit of the result before rounding,
280 v.ieee.mantissa1 & 1 is the round bit and j is our sticky
281 bit. */
282 w.d = 0.0;
283 w.ieee.mantissa1 = ((v.ieee.mantissa1 & 3) << 1) | j;
284 w.ieee.negative = v.ieee.negative;
285 v.ieee.mantissa1 &= ~3U;
286 v.d *= 0x1p-108;
287 w.d *= 0x1p-2;
288 return v.d + w.d;
289 }
290 v.ieee.mantissa1 |= j;
291 return v.d * 0x1p-108;
292 }
293}
294#ifndef __fma
295weak_alias (__fma, fma)
296#endif
297
298#ifdef NO_LONG_DOUBLE
299strong_alias (__fma, __fmal)
300weak_alias (__fmal, fmal)
301#endif
302