Implement fmal, some fma bugfixes
[glibc.git] / sysdeps / ieee754 / ldbl-128 / s_fmal.c
blob9ec5ba9ee927f9e16733093c7f85206ab3d85129
1 /* Compute x * y + z as ternary operation.
2 Copyright (C) 2010 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <float.h>
22 #include <math.h>
23 #include <fenv.h>
24 #include <ieee754.h>
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 */
30 long double
31 __fmal (long double x, long double y, long double z)
33 union ieee854_long_double u, v, w;
34 int adjust = 0;
35 u.d = x;
36 v.d = y;
37 w.d = z;
38 if (__builtin_expect (u.ieee.exponent + v.ieee.exponent
39 >= 0x7fff + IEEE854_LONG_DOUBLE_BIAS
40 - LDBL_MANT_DIG, 0)
41 || __builtin_expect (u.ieee.exponent >= 0x7fff - LDBL_MANT_DIG, 0)
42 || __builtin_expect (v.ieee.exponent >= 0x7fff - LDBL_MANT_DIG, 0)
43 || __builtin_expect (w.ieee.exponent >= 0x7fff - LDBL_MANT_DIG, 0)
44 || __builtin_expect (u.ieee.exponent + v.ieee.exponent
45 <= IEEE854_LONG_DOUBLE_BIAS + LDBL_MANT_DIG, 0))
47 /* If z is Inf, but x and y are finite, the result should be
48 z rather than NaN. */
49 if (w.ieee.exponent == 0x7fff
50 && u.ieee.exponent != 0x7fff
51 && v.ieee.exponent != 0x7fff)
52 return (z + x) + y;
53 /* If x or y or z is Inf/NaN, or if fma will certainly overflow,
54 or if x * y is less than half of LDBL_DENORM_MIN,
55 compute as x * y + z. */
56 if (u.ieee.exponent == 0x7fff
57 || v.ieee.exponent == 0x7fff
58 || w.ieee.exponent == 0x7fff
59 || u.ieee.exponent + v.ieee.exponent
60 > 0x7fff + IEEE854_LONG_DOUBLE_BIAS
61 || u.ieee.exponent + v.ieee.exponent
62 < IEEE854_LONG_DOUBLE_BIAS - LDBL_MANT_DIG - 2)
63 return x * y + z;
64 if (u.ieee.exponent + v.ieee.exponent
65 >= 0x7fff + IEEE854_LONG_DOUBLE_BIAS - LDBL_MANT_DIG)
67 /* Compute 1p-113 times smaller result and multiply
68 at the end. */
69 if (u.ieee.exponent > v.ieee.exponent)
70 u.ieee.exponent -= LDBL_MANT_DIG;
71 else
72 v.ieee.exponent -= LDBL_MANT_DIG;
73 /* If x + y exponent is very large and z exponent is very small,
74 it doesn't matter if we don't adjust it. */
75 if (w.ieee.exponent > LDBL_MANT_DIG)
76 w.ieee.exponent -= LDBL_MANT_DIG;
77 adjust = 1;
79 else if (w.ieee.exponent >= 0x7fff - LDBL_MANT_DIG)
81 /* Similarly.
82 If z exponent is very large and x and y exponents are
83 very small, it doesn't matter if we don't adjust it. */
84 if (u.ieee.exponent > v.ieee.exponent)
86 if (u.ieee.exponent > LDBL_MANT_DIG)
87 u.ieee.exponent -= LDBL_MANT_DIG;
89 else if (v.ieee.exponent > LDBL_MANT_DIG)
90 v.ieee.exponent -= LDBL_MANT_DIG;
91 w.ieee.exponent -= LDBL_MANT_DIG;
92 adjust = 1;
94 else if (u.ieee.exponent >= 0x7fff - LDBL_MANT_DIG)
96 u.ieee.exponent -= LDBL_MANT_DIG;
97 if (v.ieee.exponent)
98 v.ieee.exponent += LDBL_MANT_DIG;
99 else
100 v.d *= 0x1p113L;
102 else if (v.ieee.exponent >= 0x7fff - LDBL_MANT_DIG)
104 v.ieee.exponent -= LDBL_MANT_DIG;
105 if (u.ieee.exponent)
106 u.ieee.exponent += LDBL_MANT_DIG;
107 else
108 u.d *= 0x1p113L;
110 else /* if (u.ieee.exponent + v.ieee.exponent
111 <= IEEE854_LONG_DOUBLE_BIAS + LDBL_MANT_DIG) */
113 if (u.ieee.exponent > v.ieee.exponent)
114 u.ieee.exponent += 2 * LDBL_MANT_DIG;
115 else
116 v.ieee.exponent += 2 * LDBL_MANT_DIG;
117 if (w.ieee.exponent <= 4 * LDBL_MANT_DIG + 4)
119 if (w.ieee.exponent)
120 w.ieee.exponent += 2 * LDBL_MANT_DIG;
121 else
122 w.d *= 0x1p226L;
123 adjust = -1;
125 /* Otherwise x * y should just affect inexact
126 and nothing else. */
128 x = u.d;
129 y = v.d;
130 z = w.d;
132 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
133 #define C ((1LL << (LDBL_MANT_DIG + 1) / 2) + 1)
134 long double x1 = x * C;
135 long double y1 = y * C;
136 long double m1 = x * y;
137 x1 = (x - x1) + x1;
138 y1 = (y - y1) + y1;
139 long double x2 = x - x1;
140 long double y2 = y - y1;
141 long double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2;
143 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
144 long double a1 = z + m1;
145 long double t1 = a1 - z;
146 long double t2 = a1 - t1;
147 t1 = m1 - t1;
148 t2 = z - t2;
149 long double a2 = t1 + t2;
151 fenv_t env;
152 feholdexcept (&env);
153 fesetround (FE_TOWARDZERO);
154 /* Perform m2 + a2 addition with round to odd. */
155 u.d = a2 + m2;
157 if (__builtin_expect (adjust == 0, 1))
159 if ((u.ieee.mantissa3 & 1) == 0 && u.ieee.exponent != 0x7fff)
160 u.ieee.mantissa3 |= fetestexcept (FE_INEXACT) != 0;
161 feupdateenv (&env);
162 /* Result is a1 + u.d. */
163 return a1 + u.d;
165 else if (__builtin_expect (adjust > 0, 1))
167 if ((u.ieee.mantissa3 & 1) == 0 && u.ieee.exponent != 0x7fff)
168 u.ieee.mantissa3 |= fetestexcept (FE_INEXACT) != 0;
169 feupdateenv (&env);
170 /* Result is a1 + u.d, scaled up. */
171 return (a1 + u.d) * 0x1p113L;
173 else
175 if ((u.ieee.mantissa3 & 1) == 0)
176 u.ieee.mantissa3 |= fetestexcept (FE_INEXACT) != 0;
177 v.d = a1 + u.d;
178 int j = fetestexcept (FE_INEXACT) != 0;
179 feupdateenv (&env);
180 /* Ensure the following computations are performed in default rounding
181 mode instead of just reusing the round to zero computation. */
182 asm volatile ("" : "=m" (u) : "m" (u));
183 /* If a1 + u.d is exact, the only rounding happens during
184 scaling down. */
185 if (j == 0)
186 return v.d * 0x1p-226L;
187 /* If result rounded to zero is not subnormal, no double
188 rounding will occur. */
189 if (v.ieee.exponent > 226)
190 return (a1 + u.d) * 0x1p-226L;
191 /* If v.d * 0x1p-226L with round to zero is a subnormal above
192 or equal to LDBL_MIN / 2, then v.d * 0x1p-226L shifts mantissa
193 down just by 1 bit, which means v.ieee.mantissa3 |= j would
194 change the round bit, not sticky or guard bit.
195 v.d * 0x1p-226L never normalizes by shifting up,
196 so round bit plus sticky bit should be already enough
197 for proper rounding. */
198 if (v.ieee.exponent == 226)
200 /* v.ieee.mantissa3 & 2 is LSB bit of the result before rounding,
201 v.ieee.mantissa3 & 1 is the round bit and j is our sticky
202 bit. In round-to-nearest 001 rounds down like 00,
203 011 rounds up, even though 01 rounds down (thus we need
204 to adjust), 101 rounds down like 10 and 111 rounds up
205 like 11. */
206 if ((v.ieee.mantissa3 & 3) == 1)
208 v.d *= 0x1p-226L;
209 if (v.ieee.negative)
210 return v.d - 0x1p-16493L /* __LDBL_DENORM_MIN__ */;
211 else
212 return v.d + 0x1p-16493L /* __LDBL_DENORM_MIN__ */;
214 else
215 return v.d * 0x1p-226L;
217 v.ieee.mantissa3 |= j;
218 return v.d * 0x1p-226L;
221 weak_alias (__fmal, fmal)