]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/ldbl-96/s_fma.c
Remove "Contributed by" lines
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-96 / s_fma.c
CommitLineData
5e908464 1/* Compute x * y + z as ternary operation.
2b778ceb 2 Copyright (C) 2010-2021 Free Software Foundation, Inc.
5e908464 3 This file is part of the GNU C Library.
5e908464
JJ
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
5e908464
JJ
18
19#include <float.h>
20#include <math.h>
21#include <fenv.h>
22#include <ieee754.h>
b4d5b8b0 23#include <math-barriers.h>
f85a176f 24#include <libm-alias-double.h>
5e908464
JJ
25
26/* This implementation uses rounding to odd to avoid problems with
27 double rounding. See a paper by Boldo and Melquiond:
28 http://www.lri.fr/~melquion/doc/08-tc.pdf */
29
30double
31__fma (double x, double y, double z)
32{
ca121b11
JM
33 if (__glibc_unlikely (!isfinite (x) || !isfinite (y)))
34 return x * y + z;
35 else if (__glibc_unlikely (!isfinite (z)))
36 /* If z is Inf, but x and y are finite, the result should be z
37 rather than NaN. */
38 return (z + x) + y;
3e692e05 39
8ec5b013 40 /* Ensure correct sign of exact 0 + 0. */
a1ffb40e 41 if (__glibc_unlikely ((x == 0 || y == 0) && z == 0))
09245377
L
42 {
43 x = math_opt_barrier (x);
44 return x * y + z;
45 }
8ec5b013 46
5b5b04d6
JM
47 fenv_t env;
48 feholdexcept (&env);
49 fesetround (FE_TONEAREST);
50
5e908464
JJ
51 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
52#define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1)
3e692e05
JJ
53 long double x1 = (long double) x * C;
54 long double y1 = (long double) y * C;
55 long double m1 = (long double) x * y;
5e908464
JJ
56 x1 = (x - x1) + x1;
57 y1 = (y - y1) + y1;
58 long double x2 = x - x1;
59 long double y2 = y - y1;
60 long double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2;
61
62 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
63 long double a1 = z + m1;
64 long double t1 = a1 - z;
65 long double t2 = a1 - t1;
66 t1 = m1 - t1;
67 t2 = z - t2;
68 long double a2 = t1 + t2;
4896f049
RH
69 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
70 math_force_eval (m2);
71 math_force_eval (a2);
5b5b04d6
JM
72 feclearexcept (FE_INEXACT);
73
4896f049 74 /* If the result is an exact zero, ensure it has the correct sign. */
5b5b04d6
JM
75 if (a1 == 0 && m2 == 0)
76 {
77 feupdateenv (&env);
4896f049
RH
78 /* Ensure that round-to-nearest value of z + m1 is not reused. */
79 z = math_opt_barrier (z);
5b5b04d6
JM
80 return z + m1;
81 }
5e908464 82
5e908464
JJ
83 fesetround (FE_TOWARDZERO);
84 /* Perform m2 + a2 addition with round to odd. */
85 a2 = a2 + m2;
86
87 /* Add that to a1 again using rounding to odd. */
88 union ieee854_long_double u;
89 u.d = a1 + a2;
90 if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7fff)
91 u.ieee.mantissa1 |= fetestexcept (FE_INEXACT) != 0;
92 feupdateenv (&env);
93
94 /* Add finally round to double precision. */
95 return u.d;
96}
97#ifndef __fma
f85a176f 98libm_alias_double (__fma, fma)
5e908464 99#endif