]>
Commit | Line | Data |
---|---|---|
5e908464 | 1 | /* Compute x * y + z as ternary operation. |
8ec5b013 | 2 | Copyright (C) 2010-2012 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> | |
24 | ||
25 | /* This implementation uses rounding to odd to avoid problems with | |
26 | double rounding. See a paper by Boldo and Melquiond: | |
27 | http://www.lri.fr/~melquion/doc/08-tc.pdf */ | |
28 | ||
29 | double | |
30 | __fma (double x, double y, double z) | |
31 | { | |
3e692e05 JJ |
32 | if (__builtin_expect (isinf (z), 0)) |
33 | { | |
34 | /* If z is Inf, but x and y are finite, the result should be | |
35 | z rather than NaN. */ | |
36 | if (finite (x) && finite (y)) | |
37 | return (z + x) + y; | |
38 | return (x * y) + z; | |
39 | } | |
40 | ||
8ec5b013 JM |
41 | /* Ensure correct sign of exact 0 + 0. */ |
42 | if (__builtin_expect ((x == 0 || y == 0) && z == 0, 0)) | |
43 | return x * y + z; | |
44 | ||
5e908464 JJ |
45 | /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */ |
46 | #define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1) | |
3e692e05 JJ |
47 | long double x1 = (long double) x * C; |
48 | long double y1 = (long double) y * C; | |
49 | long double m1 = (long double) x * y; | |
5e908464 JJ |
50 | x1 = (x - x1) + x1; |
51 | y1 = (y - y1) + y1; | |
52 | long double x2 = x - x1; | |
53 | long double y2 = y - y1; | |
54 | long double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2; | |
55 | ||
56 | /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */ | |
57 | long double a1 = z + m1; | |
58 | long double t1 = a1 - z; | |
59 | long double t2 = a1 - t1; | |
60 | t1 = m1 - t1; | |
61 | t2 = z - t2; | |
62 | long double a2 = t1 + t2; | |
63 | ||
64 | fenv_t env; | |
65 | feholdexcept (&env); | |
66 | fesetround (FE_TOWARDZERO); | |
67 | /* Perform m2 + a2 addition with round to odd. */ | |
68 | a2 = a2 + m2; | |
69 | ||
70 | /* Add that to a1 again using rounding to odd. */ | |
71 | union ieee854_long_double u; | |
72 | u.d = a1 + a2; | |
73 | if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7fff) | |
74 | u.ieee.mantissa1 |= fetestexcept (FE_INEXACT) != 0; | |
75 | feupdateenv (&env); | |
76 | ||
77 | /* Add finally round to double precision. */ | |
78 | return u.d; | |
79 | } | |
80 | #ifndef __fma | |
81 | weak_alias (__fma, fma) | |
82 | #endif |