1 /* Compute x * y + z as ternary operation.
2 Copyright (C) 2010-2023 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 <libm-alias-double.h>
30 #include <math-narrow-alias.h>
32 /* This implementation uses rounding to odd to avoid problems with
33 double rounding. See a paper by Boldo and Melquiond:
34 http://www.lri.fr/~melquion/doc/08-tc.pdf */
37 __fma (double x
, double y
, double z
)
39 if (__glibc_unlikely (!isfinite (x
) || !isfinite (y
)))
41 else if (__glibc_unlikely (!isfinite (z
)))
42 /* If z is Inf, but x and y are finite, the result should be z
46 /* Ensure correct sign of exact 0 + 0. */
47 if (__glibc_unlikely ((x
== 0 || y
== 0) && z
== 0))
49 x
= math_opt_barrier (x
);
55 fesetround (FE_TONEAREST
);
57 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
58 #define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1)
59 long double x1
= (long double) x
* C
;
60 long double y1
= (long double) y
* C
;
61 long double m1
= (long double) x
* y
;
64 long double x2
= x
- x1
;
65 long double y2
= y
- y1
;
66 long double m2
= (((x1
* y1
- m1
) + x1
* y2
) + x2
* y1
) + x2
* y2
;
68 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
69 long double a1
= z
+ m1
;
70 long double t1
= a1
- z
;
71 long double t2
= a1
- t1
;
74 long double a2
= t1
+ t2
;
75 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
78 feclearexcept (FE_INEXACT
);
80 /* If the result is an exact zero, ensure it has the correct sign. */
81 if (a1
== 0 && m2
== 0)
84 /* Ensure that round-to-nearest value of z + m1 is not reused. */
85 z
= math_opt_barrier (z
);
89 fesetround (FE_TOWARDZERO
);
90 /* Perform m2 + a2 addition with round to odd. */
93 /* Add that to a1 again using rounding to odd. */
94 union ieee854_long_double u
;
96 if ((u
.ieee
.mantissa1
& 1) == 0 && u
.ieee
.exponent
!= 0x7fff)
97 u
.ieee
.mantissa1
|= fetestexcept (FE_INEXACT
) != 0;
100 /* Add finally round to double precision. */
104 libm_alias_double (__fma
, fma
)
105 libm_alias_double_narrow (__fma
, fma
)