]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/ldbl-96/s_fma.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-96 / s_fma.c
CommitLineData
5e908464 1/* Compute x * y + z as ternary operation.
688903eb 2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
5e908464
JJ
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
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
5e908464
JJ
19
20#include <float.h>
21#include <math.h>
22#include <fenv.h>
23#include <ieee754.h>
dacdc867 24#include <math_private.h>
f85a176f 25#include <libm-alias-double.h>
5e908464
JJ
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{
a1ffb40e 34 if (__glibc_unlikely (isinf (z)))
3e692e05
JJ
35 {
36 /* If z is Inf, but x and y are finite, the result should be
37 z rather than NaN. */
cbf377ed 38 if (isfinite (x) && isfinite (y))
3e692e05
JJ
39 return (z + x) + y;
40 return (x * y) + z;
41 }
42
8ec5b013 43 /* Ensure correct sign of exact 0 + 0. */
a1ffb40e 44 if (__glibc_unlikely ((x == 0 || y == 0) && z == 0))
09245377
L
45 {
46 x = math_opt_barrier (x);
47 return x * y + z;
48 }
8ec5b013 49
5b5b04d6
JM
50 fenv_t env;
51 feholdexcept (&env);
52 fesetround (FE_TONEAREST);
53
5e908464
JJ
54 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
55#define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1)
3e692e05
JJ
56 long double x1 = (long double) x * C;
57 long double y1 = (long double) y * C;
58 long double m1 = (long double) x * y;
5e908464
JJ
59 x1 = (x - x1) + x1;
60 y1 = (y - y1) + y1;
61 long double x2 = x - x1;
62 long double y2 = y - y1;
63 long double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2;
64
65 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
66 long double a1 = z + m1;
67 long double t1 = a1 - z;
68 long double t2 = a1 - t1;
69 t1 = m1 - t1;
70 t2 = z - t2;
71 long double a2 = t1 + t2;
4896f049
RH
72 /* Ensure the arithmetic is not scheduled after feclearexcept call. */
73 math_force_eval (m2);
74 math_force_eval (a2);
5b5b04d6
JM
75 feclearexcept (FE_INEXACT);
76
4896f049 77 /* If the result is an exact zero, ensure it has the correct sign. */
5b5b04d6
JM
78 if (a1 == 0 && m2 == 0)
79 {
80 feupdateenv (&env);
4896f049
RH
81 /* Ensure that round-to-nearest value of z + m1 is not reused. */
82 z = math_opt_barrier (z);
5b5b04d6
JM
83 return z + m1;
84 }
5e908464 85
5e908464
JJ
86 fesetround (FE_TOWARDZERO);
87 /* Perform m2 + a2 addition with round to odd. */
88 a2 = a2 + m2;
89
90 /* Add that to a1 again using rounding to odd. */
91 union ieee854_long_double u;
92 u.d = a1 + a2;
93 if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7fff)
94 u.ieee.mantissa1 |= fetestexcept (FE_INEXACT) != 0;
95 feupdateenv (&env);
96
97 /* Add finally round to double precision. */
98 return u.d;
99}
100#ifndef __fma
f85a176f 101libm_alias_double (__fma, fma)
5e908464 102#endif