]>
Commit | Line | Data |
---|---|---|
3e692e05 | 1 | /* Compute x * y + z as ternary operation. |
4842e4fe | 2 | Copyright (C) 2010-2012 Free Software Foundation, Inc. |
3e692e05 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/>. */ | |
3e692e05 JJ |
19 | |
20 | #include <float.h> | |
21 | #include <math.h> | |
22 | #include <fenv.h> | |
23 | #include <ieee754.h> | |
4842e4fe | 24 | #include <math_private.h> |
3e692e05 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 | ||
30 | long double | |
31 | __fmal (long double x, long double y, long double z) | |
32 | { | |
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)) | |
46 | { | |
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) | |
66 | { | |
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; | |
78 | } | |
79 | else if (w.ieee.exponent >= 0x7fff - LDBL_MANT_DIG) | |
80 | { | |
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) | |
85 | { | |
86 | if (u.ieee.exponent > LDBL_MANT_DIG) | |
87 | u.ieee.exponent -= LDBL_MANT_DIG; | |
88 | } | |
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; | |
93 | } | |
94 | else if (u.ieee.exponent >= 0x7fff - LDBL_MANT_DIG) | |
95 | { | |
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; | |
101 | } | |
102 | else if (v.ieee.exponent >= 0x7fff - LDBL_MANT_DIG) | |
103 | { | |
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; | |
109 | } | |
110 | else /* if (u.ieee.exponent + v.ieee.exponent | |
111 | <= IEEE854_LONG_DOUBLE_BIAS + LDBL_MANT_DIG) */ | |
112 | { | |
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) | |
118 | { | |
119 | if (w.ieee.exponent) | |
120 | w.ieee.exponent += 2 * LDBL_MANT_DIG; | |
121 | else | |
122 | w.d *= 0x1p226L; | |
123 | adjust = -1; | |
124 | } | |
125 | /* Otherwise x * y should just affect inexact | |
126 | and nothing else. */ | |
127 | } | |
128 | x = u.d; | |
129 | y = v.d; | |
130 | z = w.d; | |
131 | } | |
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; | |
142 | ||
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; | |
150 | ||
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; | |
156 | ||
157 | if (__builtin_expect (adjust == 0, 1)) | |
158 | { | |
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; | |
164 | } | |
165 | else if (__builtin_expect (adjust > 0, 1)) | |
166 | { | |
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; | |
172 | } | |
173 | else | |
174 | { | |
175 | if ((u.ieee.mantissa3 & 1) == 0) | |
176 | u.ieee.mantissa3 |= fetestexcept (FE_INEXACT) != 0; | |
177 | v.d = a1 + u.d; | |
7c08a05c | 178 | /* Ensure the addition is not scheduled after fetestexcept call. */ |
4842e4fe | 179 | math_force_eval (v.d); |
3e692e05 JJ |
180 | int j = fetestexcept (FE_INEXACT) != 0; |
181 | feupdateenv (&env); | |
182 | /* Ensure the following computations are performed in default rounding | |
183 | mode instead of just reusing the round to zero computation. */ | |
184 | asm volatile ("" : "=m" (u) : "m" (u)); | |
185 | /* If a1 + u.d is exact, the only rounding happens during | |
186 | scaling down. */ | |
187 | if (j == 0) | |
188 | return v.d * 0x1p-226L; | |
189 | /* If result rounded to zero is not subnormal, no double | |
190 | rounding will occur. */ | |
191 | if (v.ieee.exponent > 226) | |
192 | return (a1 + u.d) * 0x1p-226L; | |
193 | /* If v.d * 0x1p-226L with round to zero is a subnormal above | |
194 | or equal to LDBL_MIN / 2, then v.d * 0x1p-226L shifts mantissa | |
195 | down just by 1 bit, which means v.ieee.mantissa3 |= j would | |
196 | change the round bit, not sticky or guard bit. | |
197 | v.d * 0x1p-226L never normalizes by shifting up, | |
198 | so round bit plus sticky bit should be already enough | |
199 | for proper rounding. */ | |
200 | if (v.ieee.exponent == 226) | |
201 | { | |
202 | /* v.ieee.mantissa3 & 2 is LSB bit of the result before rounding, | |
203 | v.ieee.mantissa3 & 1 is the round bit and j is our sticky | |
204 | bit. In round-to-nearest 001 rounds down like 00, | |
205 | 011 rounds up, even though 01 rounds down (thus we need | |
206 | to adjust), 101 rounds down like 10 and 111 rounds up | |
207 | like 11. */ | |
208 | if ((v.ieee.mantissa3 & 3) == 1) | |
209 | { | |
210 | v.d *= 0x1p-226L; | |
211 | if (v.ieee.negative) | |
7c08a05c | 212 | return v.d - 0x1p-16494L /* __LDBL_DENORM_MIN__ */; |
3e692e05 | 213 | else |
7c08a05c | 214 | return v.d + 0x1p-16494L /* __LDBL_DENORM_MIN__ */; |
3e692e05 JJ |
215 | } |
216 | else | |
217 | return v.d * 0x1p-226L; | |
218 | } | |
219 | v.ieee.mantissa3 |= j; | |
220 | return v.d * 0x1p-226L; | |
221 | } | |
222 | } | |
223 | weak_alias (__fmal, fmal) |