]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/ldbl-96/s_fma.c
7519936bdceb648e6105c4ccfa4d211df9b98f4e
1 /* Compute x * y + z as ternary operation.
2 Copyright (C) 2010-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2010.
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.
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.
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 <https://www.gnu.org/licenses/>. */
24 #include <math-barriers.h>
25 #include <libm-alias-double.h>
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 */
32 __fma (double x
, double y
, double z
)
34 if (__glibc_unlikely (!isfinite (x
) || !isfinite (y
)))
36 else if (__glibc_unlikely (!isfinite (z
)))
37 /* If z is Inf, but x and y are finite, the result should be z
41 /* Ensure correct sign of exact 0 + 0. */
42 if (__glibc_unlikely ((x
== 0 || y
== 0) && z
== 0))
44 x
= math_opt_barrier (x
);
50 fesetround (FE_TONEAREST
);
52 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
53 #define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1)
54 long double x1
= (long double) x
* C
;
55 long double y1
= (long double) y
* C
;
56 long double m1
= (long double) x
* y
;
59 long double x2
= x
- x1
;
60 long double y2
= y
- y1
;
61 long double m2
= (((x1
* y1
- m1
) + x1
* y2
) + x2
* y1
) + x2
* y2
;
63 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
64 long double a1
= z
+ m1
;
65 long double t1
= a1
- z
;
66 long double t2
= a1
- t1
;
69 long double a2
= t1
+ t2
;
70 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
73 feclearexcept (FE_INEXACT
);
75 /* If the result is an exact zero, ensure it has the correct sign. */
76 if (a1
== 0 && m2
== 0)
79 /* Ensure that round-to-nearest value of z + m1 is not reused. */
80 z
= math_opt_barrier (z
);
84 fesetround (FE_TOWARDZERO
);
85 /* Perform m2 + a2 addition with round to odd. */
88 /* Add that to a1 again using rounding to odd. */
89 union ieee854_long_double u
;
91 if ((u
.ieee
.mantissa1
& 1) == 0 && u
.ieee
.exponent
!= 0x7fff)
92 u
.ieee
.mantissa1
|= fetestexcept (FE_INEXACT
) != 0;
95 /* Add finally round to double precision. */
99 libm_alias_double (__fma
, fma
)