1 /* Compute x * y + z as ternary operation.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define NO_MATH_REDIRECT
21 #define dfmal __hide_dfmal
22 #define f32xfmaf64 __hide_f32xfmaf64
28 #include <math-barriers.h>
29 #include <fenv_private.h>
30 #include <libm-alias-double.h>
31 #include <math-narrow-alias.h>
33 #include <math-use-builtins.h>
35 /* This implementation uses rounding to odd to avoid problems with
36 double rounding. See a paper by Boldo and Melquiond:
37 http://www.lri.fr/~melquion/doc/08-tc.pdf */
40 __fma (double x
, double y
, double z
)
43 return __builtin_fma (x
, y
, z
);
45 /* Use generic implementation. */
46 union ieee754_double u
, v
, w
;
51 if (__builtin_expect (u
.ieee
.exponent
+ v
.ieee
.exponent
52 >= 0x7ff + IEEE754_DOUBLE_BIAS
- DBL_MANT_DIG
, 0)
53 || __builtin_expect (u
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
, 0)
54 || __builtin_expect (v
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
, 0)
55 || __builtin_expect (w
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
, 0)
56 || __builtin_expect (u
.ieee
.exponent
+ v
.ieee
.exponent
57 <= IEEE754_DOUBLE_BIAS
+ DBL_MANT_DIG
, 0))
59 /* If z is Inf, but x and y are finite, the result should be
61 if (w
.ieee
.exponent
== 0x7ff
62 && u
.ieee
.exponent
!= 0x7ff
63 && v
.ieee
.exponent
!= 0x7ff)
65 /* If z is zero and x are y are nonzero, compute the result
66 as x * y to avoid the wrong sign of a zero result if x * y
68 if (z
== 0 && x
!= 0 && y
!= 0)
70 /* If x or y or z is Inf/NaN, or if x * y is zero, compute as
72 if (u
.ieee
.exponent
== 0x7ff
73 || v
.ieee
.exponent
== 0x7ff
74 || w
.ieee
.exponent
== 0x7ff
78 /* If fma will certainly overflow, compute as x * y. */
79 if (u
.ieee
.exponent
+ v
.ieee
.exponent
> 0x7ff + IEEE754_DOUBLE_BIAS
)
81 /* If x * y is less than 1/4 of DBL_TRUE_MIN, neither the
82 result nor whether there is underflow depends on its exact
83 value, only on its sign. */
84 if (u
.ieee
.exponent
+ v
.ieee
.exponent
85 < IEEE754_DOUBLE_BIAS
- DBL_MANT_DIG
- 2)
87 int neg
= u
.ieee
.negative
^ v
.ieee
.negative
;
88 double tiny
= neg
? -0x1p
-1074 : 0x1p
-1074;
89 if (w
.ieee
.exponent
>= 3)
91 /* Scaling up, adding TINY and scaling down produces the
92 correct result, because in round-to-nearest mode adding
93 TINY has no effect and in other modes double rounding is
94 harmless. But it may not produce required underflow
96 v
.d
= z
* 0x1p
54 + tiny
;
97 if (TININESS_AFTER_ROUNDING
98 ? v
.ieee
.exponent
< 55
99 : (w
.ieee
.exponent
== 0
100 || (w
.ieee
.exponent
== 1
101 && w
.ieee
.negative
!= neg
102 && w
.ieee
.mantissa1
== 0
103 && w
.ieee
.mantissa0
== 0)))
105 double force_underflow
= x
* y
;
106 math_force_eval (force_underflow
);
108 return v
.d
* 0x1p
-54;
110 if (u
.ieee
.exponent
+ v
.ieee
.exponent
111 >= 0x7ff + IEEE754_DOUBLE_BIAS
- DBL_MANT_DIG
)
113 /* Compute 1p-53 times smaller result and multiply
115 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
116 u
.ieee
.exponent
-= DBL_MANT_DIG
;
118 v
.ieee
.exponent
-= DBL_MANT_DIG
;
119 /* If x + y exponent is very large and z exponent is very small,
120 it doesn't matter if we don't adjust it. */
121 if (w
.ieee
.exponent
> DBL_MANT_DIG
)
122 w
.ieee
.exponent
-= DBL_MANT_DIG
;
125 else if (w
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
)
128 If z exponent is very large and x and y exponents are
129 very small, adjust them up to avoid spurious underflows,
131 if (u
.ieee
.exponent
+ v
.ieee
.exponent
132 <= IEEE754_DOUBLE_BIAS
+ 2 * DBL_MANT_DIG
)
134 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
135 u
.ieee
.exponent
+= 2 * DBL_MANT_DIG
+ 2;
137 v
.ieee
.exponent
+= 2 * DBL_MANT_DIG
+ 2;
139 else if (u
.ieee
.exponent
> v
.ieee
.exponent
)
141 if (u
.ieee
.exponent
> DBL_MANT_DIG
)
142 u
.ieee
.exponent
-= DBL_MANT_DIG
;
144 else if (v
.ieee
.exponent
> DBL_MANT_DIG
)
145 v
.ieee
.exponent
-= DBL_MANT_DIG
;
146 w
.ieee
.exponent
-= DBL_MANT_DIG
;
149 else if (u
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
)
151 u
.ieee
.exponent
-= DBL_MANT_DIG
;
153 v
.ieee
.exponent
+= DBL_MANT_DIG
;
157 else if (v
.ieee
.exponent
>= 0x7ff - DBL_MANT_DIG
)
159 v
.ieee
.exponent
-= DBL_MANT_DIG
;
161 u
.ieee
.exponent
+= DBL_MANT_DIG
;
165 else /* if (u.ieee.exponent + v.ieee.exponent
166 <= IEEE754_DOUBLE_BIAS + DBL_MANT_DIG) */
168 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
169 u
.ieee
.exponent
+= 2 * DBL_MANT_DIG
+ 2;
171 v
.ieee
.exponent
+= 2 * DBL_MANT_DIG
+ 2;
172 if (w
.ieee
.exponent
<= 4 * DBL_MANT_DIG
+ 6)
175 w
.ieee
.exponent
+= 2 * DBL_MANT_DIG
+ 2;
180 /* Otherwise x * y should just affect inexact
188 /* Ensure correct sign of exact 0 + 0. */
189 if (__glibc_unlikely ((x
== 0 || y
== 0) && z
== 0))
191 x
= math_opt_barrier (x
);
196 libc_feholdexcept_setround (&env
, FE_TONEAREST
);
198 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
199 #define C ((1 << (DBL_MANT_DIG + 1) / 2) + 1)
207 double m2
= (((x1
* y1
- m1
) + x1
* y2
) + x2
* y1
) + x2
* y2
;
209 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
216 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
217 math_force_eval (m2
);
218 math_force_eval (a2
);
219 feclearexcept (FE_INEXACT
);
221 /* If the result is an exact zero, ensure it has the correct sign. */
222 if (a1
== 0 && m2
== 0)
224 libc_feupdateenv (&env
);
225 /* Ensure that round-to-nearest value of z + m1 is not reused. */
226 z
= math_opt_barrier (z
);
230 libc_fesetround (FE_TOWARDZERO
);
232 /* Perform m2 + a2 addition with round to odd. */
235 if (__glibc_unlikely (adjust
< 0))
237 if ((u
.ieee
.mantissa1
& 1) == 0)
238 u
.ieee
.mantissa1
|= libc_fetestexcept (FE_INEXACT
) != 0;
240 /* Ensure the addition is not scheduled after fetestexcept call. */
241 math_force_eval (v
.d
);
244 /* Reset rounding mode and test for inexact simultaneously. */
245 int j
= libc_feupdateenv_test (&env
, FE_INEXACT
) != 0;
247 if (__glibc_likely (adjust
== 0))
249 if ((u
.ieee
.mantissa1
& 1) == 0 && u
.ieee
.exponent
!= 0x7ff)
250 u
.ieee
.mantissa1
|= j
;
251 /* Result is a1 + u.d. */
254 else if (__glibc_likely (adjust
> 0))
256 if ((u
.ieee
.mantissa1
& 1) == 0 && u
.ieee
.exponent
!= 0x7ff)
257 u
.ieee
.mantissa1
|= j
;
258 /* Result is a1 + u.d, scaled up. */
259 return (a1
+ u
.d
) * 0x1p
53;
263 /* If a1 + u.d is exact, the only rounding happens during
266 return v
.d
* 0x1p
-108;
267 /* If result rounded to zero is not subnormal, no double
268 rounding will occur. */
269 if (v
.ieee
.exponent
> 108)
270 return (a1
+ u
.d
) * 0x1p
-108;
271 /* If v.d * 0x1p-108 with round to zero is a subnormal above
272 or equal to DBL_MIN / 2, then v.d * 0x1p-108 shifts mantissa
273 down just by 1 bit, which means v.ieee.mantissa1 |= j would
274 change the round bit, not sticky or guard bit.
275 v.d * 0x1p-108 never normalizes by shifting up,
276 so round bit plus sticky bit should be already enough
277 for proper rounding. */
278 if (v
.ieee
.exponent
== 108)
280 /* If the exponent would be in the normal range when
281 rounding to normal precision with unbounded exponent
282 range, the exact result is known and spurious underflows
283 must be avoided on systems detecting tininess after
285 if (TININESS_AFTER_ROUNDING
)
288 if (w
.ieee
.exponent
== 109)
289 return w
.d
* 0x1p
-108;
291 /* v.ieee.mantissa1 & 2 is LSB bit of the result before rounding,
292 v.ieee.mantissa1 & 1 is the round bit and j is our sticky
295 w
.ieee
.mantissa1
= ((v
.ieee
.mantissa1
& 3) << 1) | j
;
296 w
.ieee
.negative
= v
.ieee
.negative
;
297 v
.ieee
.mantissa1
&= ~3U;
302 v
.ieee
.mantissa1
|= j
;
303 return v
.d
* 0x1p
-108;
305 #endif /* ! USE_FMA_BUILTIN */
308 libm_alias_double (__fma
, fma
)
309 libm_alias_double_narrow (__fma
, fma
)