49
50
&& u.ieee.exponent != 0x7ff
50
51
&& v.ieee.exponent != 0x7ff)
51
52
return (z + x) + y;
52
/* If x or y or z is Inf/NaN, or if fma will certainly overflow,
53
or if x * y is less than half of DBL_DENORM_MIN,
54
compute as x * y + z. */
53
/* If z is zero and x are y are nonzero, compute the result
54
as x * y to avoid the wrong sign of a zero result if x * y
56
if (z == 0 && x != 0 && y != 0)
58
/* If x or y or z is Inf/NaN, or if x * y is zero, compute as
55
60
if (u.ieee.exponent == 0x7ff
56
61
|| v.ieee.exponent == 0x7ff
57
62
|| w.ieee.exponent == 0x7ff
58
|| u.ieee.exponent + v.ieee.exponent
59
> 0x7ff + IEEE754_DOUBLE_BIAS
60
|| u.ieee.exponent + v.ieee.exponent
61
< IEEE754_DOUBLE_BIAS - DBL_MANT_DIG - 2)
66
/* If fma will certainly overflow, compute as x * y. */
67
if (u.ieee.exponent + v.ieee.exponent > 0x7ff + IEEE754_DOUBLE_BIAS)
69
/* If x * y is less than 1/4 of DBL_DENORM_MIN, neither the
70
result nor whether there is underflow depends on its exact
71
value, only on its sign. */
72
if (u.ieee.exponent + v.ieee.exponent
73
< IEEE754_DOUBLE_BIAS - DBL_MANT_DIG - 2)
75
int neg = u.ieee.negative ^ v.ieee.negative;
76
double tiny = neg ? -0x1p-1074 : 0x1p-1074;
77
if (w.ieee.exponent >= 3)
79
/* Scaling up, adding TINY and scaling down produces the
80
correct result, because in round-to-nearest mode adding
81
TINY has no effect and in other modes double rounding is
82
harmless. But it may not produce required underflow
84
v.d = z * 0x1p54 + tiny;
85
if (TININESS_AFTER_ROUNDING
86
? v.ieee.exponent < 55
87
: (w.ieee.exponent == 0
88
|| (w.ieee.exponent == 1
89
&& w.ieee.negative != neg
90
&& w.ieee.mantissa1 == 0
91
&& w.ieee.mantissa0 == 0)))
93
volatile double force_underflow = x * y;
94
(void) force_underflow;
63
98
if (u.ieee.exponent + v.ieee.exponent
64
99
>= 0x7ff + IEEE754_DOUBLE_BIAS - DBL_MANT_DIG)
81
116
If z exponent is very large and x and y exponents are
82
very small, it doesn't matter if we don't adjust it. */
83
if (u.ieee.exponent > v.ieee.exponent)
117
very small, adjust them up to avoid spurious underflows,
119
if (u.ieee.exponent + v.ieee.exponent
120
<= IEEE754_DOUBLE_BIAS + DBL_MANT_DIG)
122
if (u.ieee.exponent > v.ieee.exponent)
123
u.ieee.exponent += 2 * DBL_MANT_DIG + 2;
125
v.ieee.exponent += 2 * DBL_MANT_DIG + 2;
127
else if (u.ieee.exponent > v.ieee.exponent)
85
129
if (u.ieee.exponent > DBL_MANT_DIG)
86
130
u.ieee.exponent -= DBL_MANT_DIG;
110
154
<= IEEE754_DOUBLE_BIAS + DBL_MANT_DIG) */
112
156
if (u.ieee.exponent > v.ieee.exponent)
113
u.ieee.exponent += 2 * DBL_MANT_DIG;
157
u.ieee.exponent += 2 * DBL_MANT_DIG + 2;
115
v.ieee.exponent += 2 * DBL_MANT_DIG;
116
if (w.ieee.exponent <= 4 * DBL_MANT_DIG + 4)
159
v.ieee.exponent += 2 * DBL_MANT_DIG + 2;
160
if (w.ieee.exponent <= 4 * DBL_MANT_DIG + 6)
118
162
if (w.ieee.exponent)
119
w.ieee.exponent += 2 * DBL_MANT_DIG;
163
w.ieee.exponent += 2 * DBL_MANT_DIG + 2;
124
168
/* Otherwise x * y should just affect inexact
176
/* Ensure correct sign of exact 0 + 0. */
177
if (__builtin_expect ((x == 0 || y == 0) && z == 0, 0))
181
libc_feholdexcept_setround (&env, FE_TONEAREST);
131
183
/* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
132
184
#define C ((1 << (DBL_MANT_DIG + 1) / 2) + 1)
133
185
double x1 = x * C;
148
200
double a2 = t1 + t2;
151
libc_feholdexcept_setround (&env, FE_TOWARDZERO);
201
feclearexcept (FE_INEXACT);
203
/* If the result is an exact zero, ensure it has the correct
205
if (a1 == 0 && m2 == 0)
207
libc_feupdateenv (&env);
208
/* Ensure that round-to-nearest value of z + m1 is not
210
asm volatile ("" : "=m" (z) : "m" (z));
214
libc_fesetround (FE_TOWARDZERO);
153
216
/* Perform m2 + a2 addition with round to odd. */
184
247
/* If a1 + u.d is exact, the only rounding happens during
187
return v.d * 0x1p-106;
250
return v.d * 0x1p-108;
188
251
/* If result rounded to zero is not subnormal, no double
189
252
rounding will occur. */
190
if (v.ieee.exponent > 106)
191
return (a1 + u.d) * 0x1p-106;
192
/* If v.d * 0x1p-106 with round to zero is a subnormal above
193
or equal to DBL_MIN / 2, then v.d * 0x1p-106 shifts mantissa
253
if (v.ieee.exponent > 108)
254
return (a1 + u.d) * 0x1p-108;
255
/* If v.d * 0x1p-108 with round to zero is a subnormal above
256
or equal to DBL_MIN / 2, then v.d * 0x1p-108 shifts mantissa
194
257
down just by 1 bit, which means v.ieee.mantissa1 |= j would
195
258
change the round bit, not sticky or guard bit.
196
v.d * 0x1p-106 never normalizes by shifting up,
259
v.d * 0x1p-108 never normalizes by shifting up,
197
260
so round bit plus sticky bit should be already enough
198
261
for proper rounding. */
199
if (v.ieee.exponent == 106)
262
if (v.ieee.exponent == 108)
264
/* If the exponent would be in the normal range when
265
rounding to normal precision with unbounded exponent
266
range, the exact result is known and spurious underflows
267
must be avoided on systems detecting tininess after
269
if (TININESS_AFTER_ROUNDING)
272
if (w.ieee.exponent == 109)
273
return w.d * 0x1p-108;
201
275
/* v.ieee.mantissa1 & 2 is LSB bit of the result before rounding,
202
276
v.ieee.mantissa1 & 1 is the round bit and j is our sticky
203
bit. In round-to-nearest 001 rounds down like 00,
204
011 rounds up, even though 01 rounds down (thus we need
205
to adjust), 101 rounds down like 10 and 111 rounds up
207
if ((v.ieee.mantissa1 & 3) == 1)
211
return v.d - 0x1p-1074 /* __DBL_DENORM_MIN__ */;
213
return v.d + 0x1p-1074 /* __DBL_DENORM_MIN__ */;
216
return v.d * 0x1p-106;
279
w.ieee.mantissa1 = ((v.ieee.mantissa1 & 3) << 1) | j;
280
w.ieee.negative = v.ieee.negative;
281
v.ieee.mantissa1 &= ~3U;
218
286
v.ieee.mantissa1 |= j;
219
return v.d * 0x1p-106;
287
return v.d * 0x1p-108;