| 1 |
/* Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is part of GCC. |
| 4 |
|
| 5 |
GCC is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 3, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
/***************************************************************************** |
| 25 |
* |
| 26 |
* BID128 fma x * y + z |
| 27 |
* |
| 28 |
****************************************************************************/ |
| 29 |
|
| 30 |
#include "bid_internal.h" |
| 31 |
|
| 32 |
static void |
| 33 |
rounding_correction (unsigned int rnd_mode, |
| 34 |
unsigned int is_inexact_lt_midpoint, |
| 35 |
unsigned int is_inexact_gt_midpoint, |
| 36 |
unsigned int is_midpoint_lt_even, |
| 37 |
unsigned int is_midpoint_gt_even, |
| 38 |
int unbexp, |
| 39 |
UINT128 * ptrres, _IDEC_flags * ptrfpsf) { |
| 40 |
// unbiased true exponent unbexp may be larger than emax |
| 41 |
|
| 42 |
UINT128 res = *ptrres; // expected to have the correct sign and coefficient |
| 43 |
// (the exponent field is ignored, as unbexp is used instead) |
| 44 |
UINT64 sign, exp; |
| 45 |
UINT64 C_hi, C_lo; |
| 46 |
|
| 47 |
// general correction from RN to RA, RM, RP, RZ |
| 48 |
// Note: if the result is negative, then is_inexact_lt_midpoint, |
| 49 |
// is_inexact_gt_midpoint, is_midpoint_lt_even, and is_midpoint_gt_even |
| 50 |
// have to be considered as if determined for the absolute value of the |
| 51 |
// result (so they seem to be reversed) |
| 52 |
|
| 53 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 54 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 55 |
*ptrfpsf |= INEXACT_EXCEPTION; |
| 56 |
} |
| 57 |
// apply correction to result calculated with unbounded exponent |
| 58 |
sign = res.w[1] & MASK_SIGN; |
| 59 |
exp = (UINT64) (unbexp + 6176) << 49; // valid only if expmin<=unbexp<=expmax |
| 60 |
C_hi = res.w[1] & MASK_COEFF; |
| 61 |
C_lo = res.w[0]; |
| 62 |
if ((!sign && ((rnd_mode == ROUNDING_UP && is_inexact_lt_midpoint) || |
| 63 |
((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_UP) && |
| 64 |
is_midpoint_gt_even))) || |
| 65 |
(sign && ((rnd_mode == ROUNDING_DOWN && is_inexact_lt_midpoint) || |
| 66 |
((rnd_mode == ROUNDING_TIES_AWAY || rnd_mode == ROUNDING_DOWN) && |
| 67 |
is_midpoint_gt_even)))) { |
| 68 |
// C = C + 1 |
| 69 |
C_lo = C_lo + 1; |
| 70 |
if (C_lo == 0) |
| 71 |
C_hi = C_hi + 1; |
| 72 |
if (C_hi == 0x0001ed09bead87c0ull && C_lo == 0x378d8e6400000000ull) { |
| 73 |
// C = 10^34 => rounding overflow |
| 74 |
C_hi = 0x0000314dc6448d93ull; |
| 75 |
C_lo = 0x38c15b0a00000000ull; // 10^33 |
| 76 |
// exp = exp + EXP_P1; |
| 77 |
unbexp = unbexp + 1; |
| 78 |
exp = (UINT64) (unbexp + 6176) << 49; |
| 79 |
} |
| 80 |
} else if ((is_midpoint_lt_even || is_inexact_gt_midpoint) && |
| 81 |
((sign && (rnd_mode == ROUNDING_UP || rnd_mode == ROUNDING_TO_ZERO)) || |
| 82 |
(!sign && (rnd_mode == ROUNDING_DOWN || rnd_mode == ROUNDING_TO_ZERO)))) { |
| 83 |
// C = C - 1 |
| 84 |
C_lo = C_lo - 1; |
| 85 |
if (C_lo == 0xffffffffffffffffull) |
| 86 |
C_hi--; |
| 87 |
// check if we crossed into the lower decade |
| 88 |
if (C_hi == 0x0000314dc6448d93ull && C_lo == 0x38c15b09ffffffffull) { |
| 89 |
// C = 10^33 - 1 |
| 90 |
if (exp > 0) { |
| 91 |
C_hi = 0x0001ed09bead87c0ull; // 10^34 - 1 |
| 92 |
C_lo = 0x378d8e63ffffffffull; |
| 93 |
// exp = exp - EXP_P1; |
| 94 |
unbexp = unbexp - 1; |
| 95 |
exp = (UINT64) (unbexp + 6176) << 49; |
| 96 |
} else { // if exp = 0 the result is tiny & inexact |
| 97 |
*ptrfpsf |= UNDERFLOW_EXCEPTION; |
| 98 |
} |
| 99 |
} |
| 100 |
} else { |
| 101 |
; // the result is already correct |
| 102 |
} |
| 103 |
if (unbexp > expmax) { // 6111 |
| 104 |
*ptrfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 105 |
exp = 0; |
| 106 |
if (!sign) { // result is positive |
| 107 |
if (rnd_mode == ROUNDING_UP || rnd_mode == ROUNDING_TIES_AWAY) { // +inf |
| 108 |
C_hi = 0x7800000000000000ull; |
| 109 |
C_lo = 0x0000000000000000ull; |
| 110 |
} else { // res = +MAXFP = (10^34-1) * 10^emax |
| 111 |
C_hi = 0x5fffed09bead87c0ull; |
| 112 |
C_lo = 0x378d8e63ffffffffull; |
| 113 |
} |
| 114 |
} else { // result is negative |
| 115 |
if (rnd_mode == ROUNDING_DOWN || rnd_mode == ROUNDING_TIES_AWAY) { // -inf |
| 116 |
C_hi = 0xf800000000000000ull; |
| 117 |
C_lo = 0x0000000000000000ull; |
| 118 |
} else { // res = -MAXFP = -(10^34-1) * 10^emax |
| 119 |
C_hi = 0xdfffed09bead87c0ull; |
| 120 |
C_lo = 0x378d8e63ffffffffull; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
// assemble the result |
| 125 |
res.w[1] = sign | exp | C_hi; |
| 126 |
res.w[0] = C_lo; |
| 127 |
*ptrres = res; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
add256 (UINT256 x, UINT256 y, UINT256 * pz) { |
| 132 |
// *z = x + yl assume the sum fits in 256 bits |
| 133 |
UINT256 z; |
| 134 |
z.w[0] = x.w[0] + y.w[0]; |
| 135 |
if (z.w[0] < x.w[0]) { |
| 136 |
x.w[1]++; |
| 137 |
if (x.w[1] == 0x0000000000000000ull) { |
| 138 |
x.w[2]++; |
| 139 |
if (x.w[2] == 0x0000000000000000ull) { |
| 140 |
x.w[3]++; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
z.w[1] = x.w[1] + y.w[1]; |
| 145 |
if (z.w[1] < x.w[1]) { |
| 146 |
x.w[2]++; |
| 147 |
if (x.w[2] == 0x0000000000000000ull) { |
| 148 |
x.w[3]++; |
| 149 |
} |
| 150 |
} |
| 151 |
z.w[2] = x.w[2] + y.w[2]; |
| 152 |
if (z.w[2] < x.w[2]) { |
| 153 |
x.w[3]++; |
| 154 |
} |
| 155 |
z.w[3] = x.w[3] + y.w[3]; // it was assumed that no carry is possible |
| 156 |
*pz = z; |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
sub256 (UINT256 x, UINT256 y, UINT256 * pz) { |
| 161 |
// *z = x - y; assume x >= y |
| 162 |
UINT256 z; |
| 163 |
z.w[0] = x.w[0] - y.w[0]; |
| 164 |
if (z.w[0] > x.w[0]) { |
| 165 |
x.w[1]--; |
| 166 |
if (x.w[1] == 0xffffffffffffffffull) { |
| 167 |
x.w[2]--; |
| 168 |
if (x.w[2] == 0xffffffffffffffffull) { |
| 169 |
x.w[3]--; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
z.w[1] = x.w[1] - y.w[1]; |
| 174 |
if (z.w[1] > x.w[1]) { |
| 175 |
x.w[2]--; |
| 176 |
if (x.w[2] == 0xffffffffffffffffull) { |
| 177 |
x.w[3]--; |
| 178 |
} |
| 179 |
} |
| 180 |
z.w[2] = x.w[2] - y.w[2]; |
| 181 |
if (z.w[2] > x.w[2]) { |
| 182 |
x.w[3]--; |
| 183 |
} |
| 184 |
z.w[3] = x.w[3] - y.w[3]; // no borrow possible, because x >= y |
| 185 |
*pz = z; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
static int |
| 190 |
nr_digits256 (UINT256 R256) { |
| 191 |
int ind; |
| 192 |
// determine the number of decimal digits in R256 |
| 193 |
if (R256.w[3] == 0x0 && R256.w[2] == 0x0 && R256.w[1] == 0x0) { |
| 194 |
// between 1 and 19 digits |
| 195 |
for (ind = 1; ind <= 19; ind++) { |
| 196 |
if (R256.w[0] < ten2k64[ind]) { |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
// ind digits |
| 201 |
} else if (R256.w[3] == 0x0 && R256.w[2] == 0x0 && |
| 202 |
(R256.w[1] < ten2k128[0].w[1] || |
| 203 |
(R256.w[1] == ten2k128[0].w[1] |
| 204 |
&& R256.w[0] < ten2k128[0].w[0]))) { |
| 205 |
// 20 digits |
| 206 |
ind = 20; |
| 207 |
} else if (R256.w[3] == 0x0 && R256.w[2] == 0x0) { |
| 208 |
// between 21 and 38 digits |
| 209 |
for (ind = 1; ind <= 18; ind++) { |
| 210 |
if (R256.w[1] < ten2k128[ind].w[1] || |
| 211 |
(R256.w[1] == ten2k128[ind].w[1] && |
| 212 |
R256.w[0] < ten2k128[ind].w[0])) { |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
// ind + 20 digits |
| 217 |
ind = ind + 20; |
| 218 |
} else if (R256.w[3] == 0x0 && |
| 219 |
(R256.w[2] < ten2k256[0].w[2] || |
| 220 |
(R256.w[2] == ten2k256[0].w[2] && |
| 221 |
R256.w[1] < ten2k256[0].w[1]) || |
| 222 |
(R256.w[2] == ten2k256[0].w[2] && |
| 223 |
R256.w[1] == ten2k256[0].w[1] && |
| 224 |
R256.w[0] < ten2k256[0].w[0]))) { |
| 225 |
// 39 digits |
| 226 |
ind = 39; |
| 227 |
} else { |
| 228 |
// between 40 and 68 digits |
| 229 |
for (ind = 1; ind <= 29; ind++) { |
| 230 |
if (R256.w[3] < ten2k256[ind].w[3] || |
| 231 |
(R256.w[3] == ten2k256[ind].w[3] && |
| 232 |
R256.w[2] < ten2k256[ind].w[2]) || |
| 233 |
(R256.w[3] == ten2k256[ind].w[3] && |
| 234 |
R256.w[2] == ten2k256[ind].w[2] && |
| 235 |
R256.w[1] < ten2k256[ind].w[1]) || |
| 236 |
(R256.w[3] == ten2k256[ind].w[3] && |
| 237 |
R256.w[2] == ten2k256[ind].w[2] && |
| 238 |
R256.w[1] == ten2k256[ind].w[1] && |
| 239 |
R256.w[0] < ten2k256[ind].w[0])) { |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
// ind + 39 digits |
| 244 |
ind = ind + 39; |
| 245 |
} |
| 246 |
return (ind); |
| 247 |
} |
| 248 |
|
| 249 |
// add/subtract C4 and C3 * 10^scale; this may follow a previous rounding, so |
| 250 |
// use the rounding information from ptr_is_* to avoid a double rounding error |
| 251 |
static void |
| 252 |
add_and_round (int q3, |
| 253 |
int q4, |
| 254 |
int e4, |
| 255 |
int delta, |
| 256 |
int p34, |
| 257 |
UINT64 z_sign, |
| 258 |
UINT64 p_sign, |
| 259 |
UINT128 C3, |
| 260 |
UINT256 C4, |
| 261 |
int rnd_mode, |
| 262 |
int *ptr_is_midpoint_lt_even, |
| 263 |
int *ptr_is_midpoint_gt_even, |
| 264 |
int *ptr_is_inexact_lt_midpoint, |
| 265 |
int *ptr_is_inexact_gt_midpoint, |
| 266 |
_IDEC_flags * ptrfpsf, UINT128 * ptrres) { |
| 267 |
|
| 268 |
int scale; |
| 269 |
int x0; |
| 270 |
int ind; |
| 271 |
UINT64 R64; |
| 272 |
UINT128 P128, R128; |
| 273 |
UINT192 P192, R192; |
| 274 |
UINT256 R256; |
| 275 |
int is_midpoint_lt_even = 0; |
| 276 |
int is_midpoint_gt_even = 0; |
| 277 |
int is_inexact_lt_midpoint = 0; |
| 278 |
int is_inexact_gt_midpoint = 0; |
| 279 |
int is_midpoint_lt_even0 = 0; |
| 280 |
int is_midpoint_gt_even0 = 0; |
| 281 |
int is_inexact_lt_midpoint0 = 0; |
| 282 |
int is_inexact_gt_midpoint0 = 0; |
| 283 |
int incr_exp = 0; |
| 284 |
int is_tiny = 0; |
| 285 |
int lt_half_ulp = 0; |
| 286 |
int eq_half_ulp = 0; |
| 287 |
// int gt_half_ulp = 0; |
| 288 |
UINT128 res = *ptrres; |
| 289 |
|
| 290 |
// scale C3 up by 10^(q4-delta-q3), 0 <= q4-delta-q3 <= 2*P34-2 = 66 |
| 291 |
scale = q4 - delta - q3; // 0 <= scale <= 66 (or 0 <= scale <= 68 if this |
| 292 |
// comes from Cases (2), (3), (4), (5), (6), with 0 <= |delta| <= 1 |
| 293 |
|
| 294 |
// calculate C3 * 10^scale in R256 (it has at most 67 decimal digits for |
| 295 |
// Cases (15),(16),(17) and at most 69 for Cases (2),(3),(4),(5),(6)) |
| 296 |
if (scale == 0) { |
| 297 |
R256.w[3] = 0x0ull; |
| 298 |
R256.w[2] = 0x0ull; |
| 299 |
R256.w[1] = C3.w[1]; |
| 300 |
R256.w[0] = C3.w[0]; |
| 301 |
} else if (scale <= 19) { // 10^scale fits in 64 bits |
| 302 |
P128.w[1] = 0; |
| 303 |
P128.w[0] = ten2k64[scale]; |
| 304 |
__mul_128x128_to_256 (R256, P128, C3); |
| 305 |
} else if (scale <= 38) { // 10^scale fits in 128 bits |
| 306 |
__mul_128x128_to_256 (R256, ten2k128[scale - 20], C3); |
| 307 |
} else if (scale <= 57) { // 39 <= scale <= 57 |
| 308 |
// 10^scale fits in 192 bits but C3 * 10^scale fits in 223 or 230 bits |
| 309 |
// (10^67 has 223 bits; 10^69 has 230 bits); |
| 310 |
// must split the computation: |
| 311 |
// 10^scale * C3 = 10*38 * 10^(scale-38) * C3 where 10^38 takes 127 |
| 312 |
// bits and so 10^(scale-38) * C3 fits in 128 bits with certainty |
| 313 |
// Note that 1 <= scale - 38 <= 19 => 10^(scale-38) fits in 64 bits |
| 314 |
__mul_64x128_to_128 (R128, ten2k64[scale - 38], C3); |
| 315 |
// now multiply R128 by 10^38 |
| 316 |
__mul_128x128_to_256 (R256, R128, ten2k128[18]); |
| 317 |
} else { // 58 <= scale <= 66 |
| 318 |
// 10^scale takes between 193 and 220 bits, |
| 319 |
// and C3 * 10^scale fits in 223 bits (10^67/10^69 has 223/230 bits) |
| 320 |
// must split the computation: |
| 321 |
// 10^scale * C3 = 10*38 * 10^(scale-38) * C3 where 10^38 takes 127 |
| 322 |
// bits and so 10^(scale-38) * C3 fits in 128 bits with certainty |
| 323 |
// Note that 20 <= scale - 38 <= 30 => 10^(scale-38) fits in 128 bits |
| 324 |
// Calculate first 10^(scale-38) * C3, which fits in 128 bits; because |
| 325 |
// 10^(scale-38) takes more than 64 bits, C3 will take less than 64 |
| 326 |
__mul_64x128_to_128 (R128, C3.w[0], ten2k128[scale - 58]); |
| 327 |
// now calculate 10*38 * 10^(scale-38) * C3 |
| 328 |
__mul_128x128_to_256 (R256, R128, ten2k128[18]); |
| 329 |
} |
| 330 |
// C3 * 10^scale is now in R256 |
| 331 |
|
| 332 |
// for Cases (15), (16), (17) C4 > C3 * 10^scale because C4 has at least |
| 333 |
// one extra digit; for Cases (2), (3), (4), (5), or (6) any order is |
| 334 |
// possible |
| 335 |
// add/subtract C4 and C3 * 10^scale; the exponent is e4 |
| 336 |
if (p_sign == z_sign) { // R256 = C4 + R256 |
| 337 |
// calculate R256 = C4 + C3 * 10^scale = C4 + R256 which is exact, |
| 338 |
// but may require rounding |
| 339 |
add256 (C4, R256, &R256); |
| 340 |
} else { // if (p_sign != z_sign) { // R256 = C4 - R256 |
| 341 |
// calculate R256 = C4 - C3 * 10^scale = C4 - R256 or |
| 342 |
// R256 = C3 * 10^scale - C4 = R256 - C4 which is exact, |
| 343 |
// but may require rounding |
| 344 |
|
| 345 |
// compare first R256 = C3 * 10^scale and C4 |
| 346 |
if (R256.w[3] > C4.w[3] || (R256.w[3] == C4.w[3] && R256.w[2] > C4.w[2]) || |
| 347 |
(R256.w[3] == C4.w[3] && R256.w[2] == C4.w[2] && R256.w[1] > C4.w[1]) || |
| 348 |
(R256.w[3] == C4.w[3] && R256.w[2] == C4.w[2] && R256.w[1] == C4.w[1] && |
| 349 |
R256.w[0] >= C4.w[0])) { // C3 * 10^scale >= C4 |
| 350 |
// calculate R256 = C3 * 10^scale - C4 = R256 - C4, which is exact, |
| 351 |
// but may require rounding |
| 352 |
sub256 (R256, C4, &R256); |
| 353 |
// flip p_sign too, because the result has the sign of z |
| 354 |
p_sign = z_sign; |
| 355 |
} else { // if C4 > C3 * 10^scale |
| 356 |
// calculate R256 = C4 - C3 * 10^scale = C4 - R256, which is exact, |
| 357 |
// but may require rounding |
| 358 |
sub256 (C4, R256, &R256); |
| 359 |
} |
| 360 |
// if the result is pure zero, the sign depends on the rounding mode |
| 361 |
// (x*y and z had opposite signs) |
| 362 |
if (R256.w[3] == 0x0ull && R256.w[2] == 0x0ull && |
| 363 |
R256.w[1] == 0x0ull && R256.w[0] == 0x0ull) { |
| 364 |
if (rnd_mode != ROUNDING_DOWN) |
| 365 |
p_sign = 0x0000000000000000ull; |
| 366 |
else |
| 367 |
p_sign = 0x8000000000000000ull; |
| 368 |
// the exponent is max (e4, expmin) |
| 369 |
if (e4 < -6176) |
| 370 |
e4 = expmin; |
| 371 |
// assemble result |
| 372 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49); |
| 373 |
res.w[0] = 0x0; |
| 374 |
*ptrres = res; |
| 375 |
return; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
// determine the number of decimal digits in R256 |
| 380 |
ind = nr_digits256 (R256); |
| 381 |
|
| 382 |
// the exact result is (-1)^p_sign * R256 * 10^e4 where q (R256) = ind; |
| 383 |
// round to the destination precision, with unbounded exponent |
| 384 |
|
| 385 |
if (ind <= p34) { |
| 386 |
// result rounded to the destination precision with unbounded exponent |
| 387 |
// is exact |
| 388 |
if (ind + e4 < p34 + expmin) { |
| 389 |
is_tiny = 1; // applies to all rounding modes |
| 390 |
} |
| 391 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | R256.w[1]; |
| 392 |
res.w[0] = R256.w[0]; |
| 393 |
// Note: res is correct only if expmin <= e4 <= expmax |
| 394 |
} else { // if (ind > p34) |
| 395 |
// if more than P digits, round to nearest to P digits |
| 396 |
// round R256 to p34 digits |
| 397 |
x0 = ind - p34; // 1 <= x0 <= 34 as 35 <= ind <= 68 |
| 398 |
if (ind <= 38) { |
| 399 |
P128.w[1] = R256.w[1]; |
| 400 |
P128.w[0] = R256.w[0]; |
| 401 |
round128_19_38 (ind, x0, P128, &R128, &incr_exp, |
| 402 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 403 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 404 |
} else if (ind <= 57) { |
| 405 |
P192.w[2] = R256.w[2]; |
| 406 |
P192.w[1] = R256.w[1]; |
| 407 |
P192.w[0] = R256.w[0]; |
| 408 |
round192_39_57 (ind, x0, P192, &R192, &incr_exp, |
| 409 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 410 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 411 |
R128.w[1] = R192.w[1]; |
| 412 |
R128.w[0] = R192.w[0]; |
| 413 |
} else { // if (ind <= 68) |
| 414 |
round256_58_76 (ind, x0, R256, &R256, &incr_exp, |
| 415 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 416 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 417 |
R128.w[1] = R256.w[1]; |
| 418 |
R128.w[0] = R256.w[0]; |
| 419 |
} |
| 420 |
if (e4 + x0 < expmin) { // for all rounding modes |
| 421 |
is_tiny = 1; |
| 422 |
} |
| 423 |
// the rounded result has p34 = 34 digits |
| 424 |
e4 = e4 + x0 + incr_exp; |
| 425 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 426 |
// for RM, RP, RZ, RA apply correction in order to determine tininess |
| 427 |
// but do not save the result; apply the correction to |
| 428 |
// (-1)^p_sign * significand * 10^0 |
| 429 |
P128.w[1] = p_sign | 0x3040000000000000ull | R128.w[1]; |
| 430 |
P128.w[0] = R128.w[0]; |
| 431 |
rounding_correction (rnd_mode, |
| 432 |
is_inexact_lt_midpoint, |
| 433 |
is_inexact_gt_midpoint, is_midpoint_lt_even, |
| 434 |
is_midpoint_gt_even, 0, &P128, ptrfpsf); |
| 435 |
scale = ((P128.w[1] & MASK_EXP) >> 49) - 6176; // -1, 0, or +1 |
| 436 |
} |
| 437 |
ind = p34; // the number of decimal digits in the signifcand of res |
| 438 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | R128.w[1]; // RN |
| 439 |
res.w[0] = R128.w[0]; |
| 440 |
// Note: res is correct only if expmin <= e4 <= expmax |
| 441 |
// set the inexact flag after rounding with bounded exponent, if any |
| 442 |
} |
| 443 |
// at this point we have the result rounded with unbounded exponent in |
| 444 |
// res and we know its tininess: |
| 445 |
// res = (-1)^p_sign * significand * 10^e4, |
| 446 |
// where q (significand) = ind <= p34 |
| 447 |
// Note: res is correct only if expmin <= e4 <= expmax |
| 448 |
|
| 449 |
// check for overflow if RN |
| 450 |
if (rnd_mode == ROUNDING_TO_NEAREST && (ind + e4) > (p34 + expmax)) { |
| 451 |
res.w[1] = p_sign | 0x7800000000000000ull; |
| 452 |
res.w[0] = 0x0000000000000000ull; |
| 453 |
*ptrres = res; |
| 454 |
*ptrfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 455 |
return; // BID_RETURN (res) |
| 456 |
} // else not overflow or not RN, so continue |
| 457 |
|
| 458 |
// if (e4 >= expmin) we have the result rounded with bounded exponent |
| 459 |
if (e4 < expmin) { |
| 460 |
x0 = expmin - e4; // x0 >= 1; the number of digits to chop off of res |
| 461 |
// where the result rounded [at most] once is |
| 462 |
// (-1)^p_sign * significand_res * 10^e4 |
| 463 |
|
| 464 |
// avoid double rounding error |
| 465 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 466 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 467 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 468 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 469 |
is_inexact_lt_midpoint = 0; |
| 470 |
is_inexact_gt_midpoint = 0; |
| 471 |
is_midpoint_lt_even = 0; |
| 472 |
is_midpoint_gt_even = 0; |
| 473 |
|
| 474 |
if (x0 > ind) { |
| 475 |
// nothing is left of res when moving the decimal point left x0 digits |
| 476 |
is_inexact_lt_midpoint = 1; |
| 477 |
res.w[1] = p_sign | 0x0000000000000000ull; |
| 478 |
res.w[0] = 0x0000000000000000ull; |
| 479 |
e4 = expmin; |
| 480 |
} else if (x0 == ind) { // 1 <= x0 = ind <= p34 = 34 |
| 481 |
// this is <, =, or > 1/2 ulp |
| 482 |
// compare the ind-digit value in the significand of res with |
| 483 |
// 1/2 ulp = 5*10^(ind-1), i.e. determine whether it is |
| 484 |
// less than, equal to, or greater than 1/2 ulp (significand of res) |
| 485 |
R128.w[1] = res.w[1] & MASK_COEFF; |
| 486 |
R128.w[0] = res.w[0]; |
| 487 |
if (ind <= 19) { |
| 488 |
if (R128.w[0] < midpoint64[ind - 1]) { // < 1/2 ulp |
| 489 |
lt_half_ulp = 1; |
| 490 |
is_inexact_lt_midpoint = 1; |
| 491 |
} else if (R128.w[0] == midpoint64[ind - 1]) { // = 1/2 ulp |
| 492 |
eq_half_ulp = 1; |
| 493 |
is_midpoint_gt_even = 1; |
| 494 |
} else { // > 1/2 ulp |
| 495 |
// gt_half_ulp = 1; |
| 496 |
is_inexact_gt_midpoint = 1; |
| 497 |
} |
| 498 |
} else { // if (ind <= 38) { |
| 499 |
if (R128.w[1] < midpoint128[ind - 20].w[1] || |
| 500 |
(R128.w[1] == midpoint128[ind - 20].w[1] && |
| 501 |
R128.w[0] < midpoint128[ind - 20].w[0])) { // < 1/2 ulp |
| 502 |
lt_half_ulp = 1; |
| 503 |
is_inexact_lt_midpoint = 1; |
| 504 |
} else if (R128.w[1] == midpoint128[ind - 20].w[1] && |
| 505 |
R128.w[0] == midpoint128[ind - 20].w[0]) { // = 1/2 ulp |
| 506 |
eq_half_ulp = 1; |
| 507 |
is_midpoint_gt_even = 1; |
| 508 |
} else { // > 1/2 ulp |
| 509 |
// gt_half_ulp = 1; |
| 510 |
is_inexact_gt_midpoint = 1; |
| 511 |
} |
| 512 |
} |
| 513 |
if (lt_half_ulp || eq_half_ulp) { |
| 514 |
// res = +0.0 * 10^expmin |
| 515 |
res.w[1] = 0x0000000000000000ull; |
| 516 |
res.w[0] = 0x0000000000000000ull; |
| 517 |
} else { // if (gt_half_ulp) |
| 518 |
// res = +1 * 10^expmin |
| 519 |
res.w[1] = 0x0000000000000000ull; |
| 520 |
res.w[0] = 0x0000000000000001ull; |
| 521 |
} |
| 522 |
res.w[1] = p_sign | res.w[1]; |
| 523 |
e4 = expmin; |
| 524 |
} else { // if (1 <= x0 <= ind - 1 <= 33) |
| 525 |
// round the ind-digit result to ind - x0 digits |
| 526 |
|
| 527 |
if (ind <= 18) { // 2 <= ind <= 18 |
| 528 |
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp, |
| 529 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 530 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 531 |
res.w[1] = 0x0; |
| 532 |
res.w[0] = R64; |
| 533 |
} else if (ind <= 38) { |
| 534 |
P128.w[1] = res.w[1] & MASK_COEFF; |
| 535 |
P128.w[0] = res.w[0]; |
| 536 |
round128_19_38 (ind, x0, P128, &res, &incr_exp, |
| 537 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 538 |
&is_inexact_lt_midpoint, |
| 539 |
&is_inexact_gt_midpoint); |
| 540 |
} |
| 541 |
e4 = e4 + x0; // expmin |
| 542 |
// we want the exponent to be expmin, so if incr_exp = 1 then |
| 543 |
// multiply the rounded result by 10 - it will still fit in 113 bits |
| 544 |
if (incr_exp) { |
| 545 |
// 64 x 128 -> 128 |
| 546 |
P128.w[1] = res.w[1] & MASK_COEFF; |
| 547 |
P128.w[0] = res.w[0]; |
| 548 |
__mul_64x128_to_128 (res, ten2k64[1], P128); |
| 549 |
} |
| 550 |
res.w[1] = |
| 551 |
p_sign | ((UINT64) (e4 + 6176) << 49) | (res.w[1] & MASK_COEFF); |
| 552 |
// avoid a double rounding error |
| 553 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 554 |
is_midpoint_lt_even) { // double rounding error upward |
| 555 |
// res = res - 1 |
| 556 |
res.w[0]--; |
| 557 |
if (res.w[0] == 0xffffffffffffffffull) |
| 558 |
res.w[1]--; |
| 559 |
// Note: a double rounding error upward is not possible; for this |
| 560 |
// the result after the first rounding would have to be 99...95 |
| 561 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 562 |
// is not possible in Cases (2)-(6) or (15)-(17) which may get here |
| 563 |
is_midpoint_lt_even = 0; |
| 564 |
is_inexact_lt_midpoint = 1; |
| 565 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 566 |
is_midpoint_gt_even) { // double rounding error downward |
| 567 |
// res = res + 1 |
| 568 |
res.w[0]++; |
| 569 |
if (res.w[0] == 0) |
| 570 |
res.w[1]++; |
| 571 |
is_midpoint_gt_even = 0; |
| 572 |
is_inexact_gt_midpoint = 1; |
| 573 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 574 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 575 |
// if this second rounding was exact the result may still be |
| 576 |
// inexact because of the first rounding |
| 577 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 578 |
is_inexact_gt_midpoint = 1; |
| 579 |
} |
| 580 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 581 |
is_inexact_lt_midpoint = 1; |
| 582 |
} |
| 583 |
} else if (is_midpoint_gt_even && |
| 584 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 585 |
// pulled up to a midpoint |
| 586 |
is_inexact_lt_midpoint = 1; |
| 587 |
is_inexact_gt_midpoint = 0; |
| 588 |
is_midpoint_lt_even = 0; |
| 589 |
is_midpoint_gt_even = 0; |
| 590 |
} else if (is_midpoint_lt_even && |
| 591 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 592 |
// pulled down to a midpoint |
| 593 |
is_inexact_lt_midpoint = 0; |
| 594 |
is_inexact_gt_midpoint = 1; |
| 595 |
is_midpoint_lt_even = 0; |
| 596 |
is_midpoint_gt_even = 0; |
| 597 |
} else { |
| 598 |
; |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
// res contains the correct result |
| 603 |
// apply correction if not rounding to nearest |
| 604 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 605 |
rounding_correction (rnd_mode, |
| 606 |
is_inexact_lt_midpoint, is_inexact_gt_midpoint, |
| 607 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 608 |
e4, &res, ptrfpsf); |
| 609 |
} |
| 610 |
if (is_midpoint_lt_even || is_midpoint_gt_even || |
| 611 |
is_inexact_lt_midpoint || is_inexact_gt_midpoint) { |
| 612 |
// set the inexact flag |
| 613 |
*ptrfpsf |= INEXACT_EXCEPTION; |
| 614 |
if (is_tiny) |
| 615 |
*ptrfpsf |= UNDERFLOW_EXCEPTION; |
| 616 |
} |
| 617 |
|
| 618 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 619 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 620 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 621 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 622 |
*ptrres = res; |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
#if DECIMAL_CALL_BY_REFERENCE |
| 628 |
static void |
| 629 |
bid128_ext_fma (int *ptr_is_midpoint_lt_even, |
| 630 |
int *ptr_is_midpoint_gt_even, |
| 631 |
int *ptr_is_inexact_lt_midpoint, |
| 632 |
int *ptr_is_inexact_gt_midpoint, UINT128 * pres, |
| 633 |
UINT128 * px, UINT128 * py, |
| 634 |
UINT128 * |
| 635 |
pz _RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 636 |
_EXC_INFO_PARAM) { |
| 637 |
UINT128 x = *px, y = *py, z = *pz; |
| 638 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 639 |
unsigned int rnd_mode = *prnd_mode; |
| 640 |
#endif |
| 641 |
#else |
| 642 |
static UINT128 |
| 643 |
bid128_ext_fma (int *ptr_is_midpoint_lt_even, |
| 644 |
int *ptr_is_midpoint_gt_even, |
| 645 |
int *ptr_is_inexact_lt_midpoint, |
| 646 |
int *ptr_is_inexact_gt_midpoint, UINT128 x, UINT128 y, |
| 647 |
UINT128 z _RND_MODE_PARAM _EXC_FLAGS_PARAM |
| 648 |
_EXC_MASKS_PARAM _EXC_INFO_PARAM) { |
| 649 |
#endif |
| 650 |
|
| 651 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 652 |
UINT64 x_sign, y_sign, z_sign, p_sign, tmp_sign; |
| 653 |
UINT64 x_exp = 0, y_exp = 0, z_exp = 0, p_exp; |
| 654 |
int true_p_exp; |
| 655 |
UINT128 C1, C2, C3; |
| 656 |
UINT256 C4; |
| 657 |
int q1 = 0, q2 = 0, q3 = 0, q4; |
| 658 |
int e1, e2, e3, e4; |
| 659 |
int scale, ind, delta, x0; |
| 660 |
int p34 = P34; // used to modify the limit on the number of digits |
| 661 |
BID_UI64DOUBLE tmp; |
| 662 |
int x_nr_bits, y_nr_bits, z_nr_bits; |
| 663 |
unsigned int save_fpsf; |
| 664 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0; |
| 665 |
int is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 666 |
int is_midpoint_lt_even0 = 0, is_midpoint_gt_even0 = 0; |
| 667 |
int is_inexact_lt_midpoint0 = 0, is_inexact_gt_midpoint0 = 0; |
| 668 |
int incr_exp = 0; |
| 669 |
int lsb; |
| 670 |
int lt_half_ulp = 0; |
| 671 |
int eq_half_ulp = 0; |
| 672 |
int gt_half_ulp = 0; |
| 673 |
int is_tiny = 0; |
| 674 |
UINT64 R64, tmp64; |
| 675 |
UINT128 P128, R128; |
| 676 |
UINT192 P192, R192; |
| 677 |
UINT256 R256; |
| 678 |
|
| 679 |
// the following are based on the table of special cases for fma; the NaN |
| 680 |
// behavior is similar to that of the IA-64 Architecture fma |
| 681 |
|
| 682 |
// identify cases where at least one operand is NaN |
| 683 |
|
| 684 |
BID_SWAP128 (x); |
| 685 |
BID_SWAP128 (y); |
| 686 |
BID_SWAP128 (z); |
| 687 |
if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN |
| 688 |
// if x = {0, f, inf, NaN}, y = NaN, z = {0, f, inf, NaN} then res = Q (y) |
| 689 |
// check first for non-canonical NaN payload |
| 690 |
if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) || |
| 691 |
(((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) && |
| 692 |
(y.w[0] > 0x38c15b09ffffffffull))) { |
| 693 |
y.w[1] = y.w[1] & 0xffffc00000000000ull; |
| 694 |
y.w[0] = 0x0ull; |
| 695 |
} |
| 696 |
if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN |
| 697 |
// set invalid flag |
| 698 |
*pfpsf |= INVALID_EXCEPTION; |
| 699 |
// return quiet (y) |
| 700 |
res.w[1] = y.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16] |
| 701 |
res.w[0] = y.w[0]; |
| 702 |
} else { // y is QNaN |
| 703 |
// return y |
| 704 |
res.w[1] = y.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16] |
| 705 |
res.w[0] = y.w[0]; |
| 706 |
// if z = SNaN or x = SNaN signal invalid exception |
| 707 |
if ((z.w[1] & MASK_SNAN) == MASK_SNAN || |
| 708 |
(x.w[1] & MASK_SNAN) == MASK_SNAN) { |
| 709 |
// set invalid flag |
| 710 |
*pfpsf |= INVALID_EXCEPTION; |
| 711 |
} |
| 712 |
} |
| 713 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 714 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 715 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 716 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 717 |
BID_SWAP128 (res); |
| 718 |
BID_RETURN (res) |
| 719 |
} else if ((z.w[1] & MASK_NAN) == MASK_NAN) { // z is NAN |
| 720 |
// if x = {0, f, inf, NaN}, y = {0, f, inf}, z = NaN then res = Q (z) |
| 721 |
// check first for non-canonical NaN payload |
| 722 |
if (((z.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) || |
| 723 |
(((z.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) && |
| 724 |
(z.w[0] > 0x38c15b09ffffffffull))) { |
| 725 |
z.w[1] = z.w[1] & 0xffffc00000000000ull; |
| 726 |
z.w[0] = 0x0ull; |
| 727 |
} |
| 728 |
if ((z.w[1] & MASK_SNAN) == MASK_SNAN) { // z is SNAN |
| 729 |
// set invalid flag |
| 730 |
*pfpsf |= INVALID_EXCEPTION; |
| 731 |
// return quiet (z) |
| 732 |
res.w[1] = z.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16] |
| 733 |
res.w[0] = z.w[0]; |
| 734 |
} else { // z is QNaN |
| 735 |
// return z |
| 736 |
res.w[1] = z.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16] |
| 737 |
res.w[0] = z.w[0]; |
| 738 |
// if x = SNaN signal invalid exception |
| 739 |
if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { |
| 740 |
// set invalid flag |
| 741 |
*pfpsf |= INVALID_EXCEPTION; |
| 742 |
} |
| 743 |
} |
| 744 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 745 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 746 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 747 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 748 |
BID_SWAP128 (res); |
| 749 |
BID_RETURN (res) |
| 750 |
} else if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN |
| 751 |
// if x = NaN, y = {0, f, inf}, z = {0, f, inf} then res = Q (x) |
| 752 |
// check first for non-canonical NaN payload |
| 753 |
if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) || |
| 754 |
(((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) && |
| 755 |
(x.w[0] > 0x38c15b09ffffffffull))) { |
| 756 |
x.w[1] = x.w[1] & 0xffffc00000000000ull; |
| 757 |
x.w[0] = 0x0ull; |
| 758 |
} |
| 759 |
if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN |
| 760 |
// set invalid flag |
| 761 |
*pfpsf |= INVALID_EXCEPTION; |
| 762 |
// return quiet (x) |
| 763 |
res.w[1] = x.w[1] & 0xfc003fffffffffffull; // clear out also G[6]-G[16] |
| 764 |
res.w[0] = x.w[0]; |
| 765 |
} else { // x is QNaN |
| 766 |
// return x |
| 767 |
res.w[1] = x.w[1] & 0xfc003fffffffffffull; // clear out G[6]-G[16] |
| 768 |
res.w[0] = x.w[0]; |
| 769 |
} |
| 770 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 771 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 772 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 773 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 774 |
BID_SWAP128 (res); |
| 775 |
BID_RETURN (res) |
| 776 |
} |
| 777 |
// x, y, z are 0, f, or inf but not NaN => unpack the arguments and check |
| 778 |
// for non-canonical values |
| 779 |
|
| 780 |
x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative |
| 781 |
C1.w[1] = x.w[1] & MASK_COEFF; |
| 782 |
C1.w[0] = x.w[0]; |
| 783 |
if ((x.w[1] & MASK_ANY_INF) != MASK_INF) { // x != inf |
| 784 |
// if x is not infinity check for non-canonical values - treated as zero |
| 785 |
if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11 |
| 786 |
// non-canonical |
| 787 |
x_exp = (x.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits |
| 788 |
C1.w[1] = 0; // significand high |
| 789 |
C1.w[0] = 0; // significand low |
| 790 |
} else { // G0_G1 != 11 |
| 791 |
x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bits |
| 792 |
if (C1.w[1] > 0x0001ed09bead87c0ull || |
| 793 |
(C1.w[1] == 0x0001ed09bead87c0ull && |
| 794 |
C1.w[0] > 0x378d8e63ffffffffull)) { |
| 795 |
// x is non-canonical if coefficient is larger than 10^34 -1 |
| 796 |
C1.w[1] = 0; |
| 797 |
C1.w[0] = 0; |
| 798 |
} else { // canonical |
| 799 |
; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
y_sign = y.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative |
| 804 |
C2.w[1] = y.w[1] & MASK_COEFF; |
| 805 |
C2.w[0] = y.w[0]; |
| 806 |
if ((y.w[1] & MASK_ANY_INF) != MASK_INF) { // y != inf |
| 807 |
// if y is not infinity check for non-canonical values - treated as zero |
| 808 |
if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11 |
| 809 |
// non-canonical |
| 810 |
y_exp = (y.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits |
| 811 |
C2.w[1] = 0; // significand high |
| 812 |
C2.w[0] = 0; // significand low |
| 813 |
} else { // G0_G1 != 11 |
| 814 |
y_exp = y.w[1] & MASK_EXP; // biased and shifted left 49 bits |
| 815 |
if (C2.w[1] > 0x0001ed09bead87c0ull || |
| 816 |
(C2.w[1] == 0x0001ed09bead87c0ull && |
| 817 |
C2.w[0] > 0x378d8e63ffffffffull)) { |
| 818 |
// y is non-canonical if coefficient is larger than 10^34 -1 |
| 819 |
C2.w[1] = 0; |
| 820 |
C2.w[0] = 0; |
| 821 |
} else { // canonical |
| 822 |
; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
z_sign = z.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative |
| 827 |
C3.w[1] = z.w[1] & MASK_COEFF; |
| 828 |
C3.w[0] = z.w[0]; |
| 829 |
if ((z.w[1] & MASK_ANY_INF) != MASK_INF) { // z != inf |
| 830 |
// if z is not infinity check for non-canonical values - treated as zero |
| 831 |
if ((z.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) { // G0_G1=11 |
| 832 |
// non-canonical |
| 833 |
z_exp = (z.w[1] << 2) & MASK_EXP; // biased and shifted left 49 bits |
| 834 |
C3.w[1] = 0; // significand high |
| 835 |
C3.w[0] = 0; // significand low |
| 836 |
} else { // G0_G1 != 11 |
| 837 |
z_exp = z.w[1] & MASK_EXP; // biased and shifted left 49 bits |
| 838 |
if (C3.w[1] > 0x0001ed09bead87c0ull || |
| 839 |
(C3.w[1] == 0x0001ed09bead87c0ull && |
| 840 |
C3.w[0] > 0x378d8e63ffffffffull)) { |
| 841 |
// z is non-canonical if coefficient is larger than 10^34 -1 |
| 842 |
C3.w[1] = 0; |
| 843 |
C3.w[0] = 0; |
| 844 |
} else { // canonical |
| 845 |
; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
p_sign = x_sign ^ y_sign; // sign of the product |
| 850 |
|
| 851 |
// identify cases where at least one operand is infinity |
| 852 |
|
| 853 |
if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf |
| 854 |
if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf |
| 855 |
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf |
| 856 |
if (p_sign == z_sign) { |
| 857 |
res.w[1] = z_sign | MASK_INF; |
| 858 |
res.w[0] = 0x0; |
| 859 |
} else { |
| 860 |
// return QNaN Indefinite |
| 861 |
res.w[1] = 0x7c00000000000000ull; |
| 862 |
res.w[0] = 0x0000000000000000ull; |
| 863 |
// set invalid flag |
| 864 |
*pfpsf |= INVALID_EXCEPTION; |
| 865 |
} |
| 866 |
} else { // z = 0 or z = f |
| 867 |
res.w[1] = p_sign | MASK_INF; |
| 868 |
res.w[0] = 0x0; |
| 869 |
} |
| 870 |
} else if (C2.w[1] != 0 || C2.w[0] != 0) { // y = f |
| 871 |
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf |
| 872 |
if (p_sign == z_sign) { |
| 873 |
res.w[1] = z_sign | MASK_INF; |
| 874 |
res.w[0] = 0x0; |
| 875 |
} else { |
| 876 |
// return QNaN Indefinite |
| 877 |
res.w[1] = 0x7c00000000000000ull; |
| 878 |
res.w[0] = 0x0000000000000000ull; |
| 879 |
// set invalid flag |
| 880 |
*pfpsf |= INVALID_EXCEPTION; |
| 881 |
} |
| 882 |
} else { // z = 0 or z = f |
| 883 |
res.w[1] = p_sign | MASK_INF; |
| 884 |
res.w[0] = 0x0; |
| 885 |
} |
| 886 |
} else { // y = 0 |
| 887 |
// return QNaN Indefinite |
| 888 |
res.w[1] = 0x7c00000000000000ull; |
| 889 |
res.w[0] = 0x0000000000000000ull; |
| 890 |
// set invalid flag |
| 891 |
*pfpsf |= INVALID_EXCEPTION; |
| 892 |
} |
| 893 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 894 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 895 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 896 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 897 |
BID_SWAP128 (res); |
| 898 |
BID_RETURN (res) |
| 899 |
} else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf |
| 900 |
if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf |
| 901 |
// x = f, necessarily |
| 902 |
if ((p_sign != z_sign) |
| 903 |
|| (C1.w[1] == 0x0ull && C1.w[0] == 0x0ull)) { |
| 904 |
// return QNaN Indefinite |
| 905 |
res.w[1] = 0x7c00000000000000ull; |
| 906 |
res.w[0] = 0x0000000000000000ull; |
| 907 |
// set invalid flag |
| 908 |
*pfpsf |= INVALID_EXCEPTION; |
| 909 |
} else { |
| 910 |
res.w[1] = z_sign | MASK_INF; |
| 911 |
res.w[0] = 0x0; |
| 912 |
} |
| 913 |
} else if (C1.w[1] == 0x0 && C1.w[0] == 0x0) { // x = 0 |
| 914 |
// z = 0, f, inf |
| 915 |
// return QNaN Indefinite |
| 916 |
res.w[1] = 0x7c00000000000000ull; |
| 917 |
res.w[0] = 0x0000000000000000ull; |
| 918 |
// set invalid flag |
| 919 |
*pfpsf |= INVALID_EXCEPTION; |
| 920 |
} else { |
| 921 |
// x = f and z = 0, f, necessarily |
| 922 |
res.w[1] = p_sign | MASK_INF; |
| 923 |
res.w[0] = 0x0; |
| 924 |
} |
| 925 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 926 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 927 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 928 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 929 |
BID_SWAP128 (res); |
| 930 |
BID_RETURN (res) |
| 931 |
} else if ((z.w[1] & MASK_ANY_INF) == MASK_INF) { // z = inf |
| 932 |
// x = 0, f and y = 0, f, necessarily |
| 933 |
res.w[1] = z_sign | MASK_INF; |
| 934 |
res.w[0] = 0x0; |
| 935 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 936 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 937 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 938 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 939 |
BID_SWAP128 (res); |
| 940 |
BID_RETURN (res) |
| 941 |
} |
| 942 |
|
| 943 |
true_p_exp = (x_exp >> 49) - 6176 + (y_exp >> 49) - 6176; |
| 944 |
if (true_p_exp < -6176) |
| 945 |
p_exp = 0; // cannot be less than EXP_MIN |
| 946 |
else |
| 947 |
p_exp = (UINT64) (true_p_exp + 6176) << 49; |
| 948 |
|
| 949 |
if (((C1.w[1] == 0x0 && C1.w[0] == 0x0) || (C2.w[1] == 0x0 && C2.w[0] == 0x0)) && C3.w[1] == 0x0 && C3.w[0] == 0x0) { // (x = 0 or y = 0) and z = 0 |
| 950 |
// the result is 0 |
| 951 |
if (p_exp < z_exp) |
| 952 |
res.w[1] = p_exp; // preferred exponent |
| 953 |
else |
| 954 |
res.w[1] = z_exp; // preferred exponent |
| 955 |
if (p_sign == z_sign) { |
| 956 |
res.w[1] |= z_sign; |
| 957 |
res.w[0] = 0x0; |
| 958 |
} else { // x * y and z have opposite signs |
| 959 |
if (rnd_mode == ROUNDING_DOWN) { |
| 960 |
// res = -0.0 |
| 961 |
res.w[1] |= MASK_SIGN; |
| 962 |
res.w[0] = 0x0; |
| 963 |
} else { |
| 964 |
// res = +0.0 |
| 965 |
// res.w[1] |= 0x0; |
| 966 |
res.w[0] = 0x0; |
| 967 |
} |
| 968 |
} |
| 969 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 970 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 971 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 972 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 973 |
BID_SWAP128 (res); |
| 974 |
BID_RETURN (res) |
| 975 |
} |
| 976 |
// from this point on, we may need to know the number of decimal digits |
| 977 |
// in the significands of x, y, z when x, y, z != 0 |
| 978 |
|
| 979 |
if (C1.w[1] != 0 || C1.w[0] != 0) { // x = f (non-zero finite) |
| 980 |
// q1 = nr. of decimal digits in x |
| 981 |
// determine first the nr. of bits in x |
| 982 |
if (C1.w[1] == 0) { |
| 983 |
if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53 |
| 984 |
// split the 64-bit value in two 32-bit halves to avoid rounding errors |
| 985 |
|
| 986 |
tmp.d = (double) (C1.w[0] >> 32); // exact conversion |
| 987 |
x_nr_bits = |
| 988 |
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 989 |
} else { // if x < 2^53 |
| 990 |
tmp.d = (double) C1.w[0]; // exact conversion |
| 991 |
x_nr_bits = |
| 992 |
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 993 |
} |
| 994 |
} else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1]) |
| 995 |
tmp.d = (double) C1.w[1]; // exact conversion |
| 996 |
x_nr_bits = |
| 997 |
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 998 |
} |
| 999 |
q1 = nr_digits[x_nr_bits - 1].digits; |
| 1000 |
if (q1 == 0) { |
| 1001 |
q1 = nr_digits[x_nr_bits - 1].digits1; |
| 1002 |
if (C1.w[1] > nr_digits[x_nr_bits - 1].threshold_hi || |
| 1003 |
(C1.w[1] == nr_digits[x_nr_bits - 1].threshold_hi && |
| 1004 |
C1.w[0] >= nr_digits[x_nr_bits - 1].threshold_lo)) |
| 1005 |
q1++; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
// q2 = nr. of decimal digits in y |
| 1009 |
// determine first the nr. of bits in y |
| 1010 |
if (C2.w[1] != 0 || C2.w[0] != 0) { // y = f (non-zero finite) |
| 1011 |
if (C2.w[1] == 0) { |
| 1012 |
if (C2.w[0] >= 0x0020000000000000ull) { // y >= 2^53 |
| 1013 |
// split the 64-bit value in two 32-bit halves to avoid rounding errors |
| 1014 |
tmp.d = (double) (C2.w[0] >> 32); // exact conversion |
| 1015 |
y_nr_bits = |
| 1016 |
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1017 |
} else { // if y < 2^53 |
| 1018 |
tmp.d = (double) C2.w[0]; // exact conversion |
| 1019 |
y_nr_bits = |
| 1020 |
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1021 |
} |
| 1022 |
} else { // C2.w[1] != 0 => nr. bits = 64 + nr_bits (C2.w[1]) |
| 1023 |
tmp.d = (double) C2.w[1]; // exact conversion |
| 1024 |
y_nr_bits = |
| 1025 |
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1026 |
} |
| 1027 |
q2 = nr_digits[y_nr_bits - 1].digits; |
| 1028 |
if (q2 == 0) { |
| 1029 |
q2 = nr_digits[y_nr_bits - 1].digits1; |
| 1030 |
if (C2.w[1] > nr_digits[y_nr_bits - 1].threshold_hi || |
| 1031 |
(C2.w[1] == nr_digits[y_nr_bits - 1].threshold_hi && |
| 1032 |
C2.w[0] >= nr_digits[y_nr_bits - 1].threshold_lo)) |
| 1033 |
q2++; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (C3.w[1] != 0 || C3.w[0] != 0) { // z = f (non-zero finite) |
| 1038 |
if (C3.w[1] == 0) { |
| 1039 |
if (C3.w[0] >= 0x0020000000000000ull) { // z >= 2^53 |
| 1040 |
// split the 64-bit value in two 32-bit halves to avoid rounding errors |
| 1041 |
tmp.d = (double) (C3.w[0] >> 32); // exact conversion |
| 1042 |
z_nr_bits = |
| 1043 |
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1044 |
} else { // if z < 2^53 |
| 1045 |
tmp.d = (double) C3.w[0]; // exact conversion |
| 1046 |
z_nr_bits = |
| 1047 |
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1048 |
} |
| 1049 |
} else { // C3.w[1] != 0 => nr. bits = 64 + nr_bits (C3.w[1]) |
| 1050 |
tmp.d = (double) C3.w[1]; // exact conversion |
| 1051 |
z_nr_bits = |
| 1052 |
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 1053 |
} |
| 1054 |
q3 = nr_digits[z_nr_bits - 1].digits; |
| 1055 |
if (q3 == 0) { |
| 1056 |
q3 = nr_digits[z_nr_bits - 1].digits1; |
| 1057 |
if (C3.w[1] > nr_digits[z_nr_bits - 1].threshold_hi || |
| 1058 |
(C3.w[1] == nr_digits[z_nr_bits - 1].threshold_hi && |
| 1059 |
C3.w[0] >= nr_digits[z_nr_bits - 1].threshold_lo)) |
| 1060 |
q3++; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
if ((C1.w[1] == 0x0 && C1.w[0] == 0x0) || |
| 1065 |
(C2.w[1] == 0x0 && C2.w[0] == 0x0)) { |
| 1066 |
// x = 0 or y = 0 |
| 1067 |
// z = f, necessarily; for 0 + z return z, with the preferred exponent |
| 1068 |
// the result is z, but need to get the preferred exponent |
| 1069 |
if (z_exp <= p_exp) { // the preferred exponent is z_exp |
| 1070 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | C3.w[1]; |
| 1071 |
res.w[0] = C3.w[0]; |
| 1072 |
} else { // if (p_exp < z_exp) the preferred exponent is p_exp |
| 1073 |
// return (C3 * 10^scale) * 10^(z_exp - scale) |
| 1074 |
// where scale = min (p34-q3, (z_exp-p_exp) >> 49) |
| 1075 |
scale = p34 - q3; |
| 1076 |
ind = (z_exp - p_exp) >> 49; |
| 1077 |
if (ind < scale) |
| 1078 |
scale = ind; |
| 1079 |
if (scale == 0) { |
| 1080 |
res.w[1] = z.w[1]; // & MASK_COEFF, which is redundant |
| 1081 |
res.w[0] = z.w[0]; |
| 1082 |
} else if (q3 <= 19) { // z fits in 64 bits |
| 1083 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1084 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 1085 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 1086 |
} else { // 10^scale fits in 128 bits |
| 1087 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 1088 |
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]); |
| 1089 |
} |
| 1090 |
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits |
| 1091 |
// 64 x 128 ten2k64[scale] * C3 |
| 1092 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 1093 |
} |
| 1094 |
// subtract scale from the exponent |
| 1095 |
z_exp = z_exp - ((UINT64) scale << 49); |
| 1096 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 1097 |
} |
| 1098 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1099 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1100 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1101 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1102 |
BID_SWAP128 (res); |
| 1103 |
BID_RETURN (res) |
| 1104 |
} else { |
| 1105 |
; // continue with x = f, y = f, z = 0 or x = f, y = f, z = f |
| 1106 |
} |
| 1107 |
e1 = (x_exp >> 49) - 6176; // unbiased exponent of x |
| 1108 |
e2 = (y_exp >> 49) - 6176; // unbiased exponent of y |
| 1109 |
e3 = (z_exp >> 49) - 6176; // unbiased exponent of z |
| 1110 |
e4 = e1 + e2; // unbiased exponent of the exact x * y |
| 1111 |
|
| 1112 |
// calculate C1 * C2 and its number of decimal digits, q4 |
| 1113 |
|
| 1114 |
// the exact product has either q1 + q2 - 1 or q1 + q2 decimal digits |
| 1115 |
// where 2 <= q1 + q2 <= 68 |
| 1116 |
// calculate C4 = C1 * C2 and determine q |
| 1117 |
C4.w[3] = C4.w[2] = C4.w[1] = C4.w[0] = 0; |
| 1118 |
if (q1 + q2 <= 19) { // if 2 <= q1 + q2 <= 19, C4 = C1 * C2 fits in 64 bits |
| 1119 |
C4.w[0] = C1.w[0] * C2.w[0]; |
| 1120 |
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2 |
| 1121 |
if (C4.w[0] < ten2k64[q1 + q2 - 1]) |
| 1122 |
q4 = q1 + q2 - 1; // q4 in [1, 18] |
| 1123 |
else |
| 1124 |
q4 = q1 + q2; // q4 in [2, 19] |
| 1125 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64; |
| 1126 |
} else if (q1 + q2 == 20) { // C4 = C1 * C2 fits in 64 or 128 bits |
| 1127 |
// q1 <= 19 and q2 <= 19 so both C1 and C2 fit in 64 bits |
| 1128 |
__mul_64x64_to_128MACH (C4, C1.w[0], C2.w[0]); |
| 1129 |
// if C4 < 10^(q1+q2-1) = 10^19 then q4 = q1+q2-1 = 19 else q4 = q1+q2 = 20 |
| 1130 |
if (C4.w[1] == 0 && C4.w[0] < ten2k64[19]) { // 19 = q1+q2-1 |
| 1131 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64; |
| 1132 |
q4 = 19; // 19 = q1 + q2 - 1 |
| 1133 |
} else { |
| 1134 |
// if (C4.w[1] == 0) |
| 1135 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64; |
| 1136 |
// else |
| 1137 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1138 |
q4 = 20; // 20 = q1 + q2 |
| 1139 |
} |
| 1140 |
} else if (q1 + q2 <= 38) { // 21 <= q1 + q2 <= 38 |
| 1141 |
// C4 = C1 * C2 fits in 64 or 128 bits |
| 1142 |
// (64 bits possibly, but only when q1 + q2 = 21 and C4 has 20 digits) |
| 1143 |
// at least one of C1, C2 has at most 19 decimal digits & fits in 64 bits |
| 1144 |
if (q1 <= 19) { |
| 1145 |
__mul_128x64_to_128 (C4, C1.w[0], C2); |
| 1146 |
} else { // q2 <= 19 |
| 1147 |
__mul_128x64_to_128 (C4, C2.w[0], C1); |
| 1148 |
} |
| 1149 |
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2 |
| 1150 |
if (C4.w[1] < ten2k128[q1 + q2 - 21].w[1] || |
| 1151 |
(C4.w[1] == ten2k128[q1 + q2 - 21].w[1] && |
| 1152 |
C4.w[0] < ten2k128[q1 + q2 - 21].w[0])) { |
| 1153 |
// if (C4.w[1] == 0) // q4 = 20, necessarily |
| 1154 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 64; |
| 1155 |
// else |
| 1156 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1157 |
q4 = q1 + q2 - 1; // q4 in [20, 37] |
| 1158 |
} else { |
| 1159 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1160 |
q4 = q1 + q2; // q4 in [21, 38] |
| 1161 |
} |
| 1162 |
} else if (q1 + q2 == 39) { // C4 = C1 * C2 fits in 128 or 192 bits |
| 1163 |
// both C1 and C2 fit in 128 bits (actually in 113 bits) |
| 1164 |
// may replace this by 128x128_to192 |
| 1165 |
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] is 0 |
| 1166 |
// if C4 < 10^(q1+q2-1) = 10^38 then q4 = q1+q2-1 = 38 else q4 = q1+q2 = 39 |
| 1167 |
if (C4.w[2] == 0 && (C4.w[1] < ten2k128[18].w[1] || |
| 1168 |
(C4.w[1] == ten2k128[18].w[1] |
| 1169 |
&& C4.w[0] < ten2k128[18].w[0]))) { |
| 1170 |
// 18 = 38 - 20 = q1+q2-1 - 20 |
| 1171 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1172 |
q4 = 38; // 38 = q1 + q2 - 1 |
| 1173 |
} else { |
| 1174 |
// if (C4.w[2] == 0) |
| 1175 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1176 |
// else |
| 1177 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1178 |
q4 = 39; // 39 = q1 + q2 |
| 1179 |
} |
| 1180 |
} else if (q1 + q2 <= 57) { // 40 <= q1 + q2 <= 57 |
| 1181 |
// C4 = C1 * C2 fits in 128 or 192 bits |
| 1182 |
// (128 bits possibly, but only when q1 + q2 = 40 and C4 has 39 digits) |
| 1183 |
// both C1 and C2 fit in 128 bits (actually in 113 bits); at most one |
| 1184 |
// may fit in 64 bits |
| 1185 |
if (C1.w[1] == 0) { // C1 fits in 64 bits |
| 1186 |
// __mul_64x128_full (REShi64, RESlo128, A64, B128) |
| 1187 |
__mul_64x128_full (C4.w[2], C4, C1.w[0], C2); |
| 1188 |
} else if (C2.w[1] == 0) { // C2 fits in 64 bits |
| 1189 |
// __mul_64x128_full (REShi64, RESlo128, A64, B128) |
| 1190 |
__mul_64x128_full (C4.w[2], C4, C2.w[0], C1); |
| 1191 |
} else { // both C1 and C2 require 128 bits |
| 1192 |
// may use __mul_128x128_to_192 (C4.w[2], C4.w[0], C2.w[0], C1); |
| 1193 |
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] = 0 |
| 1194 |
} |
| 1195 |
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2 |
| 1196 |
if (C4.w[2] < ten2k256[q1 + q2 - 40].w[2] || |
| 1197 |
(C4.w[2] == ten2k256[q1 + q2 - 40].w[2] && |
| 1198 |
(C4.w[1] < ten2k256[q1 + q2 - 40].w[1] || |
| 1199 |
(C4.w[1] == ten2k256[q1 + q2 - 40].w[1] && |
| 1200 |
C4.w[0] < ten2k256[q1 + q2 - 40].w[0])))) { |
| 1201 |
// if (C4.w[2] == 0) // q4 = 39, necessarily |
| 1202 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 128; |
| 1203 |
// else |
| 1204 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1205 |
q4 = q1 + q2 - 1; // q4 in [39, 56] |
| 1206 |
} else { |
| 1207 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1208 |
q4 = q1 + q2; // q4 in [40, 57] |
| 1209 |
} |
| 1210 |
} else if (q1 + q2 == 58) { // C4 = C1 * C2 fits in 192 or 256 bits; |
| 1211 |
// both C1 and C2 fit in 128 bits (actually in 113 bits); none can |
| 1212 |
// fit in 64 bits, because each number must have at least 24 decimal |
| 1213 |
// digits for the sum to have 58 (as the max. nr. of digits is 34) => |
| 1214 |
// C1.w[1] != 0 and C2.w[1] != 0 |
| 1215 |
__mul_128x128_to_256 (C4, C1, C2); |
| 1216 |
// if C4 < 10^(q1+q2-1) = 10^57 then q4 = q1+q2-1 = 57 else q4 = q1+q2 = 58 |
| 1217 |
if (C4.w[3] == 0 && (C4.w[2] < ten2k256[18].w[2] || |
| 1218 |
(C4.w[2] == ten2k256[18].w[2] |
| 1219 |
&& (C4.w[1] < ten2k256[18].w[1] |
| 1220 |
|| (C4.w[1] == ten2k256[18].w[1] |
| 1221 |
&& C4.w[0] < ten2k256[18].w[0]))))) { |
| 1222 |
// 18 = 57 - 39 = q1+q2-1 - 39 |
| 1223 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1224 |
q4 = 57; // 57 = q1 + q2 - 1 |
| 1225 |
} else { |
| 1226 |
// if (C4.w[3] == 0) |
| 1227 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1228 |
// else |
| 1229 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256; |
| 1230 |
q4 = 58; // 58 = q1 + q2 |
| 1231 |
} |
| 1232 |
} else { // if 59 <= q1 + q2 <= 68 |
| 1233 |
// C4 = C1 * C2 fits in 192 or 256 bits |
| 1234 |
// (192 bits possibly, but only when q1 + q2 = 59 and C4 has 58 digits) |
| 1235 |
// both C1 and C2 fit in 128 bits (actually in 113 bits); none fits in |
| 1236 |
// 64 bits |
| 1237 |
// may use __mul_128x128_to_192 (C4.w[2], C4.w[0], C2.w[0], C1); |
| 1238 |
__mul_128x128_to_256 (C4, C1, C2); // C4.w[3] = 0 |
| 1239 |
// if C4 < 10^(q1+q2-1) then q4 = q1 + q2 - 1 else q4 = q1 + q2 |
| 1240 |
if (C4.w[3] < ten2k256[q1 + q2 - 40].w[3] || |
| 1241 |
(C4.w[3] == ten2k256[q1 + q2 - 40].w[3] && |
| 1242 |
(C4.w[2] < ten2k256[q1 + q2 - 40].w[2] || |
| 1243 |
(C4.w[2] == ten2k256[q1 + q2 - 40].w[2] && |
| 1244 |
(C4.w[1] < ten2k256[q1 + q2 - 40].w[1] || |
| 1245 |
(C4.w[1] == ten2k256[q1 + q2 - 40].w[1] && |
| 1246 |
C4.w[0] < ten2k256[q1 + q2 - 40].w[0])))))) { |
| 1247 |
// if (C4.w[3] == 0) // q4 = 58, necessarily |
| 1248 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 192; |
| 1249 |
// else |
| 1250 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256; |
| 1251 |
q4 = q1 + q2 - 1; // q4 in [58, 67] |
| 1252 |
} else { |
| 1253 |
// length of C1 * C2 rounded up to a multiple of 64 bits is len = 256; |
| 1254 |
q4 = q1 + q2; // q4 in [59, 68] |
| 1255 |
} |
| 1256 |
} |
| 1257 |
if (C3.w[1] == 0x0 && C3.w[0] == 0x0) { // x = f, y = f, z = 0 |
| 1258 |
save_fpsf = *pfpsf; // sticky bits - caller value must be preserved |
| 1259 |
*pfpsf = 0; |
| 1260 |
|
| 1261 |
if (q4 > p34) { |
| 1262 |
|
| 1263 |
// truncate C4 to p34 digits into res |
| 1264 |
// x = q4-p34, 1 <= x <= 34 because 35 <= q4 <= 68 |
| 1265 |
x0 = q4 - p34; |
| 1266 |
if (q4 <= 38) { |
| 1267 |
P128.w[1] = C4.w[1]; |
| 1268 |
P128.w[0] = C4.w[0]; |
| 1269 |
round128_19_38 (q4, x0, P128, &res, &incr_exp, |
| 1270 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1271 |
&is_inexact_lt_midpoint, |
| 1272 |
&is_inexact_gt_midpoint); |
| 1273 |
} else if (q4 <= 57) { // 35 <= q4 <= 57 |
| 1274 |
P192.w[2] = C4.w[2]; |
| 1275 |
P192.w[1] = C4.w[1]; |
| 1276 |
P192.w[0] = C4.w[0]; |
| 1277 |
round192_39_57 (q4, x0, P192, &R192, &incr_exp, |
| 1278 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1279 |
&is_inexact_lt_midpoint, |
| 1280 |
&is_inexact_gt_midpoint); |
| 1281 |
res.w[0] = R192.w[0]; |
| 1282 |
res.w[1] = R192.w[1]; |
| 1283 |
} else { // if (q4 <= 68) |
| 1284 |
round256_58_76 (q4, x0, C4, &R256, &incr_exp, |
| 1285 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1286 |
&is_inexact_lt_midpoint, |
| 1287 |
&is_inexact_gt_midpoint); |
| 1288 |
res.w[0] = R256.w[0]; |
| 1289 |
res.w[1] = R256.w[1]; |
| 1290 |
} |
| 1291 |
e4 = e4 + x0; |
| 1292 |
q4 = p34; |
| 1293 |
if (incr_exp) { |
| 1294 |
e4 = e4 + 1; |
| 1295 |
if (q4 + e4 == expmin + p34) *pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION); |
| 1296 |
} |
| 1297 |
// res is now the coefficient of the result rounded to the destination |
| 1298 |
// precision, with unbounded exponent; the exponent is e4; q4=digits(res) |
| 1299 |
} else { // if (q4 <= p34) |
| 1300 |
// C4 * 10^e4 is the result rounded to the destination precision, with |
| 1301 |
// unbounded exponent (which is exact) |
| 1302 |
|
| 1303 |
if ((q4 + e4 <= p34 + expmax) && (e4 > expmax)) { |
| 1304 |
// e4 is too large, but can be brought within range by scaling up C4 |
| 1305 |
scale = e4 - expmax; // 1 <= scale < P-q4 <= P-1 => 1 <= scale <= P-2 |
| 1306 |
// res = (C4 * 10^scale) * 10^expmax |
| 1307 |
if (q4 <= 19) { // C4 fits in 64 bits |
| 1308 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1309 |
// 64 x 64 C4.w[0] * ten2k64[scale] |
| 1310 |
__mul_64x64_to_128MACH (res, C4.w[0], ten2k64[scale]); |
| 1311 |
} else { // 10^scale fits in 128 bits |
| 1312 |
// 64 x 128 C4.w[0] * ten2k128[scale - 20] |
| 1313 |
__mul_128x64_to_128 (res, C4.w[0], ten2k128[scale - 20]); |
| 1314 |
} |
| 1315 |
} else { // C4 fits in 128 bits, but 10^scale must fit in 64 bits |
| 1316 |
// 64 x 128 ten2k64[scale] * CC43 |
| 1317 |
__mul_128x64_to_128 (res, ten2k64[scale], C4); |
| 1318 |
} |
| 1319 |
e4 = e4 - scale; // expmax |
| 1320 |
q4 = q4 + scale; |
| 1321 |
} else { |
| 1322 |
res.w[1] = C4.w[1]; |
| 1323 |
res.w[0] = C4.w[0]; |
| 1324 |
} |
| 1325 |
// res is the coefficient of the result rounded to the destination |
| 1326 |
// precision, with unbounded exponent (it has q4 digits); the exponent |
| 1327 |
// is e4 (exact result) |
| 1328 |
} |
| 1329 |
|
| 1330 |
// check for overflow |
| 1331 |
if (q4 + e4 > p34 + expmax) { |
| 1332 |
if (rnd_mode == ROUNDING_TO_NEAREST) { |
| 1333 |
res.w[1] = p_sign | 0x7800000000000000ull; // +/-inf |
| 1334 |
res.w[0] = 0x0000000000000000ull; |
| 1335 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 1336 |
} else { |
| 1337 |
res.w[1] = p_sign | res.w[1]; |
| 1338 |
rounding_correction (rnd_mode, |
| 1339 |
is_inexact_lt_midpoint, |
| 1340 |
is_inexact_gt_midpoint, |
| 1341 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 1342 |
e4, &res, pfpsf); |
| 1343 |
} |
| 1344 |
*pfpsf |= save_fpsf; |
| 1345 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1346 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1347 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1348 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1349 |
BID_SWAP128 (res); |
| 1350 |
BID_RETURN (res) |
| 1351 |
} |
| 1352 |
// check for underflow |
| 1353 |
if (q4 + e4 < expmin + P34) { |
| 1354 |
is_tiny = 1; // the result is tiny |
| 1355 |
if (e4 < expmin) { |
| 1356 |
// if e4 < expmin, we must truncate more of res |
| 1357 |
x0 = expmin - e4; // x0 >= 1 |
| 1358 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 1359 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 1360 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 1361 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 1362 |
is_inexact_lt_midpoint = 0; |
| 1363 |
is_inexact_gt_midpoint = 0; |
| 1364 |
is_midpoint_lt_even = 0; |
| 1365 |
is_midpoint_gt_even = 0; |
| 1366 |
// the number of decimal digits in res is q4 |
| 1367 |
if (x0 < q4) { // 1 <= x0 <= q4-1 => round res to q4 - x0 digits |
| 1368 |
if (q4 <= 18) { // 2 <= q4 <= 18, 1 <= x0 <= 17 |
| 1369 |
round64_2_18 (q4, x0, res.w[0], &R64, &incr_exp, |
| 1370 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1371 |
&is_inexact_lt_midpoint, |
| 1372 |
&is_inexact_gt_midpoint); |
| 1373 |
if (incr_exp) { |
| 1374 |
// R64 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 17 |
| 1375 |
R64 = ten2k64[q4 - x0]; |
| 1376 |
} |
| 1377 |
// res.w[1] = 0; (from above) |
| 1378 |
res.w[0] = R64; |
| 1379 |
} else { // if (q4 <= 34) |
| 1380 |
// 19 <= q4 <= 38 |
| 1381 |
P128.w[1] = res.w[1]; |
| 1382 |
P128.w[0] = res.w[0]; |
| 1383 |
round128_19_38 (q4, x0, P128, &res, &incr_exp, |
| 1384 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1385 |
&is_inexact_lt_midpoint, |
| 1386 |
&is_inexact_gt_midpoint); |
| 1387 |
if (incr_exp) { |
| 1388 |
// increase coefficient by a factor of 10; this will be <= 10^33 |
| 1389 |
// R128 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 37 |
| 1390 |
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19 |
| 1391 |
// res.w[1] = 0; |
| 1392 |
res.w[0] = ten2k64[q4 - x0]; |
| 1393 |
} else { // 20 <= q4 - x0 <= 37 |
| 1394 |
res.w[0] = ten2k128[q4 - x0 - 20].w[0]; |
| 1395 |
res.w[1] = ten2k128[q4 - x0 - 20].w[1]; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} |
| 1399 |
e4 = e4 + x0; // expmin |
| 1400 |
} else if (x0 == q4) { |
| 1401 |
// the second rounding is for 0.d(0)d(1)...d(q4-1) * 10^emin |
| 1402 |
// determine relationship with 1/2 ulp |
| 1403 |
if (q4 <= 19) { |
| 1404 |
if (res.w[0] < midpoint64[q4 - 1]) { // < 1/2 ulp |
| 1405 |
lt_half_ulp = 1; |
| 1406 |
is_inexact_lt_midpoint = 1; |
| 1407 |
} else if (res.w[0] == midpoint64[q4 - 1]) { // = 1/2 ulp |
| 1408 |
eq_half_ulp = 1; |
| 1409 |
is_midpoint_gt_even = 1; |
| 1410 |
} else { // > 1/2 ulp |
| 1411 |
// gt_half_ulp = 1; |
| 1412 |
is_inexact_gt_midpoint = 1; |
| 1413 |
} |
| 1414 |
} else { // if (q4 <= 34) |
| 1415 |
if (res.w[1] < midpoint128[q4 - 20].w[1] || |
| 1416 |
(res.w[1] == midpoint128[q4 - 20].w[1] && |
| 1417 |
res.w[0] < midpoint128[q4 - 20].w[0])) { // < 1/2 ulp |
| 1418 |
lt_half_ulp = 1; |
| 1419 |
is_inexact_lt_midpoint = 1; |
| 1420 |
} else if (res.w[1] == midpoint128[q4 - 20].w[1] && |
| 1421 |
res.w[0] == midpoint128[q4 - 20].w[0]) { // = 1/2 ulp |
| 1422 |
eq_half_ulp = 1; |
| 1423 |
is_midpoint_gt_even = 1; |
| 1424 |
} else { // > 1/2 ulp |
| 1425 |
// gt_half_ulp = 1; |
| 1426 |
is_inexact_gt_midpoint = 1; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
if (lt_half_ulp || eq_half_ulp) { |
| 1430 |
// res = +0.0 * 10^expmin |
| 1431 |
res.w[1] = 0x0000000000000000ull; |
| 1432 |
res.w[0] = 0x0000000000000000ull; |
| 1433 |
} else { // if (gt_half_ulp) |
| 1434 |
// res = +1 * 10^expmin |
| 1435 |
res.w[1] = 0x0000000000000000ull; |
| 1436 |
res.w[0] = 0x0000000000000001ull; |
| 1437 |
} |
| 1438 |
e4 = expmin; |
| 1439 |
} else { // if (x0 > q4) |
| 1440 |
// the second rounding is for 0.0...d(0)d(1)...d(q4-1) * 10^emin |
| 1441 |
res.w[1] = 0; |
| 1442 |
res.w[0] = 0; |
| 1443 |
e4 = expmin; |
| 1444 |
is_inexact_lt_midpoint = 1; |
| 1445 |
} |
| 1446 |
// avoid a double rounding error |
| 1447 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 1448 |
is_midpoint_lt_even) { // double rounding error upward |
| 1449 |
// res = res - 1 |
| 1450 |
res.w[0]--; |
| 1451 |
if (res.w[0] == 0xffffffffffffffffull) |
| 1452 |
res.w[1]--; |
| 1453 |
// Note: a double rounding error upward is not possible; for this |
| 1454 |
// the result after the first rounding would have to be 99...95 |
| 1455 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 1456 |
// not possible for f * f + 0 |
| 1457 |
is_midpoint_lt_even = 0; |
| 1458 |
is_inexact_lt_midpoint = 1; |
| 1459 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 1460 |
is_midpoint_gt_even) { // double rounding error downward |
| 1461 |
// res = res + 1 |
| 1462 |
res.w[0]++; |
| 1463 |
if (res.w[0] == 0) |
| 1464 |
res.w[1]++; |
| 1465 |
is_midpoint_gt_even = 0; |
| 1466 |
is_inexact_gt_midpoint = 1; |
| 1467 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 1468 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 1469 |
// if this second rounding was exact the result may still be |
| 1470 |
// inexact because of the first rounding |
| 1471 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 1472 |
is_inexact_gt_midpoint = 1; |
| 1473 |
} |
| 1474 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 1475 |
is_inexact_lt_midpoint = 1; |
| 1476 |
} |
| 1477 |
} else if (is_midpoint_gt_even && |
| 1478 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 1479 |
// pulled up to a midpoint |
| 1480 |
is_inexact_lt_midpoint = 1; |
| 1481 |
is_inexact_gt_midpoint = 0; |
| 1482 |
is_midpoint_lt_even = 0; |
| 1483 |
is_midpoint_gt_even = 0; |
| 1484 |
} else if (is_midpoint_lt_even && |
| 1485 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 1486 |
// pulled down to a midpoint |
| 1487 |
is_inexact_lt_midpoint = 0; |
| 1488 |
is_inexact_gt_midpoint = 1; |
| 1489 |
is_midpoint_lt_even = 0; |
| 1490 |
is_midpoint_gt_even = 0; |
| 1491 |
} else { |
| 1492 |
; |
| 1493 |
} |
| 1494 |
} else { // if e4 >= emin then q4 < P and the result is tiny and exact |
| 1495 |
if (e3 < e4) { |
| 1496 |
// if (e3 < e4) the preferred exponent is e3 |
| 1497 |
// return (C4 * 10^scale) * 10^(e4 - scale) |
| 1498 |
// where scale = min (p34-q4, (e4 - e3)) |
| 1499 |
scale = p34 - q4; |
| 1500 |
ind = e4 - e3; |
| 1501 |
if (ind < scale) |
| 1502 |
scale = ind; |
| 1503 |
if (scale == 0) { |
| 1504 |
; // res and e4 are unchanged |
| 1505 |
} else if (q4 <= 19) { // C4 fits in 64 bits |
| 1506 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1507 |
// 64 x 64 res.w[0] * ten2k64[scale] |
| 1508 |
__mul_64x64_to_128MACH (res, res.w[0], ten2k64[scale]); |
| 1509 |
} else { // 10^scale fits in 128 bits |
| 1510 |
// 64 x 128 res.w[0] * ten2k128[scale - 20] |
| 1511 |
__mul_128x64_to_128 (res, res.w[0], ten2k128[scale - 20]); |
| 1512 |
} |
| 1513 |
} else { // res fits in 128 bits, but 10^scale must fit in 64 bits |
| 1514 |
// 64 x 128 ten2k64[scale] * C3 |
| 1515 |
__mul_128x64_to_128 (res, ten2k64[scale], res); |
| 1516 |
} |
| 1517 |
// subtract scale from the exponent |
| 1518 |
e4 = e4 - scale; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
// check for inexact result |
| 1523 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 1524 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 1525 |
// set the inexact flag and the underflow flag |
| 1526 |
*pfpsf |= INEXACT_EXCEPTION; |
| 1527 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 1528 |
} |
| 1529 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1]; |
| 1530 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 1531 |
rounding_correction (rnd_mode, |
| 1532 |
is_inexact_lt_midpoint, |
| 1533 |
is_inexact_gt_midpoint, |
| 1534 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 1535 |
e4, &res, pfpsf); |
| 1536 |
} |
| 1537 |
*pfpsf |= save_fpsf; |
| 1538 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1539 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1540 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1541 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1542 |
BID_SWAP128 (res); |
| 1543 |
BID_RETURN (res) |
| 1544 |
} |
| 1545 |
// no overflow, and no underflow for rounding to nearest |
| 1546 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1]; |
| 1547 |
|
| 1548 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 1549 |
rounding_correction (rnd_mode, |
| 1550 |
is_inexact_lt_midpoint, |
| 1551 |
is_inexact_gt_midpoint, |
| 1552 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 1553 |
e4, &res, pfpsf); |
| 1554 |
// if e4 = expmin && significand < 10^33 => result is tiny (for RD, RZ) |
| 1555 |
if (e4 == expmin) { |
| 1556 |
if ((res.w[1] & MASK_COEFF) < 0x0000314dc6448d93ull || |
| 1557 |
((res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull && |
| 1558 |
res.w[0] < 0x38c15b0a00000000ull)) { |
| 1559 |
is_tiny = 1; |
| 1560 |
} |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 1565 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 1566 |
// set the inexact flag |
| 1567 |
*pfpsf |= INEXACT_EXCEPTION; |
| 1568 |
if (is_tiny) |
| 1569 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 1570 |
} |
| 1571 |
|
| 1572 |
if ((*pfpsf & INEXACT_EXCEPTION) == 0) { // x * y is exact |
| 1573 |
// need to ensure that the result has the preferred exponent |
| 1574 |
p_exp = res.w[1] & MASK_EXP; |
| 1575 |
if (z_exp < p_exp) { // the preferred exponent is z_exp |
| 1576 |
// signficand of res in C3 |
| 1577 |
C3.w[1] = res.w[1] & MASK_COEFF; |
| 1578 |
C3.w[0] = res.w[0]; |
| 1579 |
// the number of decimal digits of x * y is q4 <= 34 |
| 1580 |
// Note: the coefficient fits in 128 bits |
| 1581 |
|
| 1582 |
// return (C3 * 10^scale) * 10^(p_exp - scale) |
| 1583 |
// where scale = min (p34-q4, (p_exp-z_exp) >> 49) |
| 1584 |
scale = p34 - q4; |
| 1585 |
ind = (p_exp - z_exp) >> 49; |
| 1586 |
if (ind < scale) |
| 1587 |
scale = ind; |
| 1588 |
// subtract scale from the exponent |
| 1589 |
p_exp = p_exp - ((UINT64) scale << 49); |
| 1590 |
if (scale == 0) { |
| 1591 |
; // leave res unchanged |
| 1592 |
} else if (q4 <= 19) { // x * y fits in 64 bits |
| 1593 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1594 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 1595 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 1596 |
} else { // 10^scale fits in 128 bits |
| 1597 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 1598 |
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]); |
| 1599 |
} |
| 1600 |
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1]; |
| 1601 |
} else { // x * y fits in 128 bits, but 10^scale must fit in 64 bits |
| 1602 |
// 64 x 128 ten2k64[scale] * C3 |
| 1603 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 1604 |
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1]; |
| 1605 |
} |
| 1606 |
} // else leave the result as it is, because p_exp <= z_exp |
| 1607 |
} |
| 1608 |
*pfpsf |= save_fpsf; |
| 1609 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1610 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1611 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1612 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1613 |
BID_SWAP128 (res); |
| 1614 |
BID_RETURN (res) |
| 1615 |
} // else we have f * f + f |
| 1616 |
|
| 1617 |
// continue with x = f, y = f, z = f |
| 1618 |
|
| 1619 |
delta = q3 + e3 - q4 - e4; |
| 1620 |
delta_ge_zero: |
| 1621 |
if (delta >= 0) { |
| 1622 |
if (p34 <= delta - 1 || // Case (1') |
| 1623 |
(p34 == delta && e3 + 6176 < p34 - q3)) { // Case (1''A) |
| 1624 |
// check for overflow, which can occur only in Case (1') |
| 1625 |
if ((q3 + e3) > (p34 + expmax) && p34 <= delta - 1) { |
| 1626 |
// e3 > expmax implies p34 <= delta-1 and e3 > expmax is a necessary |
| 1627 |
// condition for (q3 + e3) > (p34 + expmax) |
| 1628 |
if (rnd_mode == ROUNDING_TO_NEAREST) { |
| 1629 |
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf |
| 1630 |
res.w[0] = 0x0000000000000000ull; |
| 1631 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 1632 |
} else { |
| 1633 |
if (p_sign == z_sign) { |
| 1634 |
is_inexact_lt_midpoint = 1; |
| 1635 |
} else { |
| 1636 |
is_inexact_gt_midpoint = 1; |
| 1637 |
} |
| 1638 |
// q3 <= p34; if (q3 < p34) scale C3 up by 10^(p34-q3) |
| 1639 |
scale = p34 - q3; |
| 1640 |
if (scale == 0) { |
| 1641 |
res.w[1] = z_sign | C3.w[1]; |
| 1642 |
res.w[0] = C3.w[0]; |
| 1643 |
} else { |
| 1644 |
if (q3 <= 19) { // C3 fits in 64 bits |
| 1645 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1646 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 1647 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 1648 |
} else { // 10^scale fits in 128 bits |
| 1649 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 1650 |
__mul_128x64_to_128 (res, C3.w[0], |
| 1651 |
ten2k128[scale - 20]); |
| 1652 |
} |
| 1653 |
} else { // C3 fits in 128 bits, but 10^scale must fit in 64 bits |
| 1654 |
// 64 x 128 ten2k64[scale] * C3 |
| 1655 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 1656 |
} |
| 1657 |
// the coefficient in res has q3 + scale = p34 digits |
| 1658 |
} |
| 1659 |
e3 = e3 - scale; |
| 1660 |
res.w[1] = z_sign | res.w[1]; |
| 1661 |
rounding_correction (rnd_mode, |
| 1662 |
is_inexact_lt_midpoint, |
| 1663 |
is_inexact_gt_midpoint, |
| 1664 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 1665 |
e3, &res, pfpsf); |
| 1666 |
} |
| 1667 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1668 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1669 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1670 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1671 |
BID_SWAP128 (res); |
| 1672 |
BID_RETURN (res) |
| 1673 |
} |
| 1674 |
// res = z |
| 1675 |
if (q3 < p34) { // the preferred exponent is z_exp - (p34 - q3) |
| 1676 |
// return (C3 * 10^scale) * 10^(z_exp - scale) |
| 1677 |
// where scale = min (p34-q3, z_exp-EMIN) |
| 1678 |
scale = p34 - q3; |
| 1679 |
ind = e3 + 6176; |
| 1680 |
if (ind < scale) |
| 1681 |
scale = ind; |
| 1682 |
if (scale == 0) { |
| 1683 |
res.w[1] = C3.w[1]; |
| 1684 |
res.w[0] = C3.w[0]; |
| 1685 |
} else if (q3 <= 19) { // z fits in 64 bits |
| 1686 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1687 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 1688 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 1689 |
} else { // 10^scale fits in 128 bits |
| 1690 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 1691 |
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]); |
| 1692 |
} |
| 1693 |
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits |
| 1694 |
// 64 x 128 ten2k64[scale] * C3 |
| 1695 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 1696 |
} |
| 1697 |
// the coefficient in res has q3 + scale digits |
| 1698 |
// subtract scale from the exponent |
| 1699 |
z_exp = z_exp - ((UINT64) scale << 49); |
| 1700 |
e3 = e3 - scale; |
| 1701 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 1702 |
if (scale + q3 < p34) |
| 1703 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 1704 |
} else { |
| 1705 |
scale = 0; |
| 1706 |
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | C3.w[1]; |
| 1707 |
res.w[0] = C3.w[0]; |
| 1708 |
} |
| 1709 |
|
| 1710 |
// use the following to avoid double rounding errors when operating on |
| 1711 |
// mixed formats in rounding to nearest, and for correcting the result |
| 1712 |
// if not rounding to nearest |
| 1713 |
if ((p_sign != z_sign) && (delta == (q3 + scale + 1))) { |
| 1714 |
// there is a gap of exactly one digit between the scaled C3 and C4 |
| 1715 |
// C3 * 10^ scale = 10^(q3+scale-1) <=> C3 = 10^(q3-1) is special case |
| 1716 |
if ((q3 <= 19 && C3.w[0] != ten2k64[q3 - 1]) || |
| 1717 |
(q3 == 20 && (C3.w[1] != 0 || C3.w[0] != ten2k64[19])) || |
| 1718 |
(q3 >= 21 && (C3.w[1] != ten2k128[q3 - 21].w[1] || |
| 1719 |
C3.w[0] != ten2k128[q3 - 21].w[0]))) { |
| 1720 |
// C3 * 10^ scale != 10^(q3-1) |
| 1721 |
// if ((res.w[1] & MASK_COEFF) != 0x0000314dc6448d93ull || |
| 1722 |
// res.w[0] != 0x38c15b0a00000000ull) { // C3 * 10^scale != 10^33 |
| 1723 |
is_inexact_gt_midpoint = 1; // if (z_sign), set as if for abs. value |
| 1724 |
} else { // if C3 * 10^scale = 10^(q3+scale-1) |
| 1725 |
// ok from above e3 = (z_exp >> 49) - 6176; |
| 1726 |
// the result is always inexact |
| 1727 |
if (q4 == 1) { |
| 1728 |
R64 = C4.w[0]; |
| 1729 |
} else { |
| 1730 |
// if q4 > 1 then truncate C4 from q4 digits to 1 digit; |
| 1731 |
// x = q4-1, 1 <= x <= 67 and check if this operation is exact |
| 1732 |
if (q4 <= 18) { // 2 <= q4 <= 18 |
| 1733 |
round64_2_18 (q4, q4 - 1, C4.w[0], &R64, &incr_exp, |
| 1734 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 1735 |
&is_inexact_lt_midpoint, |
| 1736 |
&is_inexact_gt_midpoint); |
| 1737 |
} else if (q4 <= 38) { |
| 1738 |
P128.w[1] = C4.w[1]; |
| 1739 |
P128.w[0] = C4.w[0]; |
| 1740 |
round128_19_38 (q4, q4 - 1, P128, &R128, &incr_exp, |
| 1741 |
&is_midpoint_lt_even, |
| 1742 |
&is_midpoint_gt_even, |
| 1743 |
&is_inexact_lt_midpoint, |
| 1744 |
&is_inexact_gt_midpoint); |
| 1745 |
R64 = R128.w[0]; // one decimal digit |
| 1746 |
} else if (q4 <= 57) { |
| 1747 |
P192.w[2] = C4.w[2]; |
| 1748 |
P192.w[1] = C4.w[1]; |
| 1749 |
P192.w[0] = C4.w[0]; |
| 1750 |
round192_39_57 (q4, q4 - 1, P192, &R192, &incr_exp, |
| 1751 |
&is_midpoint_lt_even, |
| 1752 |
&is_midpoint_gt_even, |
| 1753 |
&is_inexact_lt_midpoint, |
| 1754 |
&is_inexact_gt_midpoint); |
| 1755 |
R64 = R192.w[0]; // one decimal digit |
| 1756 |
} else { // if (q4 <= 68) |
| 1757 |
round256_58_76 (q4, q4 - 1, C4, &R256, &incr_exp, |
| 1758 |
&is_midpoint_lt_even, |
| 1759 |
&is_midpoint_gt_even, |
| 1760 |
&is_inexact_lt_midpoint, |
| 1761 |
&is_inexact_gt_midpoint); |
| 1762 |
R64 = R256.w[0]; // one decimal digit |
| 1763 |
} |
| 1764 |
if (incr_exp) { |
| 1765 |
R64 = 10; |
| 1766 |
} |
| 1767 |
} |
| 1768 |
|
| 1769 |
if (R64 == 5 && !is_inexact_lt_midpoint && !is_inexact_gt_midpoint && |
| 1770 |
!is_midpoint_lt_even && !is_midpoint_gt_even) { |
| 1771 |
//if (q4 == 1 && C4.w[0] == 5) { |
| 1772 |
is_inexact_lt_midpoint = 0; |
| 1773 |
is_inexact_gt_midpoint = 0; |
| 1774 |
is_midpoint_lt_even = 1; |
| 1775 |
is_midpoint_gt_even = 0; |
| 1776 |
} else if ((e3 == expmin) || |
| 1777 |
R64 < 5 || (R64 == 5 && is_inexact_gt_midpoint)) { |
| 1778 |
// result does not change |
| 1779 |
is_inexact_lt_midpoint = 0; |
| 1780 |
is_inexact_gt_midpoint = 1; |
| 1781 |
is_midpoint_lt_even = 0; |
| 1782 |
is_midpoint_gt_even = 0; |
| 1783 |
} else { |
| 1784 |
is_inexact_lt_midpoint = 1; |
| 1785 |
is_inexact_gt_midpoint = 0; |
| 1786 |
is_midpoint_lt_even = 0; |
| 1787 |
is_midpoint_gt_even = 0; |
| 1788 |
// result decremented is 10^(q3+scale) - 1 |
| 1789 |
if ((q3 + scale) <= 19) { |
| 1790 |
res.w[1] = 0; |
| 1791 |
res.w[0] = ten2k64[q3 + scale]; |
| 1792 |
} else { // if ((q3 + scale + 1) <= 35) |
| 1793 |
res.w[1] = ten2k128[q3 + scale - 20].w[1]; |
| 1794 |
res.w[0] = ten2k128[q3 + scale - 20].w[0]; |
| 1795 |
} |
| 1796 |
res.w[0] = res.w[0] - 1; // borrow never occurs |
| 1797 |
z_exp = z_exp - EXP_P1; |
| 1798 |
e3 = e3 - 1; |
| 1799 |
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1]; |
| 1800 |
} |
| 1801 |
if (e3 == expmin) { |
| 1802 |
*pfpsf |= UNDERFLOW_EXCEPTION; // tiny if detected before rounding |
| 1803 |
} |
| 1804 |
} // end 10^(q3+scale-1) |
| 1805 |
// set the inexact flag |
| 1806 |
*pfpsf |= INEXACT_EXCEPTION; |
| 1807 |
} else { |
| 1808 |
if (p_sign == z_sign) { |
| 1809 |
// if (z_sign), set as if for absolute value |
| 1810 |
is_inexact_lt_midpoint = 1; |
| 1811 |
} else { // if (p_sign != z_sign) |
| 1812 |
// if (z_sign), set as if for absolute value |
| 1813 |
is_inexact_gt_midpoint = 1; |
| 1814 |
} |
| 1815 |
*pfpsf |= INEXACT_EXCEPTION; |
| 1816 |
} |
| 1817 |
// the result is always inexact => set the inexact flag |
| 1818 |
// Determine tininess: |
| 1819 |
// if (exp > expmin) |
| 1820 |
// the result is not tiny |
| 1821 |
// else // if exp = emin |
| 1822 |
// if (q3 + scale < p34) |
| 1823 |
// the result is tiny |
| 1824 |
// else // if (q3 + scale = p34) |
| 1825 |
// if (C3 * 10^scale > 10^33) |
| 1826 |
// the result is not tiny |
| 1827 |
// else // if C3 * 10^scale = 10^33 |
| 1828 |
// if (xy * z > 0) |
| 1829 |
// the result is not tiny |
| 1830 |
// else // if (xy * z < 0) |
| 1831 |
// if (z > 0) |
| 1832 |
// if rnd_mode != RP |
| 1833 |
// the result is tiny |
| 1834 |
// else // if RP |
| 1835 |
// the result is not tiny |
| 1836 |
// else // if (z < 0) |
| 1837 |
// if rnd_mode != RM |
| 1838 |
// the result is tiny |
| 1839 |
// else // if RM |
| 1840 |
// the result is not tiny |
| 1841 |
// endif |
| 1842 |
// endif |
| 1843 |
// endif |
| 1844 |
// endif |
| 1845 |
// endif |
| 1846 |
// endif |
| 1847 |
if ((e3 == expmin && (q3 + scale) < p34) || |
| 1848 |
(e3 == expmin && (q3 + scale) == p34 && |
| 1849 |
(res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull && // 10^33_high |
| 1850 |
res.w[0] == 0x38c15b0a00000000ull && // 10^33_low |
| 1851 |
z_sign != p_sign)) { |
| 1852 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 1853 |
} |
| 1854 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 1855 |
rounding_correction (rnd_mode, |
| 1856 |
is_inexact_lt_midpoint, |
| 1857 |
is_inexact_gt_midpoint, |
| 1858 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 1859 |
e3, &res, pfpsf); |
| 1860 |
} |
| 1861 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1862 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1863 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1864 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1865 |
BID_SWAP128 (res); |
| 1866 |
BID_RETURN (res) |
| 1867 |
|
| 1868 |
} else if (p34 == delta) { // Case (1''B) |
| 1869 |
|
| 1870 |
// because Case (1''A) was treated above, e3 + 6176 >= p34 - q3 |
| 1871 |
// and C3 can be scaled up to p34 digits if needed |
| 1872 |
|
| 1873 |
// scale C3 to p34 digits if needed |
| 1874 |
scale = p34 - q3; // 0 <= scale <= p34 - 1 |
| 1875 |
if (scale == 0) { |
| 1876 |
res.w[1] = C3.w[1]; |
| 1877 |
res.w[0] = C3.w[0]; |
| 1878 |
} else if (q3 <= 19) { // z fits in 64 bits |
| 1879 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 1880 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 1881 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 1882 |
} else { // 10^scale fits in 128 bits |
| 1883 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 1884 |
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]); |
| 1885 |
} |
| 1886 |
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits |
| 1887 |
// 64 x 128 ten2k64[scale] * C3 |
| 1888 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 1889 |
} |
| 1890 |
// subtract scale from the exponent |
| 1891 |
z_exp = z_exp - ((UINT64) scale << 49); |
| 1892 |
e3 = e3 - scale; |
| 1893 |
// now z_sign, z_exp, and res correspond to a z scaled to p34 = 34 digits |
| 1894 |
|
| 1895 |
// determine whether x * y is less than, equal to, or greater than |
| 1896 |
// 1/2 ulp (z) |
| 1897 |
if (q4 <= 19) { |
| 1898 |
if (C4.w[0] < midpoint64[q4 - 1]) { // < 1/2 ulp |
| 1899 |
lt_half_ulp = 1; |
| 1900 |
} else if (C4.w[0] == midpoint64[q4 - 1]) { // = 1/2 ulp |
| 1901 |
eq_half_ulp = 1; |
| 1902 |
} else { // > 1/2 ulp |
| 1903 |
gt_half_ulp = 1; |
| 1904 |
} |
| 1905 |
} else if (q4 <= 38) { |
| 1906 |
if (C4.w[2] == 0 && (C4.w[1] < midpoint128[q4 - 20].w[1] || |
| 1907 |
(C4.w[1] == midpoint128[q4 - 20].w[1] && |
| 1908 |
C4.w[0] < midpoint128[q4 - 20].w[0]))) { // < 1/2 ulp |
| 1909 |
lt_half_ulp = 1; |
| 1910 |
} else if (C4.w[2] == 0 && C4.w[1] == midpoint128[q4 - 20].w[1] && |
| 1911 |
C4.w[0] == midpoint128[q4 - 20].w[0]) { // = 1/2 ulp |
| 1912 |
eq_half_ulp = 1; |
| 1913 |
} else { // > 1/2 ulp |
| 1914 |
gt_half_ulp = 1; |
| 1915 |
} |
| 1916 |
} else if (q4 <= 58) { |
| 1917 |
if (C4.w[3] == 0 && (C4.w[2] < midpoint192[q4 - 39].w[2] || |
| 1918 |
(C4.w[2] == midpoint192[q4 - 39].w[2] && |
| 1919 |
C4.w[1] < midpoint192[q4 - 39].w[1]) || |
| 1920 |
(C4.w[2] == midpoint192[q4 - 39].w[2] && |
| 1921 |
C4.w[1] == midpoint192[q4 - 39].w[1] && |
| 1922 |
C4.w[0] < midpoint192[q4 - 39].w[0]))) { // < 1/2 ulp |
| 1923 |
lt_half_ulp = 1; |
| 1924 |
} else if (C4.w[3] == 0 && C4.w[2] == midpoint192[q4 - 39].w[2] && |
| 1925 |
C4.w[1] == midpoint192[q4 - 39].w[1] && |
| 1926 |
C4.w[0] == midpoint192[q4 - 39].w[0]) { // = 1/2 ulp |
| 1927 |
eq_half_ulp = 1; |
| 1928 |
} else { // > 1/2 ulp |
| 1929 |
gt_half_ulp = 1; |
| 1930 |
} |
| 1931 |
} else { |
| 1932 |
if (C4.w[3] < midpoint256[q4 - 59].w[3] || |
| 1933 |
(C4.w[3] == midpoint256[q4 - 59].w[3] && |
| 1934 |
C4.w[2] < midpoint256[q4 - 59].w[2]) || |
| 1935 |
(C4.w[3] == midpoint256[q4 - 59].w[3] && |
| 1936 |
C4.w[2] == midpoint256[q4 - 59].w[2] && |
| 1937 |
C4.w[1] < midpoint256[q4 - 59].w[1]) || |
| 1938 |
(C4.w[3] == midpoint256[q4 - 59].w[3] && |
| 1939 |
C4.w[2] == midpoint256[q4 - 59].w[2] && |
| 1940 |
C4.w[1] == midpoint256[q4 - 59].w[1] && |
| 1941 |
C4.w[0] < midpoint256[q4 - 59].w[0])) { // < 1/2 ulp |
| 1942 |
lt_half_ulp = 1; |
| 1943 |
} else if (C4.w[3] == midpoint256[q4 - 59].w[3] && |
| 1944 |
C4.w[2] == midpoint256[q4 - 59].w[2] && |
| 1945 |
C4.w[1] == midpoint256[q4 - 59].w[1] && |
| 1946 |
C4.w[0] == midpoint256[q4 - 59].w[0]) { // = 1/2 ulp |
| 1947 |
eq_half_ulp = 1; |
| 1948 |
} else { // > 1/2 ulp |
| 1949 |
gt_half_ulp = 1; |
| 1950 |
} |
| 1951 |
} |
| 1952 |
|
| 1953 |
if (p_sign == z_sign) { |
| 1954 |
if (lt_half_ulp) { |
| 1955 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 1956 |
// use the following to avoid double rounding errors when operating on |
| 1957 |
// mixed formats in rounding to nearest |
| 1958 |
is_inexact_lt_midpoint = 1; // if (z_sign), as if for absolute value |
| 1959 |
} else if ((eq_half_ulp && (res.w[0] & 0x01)) || gt_half_ulp) { |
| 1960 |
// add 1 ulp to the significand |
| 1961 |
res.w[0]++; |
| 1962 |
if (res.w[0] == 0x0ull) |
| 1963 |
res.w[1]++; |
| 1964 |
// check for rounding overflow, when coeff == 10^34 |
| 1965 |
if ((res.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull && |
| 1966 |
res.w[0] == 0x378d8e6400000000ull) { // coefficient = 10^34 |
| 1967 |
e3 = e3 + 1; |
| 1968 |
// coeff = 10^33 |
| 1969 |
z_exp = ((UINT64) (e3 + 6176) << 49) & MASK_EXP; |
| 1970 |
res.w[1] = 0x0000314dc6448d93ull; |
| 1971 |
res.w[0] = 0x38c15b0a00000000ull; |
| 1972 |
} |
| 1973 |
// end add 1 ulp |
| 1974 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 1975 |
if (eq_half_ulp) { |
| 1976 |
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value |
| 1977 |
} else { |
| 1978 |
is_inexact_gt_midpoint = 1; // if (z_sign), as if for absolute value |
| 1979 |
} |
| 1980 |
} else { // if (eq_half_ulp && !(res.w[0] & 0x01)) |
| 1981 |
// leave unchanged |
| 1982 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 1983 |
is_midpoint_gt_even = 1; // if (z_sign), as if for absolute value |
| 1984 |
} |
| 1985 |
// the result is always inexact, and never tiny |
| 1986 |
// set the inexact flag |
| 1987 |
*pfpsf |= INEXACT_EXCEPTION; |
| 1988 |
// check for overflow |
| 1989 |
if (e3 > expmax && rnd_mode == ROUNDING_TO_NEAREST) { |
| 1990 |
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf |
| 1991 |
res.w[0] = 0x0000000000000000ull; |
| 1992 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 1993 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 1994 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 1995 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 1996 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 1997 |
BID_SWAP128 (res); |
| 1998 |
BID_RETURN (res) |
| 1999 |
} |
| 2000 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 2001 |
rounding_correction (rnd_mode, |
| 2002 |
is_inexact_lt_midpoint, |
| 2003 |
is_inexact_gt_midpoint, |
| 2004 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 2005 |
e3, &res, pfpsf); |
| 2006 |
z_exp = res.w[1] & MASK_EXP; |
| 2007 |
} |
| 2008 |
} else { // if (p_sign != z_sign) |
| 2009 |
// consider two cases, because C3 * 10^scale = 10^33 is a special case |
| 2010 |
if (res.w[1] != 0x0000314dc6448d93ull || |
| 2011 |
res.w[0] != 0x38c15b0a00000000ull) { // C3 * 10^scale != 10^33 |
| 2012 |
if (lt_half_ulp) { |
| 2013 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2014 |
// use the following to avoid double rounding errors when operating |
| 2015 |
// on mixed formats in rounding to nearest |
| 2016 |
is_inexact_gt_midpoint = 1; // if (z_sign), as if for absolute value |
| 2017 |
} else if ((eq_half_ulp && (res.w[0] & 0x01)) || gt_half_ulp) { |
| 2018 |
// subtract 1 ulp from the significand |
| 2019 |
res.w[0]--; |
| 2020 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2021 |
res.w[1]--; |
| 2022 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2023 |
if (eq_half_ulp) { |
| 2024 |
is_midpoint_gt_even = 1; // if (z_sign), as if for absolute value |
| 2025 |
} else { |
| 2026 |
is_inexact_lt_midpoint = 1; //if(z_sign), as if for absolute value |
| 2027 |
} |
| 2028 |
} else { // if (eq_half_ulp && !(res.w[0] & 0x01)) |
| 2029 |
// leave unchanged |
| 2030 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2031 |
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value |
| 2032 |
} |
| 2033 |
// the result is always inexact, and never tiny |
| 2034 |
// check for overflow for RN |
| 2035 |
if (e3 > expmax) { |
| 2036 |
if (rnd_mode == ROUNDING_TO_NEAREST) { |
| 2037 |
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf |
| 2038 |
res.w[0] = 0x0000000000000000ull; |
| 2039 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 2040 |
} else { |
| 2041 |
rounding_correction (rnd_mode, |
| 2042 |
is_inexact_lt_midpoint, |
| 2043 |
is_inexact_gt_midpoint, |
| 2044 |
is_midpoint_lt_even, |
| 2045 |
is_midpoint_gt_even, e3, &res, |
| 2046 |
pfpsf); |
| 2047 |
} |
| 2048 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2049 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2050 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2051 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2052 |
BID_SWAP128 (res); |
| 2053 |
BID_RETURN (res) |
| 2054 |
} |
| 2055 |
// set the inexact flag |
| 2056 |
*pfpsf |= INEXACT_EXCEPTION; |
| 2057 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 2058 |
rounding_correction (rnd_mode, |
| 2059 |
is_inexact_lt_midpoint, |
| 2060 |
is_inexact_gt_midpoint, |
| 2061 |
is_midpoint_lt_even, |
| 2062 |
is_midpoint_gt_even, e3, &res, pfpsf); |
| 2063 |
} |
| 2064 |
z_exp = res.w[1] & MASK_EXP; |
| 2065 |
} else { // if C3 * 10^scale = 10^33 |
| 2066 |
e3 = (z_exp >> 49) - 6176; |
| 2067 |
if (e3 > expmin) { |
| 2068 |
// the result is exact if exp > expmin and C4 = d*10^(q4-1), |
| 2069 |
// where d = 1, 2, 3, ..., 9; it could be tiny too, but exact |
| 2070 |
if (q4 == 1) { |
| 2071 |
// if q4 = 1 the result is exact |
| 2072 |
// result coefficient = 10^34 - C4 |
| 2073 |
res.w[1] = 0x0001ed09bead87c0ull; |
| 2074 |
res.w[0] = 0x378d8e6400000000ull - C4.w[0]; |
| 2075 |
z_exp = z_exp - EXP_P1; |
| 2076 |
e3 = e3 - 1; |
| 2077 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2078 |
} else { |
| 2079 |
// if q4 > 1 then truncate C4 from q4 digits to 1 digit; |
| 2080 |
// x = q4-1, 1 <= x <= 67 and check if this operation is exact |
| 2081 |
if (q4 <= 18) { // 2 <= q4 <= 18 |
| 2082 |
round64_2_18 (q4, q4 - 1, C4.w[0], &R64, &incr_exp, |
| 2083 |
&is_midpoint_lt_even, |
| 2084 |
&is_midpoint_gt_even, |
| 2085 |
&is_inexact_lt_midpoint, |
| 2086 |
&is_inexact_gt_midpoint); |
| 2087 |
} else if (q4 <= 38) { |
| 2088 |
P128.w[1] = C4.w[1]; |
| 2089 |
P128.w[0] = C4.w[0]; |
| 2090 |
round128_19_38 (q4, q4 - 1, P128, &R128, &incr_exp, |
| 2091 |
&is_midpoint_lt_even, |
| 2092 |
&is_midpoint_gt_even, |
| 2093 |
&is_inexact_lt_midpoint, |
| 2094 |
&is_inexact_gt_midpoint); |
| 2095 |
R64 = R128.w[0]; // one decimal digit |
| 2096 |
} else if (q4 <= 57) { |
| 2097 |
P192.w[2] = C4.w[2]; |
| 2098 |
P192.w[1] = C4.w[1]; |
| 2099 |
P192.w[0] = C4.w[0]; |
| 2100 |
round192_39_57 (q4, q4 - 1, P192, &R192, &incr_exp, |
| 2101 |
&is_midpoint_lt_even, |
| 2102 |
&is_midpoint_gt_even, |
| 2103 |
&is_inexact_lt_midpoint, |
| 2104 |
&is_inexact_gt_midpoint); |
| 2105 |
R64 = R192.w[0]; // one decimal digit |
| 2106 |
} else { // if (q4 <= 68) |
| 2107 |
round256_58_76 (q4, q4 - 1, C4, &R256, &incr_exp, |
| 2108 |
&is_midpoint_lt_even, |
| 2109 |
&is_midpoint_gt_even, |
| 2110 |
&is_inexact_lt_midpoint, |
| 2111 |
&is_inexact_gt_midpoint); |
| 2112 |
R64 = R256.w[0]; // one decimal digit |
| 2113 |
} |
| 2114 |
if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2115 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 2116 |
// the result is exact: 10^34 - R64 |
| 2117 |
// incr_exp = 0 with certainty |
| 2118 |
z_exp = z_exp - EXP_P1; |
| 2119 |
e3 = e3 - 1; |
| 2120 |
res.w[1] = |
| 2121 |
z_sign | (z_exp & MASK_EXP) | 0x0001ed09bead87c0ull; |
| 2122 |
res.w[0] = 0x378d8e6400000000ull - R64; |
| 2123 |
} else { |
| 2124 |
// We want R64 to be the top digit of C4, but we actually |
| 2125 |
// obtained (C4 * 10^(-q4+1))RN; a correction may be needed, |
| 2126 |
// because the top digit is (C4 * 10^(-q4+1))RZ |
| 2127 |
// however, if incr_exp = 1 then R64 = 10 with certainty |
| 2128 |
if (incr_exp) { |
| 2129 |
R64 = 10; |
| 2130 |
} |
| 2131 |
// the result is inexact as C4 has more than 1 significant digit |
| 2132 |
// and C3 * 10^scale = 10^33 |
| 2133 |
// example of case that is treated here: |
| 2134 |
// 100...0 * 10^e3 - 0.41 * 10^e3 = |
| 2135 |
// 0999...9.59 * 10^e3 -> rounds to 99...96*10^(e3-1) |
| 2136 |
// note that (e3 > expmin} |
| 2137 |
// in order to round, subtract R64 from 10^34 and then compare |
| 2138 |
// C4 - R64 * 10^(q4-1) with 1/2 ulp |
| 2139 |
// calculate 10^34 - R64 |
| 2140 |
res.w[1] = 0x0001ed09bead87c0ull; |
| 2141 |
res.w[0] = 0x378d8e6400000000ull - R64; |
| 2142 |
z_exp = z_exp - EXP_P1; // will be OR-ed with sign & significand |
| 2143 |
// calculate C4 - R64 * 10^(q4-1); this is a rare case and |
| 2144 |
// R64 is small, 1 <= R64 <= 9 |
| 2145 |
e3 = e3 - 1; |
| 2146 |
if (is_inexact_lt_midpoint) { |
| 2147 |
is_inexact_lt_midpoint = 0; |
| 2148 |
is_inexact_gt_midpoint = 1; |
| 2149 |
} else if (is_inexact_gt_midpoint) { |
| 2150 |
is_inexact_gt_midpoint = 0; |
| 2151 |
is_inexact_lt_midpoint = 1; |
| 2152 |
} else if (is_midpoint_lt_even) { |
| 2153 |
is_midpoint_lt_even = 0; |
| 2154 |
is_midpoint_gt_even = 1; |
| 2155 |
} else if (is_midpoint_gt_even) { |
| 2156 |
is_midpoint_gt_even = 0; |
| 2157 |
is_midpoint_lt_even = 1; |
| 2158 |
} else { |
| 2159 |
; |
| 2160 |
} |
| 2161 |
// the result is always inexact, and never tiny |
| 2162 |
// check for overflow for RN |
| 2163 |
if (e3 > expmax) { |
| 2164 |
if (rnd_mode == ROUNDING_TO_NEAREST) { |
| 2165 |
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf |
| 2166 |
res.w[0] = 0x0000000000000000ull; |
| 2167 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 2168 |
} else { |
| 2169 |
rounding_correction (rnd_mode, |
| 2170 |
is_inexact_lt_midpoint, |
| 2171 |
is_inexact_gt_midpoint, |
| 2172 |
is_midpoint_lt_even, |
| 2173 |
is_midpoint_gt_even, e3, &res, |
| 2174 |
pfpsf); |
| 2175 |
} |
| 2176 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2177 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2178 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2179 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2180 |
BID_SWAP128 (res); |
| 2181 |
BID_RETURN (res) |
| 2182 |
} |
| 2183 |
// set the inexact flag |
| 2184 |
*pfpsf |= INEXACT_EXCEPTION; |
| 2185 |
res.w[1] = |
| 2186 |
z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1]; |
| 2187 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 2188 |
rounding_correction (rnd_mode, |
| 2189 |
is_inexact_lt_midpoint, |
| 2190 |
is_inexact_gt_midpoint, |
| 2191 |
is_midpoint_lt_even, |
| 2192 |
is_midpoint_gt_even, e3, &res, |
| 2193 |
pfpsf); |
| 2194 |
} |
| 2195 |
z_exp = res.w[1] & MASK_EXP; |
| 2196 |
} // end result is inexact |
| 2197 |
} // end q4 > 1 |
| 2198 |
} else { // if (e3 = emin) |
| 2199 |
// if e3 = expmin the result is also tiny (the condition for |
| 2200 |
// tininess is C4 > 050...0 [q4 digits] which is met because |
| 2201 |
// the msd of C4 is not zero) |
| 2202 |
// the result is tiny and inexact in all rounding modes; |
| 2203 |
// it is either 100...0 or 0999...9 (use lt_half_ulp, eq_half_ulp, |
| 2204 |
// gt_half_ulp to calculate) |
| 2205 |
// if (lt_half_ulp || eq_half_ulp) res = 10^33 stays unchanged |
| 2206 |
|
| 2207 |
// p_sign != z_sign so swap gt_half_ulp and lt_half_ulp |
| 2208 |
if (gt_half_ulp) { // res = 10^33 - 1 |
| 2209 |
res.w[1] = 0x0000314dc6448d93ull; |
| 2210 |
res.w[0] = 0x38c15b09ffffffffull; |
| 2211 |
} else { |
| 2212 |
res.w[1] = 0x0000314dc6448d93ull; |
| 2213 |
res.w[0] = 0x38c15b0a00000000ull; |
| 2214 |
} |
| 2215 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2216 |
*pfpsf |= UNDERFLOW_EXCEPTION; // inexact is set later |
| 2217 |
|
| 2218 |
if (eq_half_ulp) { |
| 2219 |
is_midpoint_lt_even = 1; // if (z_sign), as if for absolute value |
| 2220 |
} else if (lt_half_ulp) { |
| 2221 |
is_inexact_gt_midpoint = 1; //if(z_sign), as if for absolute value |
| 2222 |
} else { // if (gt_half_ulp) |
| 2223 |
is_inexact_lt_midpoint = 1; //if(z_sign), as if for absolute value |
| 2224 |
} |
| 2225 |
|
| 2226 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 2227 |
rounding_correction (rnd_mode, |
| 2228 |
is_inexact_lt_midpoint, |
| 2229 |
is_inexact_gt_midpoint, |
| 2230 |
is_midpoint_lt_even, |
| 2231 |
is_midpoint_gt_even, e3, &res, |
| 2232 |
pfpsf); |
| 2233 |
z_exp = res.w[1] & MASK_EXP; |
| 2234 |
} |
| 2235 |
} // end e3 = emin |
| 2236 |
// set the inexact flag (if the result was not exact) |
| 2237 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 2238 |
is_midpoint_lt_even || is_midpoint_gt_even) |
| 2239 |
*pfpsf |= INEXACT_EXCEPTION; |
| 2240 |
} // end 10^33 |
| 2241 |
} // end if (p_sign != z_sign) |
| 2242 |
res.w[1] = z_sign | (z_exp & MASK_EXP) | res.w[1]; |
| 2243 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2244 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2245 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2246 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2247 |
BID_SWAP128 (res); |
| 2248 |
BID_RETURN (res) |
| 2249 |
|
| 2250 |
} else if (((q3 <= delta && delta < p34 && p34 < delta + q4) || // Case (2) |
| 2251 |
(q3 <= delta && delta + q4 <= p34) || // Case (3) |
| 2252 |
(delta < q3 && p34 < delta + q4) || // Case (4) |
| 2253 |
(delta < q3 && q3 <= delta + q4 && delta + q4 <= p34) || // Case (5) |
| 2254 |
(delta + q4 < q3)) && // Case (6) |
| 2255 |
!(delta <= 1 && p_sign != z_sign)) { // Case (2), (3), (4), (5) or (6) |
| 2256 |
|
| 2257 |
// the result has the sign of z |
| 2258 |
|
| 2259 |
if ((q3 <= delta && delta < p34 && p34 < delta + q4) || // Case (2) |
| 2260 |
(delta < q3 && p34 < delta + q4)) { // Case (4) |
| 2261 |
// round first the sum x * y + z with unbounded exponent |
| 2262 |
// scale C3 up by scale = p34 - q3, 1 <= scale <= p34-1, |
| 2263 |
// 1 <= scale <= 33 |
| 2264 |
// calculate res = C3 * 10^scale |
| 2265 |
scale = p34 - q3; |
| 2266 |
x0 = delta + q4 - p34; |
| 2267 |
} else if (delta + q4 < q3) { // Case (6) |
| 2268 |
// make Case (6) look like Case (3) or Case (5) with scale = 0 |
| 2269 |
// by scaling up C4 by 10^(q3 - delta - q4) |
| 2270 |
scale = q3 - delta - q4; // 1 <= scale <= 33 |
| 2271 |
if (q4 <= 19) { // 1 <= scale <= 19; C4 fits in 64 bits |
| 2272 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 2273 |
// 64 x 64 C4.w[0] * ten2k64[scale] |
| 2274 |
__mul_64x64_to_128MACH (P128, C4.w[0], ten2k64[scale]); |
| 2275 |
} else { // 10^scale fits in 128 bits |
| 2276 |
// 64 x 128 C4.w[0] * ten2k128[scale - 20] |
| 2277 |
__mul_128x64_to_128 (P128, C4.w[0], ten2k128[scale - 20]); |
| 2278 |
} |
| 2279 |
} else { // C4 fits in 128 bits, but 10^scale must fit in 64 bits |
| 2280 |
// 64 x 128 ten2k64[scale] * C4 |
| 2281 |
__mul_128x64_to_128 (P128, ten2k64[scale], C4); |
| 2282 |
} |
| 2283 |
C4.w[0] = P128.w[0]; |
| 2284 |
C4.w[1] = P128.w[1]; |
| 2285 |
// e4 does not need adjustment, as it is not used from this point on |
| 2286 |
scale = 0; |
| 2287 |
x0 = 0; |
| 2288 |
// now Case (6) looks like Case (3) or Case (5) with scale = 0 |
| 2289 |
} else { // if Case (3) or Case (5) |
| 2290 |
// Note: Case (3) is similar to Case (2), but scale differs and the |
| 2291 |
// result is exact, unless it is tiny (so x0 = 0 when calculating the |
| 2292 |
// result with unbounded exponent) |
| 2293 |
|
| 2294 |
// calculate first the sum x * y + z with unbounded exponent (exact) |
| 2295 |
// scale C3 up by scale = delta + q4 - q3, 1 <= scale <= p34-1, |
| 2296 |
// 1 <= scale <= 33 |
| 2297 |
// calculate res = C3 * 10^scale |
| 2298 |
scale = delta + q4 - q3; |
| 2299 |
x0 = 0; |
| 2300 |
// Note: the comments which follow refer [mainly] to Case (2)] |
| 2301 |
} |
| 2302 |
|
| 2303 |
case2_repeat: |
| 2304 |
if (scale == 0) { // this could happen e.g. if we return to case2_repeat |
| 2305 |
// or in Case (4) |
| 2306 |
res.w[1] = C3.w[1]; |
| 2307 |
res.w[0] = C3.w[0]; |
| 2308 |
} else if (q3 <= 19) { // 1 <= scale <= 19; z fits in 64 bits |
| 2309 |
if (scale <= 19) { // 10^scale fits in 64 bits |
| 2310 |
// 64 x 64 C3.w[0] * ten2k64[scale] |
| 2311 |
__mul_64x64_to_128MACH (res, C3.w[0], ten2k64[scale]); |
| 2312 |
} else { // 10^scale fits in 128 bits |
| 2313 |
// 64 x 128 C3.w[0] * ten2k128[scale - 20] |
| 2314 |
__mul_128x64_to_128 (res, C3.w[0], ten2k128[scale - 20]); |
| 2315 |
} |
| 2316 |
} else { // z fits in 128 bits, but 10^scale must fit in 64 bits |
| 2317 |
// 64 x 128 ten2k64[scale] * C3 |
| 2318 |
__mul_128x64_to_128 (res, ten2k64[scale], C3); |
| 2319 |
} |
| 2320 |
// e3 is already calculated |
| 2321 |
e3 = e3 - scale; |
| 2322 |
// now res = C3 * 10^scale and e3 = e3 - scale |
| 2323 |
// Note: C3 * 10^scale could be 10^34 if we returned to case2_repeat |
| 2324 |
// because the result was too small |
| 2325 |
|
| 2326 |
// round C4 to nearest to q4 - x0 digits, where x0 = delta + q4 - p34, |
| 2327 |
// 1 <= x0 <= min (q4 - 1, 2 * p34 - 1) <=> 1 <= x0 <= min (q4 - 1, 67) |
| 2328 |
// Also: 1 <= q4 - x0 <= p34 -1 => 1 <= q4 - x0 <= 33 (so the result of |
| 2329 |
// the rounding fits in 128 bits!) |
| 2330 |
// x0 = delta + q4 - p34 (calculated before reaching case2_repeat) |
| 2331 |
// because q3 + q4 - x0 <= P => x0 >= q3 + q4 - p34 |
| 2332 |
if (x0 == 0) { // this could happen only if we return to case2_repeat, or |
| 2333 |
// for Case (3) or Case (6) |
| 2334 |
R128.w[1] = C4.w[1]; |
| 2335 |
R128.w[0] = C4.w[0]; |
| 2336 |
} else if (q4 <= 18) { |
| 2337 |
// 2 <= q4 <= 18, max(1, q3+q4-p34) <= x0 <= q4 - 1, 1 <= x0 <= 17 |
| 2338 |
round64_2_18 (q4, x0, C4.w[0], &R64, &incr_exp, |
| 2339 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2340 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 2341 |
if (incr_exp) { |
| 2342 |
// R64 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 17 |
| 2343 |
R64 = ten2k64[q4 - x0]; |
| 2344 |
} |
| 2345 |
R128.w[1] = 0; |
| 2346 |
R128.w[0] = R64; |
| 2347 |
} else if (q4 <= 38) { |
| 2348 |
// 19 <= q4 <= 38, max(1, q3+q4-p34) <= x0 <= q4 - 1, 1 <= x0 <= 37 |
| 2349 |
P128.w[1] = C4.w[1]; |
| 2350 |
P128.w[0] = C4.w[0]; |
| 2351 |
round128_19_38 (q4, x0, P128, &R128, &incr_exp, |
| 2352 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2353 |
&is_inexact_lt_midpoint, |
| 2354 |
&is_inexact_gt_midpoint); |
| 2355 |
if (incr_exp) { |
| 2356 |
// R128 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 1, 1 <= q4 - x0 <= 37 |
| 2357 |
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19 |
| 2358 |
R128.w[0] = ten2k64[q4 - x0]; |
| 2359 |
// R128.w[1] stays 0 |
| 2360 |
} else { // 20 <= q4 - x0 <= 37 |
| 2361 |
R128.w[0] = ten2k128[q4 - x0 - 20].w[0]; |
| 2362 |
R128.w[1] = ten2k128[q4 - x0 - 20].w[1]; |
| 2363 |
} |
| 2364 |
} |
| 2365 |
} else if (q4 <= 57) { |
| 2366 |
// 38 <= q4 <= 57, max(1, q3+q4-p34) <= x0 <= q4 - 1, 5 <= x0 <= 56 |
| 2367 |
P192.w[2] = C4.w[2]; |
| 2368 |
P192.w[1] = C4.w[1]; |
| 2369 |
P192.w[0] = C4.w[0]; |
| 2370 |
round192_39_57 (q4, x0, P192, &R192, &incr_exp, |
| 2371 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2372 |
&is_inexact_lt_midpoint, |
| 2373 |
&is_inexact_gt_midpoint); |
| 2374 |
// R192.w[2] is always 0 |
| 2375 |
if (incr_exp) { |
| 2376 |
// R192 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 5, 1 <= q4 - x0 <= 52 |
| 2377 |
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19 |
| 2378 |
R192.w[0] = ten2k64[q4 - x0]; |
| 2379 |
// R192.w[1] stays 0 |
| 2380 |
// R192.w[2] stays 0 |
| 2381 |
} else { // 20 <= q4 - x0 <= 33 |
| 2382 |
R192.w[0] = ten2k128[q4 - x0 - 20].w[0]; |
| 2383 |
R192.w[1] = ten2k128[q4 - x0 - 20].w[1]; |
| 2384 |
// R192.w[2] stays 0 |
| 2385 |
} |
| 2386 |
} |
| 2387 |
R128.w[1] = R192.w[1]; |
| 2388 |
R128.w[0] = R192.w[0]; |
| 2389 |
} else { |
| 2390 |
// 58 <= q4 <= 68, max(1, q3+q4-p34) <= x0 <= q4 - 1, 25 <= x0 <= 67 |
| 2391 |
round256_58_76 (q4, x0, C4, &R256, &incr_exp, |
| 2392 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2393 |
&is_inexact_lt_midpoint, |
| 2394 |
&is_inexact_gt_midpoint); |
| 2395 |
// R256.w[3] and R256.w[2] are always 0 |
| 2396 |
if (incr_exp) { |
| 2397 |
// R256 = 10^(q4-x0), 1 <= q4 - x0 <= q4 - 25, 1 <= q4 - x0 <= 43 |
| 2398 |
if (q4 - x0 <= 19) { // 1 <= q4 - x0 <= 19 |
| 2399 |
R256.w[0] = ten2k64[q4 - x0]; |
| 2400 |
// R256.w[1] stays 0 |
| 2401 |
// R256.w[2] stays 0 |
| 2402 |
// R256.w[3] stays 0 |
| 2403 |
} else { // 20 <= q4 - x0 <= 33 |
| 2404 |
R256.w[0] = ten2k128[q4 - x0 - 20].w[0]; |
| 2405 |
R256.w[1] = ten2k128[q4 - x0 - 20].w[1]; |
| 2406 |
// R256.w[2] stays 0 |
| 2407 |
// R256.w[3] stays 0 |
| 2408 |
} |
| 2409 |
} |
| 2410 |
R128.w[1] = R256.w[1]; |
| 2411 |
R128.w[0] = R256.w[0]; |
| 2412 |
} |
| 2413 |
// now add C3 * 10^scale in res and the signed top (q4-x0) digits of C4, |
| 2414 |
// rounded to nearest, which were copied into R128 |
| 2415 |
if (z_sign == p_sign) { |
| 2416 |
lsb = res.w[0] & 0x01; // lsb of C3 * 10^scale |
| 2417 |
// the sum can result in [up to] p34 or p34 + 1 digits |
| 2418 |
res.w[0] = res.w[0] + R128.w[0]; |
| 2419 |
res.w[1] = res.w[1] + R128.w[1]; |
| 2420 |
if (res.w[0] < R128.w[0]) |
| 2421 |
res.w[1]++; // carry |
| 2422 |
// if res > 10^34 - 1 need to increase x0 and decrease scale by 1 |
| 2423 |
if (res.w[1] > 0x0001ed09bead87c0ull || |
| 2424 |
(res.w[1] == 0x0001ed09bead87c0ull && |
| 2425 |
res.w[0] > 0x378d8e63ffffffffull)) { |
| 2426 |
// avoid double rounding error |
| 2427 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 2428 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 2429 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 2430 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 2431 |
is_inexact_lt_midpoint = 0; |
| 2432 |
is_inexact_gt_midpoint = 0; |
| 2433 |
is_midpoint_lt_even = 0; |
| 2434 |
is_midpoint_gt_even = 0; |
| 2435 |
P128.w[1] = res.w[1]; |
| 2436 |
P128.w[0] = res.w[0]; |
| 2437 |
round128_19_38 (35, 1, P128, &res, &incr_exp, |
| 2438 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2439 |
&is_inexact_lt_midpoint, |
| 2440 |
&is_inexact_gt_midpoint); |
| 2441 |
// incr_exp is 0 with certainty in this case |
| 2442 |
// avoid a double rounding error |
| 2443 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 2444 |
is_midpoint_lt_even) { // double rounding error upward |
| 2445 |
// res = res - 1 |
| 2446 |
res.w[0]--; |
| 2447 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2448 |
res.w[1]--; |
| 2449 |
// Note: a double rounding error upward is not possible; for this |
| 2450 |
// the result after the first rounding would have to be 99...95 |
| 2451 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 2452 |
// not possible in Cases (2)-(6) or (15)-(17) which may get here |
| 2453 |
is_midpoint_lt_even = 0; |
| 2454 |
is_inexact_lt_midpoint = 1; |
| 2455 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 2456 |
is_midpoint_gt_even) { // double rounding error downward |
| 2457 |
// res = res + 1 |
| 2458 |
res.w[0]++; |
| 2459 |
if (res.w[0] == 0) |
| 2460 |
res.w[1]++; |
| 2461 |
is_midpoint_gt_even = 0; |
| 2462 |
is_inexact_gt_midpoint = 1; |
| 2463 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2464 |
!is_inexact_lt_midpoint |
| 2465 |
&& !is_inexact_gt_midpoint) { |
| 2466 |
// if this second rounding was exact the result may still be |
| 2467 |
// inexact because of the first rounding |
| 2468 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 2469 |
is_inexact_gt_midpoint = 1; |
| 2470 |
} |
| 2471 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 2472 |
is_inexact_lt_midpoint = 1; |
| 2473 |
} |
| 2474 |
} else if (is_midpoint_gt_even && |
| 2475 |
(is_inexact_gt_midpoint0 |
| 2476 |
|| is_midpoint_lt_even0)) { |
| 2477 |
// pulled up to a midpoint |
| 2478 |
is_inexact_lt_midpoint = 1; |
| 2479 |
is_inexact_gt_midpoint = 0; |
| 2480 |
is_midpoint_lt_even = 0; |
| 2481 |
is_midpoint_gt_even = 0; |
| 2482 |
} else if (is_midpoint_lt_even && |
| 2483 |
(is_inexact_lt_midpoint0 |
| 2484 |
|| is_midpoint_gt_even0)) { |
| 2485 |
// pulled down to a midpoint |
| 2486 |
is_inexact_lt_midpoint = 0; |
| 2487 |
is_inexact_gt_midpoint = 1; |
| 2488 |
is_midpoint_lt_even = 0; |
| 2489 |
is_midpoint_gt_even = 0; |
| 2490 |
} else { |
| 2491 |
; |
| 2492 |
} |
| 2493 |
// adjust exponent |
| 2494 |
e3 = e3 + 1; |
| 2495 |
if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2496 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 2497 |
if (is_midpoint_lt_even0 || is_midpoint_gt_even0 || |
| 2498 |
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0) { |
| 2499 |
is_inexact_lt_midpoint = 1; |
| 2500 |
} |
| 2501 |
} |
| 2502 |
} else { |
| 2503 |
// this is the result rounded with unbounded exponent, unless a |
| 2504 |
// correction is needed |
| 2505 |
res.w[1] = res.w[1] & MASK_COEFF; |
| 2506 |
if (lsb == 1) { |
| 2507 |
if (is_midpoint_gt_even) { |
| 2508 |
// res = res + 1 |
| 2509 |
is_midpoint_gt_even = 0; |
| 2510 |
is_midpoint_lt_even = 1; |
| 2511 |
res.w[0]++; |
| 2512 |
if (res.w[0] == 0x0) |
| 2513 |
res.w[1]++; |
| 2514 |
// check for rounding overflow |
| 2515 |
if (res.w[1] == 0x0001ed09bead87c0ull && |
| 2516 |
res.w[0] == 0x378d8e6400000000ull) { |
| 2517 |
// res = 10^34 => rounding overflow |
| 2518 |
res.w[1] = 0x0000314dc6448d93ull; |
| 2519 |
res.w[0] = 0x38c15b0a00000000ull; // 10^33 |
| 2520 |
e3++; |
| 2521 |
} |
| 2522 |
} else if (is_midpoint_lt_even) { |
| 2523 |
// res = res - 1 |
| 2524 |
is_midpoint_lt_even = 0; |
| 2525 |
is_midpoint_gt_even = 1; |
| 2526 |
res.w[0]--; |
| 2527 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2528 |
res.w[1]--; |
| 2529 |
// if the result is pure zero, the sign depends on the rounding |
| 2530 |
// mode (x*y and z had opposite signs) |
| 2531 |
if (res.w[1] == 0x0ull && res.w[0] == 0x0ull) { |
| 2532 |
if (rnd_mode != ROUNDING_DOWN) |
| 2533 |
z_sign = 0x0000000000000000ull; |
| 2534 |
else |
| 2535 |
z_sign = 0x8000000000000000ull; |
| 2536 |
// the exponent is max (e3, expmin) |
| 2537 |
res.w[1] = 0x0; |
| 2538 |
res.w[0] = 0x0; |
| 2539 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2540 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2541 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2542 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2543 |
BID_SWAP128 (res); |
| 2544 |
BID_RETURN (res) |
| 2545 |
} |
| 2546 |
} else { |
| 2547 |
; |
| 2548 |
} |
| 2549 |
} |
| 2550 |
} |
| 2551 |
} else { // if (z_sign != p_sign) |
| 2552 |
lsb = res.w[0] & 0x01; // lsb of C3 * 10^scale; R128 contains rounded C4 |
| 2553 |
// used to swap rounding indicators if p_sign != z_sign |
| 2554 |
// the sum can result in [up to] p34 or p34 - 1 digits |
| 2555 |
tmp64 = res.w[0]; |
| 2556 |
res.w[0] = res.w[0] - R128.w[0]; |
| 2557 |
res.w[1] = res.w[1] - R128.w[1]; |
| 2558 |
if (res.w[0] > tmp64) |
| 2559 |
res.w[1]--; // borrow |
| 2560 |
// if res < 10^33 and exp > expmin need to decrease x0 and |
| 2561 |
// increase scale by 1 |
| 2562 |
if (e3 > expmin && ((res.w[1] < 0x0000314dc6448d93ull || |
| 2563 |
(res.w[1] == 0x0000314dc6448d93ull && |
| 2564 |
res.w[0] < 0x38c15b0a00000000ull)) || |
| 2565 |
((is_inexact_lt_midpoint | is_midpoint_gt_even) |
| 2566 |
&& res.w[1] == 0x0000314dc6448d93ull |
| 2567 |
&& res.w[0] == 0x38c15b0a00000000ull)) |
| 2568 |
&& x0 >= 1) { |
| 2569 |
x0 = x0 - 1; |
| 2570 |
// first restore e3, otherwise it will be too small |
| 2571 |
e3 = e3 + scale; |
| 2572 |
scale = scale + 1; |
| 2573 |
is_inexact_lt_midpoint = 0; |
| 2574 |
is_inexact_gt_midpoint = 0; |
| 2575 |
is_midpoint_lt_even = 0; |
| 2576 |
is_midpoint_gt_even = 0; |
| 2577 |
incr_exp = 0; |
| 2578 |
goto case2_repeat; |
| 2579 |
} |
| 2580 |
// else this is the result rounded with unbounded exponent; |
| 2581 |
// because the result has opposite sign to that of C4 which was |
| 2582 |
// rounded, need to change the rounding indicators |
| 2583 |
if (is_inexact_lt_midpoint) { |
| 2584 |
is_inexact_lt_midpoint = 0; |
| 2585 |
is_inexact_gt_midpoint = 1; |
| 2586 |
} else if (is_inexact_gt_midpoint) { |
| 2587 |
is_inexact_gt_midpoint = 0; |
| 2588 |
is_inexact_lt_midpoint = 1; |
| 2589 |
} else if (lsb == 0) { |
| 2590 |
if (is_midpoint_lt_even) { |
| 2591 |
is_midpoint_lt_even = 0; |
| 2592 |
is_midpoint_gt_even = 1; |
| 2593 |
} else if (is_midpoint_gt_even) { |
| 2594 |
is_midpoint_gt_even = 0; |
| 2595 |
is_midpoint_lt_even = 1; |
| 2596 |
} else { |
| 2597 |
; |
| 2598 |
} |
| 2599 |
} else if (lsb == 1) { |
| 2600 |
if (is_midpoint_lt_even) { |
| 2601 |
// res = res + 1 |
| 2602 |
res.w[0]++; |
| 2603 |
if (res.w[0] == 0x0) |
| 2604 |
res.w[1]++; |
| 2605 |
// check for rounding overflow |
| 2606 |
if (res.w[1] == 0x0001ed09bead87c0ull && |
| 2607 |
res.w[0] == 0x378d8e6400000000ull) { |
| 2608 |
// res = 10^34 => rounding overflow |
| 2609 |
res.w[1] = 0x0000314dc6448d93ull; |
| 2610 |
res.w[0] = 0x38c15b0a00000000ull; // 10^33 |
| 2611 |
e3++; |
| 2612 |
} |
| 2613 |
} else if (is_midpoint_gt_even) { |
| 2614 |
// res = res - 1 |
| 2615 |
res.w[0]--; |
| 2616 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2617 |
res.w[1]--; |
| 2618 |
// if the result is pure zero, the sign depends on the rounding |
| 2619 |
// mode (x*y and z had opposite signs) |
| 2620 |
if (res.w[1] == 0x0ull && res.w[0] == 0x0ull) { |
| 2621 |
if (rnd_mode != ROUNDING_DOWN) |
| 2622 |
z_sign = 0x0000000000000000ull; |
| 2623 |
else |
| 2624 |
z_sign = 0x8000000000000000ull; |
| 2625 |
// the exponent is max (e3, expmin) |
| 2626 |
res.w[1] = 0x0; |
| 2627 |
res.w[0] = 0x0; |
| 2628 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2629 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2630 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2631 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2632 |
BID_SWAP128 (res); |
| 2633 |
BID_RETURN (res) |
| 2634 |
} |
| 2635 |
} else { |
| 2636 |
; |
| 2637 |
} |
| 2638 |
} else { |
| 2639 |
; |
| 2640 |
} |
| 2641 |
} |
| 2642 |
// check for underflow |
| 2643 |
if (e3 == expmin) { // and if significand < 10^33 => result is tiny |
| 2644 |
if ((res.w[1] & MASK_COEFF) < 0x0000314dc6448d93ull || |
| 2645 |
((res.w[1] & MASK_COEFF) == 0x0000314dc6448d93ull && |
| 2646 |
res.w[0] < 0x38c15b0a00000000ull)) { |
| 2647 |
is_tiny = 1; |
| 2648 |
} |
| 2649 |
if (((res.w[1] & 0x7fffffffffffffffull) == 0x0000314dc6448d93ull) && |
| 2650 |
(res.w[0] == 0x38c15b0a00000000ull) && // 10^33*10^-6176 |
| 2651 |
(z_sign != p_sign)) is_tiny = 1; |
| 2652 |
} else if (e3 < expmin) { |
| 2653 |
// the result is tiny, so we must truncate more of res |
| 2654 |
is_tiny = 1; |
| 2655 |
x0 = expmin - e3; |
| 2656 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 2657 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 2658 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 2659 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 2660 |
is_inexact_lt_midpoint = 0; |
| 2661 |
is_inexact_gt_midpoint = 0; |
| 2662 |
is_midpoint_lt_even = 0; |
| 2663 |
is_midpoint_gt_even = 0; |
| 2664 |
// determine the number of decimal digits in res |
| 2665 |
if (res.w[1] == 0x0) { |
| 2666 |
// between 1 and 19 digits |
| 2667 |
for (ind = 1; ind <= 19; ind++) { |
| 2668 |
if (res.w[0] < ten2k64[ind]) { |
| 2669 |
break; |
| 2670 |
} |
| 2671 |
} |
| 2672 |
// ind digits |
| 2673 |
} else if (res.w[1] < ten2k128[0].w[1] || |
| 2674 |
(res.w[1] == ten2k128[0].w[1] |
| 2675 |
&& res.w[0] < ten2k128[0].w[0])) { |
| 2676 |
// 20 digits |
| 2677 |
ind = 20; |
| 2678 |
} else { // between 21 and 38 digits |
| 2679 |
for (ind = 1; ind <= 18; ind++) { |
| 2680 |
if (res.w[1] < ten2k128[ind].w[1] || |
| 2681 |
(res.w[1] == ten2k128[ind].w[1] && |
| 2682 |
res.w[0] < ten2k128[ind].w[0])) { |
| 2683 |
break; |
| 2684 |
} |
| 2685 |
} |
| 2686 |
// ind + 20 digits |
| 2687 |
ind = ind + 20; |
| 2688 |
} |
| 2689 |
|
| 2690 |
// at this point ind >= x0; because delta >= 2 on this path, the case |
| 2691 |
// ind = x0 can occur only in Case (2) or case (3), when C3 has one |
| 2692 |
// digit (q3 = 1) equal to 1 (C3 = 1), e3 is expmin (e3 = expmin), |
| 2693 |
// the signs of x * y and z are opposite, and through cancellation |
| 2694 |
// the most significant decimal digit in res has the weight |
| 2695 |
// 10^(emin-1); however, it is clear that in this case the most |
| 2696 |
// significant digit is 9, so the result before rounding is |
| 2697 |
// 0.9... * 10^emin |
| 2698 |
// Otherwise, ind > x0 because there are non-zero decimal digits in the |
| 2699 |
// result with weight of at least 10^emin, and correction for underflow |
| 2700 |
// can be carried out using the round*_*_2_* () routines |
| 2701 |
if (x0 == ind) { // the result before rounding is 0.9... * 10^emin |
| 2702 |
res.w[1] = 0x0; |
| 2703 |
res.w[0] = 0x1; |
| 2704 |
is_inexact_gt_midpoint = 1; |
| 2705 |
} else if (ind <= 18) { // check that 2 <= ind |
| 2706 |
// 2 <= ind <= 18, 1 <= x0 <= 17 |
| 2707 |
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp, |
| 2708 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2709 |
&is_inexact_lt_midpoint, |
| 2710 |
&is_inexact_gt_midpoint); |
| 2711 |
if (incr_exp) { |
| 2712 |
// R64 = 10^(ind-x0), 1 <= ind - x0 <= ind - 1, 1 <= ind - x0 <= 17 |
| 2713 |
R64 = ten2k64[ind - x0]; |
| 2714 |
} |
| 2715 |
res.w[1] = 0; |
| 2716 |
res.w[0] = R64; |
| 2717 |
} else if (ind <= 38) { |
| 2718 |
// 19 <= ind <= 38 |
| 2719 |
P128.w[1] = res.w[1]; |
| 2720 |
P128.w[0] = res.w[0]; |
| 2721 |
round128_19_38 (ind, x0, P128, &res, &incr_exp, |
| 2722 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2723 |
&is_inexact_lt_midpoint, |
| 2724 |
&is_inexact_gt_midpoint); |
| 2725 |
if (incr_exp) { |
| 2726 |
// R128 = 10^(ind-x0), 1 <= ind - x0 <= ind - 1, 1 <= ind - x0 <= 37 |
| 2727 |
if (ind - x0 <= 19) { // 1 <= ind - x0 <= 19 |
| 2728 |
res.w[0] = ten2k64[ind - x0]; |
| 2729 |
// res.w[1] stays 0 |
| 2730 |
} else { // 20 <= ind - x0 <= 37 |
| 2731 |
res.w[0] = ten2k128[ind - x0 - 20].w[0]; |
| 2732 |
res.w[1] = ten2k128[ind - x0 - 20].w[1]; |
| 2733 |
} |
| 2734 |
} |
| 2735 |
} |
| 2736 |
// avoid a double rounding error |
| 2737 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 2738 |
is_midpoint_lt_even) { // double rounding error upward |
| 2739 |
// res = res - 1 |
| 2740 |
res.w[0]--; |
| 2741 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2742 |
res.w[1]--; |
| 2743 |
// Note: a double rounding error upward is not possible; for this |
| 2744 |
// the result after the first rounding would have to be 99...95 |
| 2745 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 2746 |
// not possible in Cases (2)-(6) which may get here |
| 2747 |
is_midpoint_lt_even = 0; |
| 2748 |
is_inexact_lt_midpoint = 1; |
| 2749 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 2750 |
is_midpoint_gt_even) { // double rounding error downward |
| 2751 |
// res = res + 1 |
| 2752 |
res.w[0]++; |
| 2753 |
if (res.w[0] == 0) |
| 2754 |
res.w[1]++; |
| 2755 |
is_midpoint_gt_even = 0; |
| 2756 |
is_inexact_gt_midpoint = 1; |
| 2757 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2758 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 2759 |
// if this second rounding was exact the result may still be |
| 2760 |
// inexact because of the first rounding |
| 2761 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 2762 |
is_inexact_gt_midpoint = 1; |
| 2763 |
} |
| 2764 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 2765 |
is_inexact_lt_midpoint = 1; |
| 2766 |
} |
| 2767 |
} else if (is_midpoint_gt_even && |
| 2768 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 2769 |
// pulled up to a midpoint |
| 2770 |
is_inexact_lt_midpoint = 1; |
| 2771 |
is_inexact_gt_midpoint = 0; |
| 2772 |
is_midpoint_lt_even = 0; |
| 2773 |
is_midpoint_gt_even = 0; |
| 2774 |
} else if (is_midpoint_lt_even && |
| 2775 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 2776 |
// pulled down to a midpoint |
| 2777 |
is_inexact_lt_midpoint = 0; |
| 2778 |
is_inexact_gt_midpoint = 1; |
| 2779 |
is_midpoint_lt_even = 0; |
| 2780 |
is_midpoint_gt_even = 0; |
| 2781 |
} else { |
| 2782 |
; |
| 2783 |
} |
| 2784 |
// adjust exponent |
| 2785 |
e3 = e3 + x0; |
| 2786 |
if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2787 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 2788 |
if (is_midpoint_lt_even0 || is_midpoint_gt_even0 || |
| 2789 |
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0) { |
| 2790 |
is_inexact_lt_midpoint = 1; |
| 2791 |
} |
| 2792 |
} |
| 2793 |
} else { |
| 2794 |
; // not underflow |
| 2795 |
} |
| 2796 |
// check for inexact result |
| 2797 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 2798 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 2799 |
// set the inexact flag |
| 2800 |
*pfpsf |= INEXACT_EXCEPTION; |
| 2801 |
if (is_tiny) |
| 2802 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 2803 |
} |
| 2804 |
// now check for significand = 10^34 (may have resulted from going |
| 2805 |
// back to case2_repeat) |
| 2806 |
if (res.w[1] == 0x0001ed09bead87c0ull && |
| 2807 |
res.w[0] == 0x378d8e6400000000ull) { // if res = 10^34 |
| 2808 |
res.w[1] = 0x0000314dc6448d93ull; // res = 10^33 |
| 2809 |
res.w[0] = 0x38c15b0a00000000ull; |
| 2810 |
e3 = e3 + 1; |
| 2811 |
} |
| 2812 |
res.w[1] = z_sign | ((UINT64) (e3 + 6176) << 49) | res.w[1]; |
| 2813 |
// check for overflow |
| 2814 |
if (rnd_mode == ROUNDING_TO_NEAREST && e3 > expmax) { |
| 2815 |
res.w[1] = z_sign | 0x7800000000000000ull; // +/-inf |
| 2816 |
res.w[0] = 0x0000000000000000ull; |
| 2817 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 2818 |
} |
| 2819 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 2820 |
rounding_correction (rnd_mode, |
| 2821 |
is_inexact_lt_midpoint, |
| 2822 |
is_inexact_gt_midpoint, |
| 2823 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 2824 |
e3, &res, pfpsf); |
| 2825 |
} |
| 2826 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2827 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2828 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2829 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2830 |
BID_SWAP128 (res); |
| 2831 |
BID_RETURN (res) |
| 2832 |
|
| 2833 |
} else { |
| 2834 |
|
| 2835 |
// we get here only if delta <= 1 in Cases (2), (3), (4), (5), or (6) and |
| 2836 |
// the signs of x*y and z are opposite; in these cases massive |
| 2837 |
// cancellation can occur, so it is better to scale either C3 or C4 and |
| 2838 |
// to perform the subtraction before rounding; rounding is performed |
| 2839 |
// next, depending on the number of decimal digits in the result and on |
| 2840 |
// the exponent value |
| 2841 |
// Note: overlow is not possible in this case |
| 2842 |
// this is similar to Cases (15), (16), and (17) |
| 2843 |
|
| 2844 |
if (delta + q4 < q3) { // from Case (6) |
| 2845 |
// Case (6) with 0<= delta <= 1 is similar to Cases (15), (16), and |
| 2846 |
// (17) if we swap (C3, C4), (q3, q4), (e3, e4), (z_sign, p_sign) |
| 2847 |
// and call add_and_round; delta stays positive |
| 2848 |
// C4.w[3] = 0 and C4.w[2] = 0, so swap just the low part of C4 with C3 |
| 2849 |
P128.w[1] = C3.w[1]; |
| 2850 |
P128.w[0] = C3.w[0]; |
| 2851 |
C3.w[1] = C4.w[1]; |
| 2852 |
C3.w[0] = C4.w[0]; |
| 2853 |
C4.w[1] = P128.w[1]; |
| 2854 |
C4.w[0] = P128.w[0]; |
| 2855 |
ind = q3; |
| 2856 |
q3 = q4; |
| 2857 |
q4 = ind; |
| 2858 |
ind = e3; |
| 2859 |
e3 = e4; |
| 2860 |
e4 = ind; |
| 2861 |
tmp_sign = z_sign; |
| 2862 |
z_sign = p_sign; |
| 2863 |
p_sign = tmp_sign; |
| 2864 |
} else { // from Cases (2), (3), (4), (5) |
| 2865 |
// In Cases (2), (3), (4), (5) with 0 <= delta <= 1 C3 has to be |
| 2866 |
// scaled up by q4 + delta - q3; this is the same as in Cases (15), |
| 2867 |
// (16), and (17) if we just change the sign of delta |
| 2868 |
delta = -delta; |
| 2869 |
} |
| 2870 |
add_and_round (q3, q4, e4, delta, p34, z_sign, p_sign, C3, C4, |
| 2871 |
rnd_mode, &is_midpoint_lt_even, |
| 2872 |
&is_midpoint_gt_even, &is_inexact_lt_midpoint, |
| 2873 |
&is_inexact_gt_midpoint, pfpsf, &res); |
| 2874 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 2875 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 2876 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 2877 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 2878 |
BID_SWAP128 (res); |
| 2879 |
BID_RETURN (res) |
| 2880 |
|
| 2881 |
} |
| 2882 |
|
| 2883 |
} else { // if delta < 0 |
| 2884 |
|
| 2885 |
delta = -delta; |
| 2886 |
|
| 2887 |
if (p34 < q4 && q4 <= delta) { // Case (7) |
| 2888 |
|
| 2889 |
// truncate C4 to p34 digits into res |
| 2890 |
// x = q4-p34, 1 <= x <= 34 because 35 <= q4 <= 68 |
| 2891 |
x0 = q4 - p34; |
| 2892 |
if (q4 <= 38) { |
| 2893 |
P128.w[1] = C4.w[1]; |
| 2894 |
P128.w[0] = C4.w[0]; |
| 2895 |
round128_19_38 (q4, x0, P128, &res, &incr_exp, |
| 2896 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2897 |
&is_inexact_lt_midpoint, |
| 2898 |
&is_inexact_gt_midpoint); |
| 2899 |
} else if (q4 <= 57) { // 35 <= q4 <= 57 |
| 2900 |
P192.w[2] = C4.w[2]; |
| 2901 |
P192.w[1] = C4.w[1]; |
| 2902 |
P192.w[0] = C4.w[0]; |
| 2903 |
round192_39_57 (q4, x0, P192, &R192, &incr_exp, |
| 2904 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2905 |
&is_inexact_lt_midpoint, |
| 2906 |
&is_inexact_gt_midpoint); |
| 2907 |
res.w[0] = R192.w[0]; |
| 2908 |
res.w[1] = R192.w[1]; |
| 2909 |
} else { // if (q4 <= 68) |
| 2910 |
round256_58_76 (q4, x0, C4, &R256, &incr_exp, |
| 2911 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 2912 |
&is_inexact_lt_midpoint, |
| 2913 |
&is_inexact_gt_midpoint); |
| 2914 |
res.w[0] = R256.w[0]; |
| 2915 |
res.w[1] = R256.w[1]; |
| 2916 |
} |
| 2917 |
e4 = e4 + x0; |
| 2918 |
if (incr_exp) { |
| 2919 |
e4 = e4 + 1; |
| 2920 |
} |
| 2921 |
if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 2922 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 2923 |
// if C4 rounded to p34 digits is exact then the result is inexact, |
| 2924 |
// in a way that depends on the signs of x * y and z |
| 2925 |
if (p_sign == z_sign) { |
| 2926 |
is_inexact_lt_midpoint = 1; |
| 2927 |
} else { // if (p_sign != z_sign) |
| 2928 |
if (res.w[1] != 0x0000314dc6448d93ull || |
| 2929 |
res.w[0] != 0x38c15b0a00000000ull) { // res != 10^33 |
| 2930 |
is_inexact_gt_midpoint = 1; |
| 2931 |
} else { // res = 10^33 and exact is a special case |
| 2932 |
// if C3 < 1/2 ulp then res = 10^33 and is_inexact_gt_midpoint = 1 |
| 2933 |
// if C3 = 1/2 ulp then res = 10^33 and is_midpoint_lt_even = 1 |
| 2934 |
// if C3 > 1/2 ulp then res = 10^34-1 and is_inexact_lt_midpoint = 1 |
| 2935 |
// Note: ulp is really ulp/10 (after borrow which propagates to msd) |
| 2936 |
if (delta > p34 + 1) { // C3 < 1/2 |
| 2937 |
// res = 10^33, unchanged |
| 2938 |
is_inexact_gt_midpoint = 1; |
| 2939 |
} else { // if (delta == p34 + 1) |
| 2940 |
if (q3 <= 19) { |
| 2941 |
if (C3.w[0] < midpoint64[q3 - 1]) { // C3 < 1/2 ulp |
| 2942 |
// res = 10^33, unchanged |
| 2943 |
is_inexact_gt_midpoint = 1; |
| 2944 |
} else if (C3.w[0] == midpoint64[q3 - 1]) { // C3 = 1/2 ulp |
| 2945 |
// res = 10^33, unchanged |
| 2946 |
is_midpoint_lt_even = 1; |
| 2947 |
} else { // if (C3.w[0] > midpoint64[q3-1]), C3 > 1/2 ulp |
| 2948 |
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1 |
| 2949 |
res.w[0] = 0x378d8e63ffffffffull; |
| 2950 |
e4 = e4 - 1; |
| 2951 |
is_inexact_lt_midpoint = 1; |
| 2952 |
} |
| 2953 |
} else { // if (20 <= q3 <=34) |
| 2954 |
if (C3.w[1] < midpoint128[q3 - 20].w[1] || |
| 2955 |
(C3.w[1] == midpoint128[q3 - 20].w[1] && |
| 2956 |
C3.w[0] < midpoint128[q3 - 20].w[0])) { // C3 < 1/2 ulp |
| 2957 |
// res = 10^33, unchanged |
| 2958 |
is_inexact_gt_midpoint = 1; |
| 2959 |
} else if (C3.w[1] == midpoint128[q3 - 20].w[1] && |
| 2960 |
C3.w[0] == midpoint128[q3 - 20].w[0]) { // C3 = 1/2 ulp |
| 2961 |
// res = 10^33, unchanged |
| 2962 |
is_midpoint_lt_even = 1; |
| 2963 |
} else { // if (C3 > midpoint128[q3-20]), C3 > 1/2 ulp |
| 2964 |
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1 |
| 2965 |
res.w[0] = 0x378d8e63ffffffffull; |
| 2966 |
e4 = e4 - 1; |
| 2967 |
is_inexact_lt_midpoint = 1; |
| 2968 |
} |
| 2969 |
} |
| 2970 |
} |
| 2971 |
} |
| 2972 |
} |
| 2973 |
} else if (is_midpoint_lt_even) { |
| 2974 |
if (z_sign != p_sign) { |
| 2975 |
// needs correction: res = res - 1 |
| 2976 |
res.w[0] = res.w[0] - 1; |
| 2977 |
if (res.w[0] == 0xffffffffffffffffull) |
| 2978 |
res.w[1]--; |
| 2979 |
// if it is (10^33-1)*10^e4 then the corect result is |
| 2980 |
// (10^34-1)*10(e4-1) |
| 2981 |
if (res.w[1] == 0x0000314dc6448d93ull && |
| 2982 |
res.w[0] == 0x38c15b09ffffffffull) { |
| 2983 |
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1 |
| 2984 |
res.w[0] = 0x378d8e63ffffffffull; |
| 2985 |
e4 = e4 - 1; |
| 2986 |
} |
| 2987 |
is_midpoint_lt_even = 0; |
| 2988 |
is_inexact_lt_midpoint = 1; |
| 2989 |
} else { // if (z_sign == p_sign) |
| 2990 |
is_midpoint_lt_even = 0; |
| 2991 |
is_inexact_gt_midpoint = 1; |
| 2992 |
} |
| 2993 |
} else if (is_midpoint_gt_even) { |
| 2994 |
if (z_sign == p_sign) { |
| 2995 |
// needs correction: res = res + 1 (cannot cross in the next binade) |
| 2996 |
res.w[0] = res.w[0] + 1; |
| 2997 |
if (res.w[0] == 0x0000000000000000ull) |
| 2998 |
res.w[1]++; |
| 2999 |
is_midpoint_gt_even = 0; |
| 3000 |
is_inexact_gt_midpoint = 1; |
| 3001 |
} else { // if (z_sign != p_sign) |
| 3002 |
is_midpoint_gt_even = 0; |
| 3003 |
is_inexact_lt_midpoint = 1; |
| 3004 |
} |
| 3005 |
} else { |
| 3006 |
; // the rounded result is already correct |
| 3007 |
} |
| 3008 |
// check for overflow |
| 3009 |
if (rnd_mode == ROUNDING_TO_NEAREST && e4 > expmax) { |
| 3010 |
res.w[1] = p_sign | 0x7800000000000000ull; |
| 3011 |
res.w[0] = 0x0000000000000000ull; |
| 3012 |
*pfpsf |= (OVERFLOW_EXCEPTION | INEXACT_EXCEPTION); |
| 3013 |
} else { // no overflow or not RN |
| 3014 |
p_exp = ((UINT64) (e4 + 6176) << 49); |
| 3015 |
res.w[1] = p_sign | (p_exp & MASK_EXP) | res.w[1]; |
| 3016 |
} |
| 3017 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 3018 |
rounding_correction (rnd_mode, |
| 3019 |
is_inexact_lt_midpoint, |
| 3020 |
is_inexact_gt_midpoint, |
| 3021 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 3022 |
e4, &res, pfpsf); |
| 3023 |
} |
| 3024 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 3025 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 3026 |
// set the inexact flag |
| 3027 |
*pfpsf |= INEXACT_EXCEPTION; |
| 3028 |
} |
| 3029 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 3030 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 3031 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 3032 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 3033 |
BID_SWAP128 (res); |
| 3034 |
BID_RETURN (res) |
| 3035 |
|
| 3036 |
} else if ((q4 <= p34 && p34 <= delta) || // Case (8) |
| 3037 |
(q4 <= delta && delta < p34 && p34 < delta + q3) || // Case (9) |
| 3038 |
(q4 <= delta && delta + q3 <= p34) || // Case (10) |
| 3039 |
(delta < q4 && q4 <= p34 && p34 < delta + q3) || // Case (13) |
| 3040 |
(delta < q4 && q4 <= delta + q3 && delta + q3 <= p34) || // Case (14) |
| 3041 |
(delta + q3 < q4 && q4 <= p34)) { // Case (18) |
| 3042 |
|
| 3043 |
// Case (8) is similar to Case (1), with C3 and C4 swapped |
| 3044 |
// Case (9) is similar to Case (2), with C3 and C4 swapped |
| 3045 |
// Case (10) is similar to Case (3), with C3 and C4 swapped |
| 3046 |
// Case (13) is similar to Case (4), with C3 and C4 swapped |
| 3047 |
// Case (14) is similar to Case (5), with C3 and C4 swapped |
| 3048 |
// Case (18) is similar to Case (6), with C3 and C4 swapped |
| 3049 |
|
| 3050 |
// swap (C3, C4), (q3, q4), (e3, 34), (z_sign, p_sign), (z_exp, p_exp) |
| 3051 |
// and go back to delta_ge_zero |
| 3052 |
// C4.w[3] = 0 and C4.w[2] = 0, so swap just the low part of C4 with C3 |
| 3053 |
P128.w[1] = C3.w[1]; |
| 3054 |
P128.w[0] = C3.w[0]; |
| 3055 |
C3.w[1] = C4.w[1]; |
| 3056 |
C3.w[0] = C4.w[0]; |
| 3057 |
C4.w[1] = P128.w[1]; |
| 3058 |
C4.w[0] = P128.w[0]; |
| 3059 |
ind = q3; |
| 3060 |
q3 = q4; |
| 3061 |
q4 = ind; |
| 3062 |
ind = e3; |
| 3063 |
e3 = e4; |
| 3064 |
e4 = ind; |
| 3065 |
tmp_sign = z_sign; |
| 3066 |
z_sign = p_sign; |
| 3067 |
p_sign = tmp_sign; |
| 3068 |
tmp.ui64 = z_exp; |
| 3069 |
z_exp = p_exp; |
| 3070 |
p_exp = tmp.ui64; |
| 3071 |
goto delta_ge_zero; |
| 3072 |
|
| 3073 |
} else if ((p34 <= delta && delta < q4 && q4 < delta + q3) || // Case (11) |
| 3074 |
(delta < p34 && p34 < q4 && q4 < delta + q3)) { // Case (12) |
| 3075 |
|
| 3076 |
// round C3 to nearest to q3 - x0 digits, where x0 = e4 - e3, |
| 3077 |
// 1 <= x0 <= q3 - 1 <= p34 - 1 |
| 3078 |
x0 = e4 - e3; // or x0 = delta + q3 - q4 |
| 3079 |
if (q3 <= 18) { // 2 <= q3 <= 18 |
| 3080 |
round64_2_18 (q3, x0, C3.w[0], &R64, &incr_exp, |
| 3081 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3082 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 3083 |
// C3.w[1] = 0; |
| 3084 |
C3.w[0] = R64; |
| 3085 |
} else if (q3 <= 38) { |
| 3086 |
round128_19_38 (q3, x0, C3, &R128, &incr_exp, |
| 3087 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3088 |
&is_inexact_lt_midpoint, |
| 3089 |
&is_inexact_gt_midpoint); |
| 3090 |
C3.w[1] = R128.w[1]; |
| 3091 |
C3.w[0] = R128.w[0]; |
| 3092 |
} |
| 3093 |
// the rounded result has q3 - x0 digits |
| 3094 |
// we want the exponent to be e4, so if incr_exp = 1 then |
| 3095 |
// multiply the rounded result by 10 - it will still fit in 113 bits |
| 3096 |
if (incr_exp) { |
| 3097 |
// 64 x 128 -> 128 |
| 3098 |
P128.w[1] = C3.w[1]; |
| 3099 |
P128.w[0] = C3.w[0]; |
| 3100 |
__mul_64x128_to_128 (C3, ten2k64[1], P128); |
| 3101 |
} |
| 3102 |
e3 = e3 + x0; // this is e4 |
| 3103 |
// now add/subtract the 256-bit C4 and the new (and shorter) 128-bit C3; |
| 3104 |
// the result will have the sign of x * y; the exponent is e4 |
| 3105 |
R256.w[3] = 0; |
| 3106 |
R256.w[2] = 0; |
| 3107 |
R256.w[1] = C3.w[1]; |
| 3108 |
R256.w[0] = C3.w[0]; |
| 3109 |
if (p_sign == z_sign) { // R256 = C4 + R256 |
| 3110 |
add256 (C4, R256, &R256); |
| 3111 |
} else { // if (p_sign != z_sign) { // R256 = C4 - R256 |
| 3112 |
sub256 (C4, R256, &R256); // the result cannot be pure zero |
| 3113 |
// because the result has opposite sign to that of R256 which was |
| 3114 |
// rounded, need to change the rounding indicators |
| 3115 |
lsb = C4.w[0] & 0x01; |
| 3116 |
if (is_inexact_lt_midpoint) { |
| 3117 |
is_inexact_lt_midpoint = 0; |
| 3118 |
is_inexact_gt_midpoint = 1; |
| 3119 |
} else if (is_inexact_gt_midpoint) { |
| 3120 |
is_inexact_gt_midpoint = 0; |
| 3121 |
is_inexact_lt_midpoint = 1; |
| 3122 |
} else if (lsb == 0) { |
| 3123 |
if (is_midpoint_lt_even) { |
| 3124 |
is_midpoint_lt_even = 0; |
| 3125 |
is_midpoint_gt_even = 1; |
| 3126 |
} else if (is_midpoint_gt_even) { |
| 3127 |
is_midpoint_gt_even = 0; |
| 3128 |
is_midpoint_lt_even = 1; |
| 3129 |
} else { |
| 3130 |
; |
| 3131 |
} |
| 3132 |
} else if (lsb == 1) { |
| 3133 |
if (is_midpoint_lt_even) { |
| 3134 |
// res = res + 1 |
| 3135 |
R256.w[0]++; |
| 3136 |
if (R256.w[0] == 0x0) { |
| 3137 |
R256.w[1]++; |
| 3138 |
if (R256.w[1] == 0x0) { |
| 3139 |
R256.w[2]++; |
| 3140 |
if (R256.w[2] == 0x0) { |
| 3141 |
R256.w[3]++; |
| 3142 |
} |
| 3143 |
} |
| 3144 |
} |
| 3145 |
// no check for rounding overflow - R256 was a difference |
| 3146 |
} else if (is_midpoint_gt_even) { |
| 3147 |
// res = res - 1 |
| 3148 |
R256.w[0]--; |
| 3149 |
if (R256.w[0] == 0xffffffffffffffffull) { |
| 3150 |
R256.w[1]--; |
| 3151 |
if (R256.w[1] == 0xffffffffffffffffull) { |
| 3152 |
R256.w[2]--; |
| 3153 |
if (R256.w[2] == 0xffffffffffffffffull) { |
| 3154 |
R256.w[3]--; |
| 3155 |
} |
| 3156 |
} |
| 3157 |
} |
| 3158 |
} else { |
| 3159 |
; |
| 3160 |
} |
| 3161 |
} else { |
| 3162 |
; |
| 3163 |
} |
| 3164 |
} |
| 3165 |
// determine the number of decimal digits in R256 |
| 3166 |
ind = nr_digits256 (R256); // ind >= p34 |
| 3167 |
// if R256 is sum, then ind > p34; if R256 is a difference, then |
| 3168 |
// ind >= p34; this means that we can calculate the result rounded to |
| 3169 |
// the destination precision, with unbounded exponent, starting from R256 |
| 3170 |
// and using the indicators from the rounding of C3 to avoid a double |
| 3171 |
// rounding error |
| 3172 |
|
| 3173 |
if (ind < p34) { |
| 3174 |
; |
| 3175 |
} else if (ind == p34) { |
| 3176 |
// the result rounded to the destination precision with |
| 3177 |
// unbounded exponent |
| 3178 |
// is (-1)^p_sign * R256 * 10^e4 |
| 3179 |
res.w[1] = R256.w[1]; |
| 3180 |
res.w[0] = R256.w[0]; |
| 3181 |
} else { // if (ind > p34) |
| 3182 |
// if more than P digits, round to nearest to P digits |
| 3183 |
// round R256 to p34 digits |
| 3184 |
x0 = ind - p34; // 1 <= x0 <= 34 as 35 <= ind <= 68 |
| 3185 |
// save C3 rounding indicators to help avoid double rounding error |
| 3186 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 3187 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 3188 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 3189 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 3190 |
// initialize rounding indicators |
| 3191 |
is_inexact_lt_midpoint = 0; |
| 3192 |
is_inexact_gt_midpoint = 0; |
| 3193 |
is_midpoint_lt_even = 0; |
| 3194 |
is_midpoint_gt_even = 0; |
| 3195 |
// round to p34 digits; the result fits in 113 bits |
| 3196 |
if (ind <= 38) { |
| 3197 |
P128.w[1] = R256.w[1]; |
| 3198 |
P128.w[0] = R256.w[0]; |
| 3199 |
round128_19_38 (ind, x0, P128, &R128, &incr_exp, |
| 3200 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3201 |
&is_inexact_lt_midpoint, |
| 3202 |
&is_inexact_gt_midpoint); |
| 3203 |
} else if (ind <= 57) { |
| 3204 |
P192.w[2] = R256.w[2]; |
| 3205 |
P192.w[1] = R256.w[1]; |
| 3206 |
P192.w[0] = R256.w[0]; |
| 3207 |
round192_39_57 (ind, x0, P192, &R192, &incr_exp, |
| 3208 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3209 |
&is_inexact_lt_midpoint, |
| 3210 |
&is_inexact_gt_midpoint); |
| 3211 |
R128.w[1] = R192.w[1]; |
| 3212 |
R128.w[0] = R192.w[0]; |
| 3213 |
} else { // if (ind <= 68) |
| 3214 |
round256_58_76 (ind, x0, R256, &R256, &incr_exp, |
| 3215 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3216 |
&is_inexact_lt_midpoint, |
| 3217 |
&is_inexact_gt_midpoint); |
| 3218 |
R128.w[1] = R256.w[1]; |
| 3219 |
R128.w[0] = R256.w[0]; |
| 3220 |
} |
| 3221 |
// the rounded result has p34 = 34 digits |
| 3222 |
e4 = e4 + x0 + incr_exp; |
| 3223 |
|
| 3224 |
res.w[1] = R128.w[1]; |
| 3225 |
res.w[0] = R128.w[0]; |
| 3226 |
|
| 3227 |
// avoid a double rounding error |
| 3228 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 3229 |
is_midpoint_lt_even) { // double rounding error upward |
| 3230 |
// res = res - 1 |
| 3231 |
res.w[0]--; |
| 3232 |
if (res.w[0] == 0xffffffffffffffffull) |
| 3233 |
res.w[1]--; |
| 3234 |
is_midpoint_lt_even = 0; |
| 3235 |
is_inexact_lt_midpoint = 1; |
| 3236 |
// Note: a double rounding error upward is not possible; for this |
| 3237 |
// the result after the first rounding would have to be 99...95 |
| 3238 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 3239 |
// not possible in Cases (2)-(6) or (15)-(17) which may get here |
| 3240 |
// if this is 10^33 - 1 make it 10^34 - 1 and decrement exponent |
| 3241 |
if (res.w[1] == 0x0000314dc6448d93ull && |
| 3242 |
res.w[0] == 0x38c15b09ffffffffull) { // 10^33 - 1 |
| 3243 |
res.w[1] = 0x0001ed09bead87c0ull; // 10^34 - 1 |
| 3244 |
res.w[0] = 0x378d8e63ffffffffull; |
| 3245 |
e4--; |
| 3246 |
} |
| 3247 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 3248 |
is_midpoint_gt_even) { // double rounding error downward |
| 3249 |
// res = res + 1 |
| 3250 |
res.w[0]++; |
| 3251 |
if (res.w[0] == 0) |
| 3252 |
res.w[1]++; |
| 3253 |
is_midpoint_gt_even = 0; |
| 3254 |
is_inexact_gt_midpoint = 1; |
| 3255 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 3256 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 3257 |
// if this second rounding was exact the result may still be |
| 3258 |
// inexact because of the first rounding |
| 3259 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 3260 |
is_inexact_gt_midpoint = 1; |
| 3261 |
} |
| 3262 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 3263 |
is_inexact_lt_midpoint = 1; |
| 3264 |
} |
| 3265 |
} else if (is_midpoint_gt_even && |
| 3266 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 3267 |
// pulled up to a midpoint |
| 3268 |
is_inexact_lt_midpoint = 1; |
| 3269 |
is_inexact_gt_midpoint = 0; |
| 3270 |
is_midpoint_lt_even = 0; |
| 3271 |
is_midpoint_gt_even = 0; |
| 3272 |
} else if (is_midpoint_lt_even && |
| 3273 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 3274 |
// pulled down to a midpoint |
| 3275 |
is_inexact_lt_midpoint = 0; |
| 3276 |
is_inexact_gt_midpoint = 1; |
| 3277 |
is_midpoint_lt_even = 0; |
| 3278 |
is_midpoint_gt_even = 0; |
| 3279 |
} else { |
| 3280 |
; |
| 3281 |
} |
| 3282 |
} |
| 3283 |
|
| 3284 |
// determine tininess |
| 3285 |
if (rnd_mode == ROUNDING_TO_NEAREST) { |
| 3286 |
if (e4 < expmin) { |
| 3287 |
is_tiny = 1; // for other rounding modes apply correction |
| 3288 |
} |
| 3289 |
} else { |
| 3290 |
// for RM, RP, RZ, RA apply correction in order to determine tininess |
| 3291 |
// but do not save the result; apply the correction to |
| 3292 |
// (-1)^p_sign * res * 10^0 |
| 3293 |
P128.w[1] = p_sign | 0x3040000000000000ull | res.w[1]; |
| 3294 |
P128.w[0] = res.w[0]; |
| 3295 |
rounding_correction (rnd_mode, |
| 3296 |
is_inexact_lt_midpoint, |
| 3297 |
is_inexact_gt_midpoint, |
| 3298 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 3299 |
0, &P128, pfpsf); |
| 3300 |
scale = ((P128.w[1] & MASK_EXP) >> 49) - 6176; // -1, 0, or +1 |
| 3301 |
// the number of digits in the significand is p34 = 34 |
| 3302 |
} |
| 3303 |
|
| 3304 |
// the result rounded to the destination precision with unbounded exponent |
| 3305 |
// is (-1)^p_sign * res * 10^e4 |
| 3306 |
res.w[1] = p_sign | ((UINT64) (e4 + 6176) << 49) | res.w[1]; // RN |
| 3307 |
// res.w[0] unchanged; |
| 3308 |
// Note: res is correct only if expmin <= e4 <= expmax |
| 3309 |
ind = p34; // the number of decimal digits in the signifcand of res |
| 3310 |
|
| 3311 |
// at this point we have the result rounded with unbounded exponent in |
| 3312 |
// res and we know its tininess: |
| 3313 |
// res = (-1)^p_sign * significand * 10^e4, |
| 3314 |
// where q (significand) = ind = p34 |
| 3315 |
// Note: res is correct only if expmin <= e4 <= expmax |
| 3316 |
|
| 3317 |
// check for overflow if RN |
| 3318 |
if (rnd_mode == ROUNDING_TO_NEAREST |
| 3319 |
&& (ind + e4) > (p34 + expmax)) { |
| 3320 |
res.w[1] = p_sign | 0x7800000000000000ull; |
| 3321 |
res.w[0] = 0x0000000000000000ull; |
| 3322 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 3323 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 3324 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 3325 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 3326 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 3327 |
BID_SWAP128 (res); |
| 3328 |
BID_RETURN (res) |
| 3329 |
} // else not overflow or not RN, so continue |
| 3330 |
|
| 3331 |
// from this point on this is similar to the last part of the computation |
| 3332 |
// for Cases (15), (16), (17) |
| 3333 |
|
| 3334 |
// if (e4 >= expmin) we have the result rounded with bounded exponent |
| 3335 |
if (e4 < expmin) { |
| 3336 |
x0 = expmin - e4; // x0 >= 1; the number of digits to chop off of res |
| 3337 |
// where the result rounded [at most] once is |
| 3338 |
// (-1)^p_sign * significand_res * 10^e4 |
| 3339 |
|
| 3340 |
// avoid double rounding error |
| 3341 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 3342 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 3343 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 3344 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 3345 |
is_inexact_lt_midpoint = 0; |
| 3346 |
is_inexact_gt_midpoint = 0; |
| 3347 |
is_midpoint_lt_even = 0; |
| 3348 |
is_midpoint_gt_even = 0; |
| 3349 |
|
| 3350 |
if (x0 > ind) { |
| 3351 |
// nothing is left of res when moving the decimal point left x0 digits |
| 3352 |
is_inexact_lt_midpoint = 1; |
| 3353 |
res.w[1] = p_sign | 0x0000000000000000ull; |
| 3354 |
res.w[0] = 0x0000000000000000ull; |
| 3355 |
e4 = expmin; |
| 3356 |
} else if (x0 == ind) { // 1 <= x0 = ind <= p34 = 34 |
| 3357 |
// this is <, =, or > 1/2 ulp |
| 3358 |
// compare the ind-digit value in the significand of res with |
| 3359 |
// 1/2 ulp = 5*10^(ind-1), i.e. determine whether it is |
| 3360 |
// less than, equal to, or greater than 1/2 ulp (significand of res) |
| 3361 |
R128.w[1] = res.w[1] & MASK_COEFF; |
| 3362 |
R128.w[0] = res.w[0]; |
| 3363 |
if (ind <= 19) { |
| 3364 |
if (R128.w[0] < midpoint64[ind - 1]) { // < 1/2 ulp |
| 3365 |
lt_half_ulp = 1; |
| 3366 |
is_inexact_lt_midpoint = 1; |
| 3367 |
} else if (R128.w[0] == midpoint64[ind - 1]) { // = 1/2 ulp |
| 3368 |
eq_half_ulp = 1; |
| 3369 |
is_midpoint_gt_even = 1; |
| 3370 |
} else { // > 1/2 ulp |
| 3371 |
gt_half_ulp = 1; |
| 3372 |
is_inexact_gt_midpoint = 1; |
| 3373 |
} |
| 3374 |
} else { // if (ind <= 38) |
| 3375 |
if (R128.w[1] < midpoint128[ind - 20].w[1] || |
| 3376 |
(R128.w[1] == midpoint128[ind - 20].w[1] && |
| 3377 |
R128.w[0] < midpoint128[ind - 20].w[0])) { // < 1/2 ulp |
| 3378 |
lt_half_ulp = 1; |
| 3379 |
is_inexact_lt_midpoint = 1; |
| 3380 |
} else if (R128.w[1] == midpoint128[ind - 20].w[1] && |
| 3381 |
R128.w[0] == midpoint128[ind - 20].w[0]) { // = 1/2 ulp |
| 3382 |
eq_half_ulp = 1; |
| 3383 |
is_midpoint_gt_even = 1; |
| 3384 |
} else { // > 1/2 ulp |
| 3385 |
gt_half_ulp = 1; |
| 3386 |
is_inexact_gt_midpoint = 1; |
| 3387 |
} |
| 3388 |
} |
| 3389 |
if (lt_half_ulp || eq_half_ulp) { |
| 3390 |
// res = +0.0 * 10^expmin |
| 3391 |
res.w[1] = 0x0000000000000000ull; |
| 3392 |
res.w[0] = 0x0000000000000000ull; |
| 3393 |
} else { // if (gt_half_ulp) |
| 3394 |
// res = +1 * 10^expmin |
| 3395 |
res.w[1] = 0x0000000000000000ull; |
| 3396 |
res.w[0] = 0x0000000000000001ull; |
| 3397 |
} |
| 3398 |
res.w[1] = p_sign | res.w[1]; |
| 3399 |
e4 = expmin; |
| 3400 |
} else { // if (1 <= x0 <= ind - 1 <= 33) |
| 3401 |
// round the ind-digit result to ind - x0 digits |
| 3402 |
|
| 3403 |
if (ind <= 18) { // 2 <= ind <= 18 |
| 3404 |
round64_2_18 (ind, x0, res.w[0], &R64, &incr_exp, |
| 3405 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3406 |
&is_inexact_lt_midpoint, |
| 3407 |
&is_inexact_gt_midpoint); |
| 3408 |
res.w[1] = 0x0; |
| 3409 |
res.w[0] = R64; |
| 3410 |
} else if (ind <= 38) { |
| 3411 |
P128.w[1] = res.w[1] & MASK_COEFF; |
| 3412 |
P128.w[0] = res.w[0]; |
| 3413 |
round128_19_38 (ind, x0, P128, &res, &incr_exp, |
| 3414 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3415 |
&is_inexact_lt_midpoint, |
| 3416 |
&is_inexact_gt_midpoint); |
| 3417 |
} |
| 3418 |
e4 = e4 + x0; // expmin |
| 3419 |
// we want the exponent to be expmin, so if incr_exp = 1 then |
| 3420 |
// multiply the rounded result by 10 - it will still fit in 113 bits |
| 3421 |
if (incr_exp) { |
| 3422 |
// 64 x 128 -> 128 |
| 3423 |
P128.w[1] = res.w[1] & MASK_COEFF; |
| 3424 |
P128.w[0] = res.w[0]; |
| 3425 |
__mul_64x128_to_128 (res, ten2k64[1], P128); |
| 3426 |
} |
| 3427 |
res.w[1] = |
| 3428 |
p_sign | ((UINT64) (e4 + 6176) << 49) | (res. |
| 3429 |
w[1] & MASK_COEFF); |
| 3430 |
// avoid a double rounding error |
| 3431 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 3432 |
is_midpoint_lt_even) { // double rounding error upward |
| 3433 |
// res = res - 1 |
| 3434 |
res.w[0]--; |
| 3435 |
if (res.w[0] == 0xffffffffffffffffull) |
| 3436 |
res.w[1]--; |
| 3437 |
// Note: a double rounding error upward is not possible; for this |
| 3438 |
// the result after the first rounding would have to be 99...95 |
| 3439 |
// (35 digits in all), possibly followed by a number of zeros; this |
| 3440 |
// not possible in this underflow case |
| 3441 |
is_midpoint_lt_even = 0; |
| 3442 |
is_inexact_lt_midpoint = 1; |
| 3443 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 3444 |
is_midpoint_gt_even) { // double rounding error downward |
| 3445 |
// res = res + 1 |
| 3446 |
res.w[0]++; |
| 3447 |
if (res.w[0] == 0) |
| 3448 |
res.w[1]++; |
| 3449 |
is_midpoint_gt_even = 0; |
| 3450 |
is_inexact_gt_midpoint = 1; |
| 3451 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 3452 |
!is_inexact_lt_midpoint |
| 3453 |
&& !is_inexact_gt_midpoint) { |
| 3454 |
// if this second rounding was exact the result may still be |
| 3455 |
// inexact because of the first rounding |
| 3456 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 3457 |
is_inexact_gt_midpoint = 1; |
| 3458 |
} |
| 3459 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 3460 |
is_inexact_lt_midpoint = 1; |
| 3461 |
} |
| 3462 |
} else if (is_midpoint_gt_even && |
| 3463 |
(is_inexact_gt_midpoint0 |
| 3464 |
|| is_midpoint_lt_even0)) { |
| 3465 |
// pulled up to a midpoint |
| 3466 |
is_inexact_lt_midpoint = 1; |
| 3467 |
is_inexact_gt_midpoint = 0; |
| 3468 |
is_midpoint_lt_even = 0; |
| 3469 |
is_midpoint_gt_even = 0; |
| 3470 |
} else if (is_midpoint_lt_even && |
| 3471 |
(is_inexact_lt_midpoint0 |
| 3472 |
|| is_midpoint_gt_even0)) { |
| 3473 |
// pulled down to a midpoint |
| 3474 |
is_inexact_lt_midpoint = 0; |
| 3475 |
is_inexact_gt_midpoint = 1; |
| 3476 |
is_midpoint_lt_even = 0; |
| 3477 |
is_midpoint_gt_even = 0; |
| 3478 |
} else { |
| 3479 |
; |
| 3480 |
} |
| 3481 |
} |
| 3482 |
} |
| 3483 |
// res contains the correct result |
| 3484 |
// apply correction if not rounding to nearest |
| 3485 |
if (rnd_mode != ROUNDING_TO_NEAREST) { |
| 3486 |
rounding_correction (rnd_mode, |
| 3487 |
is_inexact_lt_midpoint, |
| 3488 |
is_inexact_gt_midpoint, |
| 3489 |
is_midpoint_lt_even, is_midpoint_gt_even, |
| 3490 |
e4, &res, pfpsf); |
| 3491 |
} |
| 3492 |
// correction needed for tininess detection before rounding |
| 3493 |
if ((((res.w[1] & 0x7fffffffffffffffull) == 0x0000314dc6448d93ull) && |
| 3494 |
// 10^33*10^-6176_high |
| 3495 |
(res.w[0] == 0x38c15b0a00000000ull)) && // 10^33*10^-6176_low |
| 3496 |
(((rnd_mode == ROUNDING_TO_NEAREST || |
| 3497 |
rnd_mode == ROUNDING_TIES_AWAY) && |
| 3498 |
(is_midpoint_lt_even || is_inexact_gt_midpoint)) || |
| 3499 |
((((rnd_mode == ROUNDING_UP) && !(res.w[1] & MASK_SIGN)) || |
| 3500 |
((rnd_mode == ROUNDING_DOWN) && (res.w[1] & MASK_SIGN))) |
| 3501 |
&& (is_midpoint_lt_even || is_midpoint_gt_even || |
| 3502 |
is_inexact_lt_midpoint || is_inexact_gt_midpoint)))) { |
| 3503 |
is_tiny = 1; |
| 3504 |
} |
| 3505 |
if (is_midpoint_lt_even || is_midpoint_gt_even || |
| 3506 |
is_inexact_lt_midpoint || is_inexact_gt_midpoint) { |
| 3507 |
// set the inexact flag |
| 3508 |
*pfpsf |= INEXACT_EXCEPTION; |
| 3509 |
if (is_tiny) |
| 3510 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 3511 |
} |
| 3512 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 3513 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 3514 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 3515 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 3516 |
BID_SWAP128 (res); |
| 3517 |
BID_RETURN (res) |
| 3518 |
|
| 3519 |
} else if ((p34 <= delta && delta + q3 <= q4) || // Case (15) |
| 3520 |
(delta < p34 && p34 < delta + q3 && delta + q3 <= q4) || //Case (16) |
| 3521 |
(delta + q3 <= p34 && p34 < q4)) { // Case (17) |
| 3522 |
|
| 3523 |
// calculate first the result rounded to the destination precision, with |
| 3524 |
// unbounded exponent |
| 3525 |
|
| 3526 |
add_and_round (q3, q4, e4, delta, p34, z_sign, p_sign, C3, C4, |
| 3527 |
rnd_mode, &is_midpoint_lt_even, |
| 3528 |
&is_midpoint_gt_even, &is_inexact_lt_midpoint, |
| 3529 |
&is_inexact_gt_midpoint, pfpsf, &res); |
| 3530 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 3531 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 3532 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 3533 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 3534 |
BID_SWAP128 (res); |
| 3535 |
BID_RETURN (res) |
| 3536 |
|
| 3537 |
} else { |
| 3538 |
; |
| 3539 |
} |
| 3540 |
|
| 3541 |
} // end if delta < 0 |
| 3542 |
|
| 3543 |
*ptr_is_midpoint_lt_even = is_midpoint_lt_even; |
| 3544 |
*ptr_is_midpoint_gt_even = is_midpoint_gt_even; |
| 3545 |
*ptr_is_inexact_lt_midpoint = is_inexact_lt_midpoint; |
| 3546 |
*ptr_is_inexact_gt_midpoint = is_inexact_gt_midpoint; |
| 3547 |
BID_SWAP128 (res); |
| 3548 |
BID_RETURN (res) |
| 3549 |
|
| 3550 |
} |
| 3551 |
|
| 3552 |
|
| 3553 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3554 |
void |
| 3555 |
bid128_fma (UINT128 * pres, UINT128 * px, UINT128 * py, UINT128 * pz |
| 3556 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3557 |
_EXC_INFO_PARAM) { |
| 3558 |
UINT128 x = *px, y = *py, z = *pz; |
| 3559 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3560 |
unsigned int rnd_mode = *prnd_mode; |
| 3561 |
#endif |
| 3562 |
#else |
| 3563 |
UINT128 |
| 3564 |
bid128_fma (UINT128 x, UINT128 y, UINT128 z |
| 3565 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3566 |
_EXC_INFO_PARAM) { |
| 3567 |
#endif |
| 3568 |
int is_midpoint_lt_even, is_midpoint_gt_even, |
| 3569 |
is_inexact_lt_midpoint, is_inexact_gt_midpoint; |
| 3570 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3571 |
|
| 3572 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3573 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3574 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3575 |
&res, &x, &y, &z |
| 3576 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3577 |
_EXC_INFO_ARG); |
| 3578 |
#else |
| 3579 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3580 |
&is_inexact_lt_midpoint, |
| 3581 |
&is_inexact_gt_midpoint, x, y, |
| 3582 |
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3583 |
_EXC_INFO_ARG); |
| 3584 |
#endif |
| 3585 |
BID_RETURN (res); |
| 3586 |
} |
| 3587 |
|
| 3588 |
|
| 3589 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3590 |
void |
| 3591 |
bid128ddd_fma (UINT128 * pres, UINT64 * px, UINT64 * py, UINT64 * pz |
| 3592 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3593 |
_EXC_INFO_PARAM) { |
| 3594 |
UINT64 x = *px, y = *py, z = *pz; |
| 3595 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3596 |
unsigned int rnd_mode = *prnd_mode; |
| 3597 |
#endif |
| 3598 |
#else |
| 3599 |
UINT128 |
| 3600 |
bid128ddd_fma (UINT64 x, UINT64 y, UINT64 z |
| 3601 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3602 |
_EXC_INFO_PARAM) { |
| 3603 |
#endif |
| 3604 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3605 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3606 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3607 |
UINT128 x1, y1, z1; |
| 3608 |
|
| 3609 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3610 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3611 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3612 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3613 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3614 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3615 |
&res, &x1, &y1, &z1 |
| 3616 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3617 |
_EXC_INFO_ARG); |
| 3618 |
#else |
| 3619 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3620 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3621 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3622 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3623 |
&is_inexact_lt_midpoint, |
| 3624 |
&is_inexact_gt_midpoint, x1, y1, |
| 3625 |
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3626 |
_EXC_INFO_ARG); |
| 3627 |
#endif |
| 3628 |
BID_RETURN (res); |
| 3629 |
} |
| 3630 |
|
| 3631 |
|
| 3632 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3633 |
void |
| 3634 |
bid128ddq_fma (UINT128 * pres, UINT64 * px, UINT64 * py, UINT128 * pz |
| 3635 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3636 |
_EXC_INFO_PARAM) { |
| 3637 |
UINT64 x = *px, y = *py; |
| 3638 |
UINT128 z = *pz; |
| 3639 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3640 |
unsigned int rnd_mode = *prnd_mode; |
| 3641 |
#endif |
| 3642 |
#else |
| 3643 |
UINT128 |
| 3644 |
bid128ddq_fma (UINT64 x, UINT64 y, UINT128 z |
| 3645 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3646 |
_EXC_INFO_PARAM) { |
| 3647 |
#endif |
| 3648 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3649 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3650 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3651 |
UINT128 x1, y1; |
| 3652 |
|
| 3653 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3654 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3655 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3656 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3657 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3658 |
&res, &x1, &y1, &z |
| 3659 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3660 |
_EXC_INFO_ARG); |
| 3661 |
#else |
| 3662 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3663 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3664 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3665 |
&is_inexact_lt_midpoint, |
| 3666 |
&is_inexact_gt_midpoint, x1, y1, |
| 3667 |
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3668 |
_EXC_INFO_ARG); |
| 3669 |
#endif |
| 3670 |
BID_RETURN (res); |
| 3671 |
} |
| 3672 |
|
| 3673 |
|
| 3674 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3675 |
void |
| 3676 |
bid128dqd_fma (UINT128 * pres, UINT64 * px, UINT128 * py, UINT64 * pz |
| 3677 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3678 |
_EXC_INFO_PARAM) { |
| 3679 |
UINT64 x = *px, z = *pz; |
| 3680 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3681 |
unsigned int rnd_mode = *prnd_mode; |
| 3682 |
#endif |
| 3683 |
#else |
| 3684 |
UINT128 |
| 3685 |
bid128dqd_fma (UINT64 x, UINT128 y, UINT64 z |
| 3686 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3687 |
_EXC_INFO_PARAM) { |
| 3688 |
#endif |
| 3689 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3690 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3691 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3692 |
UINT128 x1, z1; |
| 3693 |
|
| 3694 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3695 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3696 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3697 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3698 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3699 |
&res, &x1, py, &z1 |
| 3700 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3701 |
_EXC_INFO_ARG); |
| 3702 |
#else |
| 3703 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3704 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3705 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3706 |
&is_inexact_lt_midpoint, |
| 3707 |
&is_inexact_gt_midpoint, x1, y, |
| 3708 |
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3709 |
_EXC_INFO_ARG); |
| 3710 |
#endif |
| 3711 |
BID_RETURN (res); |
| 3712 |
} |
| 3713 |
|
| 3714 |
|
| 3715 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3716 |
void |
| 3717 |
bid128dqq_fma (UINT128 * pres, UINT64 * px, UINT128 * py, UINT128 * pz |
| 3718 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3719 |
_EXC_INFO_PARAM) { |
| 3720 |
UINT64 x = *px; |
| 3721 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3722 |
unsigned int rnd_mode = *prnd_mode; |
| 3723 |
#endif |
| 3724 |
#else |
| 3725 |
UINT128 |
| 3726 |
bid128dqq_fma (UINT64 x, UINT128 y, UINT128 z |
| 3727 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3728 |
_EXC_INFO_PARAM) { |
| 3729 |
#endif |
| 3730 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3731 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3732 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3733 |
UINT128 x1; |
| 3734 |
|
| 3735 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3736 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3737 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3738 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3739 |
&res, &x1, py, pz |
| 3740 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3741 |
_EXC_INFO_ARG); |
| 3742 |
#else |
| 3743 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3744 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3745 |
&is_inexact_lt_midpoint, |
| 3746 |
&is_inexact_gt_midpoint, x1, y, |
| 3747 |
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3748 |
_EXC_INFO_ARG); |
| 3749 |
#endif |
| 3750 |
BID_RETURN (res); |
| 3751 |
} |
| 3752 |
|
| 3753 |
|
| 3754 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3755 |
void |
| 3756 |
bid128qdd_fma (UINT128 * pres, UINT128 * px, UINT64 * py, UINT64 * pz |
| 3757 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3758 |
_EXC_INFO_PARAM) { |
| 3759 |
UINT64 y = *py, z = *pz; |
| 3760 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3761 |
unsigned int rnd_mode = *prnd_mode; |
| 3762 |
#endif |
| 3763 |
#else |
| 3764 |
UINT128 |
| 3765 |
bid128qdd_fma (UINT128 x, UINT64 y, UINT64 z |
| 3766 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3767 |
_EXC_INFO_PARAM) { |
| 3768 |
#endif |
| 3769 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3770 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3771 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3772 |
UINT128 y1, z1; |
| 3773 |
|
| 3774 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3775 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3776 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3777 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3778 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3779 |
&res, px, &y1, &z1 |
| 3780 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3781 |
_EXC_INFO_ARG); |
| 3782 |
#else |
| 3783 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3784 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3785 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3786 |
&is_inexact_lt_midpoint, |
| 3787 |
&is_inexact_gt_midpoint, x, y1, |
| 3788 |
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3789 |
_EXC_INFO_ARG); |
| 3790 |
#endif |
| 3791 |
BID_RETURN (res); |
| 3792 |
} |
| 3793 |
|
| 3794 |
|
| 3795 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3796 |
void |
| 3797 |
bid128qdq_fma (UINT128 * pres, UINT128 * px, UINT64 * py, UINT128 * pz |
| 3798 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3799 |
_EXC_INFO_PARAM) { |
| 3800 |
UINT64 y = *py; |
| 3801 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3802 |
unsigned int rnd_mode = *prnd_mode; |
| 3803 |
#endif |
| 3804 |
#else |
| 3805 |
UINT128 |
| 3806 |
bid128qdq_fma (UINT128 x, UINT64 y, UINT128 z |
| 3807 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3808 |
_EXC_INFO_PARAM) { |
| 3809 |
#endif |
| 3810 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3811 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3812 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3813 |
UINT128 y1; |
| 3814 |
|
| 3815 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3816 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3817 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3818 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3819 |
&res, px, &y1, pz |
| 3820 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3821 |
_EXC_INFO_ARG); |
| 3822 |
#else |
| 3823 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3824 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3825 |
&is_inexact_lt_midpoint, |
| 3826 |
&is_inexact_gt_midpoint, x, y1, |
| 3827 |
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3828 |
_EXC_INFO_ARG); |
| 3829 |
#endif |
| 3830 |
BID_RETURN (res); |
| 3831 |
} |
| 3832 |
|
| 3833 |
|
| 3834 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3835 |
void |
| 3836 |
bid128qqd_fma (UINT128 * pres, UINT128 * px, UINT128 * py, UINT64 * pz |
| 3837 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3838 |
_EXC_INFO_PARAM) { |
| 3839 |
UINT64 z = *pz; |
| 3840 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3841 |
unsigned int rnd_mode = *prnd_mode; |
| 3842 |
#endif |
| 3843 |
#else |
| 3844 |
UINT128 |
| 3845 |
bid128qqd_fma (UINT128 x, UINT128 y, UINT64 z |
| 3846 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3847 |
_EXC_INFO_PARAM) { |
| 3848 |
#endif |
| 3849 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 3850 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 3851 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 3852 |
UINT128 z1; |
| 3853 |
|
| 3854 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3855 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3856 |
bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3857 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint, |
| 3858 |
&res, px, py, &z1 |
| 3859 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3860 |
_EXC_INFO_ARG); |
| 3861 |
#else |
| 3862 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3863 |
res = bid128_ext_fma (&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 3864 |
&is_inexact_lt_midpoint, |
| 3865 |
&is_inexact_gt_midpoint, x, y, |
| 3866 |
z1 _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3867 |
_EXC_INFO_ARG); |
| 3868 |
#endif |
| 3869 |
BID_RETURN (res); |
| 3870 |
} |
| 3871 |
|
| 3872 |
// Note: bid128qqq_fma is represented by bid128_fma |
| 3873 |
|
| 3874 |
// Note: bid64ddd_fma is represented by bid64_fma |
| 3875 |
|
| 3876 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3877 |
void |
| 3878 |
bid64ddq_fma (UINT64 * pres, UINT64 * px, UINT64 * py, UINT128 * pz |
| 3879 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3880 |
_EXC_INFO_PARAM) { |
| 3881 |
UINT64 x = *px, y = *py; |
| 3882 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3883 |
unsigned int rnd_mode = *prnd_mode; |
| 3884 |
#endif |
| 3885 |
#else |
| 3886 |
UINT64 |
| 3887 |
bid64ddq_fma (UINT64 x, UINT64 y, UINT128 z |
| 3888 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3889 |
_EXC_INFO_PARAM) { |
| 3890 |
#endif |
| 3891 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 3892 |
UINT128 x1, y1; |
| 3893 |
|
| 3894 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3895 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3896 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3897 |
bid64qqq_fma (&res1, &x1, &y1, pz |
| 3898 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3899 |
_EXC_INFO_ARG); |
| 3900 |
#else |
| 3901 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3902 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3903 |
res1 = bid64qqq_fma (x1, y1, z |
| 3904 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3905 |
_EXC_INFO_ARG); |
| 3906 |
#endif |
| 3907 |
BID_RETURN (res1); |
| 3908 |
} |
| 3909 |
|
| 3910 |
|
| 3911 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3912 |
void |
| 3913 |
bid64dqd_fma (UINT64 * pres, UINT64 * px, UINT128 * py, UINT64 * pz |
| 3914 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3915 |
_EXC_INFO_PARAM) { |
| 3916 |
UINT64 x = *px, z = *pz; |
| 3917 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3918 |
unsigned int rnd_mode = *prnd_mode; |
| 3919 |
#endif |
| 3920 |
#else |
| 3921 |
UINT64 |
| 3922 |
bid64dqd_fma (UINT64 x, UINT128 y, UINT64 z |
| 3923 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3924 |
_EXC_INFO_PARAM) { |
| 3925 |
#endif |
| 3926 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 3927 |
UINT128 x1, z1; |
| 3928 |
|
| 3929 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3930 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3931 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3932 |
bid64qqq_fma (&res1, &x1, py, &z1 |
| 3933 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3934 |
_EXC_INFO_ARG); |
| 3935 |
#else |
| 3936 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3937 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3938 |
res1 = bid64qqq_fma (x1, y, z1 |
| 3939 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3940 |
_EXC_INFO_ARG); |
| 3941 |
#endif |
| 3942 |
BID_RETURN (res1); |
| 3943 |
} |
| 3944 |
|
| 3945 |
|
| 3946 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3947 |
void |
| 3948 |
bid64dqq_fma (UINT64 * pres, UINT64 * px, UINT128 * py, UINT128 * pz |
| 3949 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3950 |
_EXC_INFO_PARAM) { |
| 3951 |
UINT64 x = *px; |
| 3952 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3953 |
unsigned int rnd_mode = *prnd_mode; |
| 3954 |
#endif |
| 3955 |
#else |
| 3956 |
UINT64 |
| 3957 |
bid64dqq_fma (UINT64 x, UINT128 y, UINT128 z |
| 3958 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3959 |
_EXC_INFO_PARAM) { |
| 3960 |
#endif |
| 3961 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 3962 |
UINT128 x1; |
| 3963 |
|
| 3964 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3965 |
bid64_to_bid128 (&x1, &x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3966 |
bid64qqq_fma (&res1, &x1, py, pz |
| 3967 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3968 |
_EXC_INFO_ARG); |
| 3969 |
#else |
| 3970 |
x1 = bid64_to_bid128 (x _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3971 |
res1 = bid64qqq_fma (x1, y, z |
| 3972 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 3973 |
_EXC_INFO_ARG); |
| 3974 |
#endif |
| 3975 |
BID_RETURN (res1); |
| 3976 |
} |
| 3977 |
|
| 3978 |
|
| 3979 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3980 |
void |
| 3981 |
bid64qdd_fma (UINT64 * pres, UINT128 * px, UINT64 * py, UINT64 * pz |
| 3982 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3983 |
_EXC_INFO_PARAM) { |
| 3984 |
UINT64 y = *py, z = *pz; |
| 3985 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 3986 |
unsigned int rnd_mode = *prnd_mode; |
| 3987 |
#endif |
| 3988 |
#else |
| 3989 |
UINT64 |
| 3990 |
bid64qdd_fma (UINT128 x, UINT64 y, UINT64 z |
| 3991 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 3992 |
_EXC_INFO_PARAM) { |
| 3993 |
#endif |
| 3994 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 3995 |
UINT128 y1, z1; |
| 3996 |
|
| 3997 |
#if DECIMAL_CALL_BY_REFERENCE |
| 3998 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 3999 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4000 |
bid64qqq_fma (&res1, px, &y1, &z1 |
| 4001 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4002 |
_EXC_INFO_ARG); |
| 4003 |
#else |
| 4004 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4005 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4006 |
res1 = bid64qqq_fma (x, y1, z1 |
| 4007 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4008 |
_EXC_INFO_ARG); |
| 4009 |
#endif |
| 4010 |
BID_RETURN (res1); |
| 4011 |
} |
| 4012 |
|
| 4013 |
|
| 4014 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4015 |
void |
| 4016 |
bid64qdq_fma (UINT64 * pres, UINT128 * px, UINT64 * py, UINT128 * pz |
| 4017 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4018 |
_EXC_INFO_PARAM) { |
| 4019 |
UINT64 y = *py; |
| 4020 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 4021 |
unsigned int rnd_mode = *prnd_mode; |
| 4022 |
#endif |
| 4023 |
#else |
| 4024 |
UINT64 |
| 4025 |
bid64qdq_fma (UINT128 x, UINT64 y, UINT128 z |
| 4026 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4027 |
_EXC_INFO_PARAM) { |
| 4028 |
#endif |
| 4029 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 4030 |
UINT128 y1; |
| 4031 |
|
| 4032 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4033 |
bid64_to_bid128 (&y1, &y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4034 |
bid64qqq_fma (&res1, px, &y1, pz |
| 4035 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4036 |
_EXC_INFO_ARG); |
| 4037 |
#else |
| 4038 |
y1 = bid64_to_bid128 (y _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4039 |
res1 = bid64qqq_fma (x, y1, z |
| 4040 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4041 |
_EXC_INFO_ARG); |
| 4042 |
#endif |
| 4043 |
BID_RETURN (res1); |
| 4044 |
} |
| 4045 |
|
| 4046 |
|
| 4047 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4048 |
void |
| 4049 |
bid64qqd_fma (UINT64 * pres, UINT128 * px, UINT128 * py, UINT64 * pz |
| 4050 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4051 |
_EXC_INFO_PARAM) { |
| 4052 |
UINT64 z = *pz; |
| 4053 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 4054 |
unsigned int rnd_mode = *prnd_mode; |
| 4055 |
#endif |
| 4056 |
#else |
| 4057 |
UINT64 |
| 4058 |
bid64qqd_fma (UINT128 x, UINT128 y, UINT64 z |
| 4059 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4060 |
_EXC_INFO_PARAM) { |
| 4061 |
#endif |
| 4062 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 4063 |
UINT128 z1; |
| 4064 |
|
| 4065 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4066 |
bid64_to_bid128 (&z1, &z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4067 |
bid64qqq_fma (&res1, px, py, &z1 |
| 4068 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4069 |
_EXC_INFO_ARG); |
| 4070 |
#else |
| 4071 |
z1 = bid64_to_bid128 (z _EXC_FLAGS_ARG _EXC_MASKS_ARG _EXC_INFO_ARG); |
| 4072 |
res1 = bid64qqq_fma (x, y, z1 |
| 4073 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4074 |
_EXC_INFO_ARG); |
| 4075 |
#endif |
| 4076 |
BID_RETURN (res1); |
| 4077 |
} |
| 4078 |
|
| 4079 |
|
| 4080 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4081 |
void |
| 4082 |
bid64qqq_fma (UINT64 * pres, UINT128 * px, UINT128 * py, UINT128 * pz |
| 4083 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4084 |
_EXC_INFO_PARAM) { |
| 4085 |
UINT128 x = *px, y = *py, z = *pz; |
| 4086 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 4087 |
unsigned int rnd_mode = *prnd_mode; |
| 4088 |
#endif |
| 4089 |
#else |
| 4090 |
UINT64 |
| 4091 |
bid64qqq_fma (UINT128 x, UINT128 y, UINT128 z |
| 4092 |
_RND_MODE_PARAM _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 4093 |
_EXC_INFO_PARAM) { |
| 4094 |
#endif |
| 4095 |
int is_midpoint_lt_even0 = 0, is_midpoint_gt_even0 = 0, |
| 4096 |
is_inexact_lt_midpoint0 = 0, is_inexact_gt_midpoint0 = 0; |
| 4097 |
int is_midpoint_lt_even = 0, is_midpoint_gt_even = 0, |
| 4098 |
is_inexact_lt_midpoint = 0, is_inexact_gt_midpoint = 0; |
| 4099 |
int incr_exp; |
| 4100 |
UINT128 res = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 4101 |
UINT128 res128 = { {0xbaddbaddbaddbaddull, 0xbaddbaddbaddbaddull} }; |
| 4102 |
UINT64 res1 = 0xbaddbaddbaddbaddull; |
| 4103 |
unsigned int save_fpsf; // needed because of the call to bid128_ext_fma |
| 4104 |
UINT64 sign; |
| 4105 |
UINT64 exp; |
| 4106 |
int unbexp; |
| 4107 |
UINT128 C; |
| 4108 |
BID_UI64DOUBLE tmp; |
| 4109 |
int nr_bits; |
| 4110 |
int q, x0; |
| 4111 |
int scale; |
| 4112 |
int lt_half_ulp = 0, eq_half_ulp = 0; |
| 4113 |
|
| 4114 |
// Note: for rounding modes other than RN or RA, the result can be obtained |
| 4115 |
// by rounding first to BID128 and then to BID64 |
| 4116 |
|
| 4117 |
save_fpsf = *pfpsf; // sticky bits - caller value must be preserved |
| 4118 |
*pfpsf = 0; |
| 4119 |
|
| 4120 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4121 |
bid128_ext_fma (&is_midpoint_lt_even0, &is_midpoint_gt_even0, |
| 4122 |
&is_inexact_lt_midpoint0, &is_inexact_gt_midpoint0, |
| 4123 |
&res, &x, &y, &z |
| 4124 |
_RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4125 |
_EXC_INFO_ARG); |
| 4126 |
#else |
| 4127 |
res = bid128_ext_fma (&is_midpoint_lt_even0, &is_midpoint_gt_even0, |
| 4128 |
&is_inexact_lt_midpoint0, |
| 4129 |
&is_inexact_gt_midpoint0, x, y, |
| 4130 |
z _RND_MODE_ARG _EXC_FLAGS_ARG _EXC_MASKS_ARG |
| 4131 |
_EXC_INFO_ARG); |
| 4132 |
#endif |
| 4133 |
|
| 4134 |
if ((rnd_mode == ROUNDING_DOWN) || (rnd_mode == ROUNDING_UP) || |
| 4135 |
(rnd_mode == ROUNDING_TO_ZERO) || // no double rounding error is possible |
| 4136 |
((res.w[HIGH_128W] & MASK_NAN) == MASK_NAN) || //res=QNaN (cannot be SNaN) |
| 4137 |
((res.w[HIGH_128W] & MASK_ANY_INF) == MASK_INF)) { // result is infinity |
| 4138 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4139 |
bid128_to_bid64 (&res1, &res _RND_MODE_ARG _EXC_FLAGS_ARG); |
| 4140 |
#else |
| 4141 |
res1 = bid128_to_bid64 (res _RND_MODE_ARG _EXC_FLAGS_ARG); |
| 4142 |
#endif |
| 4143 |
// determine the unbiased exponent of the result |
| 4144 |
unbexp = ((res1 >> 53) & 0x3ff) - 398; |
| 4145 |
|
| 4146 |
if (!((res1 & MASK_NAN) == MASK_NAN)) { // res1 not NaN |
| 4147 |
// if subnormal, res1 must have exp = -398 |
| 4148 |
// if tiny and inexact set underflow and inexact status flags |
| 4149 |
if ((unbexp == -398) |
| 4150 |
&& ((res1 & MASK_BINARY_SIG1) < 1000000000000000ull) |
| 4151 |
&& (is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0 |
| 4152 |
|| is_midpoint_lt_even0 || is_midpoint_gt_even0)) { |
| 4153 |
// set the inexact flag and the underflow flag |
| 4154 |
*pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION); |
| 4155 |
} else if (is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0 || |
| 4156 |
is_midpoint_lt_even0 || is_midpoint_gt_even0) { |
| 4157 |
// set the inexact flag and the underflow flag |
| 4158 |
*pfpsf |= INEXACT_EXCEPTION; |
| 4159 |
} |
| 4160 |
|
| 4161 |
// correction needed for tininess detection before rounding |
| 4162 |
if (((res1 & 0x7fffffffffffffffull) == 1000000000000000ull) && |
| 4163 |
// 10^15*10^-398 |
| 4164 |
(((rnd_mode == ROUNDING_TO_NEAREST || |
| 4165 |
rnd_mode == ROUNDING_TIES_AWAY) && |
| 4166 |
(is_midpoint_lt_even || is_inexact_gt_midpoint)) || |
| 4167 |
((((rnd_mode == ROUNDING_UP) && !(res1 & MASK_SIGN)) || |
| 4168 |
((rnd_mode == ROUNDING_DOWN) && (res1 & MASK_SIGN))) |
| 4169 |
&& (is_midpoint_lt_even || is_midpoint_gt_even || |
| 4170 |
is_inexact_lt_midpoint || is_inexact_gt_midpoint)))) { |
| 4171 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 4172 |
} |
| 4173 |
} |
| 4174 |
*pfpsf |= save_fpsf; |
| 4175 |
BID_RETURN (res1); |
| 4176 |
} // else continue, and use rounding to nearest to round to 16 digits |
| 4177 |
|
| 4178 |
// at this point the result is rounded to nearest (even or away) to 34 digits |
| 4179 |
// (or less if exact), and it is zero or finite non-zero canonical [sub]normal |
| 4180 |
sign = res.w[HIGH_128W] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative |
| 4181 |
exp = res.w[HIGH_128W] & MASK_EXP; // biased and shifted left 49 bits |
| 4182 |
unbexp = (exp >> 49) - 6176; |
| 4183 |
C.w[1] = res.w[HIGH_128W] & MASK_COEFF; |
| 4184 |
C.w[0] = res.w[LOW_128W]; |
| 4185 |
|
| 4186 |
if ((C.w[1] == 0x0 && C.w[0] == 0x0) || // result is zero |
| 4187 |
(unbexp <= (-398 - 35)) || (unbexp >= (369 + 16))) { |
| 4188 |
// clear under/overflow |
| 4189 |
#if DECIMAL_CALL_BY_REFERENCE |
| 4190 |
bid128_to_bid64 (&res1, &res _RND_MODE_ARG _EXC_FLAGS_ARG); |
| 4191 |
#else |
| 4192 |
res1 = bid128_to_bid64 (res _RND_MODE_ARG _EXC_FLAGS_ARG); |
| 4193 |
#endif |
| 4194 |
*pfpsf |= save_fpsf; |
| 4195 |
BID_RETURN (res1); |
| 4196 |
} // else continue |
| 4197 |
|
| 4198 |
// -398 - 34 <= unbexp <= 369 + 15 |
| 4199 |
if (rnd_mode == ROUNDING_TIES_AWAY) { |
| 4200 |
// apply correction, if needed, to make the result rounded to nearest-even |
| 4201 |
if (is_midpoint_gt_even) { |
| 4202 |
// res = res - 1 |
| 4203 |
res1--; // res1 is now even |
| 4204 |
} // else the result is already correctly rounded to nearest-even |
| 4205 |
} |
| 4206 |
// at this point the result is finite, non-zero canonical normal or subnormal, |
| 4207 |
// and in most cases overflow or underflow will not occur |
| 4208 |
|
| 4209 |
// determine the number of digits q in the result |
| 4210 |
// q = nr. of decimal digits in x |
| 4211 |
// determine first the nr. of bits in x |
| 4212 |
if (C.w[1] == 0) { |
| 4213 |
if (C.w[0] >= 0x0020000000000000ull) { // x >= 2^53 |
| 4214 |
// split the 64-bit value in two 32-bit halves to avoid rounding errors |
| 4215 |
if (C.w[0] >= 0x0000000100000000ull) { // x >= 2^32 |
| 4216 |
tmp.d = (double) (C.w[0] >> 32); // exact conversion |
| 4217 |
nr_bits = |
| 4218 |
33 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 4219 |
} else { // x < 2^32 |
| 4220 |
tmp.d = (double) (C.w[0]); // exact conversion |
| 4221 |
nr_bits = |
| 4222 |
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 4223 |
} |
| 4224 |
} else { // if x < 2^53 |
| 4225 |
tmp.d = (double) C.w[0]; // exact conversion |
| 4226 |
nr_bits = |
| 4227 |
1 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 4228 |
} |
| 4229 |
} else { // C.w[1] != 0 => nr. bits = 64 + nr_bits (C.w[1]) |
| 4230 |
tmp.d = (double) C.w[1]; // exact conversion |
| 4231 |
nr_bits = |
| 4232 |
65 + ((((unsigned int) (tmp.ui64 >> 52)) & 0x7ff) - 0x3ff); |
| 4233 |
} |
| 4234 |
q = nr_digits[nr_bits - 1].digits; |
| 4235 |
if (q == 0) { |
| 4236 |
q = nr_digits[nr_bits - 1].digits1; |
| 4237 |
if (C.w[1] > nr_digits[nr_bits - 1].threshold_hi || |
| 4238 |
(C.w[1] == nr_digits[nr_bits - 1].threshold_hi && |
| 4239 |
C.w[0] >= nr_digits[nr_bits - 1].threshold_lo)) |
| 4240 |
q++; |
| 4241 |
} |
| 4242 |
// if q > 16, round to nearest even to 16 digits (but for underflow it may |
| 4243 |
// have to be truncated even more) |
| 4244 |
if (q > 16) { |
| 4245 |
x0 = q - 16; |
| 4246 |
if (q <= 18) { |
| 4247 |
round64_2_18 (q, x0, C.w[0], &res1, &incr_exp, |
| 4248 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 4249 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 4250 |
} else { // 19 <= q <= 34 |
| 4251 |
round128_19_38 (q, x0, C, &res128, &incr_exp, |
| 4252 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 4253 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 4254 |
res1 = res128.w[0]; // the result fits in 64 bits |
| 4255 |
} |
| 4256 |
unbexp = unbexp + x0; |
| 4257 |
if (incr_exp) |
| 4258 |
unbexp++; |
| 4259 |
q = 16; // need to set in case denormalization is necessary |
| 4260 |
} else { |
| 4261 |
// the result does not require a second rounding (and it must have |
| 4262 |
// been exact in the first rounding, since q <= 16) |
| 4263 |
res1 = C.w[0]; |
| 4264 |
} |
| 4265 |
|
| 4266 |
// avoid a double rounding error |
| 4267 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 4268 |
is_midpoint_lt_even) { // double rounding error upward |
| 4269 |
// res = res - 1 |
| 4270 |
res1--; // res1 becomes odd |
| 4271 |
is_midpoint_lt_even = 0; |
| 4272 |
is_inexact_lt_midpoint = 1; |
| 4273 |
if (res1 == 0x00038d7ea4c67fffull) { // 10^15 - 1 |
| 4274 |
res1 = 0x002386f26fc0ffffull; // 10^16 - 1 |
| 4275 |
unbexp--; |
| 4276 |
} |
| 4277 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 4278 |
is_midpoint_gt_even) { // double rounding error downward |
| 4279 |
// res = res + 1 |
| 4280 |
res1++; // res1 becomes odd (so it cannot be 10^16) |
| 4281 |
is_midpoint_gt_even = 0; |
| 4282 |
is_inexact_gt_midpoint = 1; |
| 4283 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 4284 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 4285 |
// if this second rounding was exact the result may still be |
| 4286 |
// inexact because of the first rounding |
| 4287 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 4288 |
is_inexact_gt_midpoint = 1; |
| 4289 |
} |
| 4290 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 4291 |
is_inexact_lt_midpoint = 1; |
| 4292 |
} |
| 4293 |
} else if (is_midpoint_gt_even && |
| 4294 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 4295 |
// pulled up to a midpoint |
| 4296 |
is_inexact_lt_midpoint = 1; |
| 4297 |
is_inexact_gt_midpoint = 0; |
| 4298 |
is_midpoint_lt_even = 0; |
| 4299 |
is_midpoint_gt_even = 0; |
| 4300 |
} else if (is_midpoint_lt_even && |
| 4301 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 4302 |
// pulled down to a midpoint |
| 4303 |
is_inexact_lt_midpoint = 0; |
| 4304 |
is_inexact_gt_midpoint = 1; |
| 4305 |
is_midpoint_lt_even = 0; |
| 4306 |
is_midpoint_gt_even = 0; |
| 4307 |
} else { |
| 4308 |
; |
| 4309 |
} |
| 4310 |
// this is the result rounded correctly to nearest even, with unbounded exp. |
| 4311 |
|
| 4312 |
// check for overflow |
| 4313 |
if (q + unbexp > P16 + expmax16) { |
| 4314 |
res1 = sign | 0x7800000000000000ull; |
| 4315 |
*pfpsf |= (INEXACT_EXCEPTION | OVERFLOW_EXCEPTION); |
| 4316 |
*pfpsf |= save_fpsf; |
| 4317 |
BID_RETURN (res1) |
| 4318 |
} else if (unbexp > expmax16) { // q + unbexp <= P16 + expmax16 |
| 4319 |
// not overflow; the result must be exact, and we can multiply res1 by |
| 4320 |
// 10^(unbexp - expmax16) and the product will fit in 16 decimal digits |
| 4321 |
scale = unbexp - expmax16; |
| 4322 |
res1 = res1 * ten2k64[scale]; // res1 * 10^scale |
| 4323 |
unbexp = expmax16; // unbexp - scale |
| 4324 |
} else { |
| 4325 |
; // continue |
| 4326 |
} |
| 4327 |
|
| 4328 |
// check for underflow |
| 4329 |
if (q + unbexp < P16 + expmin16) { |
| 4330 |
if (unbexp < expmin16) { |
| 4331 |
// we must truncate more of res |
| 4332 |
x0 = expmin16 - unbexp; // x0 >= 1 |
| 4333 |
is_inexact_lt_midpoint0 = is_inexact_lt_midpoint; |
| 4334 |
is_inexact_gt_midpoint0 = is_inexact_gt_midpoint; |
| 4335 |
is_midpoint_lt_even0 = is_midpoint_lt_even; |
| 4336 |
is_midpoint_gt_even0 = is_midpoint_gt_even; |
| 4337 |
is_inexact_lt_midpoint = 0; |
| 4338 |
is_inexact_gt_midpoint = 0; |
| 4339 |
is_midpoint_lt_even = 0; |
| 4340 |
is_midpoint_gt_even = 0; |
| 4341 |
// the number of decimal digits in res1 is q |
| 4342 |
if (x0 < q) { // 1 <= x0 <= q-1 => round res to q - x0 digits |
| 4343 |
// 2 <= q <= 16, 1 <= x0 <= 15 |
| 4344 |
round64_2_18 (q, x0, res1, &res1, &incr_exp, |
| 4345 |
&is_midpoint_lt_even, &is_midpoint_gt_even, |
| 4346 |
&is_inexact_lt_midpoint, &is_inexact_gt_midpoint); |
| 4347 |
if (incr_exp) { |
| 4348 |
// res1 = 10^(q-x0), 1 <= q - x0 <= q - 1, 1 <= q - x0 <= 15 |
| 4349 |
res1 = ten2k64[q - x0]; |
| 4350 |
} |
| 4351 |
unbexp = unbexp + x0; // expmin16 |
| 4352 |
} else if (x0 == q) { |
| 4353 |
// the second rounding is for 0.d(0)d(1)...d(q-1) * 10^emin |
| 4354 |
// determine relationship with 1/2 ulp |
| 4355 |
// q <= 16 |
| 4356 |
if (res1 < midpoint64[q - 1]) { // < 1/2 ulp |
| 4357 |
lt_half_ulp = 1; |
| 4358 |
is_inexact_lt_midpoint = 1; |
| 4359 |
} else if (res1 == midpoint64[q - 1]) { // = 1/2 ulp |
| 4360 |
eq_half_ulp = 1; |
| 4361 |
is_midpoint_gt_even = 1; |
| 4362 |
} else { // > 1/2 ulp |
| 4363 |
// gt_half_ulp = 1; |
| 4364 |
is_inexact_gt_midpoint = 1; |
| 4365 |
} |
| 4366 |
if (lt_half_ulp || eq_half_ulp) { |
| 4367 |
// res = +0.0 * 10^expmin16 |
| 4368 |
res1 = 0x0000000000000000ull; |
| 4369 |
} else { // if (gt_half_ulp) |
| 4370 |
// res = +1 * 10^expmin16 |
| 4371 |
res1 = 0x0000000000000001ull; |
| 4372 |
} |
| 4373 |
unbexp = expmin16; |
| 4374 |
} else { // if (x0 > q) |
| 4375 |
// the second rounding is for 0.0...d(0)d(1)...d(q-1) * 10^emin |
| 4376 |
res1 = 0x0000000000000000ull; |
| 4377 |
unbexp = expmin16; |
| 4378 |
is_inexact_lt_midpoint = 1; |
| 4379 |
} |
| 4380 |
// avoid a double rounding error |
| 4381 |
if ((is_inexact_gt_midpoint0 || is_midpoint_lt_even0) && |
| 4382 |
is_midpoint_lt_even) { // double rounding error upward |
| 4383 |
// res = res - 1 |
| 4384 |
res1--; // res1 becomes odd |
| 4385 |
is_midpoint_lt_even = 0; |
| 4386 |
is_inexact_lt_midpoint = 1; |
| 4387 |
} else if ((is_inexact_lt_midpoint0 || is_midpoint_gt_even0) && |
| 4388 |
is_midpoint_gt_even) { // double rounding error downward |
| 4389 |
// res = res + 1 |
| 4390 |
res1++; // res1 becomes odd |
| 4391 |
is_midpoint_gt_even = 0; |
| 4392 |
is_inexact_gt_midpoint = 1; |
| 4393 |
} else if (!is_midpoint_lt_even && !is_midpoint_gt_even && |
| 4394 |
!is_inexact_lt_midpoint && !is_inexact_gt_midpoint) { |
| 4395 |
// if this rounding was exact the result may still be |
| 4396 |
// inexact because of the previous roundings |
| 4397 |
if (is_inexact_gt_midpoint0 || is_midpoint_lt_even0) { |
| 4398 |
is_inexact_gt_midpoint = 1; |
| 4399 |
} |
| 4400 |
if (is_inexact_lt_midpoint0 || is_midpoint_gt_even0) { |
| 4401 |
is_inexact_lt_midpoint = 1; |
| 4402 |
} |
| 4403 |
} else if (is_midpoint_gt_even && |
| 4404 |
(is_inexact_gt_midpoint0 || is_midpoint_lt_even0)) { |
| 4405 |
// pulled up to a midpoint |
| 4406 |
is_inexact_lt_midpoint = 1; |
| 4407 |
is_inexact_gt_midpoint = 0; |
| 4408 |
is_midpoint_lt_even = 0; |
| 4409 |
is_midpoint_gt_even = 0; |
| 4410 |
} else if (is_midpoint_lt_even && |
| 4411 |
(is_inexact_lt_midpoint0 || is_midpoint_gt_even0)) { |
| 4412 |
// pulled down to a midpoint |
| 4413 |
is_inexact_lt_midpoint = 0; |
| 4414 |
is_inexact_gt_midpoint = 1; |
| 4415 |
is_midpoint_lt_even = 0; |
| 4416 |
is_midpoint_gt_even = 0; |
| 4417 |
} else { |
| 4418 |
; |
| 4419 |
} |
| 4420 |
} |
| 4421 |
// else if unbexp >= emin then q < P (because q + unbexp < P16 + expmin16) |
| 4422 |
// and the result is tiny and exact |
| 4423 |
|
| 4424 |
// check for inexact result |
| 4425 |
if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 4426 |
is_midpoint_lt_even || is_midpoint_gt_even || |
| 4427 |
is_inexact_lt_midpoint0 || is_inexact_gt_midpoint0 || |
| 4428 |
is_midpoint_lt_even0 || is_midpoint_gt_even0) { |
| 4429 |
// set the inexact flag and the underflow flag |
| 4430 |
*pfpsf |= (INEXACT_EXCEPTION | UNDERFLOW_EXCEPTION); |
| 4431 |
} |
| 4432 |
} else if (is_inexact_lt_midpoint || is_inexact_gt_midpoint || |
| 4433 |
is_midpoint_lt_even || is_midpoint_gt_even) { |
| 4434 |
*pfpsf |= INEXACT_EXCEPTION; |
| 4435 |
} |
| 4436 |
// this is the result rounded correctly to nearest, with bounded exponent |
| 4437 |
|
| 4438 |
if (rnd_mode == ROUNDING_TIES_AWAY && is_midpoint_gt_even) { // correction |
| 4439 |
// res = res + 1 |
| 4440 |
res1++; // res1 is now odd |
| 4441 |
} // else the result is already correct |
| 4442 |
|
| 4443 |
// assemble the result |
| 4444 |
if (res1 < 0x0020000000000000ull) { // res < 2^53 |
| 4445 |
res1 = sign | ((UINT64) (unbexp + 398) << 53) | res1; |
| 4446 |
} else { // res1 >= 2^53 |
| 4447 |
res1 = sign | MASK_STEERING_BITS | |
| 4448 |
((UINT64) (unbexp + 398) << 51) | (res1 & MASK_BINARY_SIG2); |
| 4449 |
} |
| 4450 |
|
| 4451 |
// correction needed for tininess detection before rounding |
| 4452 |
if (((res1 & 0x7fffffffffffffffull) == 1000000000000000ull) && |
| 4453 |
// 10^15*10^-398 |
| 4454 |
(((rnd_mode == ROUNDING_TO_NEAREST || |
| 4455 |
rnd_mode == ROUNDING_TIES_AWAY) && |
| 4456 |
(is_midpoint_lt_even || is_inexact_gt_midpoint)) || |
| 4457 |
((((rnd_mode == ROUNDING_UP) && !(res1 & MASK_SIGN)) || |
| 4458 |
((rnd_mode == ROUNDING_DOWN) && (res1 & MASK_SIGN))) |
| 4459 |
&& (is_midpoint_lt_even || is_midpoint_gt_even || |
| 4460 |
is_inexact_lt_midpoint || is_inexact_gt_midpoint)))) { |
| 4461 |
*pfpsf |= UNDERFLOW_EXCEPTION; |
| 4462 |
} |
| 4463 |
|
| 4464 |
*pfpsf |= save_fpsf; |
| 4465 |
BID_RETURN (res1); |
| 4466 |
} |