| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#include "Python.h" |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#ifndef PY_NO_SHORT_FLOAT_REPR |
| 122 |
|
| 123 |
#include "float.h" |
| 124 |
|
| 125 |
#define MALLOC PyMem_Malloc |
| 126 |
#define FREE PyMem_Free |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754 |
| 132 |
# define IEEE_8087 |
| 133 |
#endif |
| 134 |
#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \ |
| 135 |
defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754) |
| 136 |
# define IEEE_MC68k |
| 137 |
#endif |
| 138 |
#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1 |
| 139 |
#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined." |
| 140 |
#endif |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \ |
| 145 |
defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)) |
| 146 |
#error "doubles and ints have incompatible endianness" |
| 147 |
#endif |
| 148 |
|
| 149 |
#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) |
| 150 |
#error "doubles and ints have incompatible endianness" |
| 151 |
#endif |
| 152 |
|
| 153 |
|
| 154 |
#if defined(HAVE_UINT32_T) && defined(HAVE_INT32_T) |
| 155 |
typedef PY_UINT32_T ULong; |
| 156 |
typedef PY_INT32_T Long; |
| 157 |
#else |
| 158 |
#error "Failed to find an exact-width 32-bit integer type" |
| 159 |
#endif |
| 160 |
|
| 161 |
#if defined(HAVE_UINT64_T) |
| 162 |
#define ULLong PY_UINT64_T |
| 163 |
#else |
| 164 |
#undef ULLong |
| 165 |
#endif |
| 166 |
|
| 167 |
#undef DEBUG |
| 168 |
#ifdef Py_DEBUG |
| 169 |
#define DEBUG |
| 170 |
#endif |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#ifdef DEBUG |
| 175 |
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} |
| 176 |
#endif |
| 177 |
|
| 178 |
#ifndef PRIVATE_MEM |
| 179 |
#define PRIVATE_MEM 2304 |
| 180 |
#endif |
| 181 |
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) |
| 182 |
static double private_mem[PRIVATE_mem], *pmem_next = private_mem; |
| 183 |
|
| 184 |
#ifdef __cplusplus |
| 185 |
extern "C" { |
| 186 |
#endif |
| 187 |
|
| 188 |
typedef union { double d; ULong L[2]; } U; |
| 189 |
|
| 190 |
#ifdef IEEE_8087 |
| 191 |
#define word0(x) (x)->L[1] |
| 192 |
#define word1(x) (x)->L[0] |
| 193 |
#else |
| 194 |
#define word0(x) (x)->L[0] |
| 195 |
#define word1(x) (x)->L[1] |
| 196 |
#endif |
| 197 |
#define dval(x) (x)->d |
| 198 |
|
| 199 |
#ifndef STRTOD_DIGLIM |
| 200 |
#define STRTOD_DIGLIM 40 |
| 201 |
#endif |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
#ifndef MAX_ABS_EXP |
| 207 |
#define MAX_ABS_EXP 1100000000U |
| 208 |
#endif |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
#ifndef MAX_DIGITS |
| 215 |
#define MAX_DIGITS 1000000000U |
| 216 |
#endif |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#if MAX_ABS_EXP > INT_MAX |
| 221 |
#error "MAX_ABS_EXP should fit in an int" |
| 222 |
#endif |
| 223 |
#if MAX_DIGITS > INT_MAX |
| 224 |
#error "MAX_DIGITS should fit in an int" |
| 225 |
#endif |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
#if defined(IEEE_8087) |
| 232 |
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ |
| 233 |
((unsigned short *)a)[0] = (unsigned short)c, a++) |
| 234 |
#else |
| 235 |
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ |
| 236 |
((unsigned short *)a)[1] = (unsigned short)c, a++) |
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#define Exp_shift 20 |
| 246 |
#define Exp_shift1 20 |
| 247 |
#define Exp_msk1 0x100000 |
| 248 |
#define Exp_msk11 0x100000 |
| 249 |
#define Exp_mask 0x7ff00000 |
| 250 |
#define P 53 |
| 251 |
#define Nbits 53 |
| 252 |
#define Bias 1023 |
| 253 |
#define Emax 1023 |
| 254 |
#define Emin (-1022) |
| 255 |
#define Etiny (-1074) |
| 256 |
#define Exp_1 0x3ff00000 |
| 257 |
#define Exp_11 0x3ff00000 |
| 258 |
#define Ebits 11 |
| 259 |
#define Frac_mask 0xfffff |
| 260 |
#define Frac_mask1 0xfffff |
| 261 |
#define Ten_pmax 22 |
| 262 |
#define Bletch 0x10 |
| 263 |
#define Bndry_mask 0xfffff |
| 264 |
#define Bndry_mask1 0xfffff |
| 265 |
#define Sign_bit 0x80000000 |
| 266 |
#define Log2P 1 |
| 267 |
#define Tiny0 0 |
| 268 |
#define Tiny1 1 |
| 269 |
#define Quick_max 14 |
| 270 |
#define Int_max 14 |
| 271 |
|
| 272 |
#ifndef Flt_Rounds |
| 273 |
#ifdef FLT_ROUNDS |
| 274 |
#define Flt_Rounds FLT_ROUNDS |
| 275 |
#else |
| 276 |
#define Flt_Rounds 1 |
| 277 |
#endif |
| 278 |
#endif |
| 279 |
|
| 280 |
#define Rounding Flt_Rounds |
| 281 |
|
| 282 |
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) |
| 283 |
#define Big1 0xffffffff |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
typedef struct BCinfo BCinfo; |
| 288 |
struct |
| 289 |
BCinfo { |
| 290 |
int e0, nd, nd0, scale; |
| 291 |
}; |
| 292 |
|
| 293 |
#define FFFFFFFF 0xffffffffUL |
| 294 |
|
| 295 |
#define Kmax 7 |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
struct |
| 320 |
Bigint { |
| 321 |
struct Bigint *next; |
| 322 |
int k, maxwds, sign, wds; |
| 323 |
ULong x[1]; |
| 324 |
}; |
| 325 |
|
| 326 |
typedef struct Bigint Bigint; |
| 327 |
|
| 328 |
#ifndef Py_USING_MEMORY_DEBUGGER |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static Bigint *freelist[Kmax+1]; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
static Bigint * |
| 354 |
Balloc(int k) |
| 355 |
{ |
| 356 |
int x; |
| 357 |
Bigint *rv; |
| 358 |
unsigned int len; |
| 359 |
|
| 360 |
if (k <= Kmax && (rv = freelist[k])) |
| 361 |
freelist[k] = rv->next; |
| 362 |
else { |
| 363 |
x = 1 << k; |
| 364 |
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) |
| 365 |
/sizeof(double); |
| 366 |
if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) { |
| 367 |
rv = (Bigint*)pmem_next; |
| 368 |
pmem_next += len; |
| 369 |
} |
| 370 |
else { |
| 371 |
rv = (Bigint*)MALLOC(len*sizeof(double)); |
| 372 |
if (rv == NULL) |
| 373 |
return NULL; |
| 374 |
} |
| 375 |
rv->k = k; |
| 376 |
rv->maxwds = x; |
| 377 |
} |
| 378 |
rv->sign = rv->wds = 0; |
| 379 |
return rv; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static void |
| 385 |
Bfree(Bigint *v) |
| 386 |
{ |
| 387 |
if (v) { |
| 388 |
if (v->k > Kmax) |
| 389 |
FREE((void*)v); |
| 390 |
else { |
| 391 |
v->next = freelist[v->k]; |
| 392 |
freelist[v->k] = v; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
#else |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
static Bigint * |
| 407 |
Balloc(int k) |
| 408 |
{ |
| 409 |
int x; |
| 410 |
Bigint *rv; |
| 411 |
unsigned int len; |
| 412 |
|
| 413 |
x = 1 << k; |
| 414 |
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) |
| 415 |
/sizeof(double); |
| 416 |
|
| 417 |
rv = (Bigint*)MALLOC(len*sizeof(double)); |
| 418 |
if (rv == NULL) |
| 419 |
return NULL; |
| 420 |
|
| 421 |
rv->k = k; |
| 422 |
rv->maxwds = x; |
| 423 |
rv->sign = rv->wds = 0; |
| 424 |
return rv; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static void |
| 430 |
Bfree(Bigint *v) |
| 431 |
{ |
| 432 |
if (v) { |
| 433 |
FREE((void*)v); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
#endif |
| 438 |
|
| 439 |
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ |
| 440 |
y->wds*sizeof(Long) + 2*sizeof(int)) |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static Bigint * |
| 447 |
multadd(Bigint *b, int m, int a) |
| 448 |
{ |
| 449 |
int i, wds; |
| 450 |
#ifdef ULLong |
| 451 |
ULong *x; |
| 452 |
ULLong carry, y; |
| 453 |
#else |
| 454 |
ULong carry, *x, y; |
| 455 |
ULong xi, z; |
| 456 |
#endif |
| 457 |
Bigint *b1; |
| 458 |
|
| 459 |
wds = b->wds; |
| 460 |
x = b->x; |
| 461 |
i = 0; |
| 462 |
carry = a; |
| 463 |
do { |
| 464 |
#ifdef ULLong |
| 465 |
y = *x * (ULLong)m + carry; |
| 466 |
carry = y >> 32; |
| 467 |
*x++ = (ULong)(y & FFFFFFFF); |
| 468 |
#else |
| 469 |
xi = *x; |
| 470 |
y = (xi & 0xffff) * m + carry; |
| 471 |
z = (xi >> 16) * m + (y >> 16); |
| 472 |
carry = z >> 16; |
| 473 |
*x++ = (z << 16) + (y & 0xffff); |
| 474 |
#endif |
| 475 |
} |
| 476 |
while(++i < wds); |
| 477 |
if (carry) { |
| 478 |
if (wds >= b->maxwds) { |
| 479 |
b1 = Balloc(b->k+1); |
| 480 |
if (b1 == NULL){ |
| 481 |
Bfree(b); |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
Bcopy(b1, b); |
| 485 |
Bfree(b); |
| 486 |
b = b1; |
| 487 |
} |
| 488 |
b->x[wds++] = (ULong)carry; |
| 489 |
b->wds = wds; |
| 490 |
} |
| 491 |
return b; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
static Bigint * |
| 501 |
s2b(const char *s, int nd0, int nd, ULong y9) |
| 502 |
{ |
| 503 |
Bigint *b; |
| 504 |
int i, k; |
| 505 |
Long x, y; |
| 506 |
|
| 507 |
x = (nd + 8) / 9; |
| 508 |
for(k = 0, y = 1; x > y; y <<= 1, k++) ; |
| 509 |
b = Balloc(k); |
| 510 |
if (b == NULL) |
| 511 |
return NULL; |
| 512 |
b->x[0] = y9; |
| 513 |
b->wds = 1; |
| 514 |
|
| 515 |
if (nd <= 9) |
| 516 |
return b; |
| 517 |
|
| 518 |
s += 9; |
| 519 |
for (i = 9; i < nd0; i++) { |
| 520 |
b = multadd(b, 10, *s++ - '0'); |
| 521 |
if (b == NULL) |
| 522 |
return NULL; |
| 523 |
} |
| 524 |
s++; |
| 525 |
for(; i < nd; i++) { |
| 526 |
b = multadd(b, 10, *s++ - '0'); |
| 527 |
if (b == NULL) |
| 528 |
return NULL; |
| 529 |
} |
| 530 |
return b; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
static int |
| 536 |
hi0bits(ULong x) |
| 537 |
{ |
| 538 |
int k = 0; |
| 539 |
|
| 540 |
if (!(x & 0xffff0000)) { |
| 541 |
k = 16; |
| 542 |
x <<= 16; |
| 543 |
} |
| 544 |
if (!(x & 0xff000000)) { |
| 545 |
k += 8; |
| 546 |
x <<= 8; |
| 547 |
} |
| 548 |
if (!(x & 0xf0000000)) { |
| 549 |
k += 4; |
| 550 |
x <<= 4; |
| 551 |
} |
| 552 |
if (!(x & 0xc0000000)) { |
| 553 |
k += 2; |
| 554 |
x <<= 2; |
| 555 |
} |
| 556 |
if (!(x & 0x80000000)) { |
| 557 |
k++; |
| 558 |
if (!(x & 0x40000000)) |
| 559 |
return 32; |
| 560 |
} |
| 561 |
return k; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static int |
| 568 |
lo0bits(ULong *y) |
| 569 |
{ |
| 570 |
int k; |
| 571 |
ULong x = *y; |
| 572 |
|
| 573 |
if (x & 7) { |
| 574 |
if (x & 1) |
| 575 |
return 0; |
| 576 |
if (x & 2) { |
| 577 |
*y = x >> 1; |
| 578 |
return 1; |
| 579 |
} |
| 580 |
*y = x >> 2; |
| 581 |
return 2; |
| 582 |
} |
| 583 |
k = 0; |
| 584 |
if (!(x & 0xffff)) { |
| 585 |
k = 16; |
| 586 |
x >>= 16; |
| 587 |
} |
| 588 |
if (!(x & 0xff)) { |
| 589 |
k += 8; |
| 590 |
x >>= 8; |
| 591 |
} |
| 592 |
if (!(x & 0xf)) { |
| 593 |
k += 4; |
| 594 |
x >>= 4; |
| 595 |
} |
| 596 |
if (!(x & 0x3)) { |
| 597 |
k += 2; |
| 598 |
x >>= 2; |
| 599 |
} |
| 600 |
if (!(x & 1)) { |
| 601 |
k++; |
| 602 |
x >>= 1; |
| 603 |
if (!x) |
| 604 |
return 32; |
| 605 |
} |
| 606 |
*y = x; |
| 607 |
return k; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
static Bigint * |
| 613 |
i2b(int i) |
| 614 |
{ |
| 615 |
Bigint *b; |
| 616 |
|
| 617 |
b = Balloc(1); |
| 618 |
if (b == NULL) |
| 619 |
return NULL; |
| 620 |
b->x[0] = i; |
| 621 |
b->wds = 1; |
| 622 |
return b; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
static Bigint * |
| 629 |
mult(Bigint *a, Bigint *b) |
| 630 |
{ |
| 631 |
Bigint *c; |
| 632 |
int k, wa, wb, wc; |
| 633 |
ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; |
| 634 |
ULong y; |
| 635 |
#ifdef ULLong |
| 636 |
ULLong carry, z; |
| 637 |
#else |
| 638 |
ULong carry, z; |
| 639 |
ULong z2; |
| 640 |
#endif |
| 641 |
|
| 642 |
if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) { |
| 643 |
c = Balloc(0); |
| 644 |
if (c == NULL) |
| 645 |
return NULL; |
| 646 |
c->wds = 1; |
| 647 |
c->x[0] = 0; |
| 648 |
return c; |
| 649 |
} |
| 650 |
|
| 651 |
if (a->wds < b->wds) { |
| 652 |
c = a; |
| 653 |
a = b; |
| 654 |
b = c; |
| 655 |
} |
| 656 |
k = a->k; |
| 657 |
wa = a->wds; |
| 658 |
wb = b->wds; |
| 659 |
wc = wa + wb; |
| 660 |
if (wc > a->maxwds) |
| 661 |
k++; |
| 662 |
c = Balloc(k); |
| 663 |
if (c == NULL) |
| 664 |
return NULL; |
| 665 |
for(x = c->x, xa = x + wc; x < xa; x++) |
| 666 |
*x = 0; |
| 667 |
xa = a->x; |
| 668 |
xae = xa + wa; |
| 669 |
xb = b->x; |
| 670 |
xbe = xb + wb; |
| 671 |
xc0 = c->x; |
| 672 |
#ifdef ULLong |
| 673 |
for(; xb < xbe; xc0++) { |
| 674 |
if ((y = *xb++)) { |
| 675 |
x = xa; |
| 676 |
xc = xc0; |
| 677 |
carry = 0; |
| 678 |
do { |
| 679 |
z = *x++ * (ULLong)y + *xc + carry; |
| 680 |
carry = z >> 32; |
| 681 |
*xc++ = (ULong)(z & FFFFFFFF); |
| 682 |
} |
| 683 |
while(x < xae); |
| 684 |
*xc = (ULong)carry; |
| 685 |
} |
| 686 |
} |
| 687 |
#else |
| 688 |
for(; xb < xbe; xb++, xc0++) { |
| 689 |
if (y = *xb & 0xffff) { |
| 690 |
x = xa; |
| 691 |
xc = xc0; |
| 692 |
carry = 0; |
| 693 |
do { |
| 694 |
z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; |
| 695 |
carry = z >> 16; |
| 696 |
z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; |
| 697 |
carry = z2 >> 16; |
| 698 |
Storeinc(xc, z2, z); |
| 699 |
} |
| 700 |
while(x < xae); |
| 701 |
*xc = carry; |
| 702 |
} |
| 703 |
if (y = *xb >> 16) { |
| 704 |
x = xa; |
| 705 |
xc = xc0; |
| 706 |
carry = 0; |
| 707 |
z2 = *xc; |
| 708 |
do { |
| 709 |
z = (*x & 0xffff) * y + (*xc >> 16) + carry; |
| 710 |
carry = z >> 16; |
| 711 |
Storeinc(xc, z, z2); |
| 712 |
z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; |
| 713 |
carry = z2 >> 16; |
| 714 |
} |
| 715 |
while(x < xae); |
| 716 |
*xc = z2; |
| 717 |
} |
| 718 |
} |
| 719 |
#endif |
| 720 |
for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; |
| 721 |
c->wds = wc; |
| 722 |
return c; |
| 723 |
} |
| 724 |
|
| 725 |
#ifndef Py_USING_MEMORY_DEBUGGER |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
static Bigint *p5s; |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
static Bigint * |
| 736 |
pow5mult(Bigint *b, int k) |
| 737 |
{ |
| 738 |
Bigint *b1, *p5, *p51; |
| 739 |
int i; |
| 740 |
static int p05[3] = { 5, 25, 125 }; |
| 741 |
|
| 742 |
if ((i = k & 3)) { |
| 743 |
b = multadd(b, p05[i-1], 0); |
| 744 |
if (b == NULL) |
| 745 |
return NULL; |
| 746 |
} |
| 747 |
|
| 748 |
if (!(k >>= 2)) |
| 749 |
return b; |
| 750 |
p5 = p5s; |
| 751 |
if (!p5) { |
| 752 |
|
| 753 |
p5 = i2b(625); |
| 754 |
if (p5 == NULL) { |
| 755 |
Bfree(b); |
| 756 |
return NULL; |
| 757 |
} |
| 758 |
p5s = p5; |
| 759 |
p5->next = 0; |
| 760 |
} |
| 761 |
for(;;) { |
| 762 |
if (k & 1) { |
| 763 |
b1 = mult(b, p5); |
| 764 |
Bfree(b); |
| 765 |
b = b1; |
| 766 |
if (b == NULL) |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
if (!(k >>= 1)) |
| 770 |
break; |
| 771 |
p51 = p5->next; |
| 772 |
if (!p51) { |
| 773 |
p51 = mult(p5,p5); |
| 774 |
if (p51 == NULL) { |
| 775 |
Bfree(b); |
| 776 |
return NULL; |
| 777 |
} |
| 778 |
p51->next = 0; |
| 779 |
p5->next = p51; |
| 780 |
} |
| 781 |
p5 = p51; |
| 782 |
} |
| 783 |
return b; |
| 784 |
} |
| 785 |
|
| 786 |
#else |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
static Bigint * |
| 792 |
pow5mult(Bigint *b, int k) |
| 793 |
{ |
| 794 |
Bigint *b1, *p5, *p51; |
| 795 |
int i; |
| 796 |
static int p05[3] = { 5, 25, 125 }; |
| 797 |
|
| 798 |
if ((i = k & 3)) { |
| 799 |
b = multadd(b, p05[i-1], 0); |
| 800 |
if (b == NULL) |
| 801 |
return NULL; |
| 802 |
} |
| 803 |
|
| 804 |
if (!(k >>= 2)) |
| 805 |
return b; |
| 806 |
p5 = i2b(625); |
| 807 |
if (p5 == NULL) { |
| 808 |
Bfree(b); |
| 809 |
return NULL; |
| 810 |
} |
| 811 |
|
| 812 |
for(;;) { |
| 813 |
if (k & 1) { |
| 814 |
b1 = mult(b, p5); |
| 815 |
Bfree(b); |
| 816 |
b = b1; |
| 817 |
if (b == NULL) { |
| 818 |
Bfree(p5); |
| 819 |
return NULL; |
| 820 |
} |
| 821 |
} |
| 822 |
if (!(k >>= 1)) |
| 823 |
break; |
| 824 |
p51 = mult(p5, p5); |
| 825 |
Bfree(p5); |
| 826 |
p5 = p51; |
| 827 |
if (p5 == NULL) { |
| 828 |
Bfree(b); |
| 829 |
return NULL; |
| 830 |
} |
| 831 |
} |
| 832 |
Bfree(p5); |
| 833 |
return b; |
| 834 |
} |
| 835 |
|
| 836 |
#endif |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
static Bigint * |
| 843 |
lshift(Bigint *b, int k) |
| 844 |
{ |
| 845 |
int i, k1, n, n1; |
| 846 |
Bigint *b1; |
| 847 |
ULong *x, *x1, *xe, z; |
| 848 |
|
| 849 |
if (!k || (!b->x[0] && b->wds == 1)) |
| 850 |
return b; |
| 851 |
|
| 852 |
n = k >> 5; |
| 853 |
k1 = b->k; |
| 854 |
n1 = n + b->wds + 1; |
| 855 |
for(i = b->maxwds; n1 > i; i <<= 1) |
| 856 |
k1++; |
| 857 |
b1 = Balloc(k1); |
| 858 |
if (b1 == NULL) { |
| 859 |
Bfree(b); |
| 860 |
return NULL; |
| 861 |
} |
| 862 |
x1 = b1->x; |
| 863 |
for(i = 0; i < n; i++) |
| 864 |
*x1++ = 0; |
| 865 |
x = b->x; |
| 866 |
xe = x + b->wds; |
| 867 |
if (k &= 0x1f) { |
| 868 |
k1 = 32 - k; |
| 869 |
z = 0; |
| 870 |
do { |
| 871 |
*x1++ = *x << k | z; |
| 872 |
z = *x++ >> k1; |
| 873 |
} |
| 874 |
while(x < xe); |
| 875 |
if ((*x1 = z)) |
| 876 |
++n1; |
| 877 |
} |
| 878 |
else do |
| 879 |
*x1++ = *x++; |
| 880 |
while(x < xe); |
| 881 |
b1->wds = n1 - 1; |
| 882 |
Bfree(b); |
| 883 |
return b1; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
static int |
| 890 |
cmp(Bigint *a, Bigint *b) |
| 891 |
{ |
| 892 |
ULong *xa, *xa0, *xb, *xb0; |
| 893 |
int i, j; |
| 894 |
|
| 895 |
i = a->wds; |
| 896 |
j = b->wds; |
| 897 |
#ifdef DEBUG |
| 898 |
if (i > 1 && !a->x[i-1]) |
| 899 |
Bug("cmp called with a->x[a->wds-1] == 0"); |
| 900 |
if (j > 1 && !b->x[j-1]) |
| 901 |
Bug("cmp called with b->x[b->wds-1] == 0"); |
| 902 |
#endif |
| 903 |
if (i -= j) |
| 904 |
return i; |
| 905 |
xa0 = a->x; |
| 906 |
xa = xa0 + j; |
| 907 |
xb0 = b->x; |
| 908 |
xb = xb0 + j; |
| 909 |
for(;;) { |
| 910 |
if (*--xa != *--xb) |
| 911 |
return *xa < *xb ? -1 : 1; |
| 912 |
if (xa <= xa0) |
| 913 |
break; |
| 914 |
} |
| 915 |
return 0; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
static Bigint * |
| 923 |
diff(Bigint *a, Bigint *b) |
| 924 |
{ |
| 925 |
Bigint *c; |
| 926 |
int i, wa, wb; |
| 927 |
ULong *xa, *xae, *xb, *xbe, *xc; |
| 928 |
#ifdef ULLong |
| 929 |
ULLong borrow, y; |
| 930 |
#else |
| 931 |
ULong borrow, y; |
| 932 |
ULong z; |
| 933 |
#endif |
| 934 |
|
| 935 |
i = cmp(a,b); |
| 936 |
if (!i) { |
| 937 |
c = Balloc(0); |
| 938 |
if (c == NULL) |
| 939 |
return NULL; |
| 940 |
c->wds = 1; |
| 941 |
c->x[0] = 0; |
| 942 |
return c; |
| 943 |
} |
| 944 |
if (i < 0) { |
| 945 |
c = a; |
| 946 |
a = b; |
| 947 |
b = c; |
| 948 |
i = 1; |
| 949 |
} |
| 950 |
else |
| 951 |
i = 0; |
| 952 |
c = Balloc(a->k); |
| 953 |
if (c == NULL) |
| 954 |
return NULL; |
| 955 |
c->sign = i; |
| 956 |
wa = a->wds; |
| 957 |
xa = a->x; |
| 958 |
xae = xa + wa; |
| 959 |
wb = b->wds; |
| 960 |
xb = b->x; |
| 961 |
xbe = xb + wb; |
| 962 |
xc = c->x; |
| 963 |
borrow = 0; |
| 964 |
#ifdef ULLong |
| 965 |
do { |
| 966 |
y = (ULLong)*xa++ - *xb++ - borrow; |
| 967 |
borrow = y >> 32 & (ULong)1; |
| 968 |
*xc++ = (ULong)(y & FFFFFFFF); |
| 969 |
} |
| 970 |
while(xb < xbe); |
| 971 |
while(xa < xae) { |
| 972 |
y = *xa++ - borrow; |
| 973 |
borrow = y >> 32 & (ULong)1; |
| 974 |
*xc++ = (ULong)(y & FFFFFFFF); |
| 975 |
} |
| 976 |
#else |
| 977 |
do { |
| 978 |
y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; |
| 979 |
borrow = (y & 0x10000) >> 16; |
| 980 |
z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; |
| 981 |
borrow = (z & 0x10000) >> 16; |
| 982 |
Storeinc(xc, z, y); |
| 983 |
} |
| 984 |
while(xb < xbe); |
| 985 |
while(xa < xae) { |
| 986 |
y = (*xa & 0xffff) - borrow; |
| 987 |
borrow = (y & 0x10000) >> 16; |
| 988 |
z = (*xa++ >> 16) - borrow; |
| 989 |
borrow = (z & 0x10000) >> 16; |
| 990 |
Storeinc(xc, z, y); |
| 991 |
} |
| 992 |
#endif |
| 993 |
while(!*--xc) |
| 994 |
wa--; |
| 995 |
c->wds = wa; |
| 996 |
return c; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
static double |
| 1003 |
ulp(U *x) |
| 1004 |
{ |
| 1005 |
Long L; |
| 1006 |
U u; |
| 1007 |
|
| 1008 |
L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; |
| 1009 |
word0(&u) = L; |
| 1010 |
word1(&u) = 0; |
| 1011 |
return dval(&u); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
static double |
| 1017 |
b2d(Bigint *a, int *e) |
| 1018 |
{ |
| 1019 |
ULong *xa, *xa0, w, y, z; |
| 1020 |
int k; |
| 1021 |
U d; |
| 1022 |
|
| 1023 |
xa0 = a->x; |
| 1024 |
xa = xa0 + a->wds; |
| 1025 |
y = *--xa; |
| 1026 |
#ifdef DEBUG |
| 1027 |
if (!y) Bug("zero y in b2d"); |
| 1028 |
#endif |
| 1029 |
k = hi0bits(y); |
| 1030 |
*e = 32 - k; |
| 1031 |
if (k < Ebits) { |
| 1032 |
word0(&d) = Exp_1 | y >> (Ebits - k); |
| 1033 |
w = xa > xa0 ? *--xa : 0; |
| 1034 |
word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k); |
| 1035 |
goto ret_d; |
| 1036 |
} |
| 1037 |
z = xa > xa0 ? *--xa : 0; |
| 1038 |
if (k -= Ebits) { |
| 1039 |
word0(&d) = Exp_1 | y << k | z >> (32 - k); |
| 1040 |
y = xa > xa0 ? *--xa : 0; |
| 1041 |
word1(&d) = z << k | y >> (32 - k); |
| 1042 |
} |
| 1043 |
else { |
| 1044 |
word0(&d) = Exp_1 | y; |
| 1045 |
word1(&d) = z; |
| 1046 |
} |
| 1047 |
ret_d: |
| 1048 |
return dval(&d); |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
static Bigint * |
| 1073 |
sd2b(U *d, int scale, int *e) |
| 1074 |
{ |
| 1075 |
Bigint *b; |
| 1076 |
|
| 1077 |
b = Balloc(1); |
| 1078 |
if (b == NULL) |
| 1079 |
return NULL; |
| 1080 |
|
| 1081 |
|
| 1082 |
b->wds = 2; |
| 1083 |
b->x[0] = word1(d); |
| 1084 |
b->x[1] = word0(d) & Frac_mask; |
| 1085 |
*e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift); |
| 1086 |
if (*e < Etiny) |
| 1087 |
*e = Etiny; |
| 1088 |
else |
| 1089 |
b->x[1] |= Exp_msk1; |
| 1090 |
|
| 1091 |
|
| 1092 |
if (scale && (b->x[0] || b->x[1])) { |
| 1093 |
*e -= scale; |
| 1094 |
if (*e < Etiny) { |
| 1095 |
scale = Etiny - *e; |
| 1096 |
*e = Etiny; |
| 1097 |
|
| 1098 |
assert(0 < scale && scale <= P - 1); |
| 1099 |
if (scale >= 32) { |
| 1100 |
|
| 1101 |
assert(b->x[0] == 0); |
| 1102 |
b->x[0] = b->x[1]; |
| 1103 |
b->x[1] = 0; |
| 1104 |
scale -= 32; |
| 1105 |
} |
| 1106 |
if (scale) { |
| 1107 |
|
| 1108 |
assert(b->x[0] << (32 - scale) == 0); |
| 1109 |
b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale)); |
| 1110 |
b->x[1] >>= scale; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (!b->x[1]) |
| 1116 |
b->wds = 1; |
| 1117 |
|
| 1118 |
return b; |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
static Bigint * |
| 1131 |
d2b(U *d, int *e, int *bits) |
| 1132 |
{ |
| 1133 |
Bigint *b; |
| 1134 |
int de, k; |
| 1135 |
ULong *x, y, z; |
| 1136 |
int i; |
| 1137 |
|
| 1138 |
b = Balloc(1); |
| 1139 |
if (b == NULL) |
| 1140 |
return NULL; |
| 1141 |
x = b->x; |
| 1142 |
|
| 1143 |
z = word0(d) & Frac_mask; |
| 1144 |
word0(d) &= 0x7fffffff; |
| 1145 |
if ((de = (int)(word0(d) >> Exp_shift))) |
| 1146 |
z |= Exp_msk1; |
| 1147 |
if ((y = word1(d))) { |
| 1148 |
if ((k = lo0bits(&y))) { |
| 1149 |
x[0] = y | z << (32 - k); |
| 1150 |
z >>= k; |
| 1151 |
} |
| 1152 |
else |
| 1153 |
x[0] = y; |
| 1154 |
i = |
| 1155 |
b->wds = (x[1] = z) ? 2 : 1; |
| 1156 |
} |
| 1157 |
else { |
| 1158 |
k = lo0bits(&z); |
| 1159 |
x[0] = z; |
| 1160 |
i = |
| 1161 |
b->wds = 1; |
| 1162 |
k += 32; |
| 1163 |
} |
| 1164 |
if (de) { |
| 1165 |
*e = de - Bias - (P-1) + k; |
| 1166 |
*bits = P - k; |
| 1167 |
} |
| 1168 |
else { |
| 1169 |
*e = de - Bias - (P-1) + 1 + k; |
| 1170 |
*bits = 32*i - hi0bits(x[i-1]); |
| 1171 |
} |
| 1172 |
return b; |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
static double |
| 1179 |
ratio(Bigint *a, Bigint *b) |
| 1180 |
{ |
| 1181 |
U da, db; |
| 1182 |
int k, ka, kb; |
| 1183 |
|
| 1184 |
dval(&da) = b2d(a, &ka); |
| 1185 |
dval(&db) = b2d(b, &kb); |
| 1186 |
k = ka - kb + 32*(a->wds - b->wds); |
| 1187 |
if (k > 0) |
| 1188 |
word0(&da) += k*Exp_msk1; |
| 1189 |
else { |
| 1190 |
k = -k; |
| 1191 |
word0(&db) += k*Exp_msk1; |
| 1192 |
} |
| 1193 |
return dval(&da) / dval(&db); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static const double |
| 1197 |
tens[] = { |
| 1198 |
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 1199 |
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 1200 |
1e20, 1e21, 1e22 |
| 1201 |
}; |
| 1202 |
|
| 1203 |
static const double |
| 1204 |
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; |
| 1205 |
static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, |
| 1206 |
9007199254740992.*9007199254740992.e-256 |
| 1207 |
|
| 1208 |
}; |
| 1209 |
|
| 1210 |
|
| 1211 |
#define Scale_Bit 0x10 |
| 1212 |
#define n_bigtens 5 |
| 1213 |
|
| 1214 |
#define ULbits 32 |
| 1215 |
#define kshift 5 |
| 1216 |
#define kmask 31 |
| 1217 |
|
| 1218 |
|
| 1219 |
static int |
| 1220 |
dshift(Bigint *b, int p2) |
| 1221 |
{ |
| 1222 |
int rv = hi0bits(b->x[b->wds-1]) - 4; |
| 1223 |
if (p2 > 0) |
| 1224 |
rv -= p2; |
| 1225 |
return rv & kmask; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
static int |
| 1233 |
quorem(Bigint *b, Bigint *S) |
| 1234 |
{ |
| 1235 |
int n; |
| 1236 |
ULong *bx, *bxe, q, *sx, *sxe; |
| 1237 |
#ifdef ULLong |
| 1238 |
ULLong borrow, carry, y, ys; |
| 1239 |
#else |
| 1240 |
ULong borrow, carry, y, ys; |
| 1241 |
ULong si, z, zs; |
| 1242 |
#endif |
| 1243 |
|
| 1244 |
n = S->wds; |
| 1245 |
#ifdef DEBUG |
| 1246 |
if (b->wds > n) |
| 1247 |
Bug("oversize b in quorem"); |
| 1248 |
#endif |
| 1249 |
if (b->wds < n) |
| 1250 |
return 0; |
| 1251 |
sx = S->x; |
| 1252 |
sxe = sx + --n; |
| 1253 |
bx = b->x; |
| 1254 |
bxe = bx + n; |
| 1255 |
q = *bxe / (*sxe + 1); |
| 1256 |
#ifdef DEBUG |
| 1257 |
if (q > 9) |
| 1258 |
Bug("oversized quotient in quorem"); |
| 1259 |
#endif |
| 1260 |
if (q) { |
| 1261 |
borrow = 0; |
| 1262 |
carry = 0; |
| 1263 |
do { |
| 1264 |
#ifdef ULLong |
| 1265 |
ys = *sx++ * (ULLong)q + carry; |
| 1266 |
carry = ys >> 32; |
| 1267 |
y = *bx - (ys & FFFFFFFF) - borrow; |
| 1268 |
borrow = y >> 32 & (ULong)1; |
| 1269 |
*bx++ = (ULong)(y & FFFFFFFF); |
| 1270 |
#else |
| 1271 |
si = *sx++; |
| 1272 |
ys = (si & 0xffff) * q + carry; |
| 1273 |
zs = (si >> 16) * q + (ys >> 16); |
| 1274 |
carry = zs >> 16; |
| 1275 |
y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
| 1276 |
borrow = (y & 0x10000) >> 16; |
| 1277 |
z = (*bx >> 16) - (zs & 0xffff) - borrow; |
| 1278 |
borrow = (z & 0x10000) >> 16; |
| 1279 |
Storeinc(bx, z, y); |
| 1280 |
#endif |
| 1281 |
} |
| 1282 |
while(sx <= sxe); |
| 1283 |
if (!*bxe) { |
| 1284 |
bx = b->x; |
| 1285 |
while(--bxe > bx && !*bxe) |
| 1286 |
--n; |
| 1287 |
b->wds = n; |
| 1288 |
} |
| 1289 |
} |
| 1290 |
if (cmp(b, S) >= 0) { |
| 1291 |
q++; |
| 1292 |
borrow = 0; |
| 1293 |
carry = 0; |
| 1294 |
bx = b->x; |
| 1295 |
sx = S->x; |
| 1296 |
do { |
| 1297 |
#ifdef ULLong |
| 1298 |
ys = *sx++ + carry; |
| 1299 |
carry = ys >> 32; |
| 1300 |
y = *bx - (ys & FFFFFFFF) - borrow; |
| 1301 |
borrow = y >> 32 & (ULong)1; |
| 1302 |
*bx++ = (ULong)(y & FFFFFFFF); |
| 1303 |
#else |
| 1304 |
si = *sx++; |
| 1305 |
ys = (si & 0xffff) + carry; |
| 1306 |
zs = (si >> 16) + (ys >> 16); |
| 1307 |
carry = zs >> 16; |
| 1308 |
y = (*bx & 0xffff) - (ys & 0xffff) - borrow; |
| 1309 |
borrow = (y & 0x10000) >> 16; |
| 1310 |
z = (*bx >> 16) - (zs & 0xffff) - borrow; |
| 1311 |
borrow = (z & 0x10000) >> 16; |
| 1312 |
Storeinc(bx, z, y); |
| 1313 |
#endif |
| 1314 |
} |
| 1315 |
while(sx <= sxe); |
| 1316 |
bx = b->x; |
| 1317 |
bxe = bx + n; |
| 1318 |
if (!*bxe) { |
| 1319 |
while(--bxe > bx && !*bxe) |
| 1320 |
--n; |
| 1321 |
b->wds = n; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
return q; |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
static double |
| 1334 |
sulp(U *x, BCinfo *bc) |
| 1335 |
{ |
| 1336 |
U u; |
| 1337 |
|
| 1338 |
if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) { |
| 1339 |
|
| 1340 |
word0(&u) = (P+2)*Exp_msk1; |
| 1341 |
word1(&u) = 0; |
| 1342 |
return u.d; |
| 1343 |
} |
| 1344 |
else { |
| 1345 |
assert(word0(x) || word1(x)); |
| 1346 |
return ulp(x); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
static int |
| 1397 |
bigcomp(U *rv, const char *s0, BCinfo *bc) |
| 1398 |
{ |
| 1399 |
Bigint *b, *d; |
| 1400 |
int b2, d2, dd, i, nd, nd0, odd, p2, p5; |
| 1401 |
|
| 1402 |
nd = bc->nd; |
| 1403 |
nd0 = bc->nd0; |
| 1404 |
p5 = nd + bc->e0; |
| 1405 |
b = sd2b(rv, bc->scale, &p2); |
| 1406 |
if (b == NULL) |
| 1407 |
return -1; |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
odd = b->x[0] & 1; |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
b = lshift(b, 1); |
| 1416 |
if (b == NULL) |
| 1417 |
return -1; |
| 1418 |
b->x[0] |= 1; |
| 1419 |
p2--; |
| 1420 |
|
| 1421 |
p2 -= p5; |
| 1422 |
d = i2b(1); |
| 1423 |
if (d == NULL) { |
| 1424 |
Bfree(b); |
| 1425 |
return -1; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
if (p5 > 0) { |
| 1431 |
d = pow5mult(d, p5); |
| 1432 |
if (d == NULL) { |
| 1433 |
Bfree(b); |
| 1434 |
return -1; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
else if (p5 < 0) { |
| 1438 |
b = pow5mult(b, -p5); |
| 1439 |
if (b == NULL) { |
| 1440 |
Bfree(d); |
| 1441 |
return -1; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
if (p2 > 0) { |
| 1445 |
b2 = p2; |
| 1446 |
d2 = 0; |
| 1447 |
} |
| 1448 |
else { |
| 1449 |
b2 = 0; |
| 1450 |
d2 = -p2; |
| 1451 |
} |
| 1452 |
i = dshift(d, d2); |
| 1453 |
if ((b2 += i) > 0) { |
| 1454 |
b = lshift(b, b2); |
| 1455 |
if (b == NULL) { |
| 1456 |
Bfree(d); |
| 1457 |
return -1; |
| 1458 |
} |
| 1459 |
} |
| 1460 |
if ((d2 += i) > 0) { |
| 1461 |
d = lshift(d, d2); |
| 1462 |
if (d == NULL) { |
| 1463 |
Bfree(b); |
| 1464 |
return -1; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
if (cmp(b, d) >= 0) |
| 1472 |
|
| 1473 |
dd = -1; |
| 1474 |
else { |
| 1475 |
i = 0; |
| 1476 |
for(;;) { |
| 1477 |
b = multadd(b, 10, 0); |
| 1478 |
if (b == NULL) { |
| 1479 |
Bfree(d); |
| 1480 |
return -1; |
| 1481 |
} |
| 1482 |
dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d); |
| 1483 |
i++; |
| 1484 |
|
| 1485 |
if (dd) |
| 1486 |
break; |
| 1487 |
if (!b->x[0] && b->wds == 1) { |
| 1488 |
|
| 1489 |
dd = i < nd; |
| 1490 |
break; |
| 1491 |
} |
| 1492 |
if (!(i < nd)) { |
| 1493 |
|
| 1494 |
dd = -1; |
| 1495 |
break; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
Bfree(b); |
| 1500 |
Bfree(d); |
| 1501 |
if (dd > 0 || (dd == 0 && odd)) |
| 1502 |
dval(rv) += sulp(rv, bc); |
| 1503 |
return 0; |
| 1504 |
} |
| 1505 |
|
| 1506 |
double |
| 1507 |
_Py_dg_strtod(const char *s00, char **se) |
| 1508 |
{ |
| 1509 |
int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error; |
| 1510 |
int esign, i, j, k, lz, nd, nd0, odd, sign; |
| 1511 |
const char *s, *s0, *s1; |
| 1512 |
double aadj, aadj1; |
| 1513 |
U aadj2, adj, rv, rv0; |
| 1514 |
ULong y, z, abs_exp; |
| 1515 |
Long L; |
| 1516 |
BCinfo bc; |
| 1517 |
Bigint *bb = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL; |
| 1518 |
size_t ndigits, fraclen; |
| 1519 |
double result; |
| 1520 |
|
| 1521 |
dval(&rv) = 0.; |
| 1522 |
|
| 1523 |
|
| 1524 |
c = *(s = s00); |
| 1525 |
|
| 1526 |
|
| 1527 |
sign = 0; |
| 1528 |
switch (c) { |
| 1529 |
case '-': |
| 1530 |
sign = 1; |
| 1531 |
|
| 1532 |
case '+': |
| 1533 |
c = *++s; |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
s1 = s; |
| 1538 |
while (c == '0') |
| 1539 |
c = *++s; |
| 1540 |
lz = s != s1; |
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
s0 = s1 = s; |
| 1547 |
while ('0' <= c && c <= '9') |
| 1548 |
c = *++s; |
| 1549 |
ndigits = s - s1; |
| 1550 |
fraclen = 0; |
| 1551 |
|
| 1552 |
|
| 1553 |
if (c == '.') { |
| 1554 |
c = *++s; |
| 1555 |
if (!ndigits) { |
| 1556 |
s1 = s; |
| 1557 |
while (c == '0') |
| 1558 |
c = *++s; |
| 1559 |
lz = lz || s != s1; |
| 1560 |
fraclen += (s - s1); |
| 1561 |
s0 = s; |
| 1562 |
} |
| 1563 |
s1 = s; |
| 1564 |
while ('0' <= c && c <= '9') |
| 1565 |
c = *++s; |
| 1566 |
ndigits += s - s1; |
| 1567 |
fraclen += s - s1; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
if (!ndigits && !lz) { |
| 1574 |
if (se) |
| 1575 |
*se = (char *)s00; |
| 1576 |
goto parse_error; |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) { |
| 1582 |
if (se) |
| 1583 |
*se = (char *)s00; |
| 1584 |
goto parse_error; |
| 1585 |
} |
| 1586 |
nd = (int)ndigits; |
| 1587 |
nd0 = (int)ndigits - (int)fraclen; |
| 1588 |
|
| 1589 |
|
| 1590 |
e = 0; |
| 1591 |
if (c == 'e' || c == 'E') { |
| 1592 |
s00 = s; |
| 1593 |
c = *++s; |
| 1594 |
|
| 1595 |
|
| 1596 |
esign = 0; |
| 1597 |
switch (c) { |
| 1598 |
case '-': |
| 1599 |
esign = 1; |
| 1600 |
|
| 1601 |
case '+': |
| 1602 |
c = *++s; |
| 1603 |
} |
| 1604 |
|
| 1605 |
|
| 1606 |
s1 = s; |
| 1607 |
while (c == '0') |
| 1608 |
c = *++s; |
| 1609 |
lz = s != s1; |
| 1610 |
|
| 1611 |
|
| 1612 |
s1 = s; |
| 1613 |
abs_exp = 0; |
| 1614 |
while ('0' <= c && c <= '9') { |
| 1615 |
abs_exp = 10*abs_exp + (c - '0'); |
| 1616 |
c = *++s; |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
if (s - s1 > 9 || abs_exp > MAX_ABS_EXP) |
| 1623 |
e = (int)MAX_ABS_EXP; |
| 1624 |
else |
| 1625 |
e = (int)abs_exp; |
| 1626 |
if (esign) |
| 1627 |
e = -e; |
| 1628 |
|
| 1629 |
|
| 1630 |
if (s == s1 && !lz) |
| 1631 |
s = s00; |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
e -= nd - nd0; |
| 1636 |
if (nd0 <= 0) |
| 1637 |
nd0 = nd; |
| 1638 |
|
| 1639 |
|
| 1640 |
if (se) |
| 1641 |
*se = (char *)s; |
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
if (!nd) |
| 1646 |
goto ret; |
| 1647 |
for (i = nd; i > 0; ) { |
| 1648 |
--i; |
| 1649 |
if (s0[i < nd0 ? i : i+1] != '0') { |
| 1650 |
++i; |
| 1651 |
break; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
e += nd - i; |
| 1655 |
nd = i; |
| 1656 |
if (nd0 > nd) |
| 1657 |
nd0 = nd; |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
bc.e0 = e1 = e; |
| 1696 |
y = z = 0; |
| 1697 |
for (i = 0; i < nd; i++) { |
| 1698 |
if (i < 9) |
| 1699 |
y = 10*y + s0[i < nd0 ? i : i+1] - '0'; |
| 1700 |
else if (i < DBL_DIG+1) |
| 1701 |
z = 10*z + s0[i < nd0 ? i : i+1] - '0'; |
| 1702 |
else |
| 1703 |
break; |
| 1704 |
} |
| 1705 |
|
| 1706 |
k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; |
| 1707 |
dval(&rv) = y; |
| 1708 |
if (k > 9) { |
| 1709 |
dval(&rv) = tens[k - 9] * dval(&rv) + z; |
| 1710 |
} |
| 1711 |
if (nd <= DBL_DIG |
| 1712 |
&& Flt_Rounds == 1 |
| 1713 |
) { |
| 1714 |
if (!e) |
| 1715 |
goto ret; |
| 1716 |
if (e > 0) { |
| 1717 |
if (e <= Ten_pmax) { |
| 1718 |
dval(&rv) *= tens[e]; |
| 1719 |
goto ret; |
| 1720 |
} |
| 1721 |
i = DBL_DIG - nd; |
| 1722 |
if (e <= Ten_pmax + i) { |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
e -= i; |
| 1727 |
dval(&rv) *= tens[i]; |
| 1728 |
dval(&rv) *= tens[e]; |
| 1729 |
goto ret; |
| 1730 |
} |
| 1731 |
} |
| 1732 |
else if (e >= -Ten_pmax) { |
| 1733 |
dval(&rv) /= tens[-e]; |
| 1734 |
goto ret; |
| 1735 |
} |
| 1736 |
} |
| 1737 |
e1 += nd - k; |
| 1738 |
|
| 1739 |
bc.scale = 0; |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
if (e1 > 0) { |
| 1744 |
if ((i = e1 & 15)) |
| 1745 |
dval(&rv) *= tens[i]; |
| 1746 |
if (e1 &= ~15) { |
| 1747 |
if (e1 > DBL_MAX_10_EXP) |
| 1748 |
goto ovfl; |
| 1749 |
e1 >>= 4; |
| 1750 |
for(j = 0; e1 > 1; j++, e1 >>= 1) |
| 1751 |
if (e1 & 1) |
| 1752 |
dval(&rv) *= bigtens[j]; |
| 1753 |
|
| 1754 |
word0(&rv) -= P*Exp_msk1; |
| 1755 |
dval(&rv) *= bigtens[j]; |
| 1756 |
if ((z = word0(&rv) & Exp_mask) |
| 1757 |
> Exp_msk1*(DBL_MAX_EXP+Bias-P)) |
| 1758 |
goto ovfl; |
| 1759 |
if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { |
| 1760 |
|
| 1761 |
|
| 1762 |
word0(&rv) = Big0; |
| 1763 |
word1(&rv) = Big1; |
| 1764 |
} |
| 1765 |
else |
| 1766 |
word0(&rv) += P*Exp_msk1; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
else if (e1 < 0) { |
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
e1 = -e1; |
| 1781 |
if ((i = e1 & 15)) |
| 1782 |
dval(&rv) /= tens[i]; |
| 1783 |
if (e1 >>= 4) { |
| 1784 |
if (e1 >= 1 << n_bigtens) |
| 1785 |
goto undfl; |
| 1786 |
if (e1 & Scale_Bit) |
| 1787 |
bc.scale = 2*P; |
| 1788 |
for(j = 0; e1 > 0; j++, e1 >>= 1) |
| 1789 |
if (e1 & 1) |
| 1790 |
dval(&rv) *= tinytens[j]; |
| 1791 |
if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask) |
| 1792 |
>> Exp_shift)) > 0) { |
| 1793 |
|
| 1794 |
if (j >= 32) { |
| 1795 |
word1(&rv) = 0; |
| 1796 |
if (j >= 53) |
| 1797 |
word0(&rv) = (P+2)*Exp_msk1; |
| 1798 |
else |
| 1799 |
word0(&rv) &= 0xffffffff << (j-32); |
| 1800 |
} |
| 1801 |
else |
| 1802 |
word1(&rv) &= 0xffffffff << j; |
| 1803 |
} |
| 1804 |
if (!dval(&rv)) |
| 1805 |
goto undfl; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
bc.nd = nd; |
| 1814 |
bc.nd0 = nd0; |
| 1815 |
|
| 1816 |
|
| 1817 |
if (nd > STRTOD_DIGLIM) { |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
for (i = 18; i > 0; ) { |
| 1826 |
|
| 1827 |
|
| 1828 |
--i; |
| 1829 |
if (s0[i < nd0 ? i : i+1] != '0') { |
| 1830 |
++i; |
| 1831 |
break; |
| 1832 |
} |
| 1833 |
} |
| 1834 |
e += nd - i; |
| 1835 |
nd = i; |
| 1836 |
if (nd0 > nd) |
| 1837 |
nd0 = nd; |
| 1838 |
if (nd < 9) { |
| 1839 |
y = 0; |
| 1840 |
for(i = 0; i < nd0; ++i) |
| 1841 |
y = 10*y + s0[i] - '0'; |
| 1842 |
for(; i < nd; ++i) |
| 1843 |
y = 10*y + s0[i+1] - '0'; |
| 1844 |
} |
| 1845 |
} |
| 1846 |
bd0 = s2b(s0, nd0, nd, y); |
| 1847 |
if (bd0 == NULL) |
| 1848 |
goto failed_malloc; |
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
for(;;) { |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
bd = Balloc(bd0->k); |
| 1879 |
if (bd == NULL) { |
| 1880 |
goto failed_malloc; |
| 1881 |
} |
| 1882 |
Bcopy(bd, bd0); |
| 1883 |
bb = sd2b(&rv, bc.scale, &bbe); |
| 1884 |
if (bb == NULL) { |
| 1885 |
goto failed_malloc; |
| 1886 |
} |
| 1887 |
|
| 1888 |
|
| 1889 |
odd = bb->x[0] & 1; |
| 1890 |
|
| 1891 |
|
| 1892 |
bs = i2b(1); |
| 1893 |
if (bs == NULL) { |
| 1894 |
goto failed_malloc; |
| 1895 |
} |
| 1896 |
|
| 1897 |
if (e >= 0) { |
| 1898 |
bb2 = bb5 = 0; |
| 1899 |
bd2 = bd5 = e; |
| 1900 |
} |
| 1901 |
else { |
| 1902 |
bb2 = bb5 = -e; |
| 1903 |
bd2 = bd5 = 0; |
| 1904 |
} |
| 1905 |
if (bbe >= 0) |
| 1906 |
bb2 += bbe; |
| 1907 |
else |
| 1908 |
bd2 -= bbe; |
| 1909 |
bs2 = bb2; |
| 1910 |
bb2++; |
| 1911 |
bd2++; |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
i = bb2 < bd2 ? bb2 : bd2; |
| 1932 |
if (i > bs2) |
| 1933 |
i = bs2; |
| 1934 |
if (i > 0) { |
| 1935 |
bb2 -= i; |
| 1936 |
bd2 -= i; |
| 1937 |
bs2 -= i; |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
if (bb5 > 0) { |
| 1942 |
Bigint *bb1; |
| 1943 |
bs = pow5mult(bs, bb5); |
| 1944 |
if (bs == NULL) { |
| 1945 |
goto failed_malloc; |
| 1946 |
} |
| 1947 |
bb1 = mult(bs, bb); |
| 1948 |
Bfree(bb); |
| 1949 |
bb = bb1; |
| 1950 |
if (bb == NULL) { |
| 1951 |
goto failed_malloc; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
if (bb2 > 0) { |
| 1955 |
bb = lshift(bb, bb2); |
| 1956 |
if (bb == NULL) { |
| 1957 |
goto failed_malloc; |
| 1958 |
} |
| 1959 |
} |
| 1960 |
if (bd5 > 0) { |
| 1961 |
bd = pow5mult(bd, bd5); |
| 1962 |
if (bd == NULL) { |
| 1963 |
goto failed_malloc; |
| 1964 |
} |
| 1965 |
} |
| 1966 |
if (bd2 > 0) { |
| 1967 |
bd = lshift(bd, bd2); |
| 1968 |
if (bd == NULL) { |
| 1969 |
goto failed_malloc; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
if (bs2 > 0) { |
| 1973 |
bs = lshift(bs, bs2); |
| 1974 |
if (bs == NULL) { |
| 1975 |
goto failed_malloc; |
| 1976 |
} |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
delta = diff(bb, bd); |
| 1984 |
if (delta == NULL) { |
| 1985 |
goto failed_malloc; |
| 1986 |
} |
| 1987 |
dsign = delta->sign; |
| 1988 |
delta->sign = 0; |
| 1989 |
i = cmp(delta, bs); |
| 1990 |
if (bc.nd > nd && i <= 0) { |
| 1991 |
if (dsign) |
| 1992 |
break; |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) { |
| 2006 |
|
| 2007 |
|
| 2008 |
j = (int)(word0(&rv) & Exp_mask) >> Exp_shift; |
| 2009 |
|
| 2010 |
|
| 2011 |
if (j - bc.scale >= 2) { |
| 2012 |
dval(&rv) -= 0.5 * sulp(&rv, &bc); |
| 2013 |
break; |
| 2014 |
} |
| 2015 |
} |
| 2016 |
|
| 2017 |
{ |
| 2018 |
bc.nd = nd; |
| 2019 |
i = -1; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
if (i < 0) { |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask |
| 2028 |
|| (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1 |
| 2029 |
) { |
| 2030 |
break; |
| 2031 |
} |
| 2032 |
if (!delta->x[0] && delta->wds <= 1) { |
| 2033 |
|
| 2034 |
break; |
| 2035 |
} |
| 2036 |
delta = lshift(delta,Log2P); |
| 2037 |
if (delta == NULL) { |
| 2038 |
goto failed_malloc; |
| 2039 |
} |
| 2040 |
if (cmp(delta, bs) > 0) |
| 2041 |
goto drop_down; |
| 2042 |
break; |
| 2043 |
} |
| 2044 |
if (i == 0) { |
| 2045 |
|
| 2046 |
if (dsign) { |
| 2047 |
if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 |
| 2048 |
&& word1(&rv) == ( |
| 2049 |
(bc.scale && |
| 2050 |
(y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ? |
| 2051 |
(0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : |
| 2052 |
0xffffffff)) { |
| 2053 |
|
| 2054 |
word0(&rv) = (word0(&rv) & Exp_mask) |
| 2055 |
+ Exp_msk1 |
| 2056 |
; |
| 2057 |
word1(&rv) = 0; |
| 2058 |
dsign = 0; |
| 2059 |
break; |
| 2060 |
} |
| 2061 |
} |
| 2062 |
else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) { |
| 2063 |
drop_down: |
| 2064 |
|
| 2065 |
if (bc.scale) { |
| 2066 |
L = word0(&rv) & Exp_mask; |
| 2067 |
if (L <= (2*P+1)*Exp_msk1) { |
| 2068 |
if (L > (P+2)*Exp_msk1) |
| 2069 |
|
| 2070 |
|
| 2071 |
break; |
| 2072 |
|
| 2073 |
if (bc.nd > nd) |
| 2074 |
break; |
| 2075 |
goto undfl; |
| 2076 |
} |
| 2077 |
} |
| 2078 |
L = (word0(&rv) & Exp_mask) - Exp_msk1; |
| 2079 |
word0(&rv) = L | Bndry_mask1; |
| 2080 |
word1(&rv) = 0xffffffff; |
| 2081 |
break; |
| 2082 |
} |
| 2083 |
if (!odd) |
| 2084 |
break; |
| 2085 |
if (dsign) |
| 2086 |
dval(&rv) += sulp(&rv, &bc); |
| 2087 |
else { |
| 2088 |
dval(&rv) -= sulp(&rv, &bc); |
| 2089 |
if (!dval(&rv)) { |
| 2090 |
if (bc.nd >nd) |
| 2091 |
break; |
| 2092 |
goto undfl; |
| 2093 |
} |
| 2094 |
} |
| 2095 |
dsign = 1 - dsign; |
| 2096 |
break; |
| 2097 |
} |
| 2098 |
if ((aadj = ratio(delta, bs)) <= 2.) { |
| 2099 |
if (dsign) |
| 2100 |
aadj = aadj1 = 1.; |
| 2101 |
else if (word1(&rv) || word0(&rv) & Bndry_mask) { |
| 2102 |
if (word1(&rv) == Tiny1 && !word0(&rv)) { |
| 2103 |
if (bc.nd >nd) |
| 2104 |
break; |
| 2105 |
goto undfl; |
| 2106 |
} |
| 2107 |
aadj = 1.; |
| 2108 |
aadj1 = -1.; |
| 2109 |
} |
| 2110 |
else { |
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
if (aadj < 2./FLT_RADIX) |
| 2115 |
aadj = 1./FLT_RADIX; |
| 2116 |
else |
| 2117 |
aadj *= 0.5; |
| 2118 |
aadj1 = -aadj; |
| 2119 |
} |
| 2120 |
} |
| 2121 |
else { |
| 2122 |
aadj *= 0.5; |
| 2123 |
aadj1 = dsign ? aadj : -aadj; |
| 2124 |
if (Flt_Rounds == 0) |
| 2125 |
aadj1 += 0.5; |
| 2126 |
} |
| 2127 |
y = word0(&rv) & Exp_mask; |
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { |
| 2132 |
dval(&rv0) = dval(&rv); |
| 2133 |
word0(&rv) -= P*Exp_msk1; |
| 2134 |
adj.d = aadj1 * ulp(&rv); |
| 2135 |
dval(&rv) += adj.d; |
| 2136 |
if ((word0(&rv) & Exp_mask) >= |
| 2137 |
Exp_msk1*(DBL_MAX_EXP+Bias-P)) { |
| 2138 |
if (word0(&rv0) == Big0 && word1(&rv0) == Big1) { |
| 2139 |
goto ovfl; |
| 2140 |
} |
| 2141 |
word0(&rv) = Big0; |
| 2142 |
word1(&rv) = Big1; |
| 2143 |
goto cont; |
| 2144 |
} |
| 2145 |
else |
| 2146 |
word0(&rv) += P*Exp_msk1; |
| 2147 |
} |
| 2148 |
else { |
| 2149 |
if (bc.scale && y <= 2*P*Exp_msk1) { |
| 2150 |
if (aadj <= 0x7fffffff) { |
| 2151 |
if ((z = (ULong)aadj) <= 0) |
| 2152 |
z = 1; |
| 2153 |
aadj = z; |
| 2154 |
aadj1 = dsign ? aadj : -aadj; |
| 2155 |
} |
| 2156 |
dval(&aadj2) = aadj1; |
| 2157 |
word0(&aadj2) += (2*P+1)*Exp_msk1 - y; |
| 2158 |
aadj1 = dval(&aadj2); |
| 2159 |
} |
| 2160 |
adj.d = aadj1 * ulp(&rv); |
| 2161 |
dval(&rv) += adj.d; |
| 2162 |
} |
| 2163 |
z = word0(&rv) & Exp_mask; |
| 2164 |
if (bc.nd == nd) { |
| 2165 |
if (!bc.scale) |
| 2166 |
if (y == z) { |
| 2167 |
|
| 2168 |
L = (Long)aadj; |
| 2169 |
aadj -= L; |
| 2170 |
|
| 2171 |
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) { |
| 2172 |
if (aadj < .4999999 || aadj > .5000001) |
| 2173 |
break; |
| 2174 |
} |
| 2175 |
else if (aadj < .4999999/FLT_RADIX) |
| 2176 |
break; |
| 2177 |
} |
| 2178 |
} |
| 2179 |
cont: |
| 2180 |
Bfree(bb); bb = NULL; |
| 2181 |
Bfree(bd); bd = NULL; |
| 2182 |
Bfree(bs); bs = NULL; |
| 2183 |
Bfree(delta); delta = NULL; |
| 2184 |
} |
| 2185 |
if (bc.nd > nd) { |
| 2186 |
error = bigcomp(&rv, s0, &bc); |
| 2187 |
if (error) |
| 2188 |
goto failed_malloc; |
| 2189 |
} |
| 2190 |
|
| 2191 |
if (bc.scale) { |
| 2192 |
word0(&rv0) = Exp_1 - 2*P*Exp_msk1; |
| 2193 |
word1(&rv0) = 0; |
| 2194 |
dval(&rv) *= dval(&rv0); |
| 2195 |
} |
| 2196 |
|
| 2197 |
ret: |
| 2198 |
result = sign ? -dval(&rv) : dval(&rv); |
| 2199 |
goto done; |
| 2200 |
|
| 2201 |
parse_error: |
| 2202 |
result = 0.0; |
| 2203 |
goto done; |
| 2204 |
|
| 2205 |
failed_malloc: |
| 2206 |
errno = ENOMEM; |
| 2207 |
result = -1.0; |
| 2208 |
goto done; |
| 2209 |
|
| 2210 |
undfl: |
| 2211 |
result = sign ? -0.0 : 0.0; |
| 2212 |
goto done; |
| 2213 |
|
| 2214 |
ovfl: |
| 2215 |
errno = ERANGE; |
| 2216 |
|
| 2217 |
word0(&rv) = Exp_mask; |
| 2218 |
word1(&rv) = 0; |
| 2219 |
result = sign ? -dval(&rv) : dval(&rv); |
| 2220 |
goto done; |
| 2221 |
|
| 2222 |
done: |
| 2223 |
Bfree(bb); |
| 2224 |
Bfree(bd); |
| 2225 |
Bfree(bs); |
| 2226 |
Bfree(bd0); |
| 2227 |
Bfree(delta); |
| 2228 |
return result; |
| 2229 |
|
| 2230 |
} |
| 2231 |
|
| 2232 |
static char * |
| 2233 |
rv_alloc(int i) |
| 2234 |
{ |
| 2235 |
int j, k, *r; |
| 2236 |
|
| 2237 |
j = sizeof(ULong); |
| 2238 |
for(k = 0; |
| 2239 |
sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i; |
| 2240 |
j <<= 1) |
| 2241 |
k++; |
| 2242 |
r = (int*)Balloc(k); |
| 2243 |
if (r == NULL) |
| 2244 |
return NULL; |
| 2245 |
*r = k; |
| 2246 |
return (char *)(r+1); |
| 2247 |
} |
| 2248 |
|
| 2249 |
static char * |
| 2250 |
nrv_alloc(char *s, char **rve, int n) |
| 2251 |
{ |
| 2252 |
char *rv, *t; |
| 2253 |
|
| 2254 |
rv = rv_alloc(n); |
| 2255 |
if (rv == NULL) |
| 2256 |
return NULL; |
| 2257 |
t = rv; |
| 2258 |
while((*t = *s++)) t++; |
| 2259 |
if (rve) |
| 2260 |
*rve = t; |
| 2261 |
return rv; |
| 2262 |
} |
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
void |
| 2271 |
_Py_dg_freedtoa(char *s) |
| 2272 |
{ |
| 2273 |
Bigint *b = (Bigint *)((int *)s - 1); |
| 2274 |
b->maxwds = 1 << (b->k = *(int*)b); |
| 2275 |
Bfree(b); |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
char * |
| 2317 |
_Py_dg_dtoa(double dd, int mode, int ndigits, |
| 2318 |
int *decpt, int *sign, char **rve) |
| 2319 |
{ |
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, |
| 2355 |
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, |
| 2356 |
spec_case, try_quick; |
| 2357 |
Long L; |
| 2358 |
int denorm; |
| 2359 |
ULong x; |
| 2360 |
Bigint *b, *b1, *delta, *mlo, *mhi, *S; |
| 2361 |
U d2, eps, u; |
| 2362 |
double ds; |
| 2363 |
char *s, *s0; |
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
mlo = mhi = S = 0; |
| 2368 |
s0 = 0; |
| 2369 |
|
| 2370 |
u.d = dd; |
| 2371 |
if (word0(&u) & Sign_bit) { |
| 2372 |
|
| 2373 |
*sign = 1; |
| 2374 |
word0(&u) &= ~Sign_bit; |
| 2375 |
} |
| 2376 |
else |
| 2377 |
*sign = 0; |
| 2378 |
|
| 2379 |
|
| 2380 |
if ((word0(&u) & Exp_mask) == Exp_mask) |
| 2381 |
{ |
| 2382 |
|
| 2383 |
*decpt = 9999; |
| 2384 |
if (!word1(&u) && !(word0(&u) & 0xfffff)) |
| 2385 |
return nrv_alloc("Infinity", rve, 8); |
| 2386 |
return nrv_alloc("NaN", rve, 3); |
| 2387 |
} |
| 2388 |
if (!dval(&u)) { |
| 2389 |
*decpt = 1; |
| 2390 |
return nrv_alloc("0", rve, 1); |
| 2391 |
} |
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
b = d2b(&u, &be, &bbits); |
| 2396 |
if (b == NULL) |
| 2397 |
goto failed_malloc; |
| 2398 |
if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) { |
| 2399 |
dval(&d2) = dval(&u); |
| 2400 |
word0(&d2) &= Frac_mask1; |
| 2401 |
word0(&d2) |= Exp_11; |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
i -= Bias; |
| 2426 |
denorm = 0; |
| 2427 |
} |
| 2428 |
else { |
| 2429 |
|
| 2430 |
|
| 2431 |
i = bbits + be + (Bias + (P-1) - 1); |
| 2432 |
x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32) |
| 2433 |
: word1(&u) << (32 - i); |
| 2434 |
dval(&d2) = x; |
| 2435 |
word0(&d2) -= 31*Exp_msk1; |
| 2436 |
i -= (Bias + (P-1) - 1) + 1; |
| 2437 |
denorm = 1; |
| 2438 |
} |
| 2439 |
ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + |
| 2440 |
i*0.301029995663981; |
| 2441 |
k = (int)ds; |
| 2442 |
if (ds < 0. && ds != k) |
| 2443 |
k--; |
| 2444 |
k_check = 1; |
| 2445 |
if (k >= 0 && k <= Ten_pmax) { |
| 2446 |
if (dval(&u) < tens[k]) |
| 2447 |
k--; |
| 2448 |
k_check = 0; |
| 2449 |
} |
| 2450 |
j = bbits - i - 1; |
| 2451 |
if (j >= 0) { |
| 2452 |
b2 = 0; |
| 2453 |
s2 = j; |
| 2454 |
} |
| 2455 |
else { |
| 2456 |
b2 = -j; |
| 2457 |
s2 = 0; |
| 2458 |
} |
| 2459 |
if (k >= 0) { |
| 2460 |
b5 = 0; |
| 2461 |
s5 = k; |
| 2462 |
s2 += k; |
| 2463 |
} |
| 2464 |
else { |
| 2465 |
b2 -= k; |
| 2466 |
b5 = -k; |
| 2467 |
s5 = 0; |
| 2468 |
} |
| 2469 |
if (mode < 0 || mode > 9) |
| 2470 |
mode = 0; |
| 2471 |
|
| 2472 |
try_quick = 1; |
| 2473 |
|
| 2474 |
if (mode > 5) { |
| 2475 |
mode -= 4; |
| 2476 |
try_quick = 0; |
| 2477 |
} |
| 2478 |
leftright = 1; |
| 2479 |
ilim = ilim1 = -1; |
| 2480 |
|
| 2481 |
switch(mode) { |
| 2482 |
case 0: |
| 2483 |
case 1: |
| 2484 |
i = 18; |
| 2485 |
ndigits = 0; |
| 2486 |
break; |
| 2487 |
case 2: |
| 2488 |
leftright = 0; |
| 2489 |
|
| 2490 |
case 4: |
| 2491 |
if (ndigits <= 0) |
| 2492 |
ndigits = 1; |
| 2493 |
ilim = ilim1 = i = ndigits; |
| 2494 |
break; |
| 2495 |
case 3: |
| 2496 |
leftright = 0; |
| 2497 |
|
| 2498 |
case 5: |
| 2499 |
i = ndigits + k + 1; |
| 2500 |
ilim = i; |
| 2501 |
ilim1 = i - 1; |
| 2502 |
if (i <= 0) |
| 2503 |
i = 1; |
| 2504 |
} |
| 2505 |
s0 = rv_alloc(i); |
| 2506 |
if (s0 == NULL) |
| 2507 |
goto failed_malloc; |
| 2508 |
s = s0; |
| 2509 |
|
| 2510 |
|
| 2511 |
if (ilim >= 0 && ilim <= Quick_max && try_quick) { |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
i = 0; |
| 2516 |
dval(&d2) = dval(&u); |
| 2517 |
k0 = k; |
| 2518 |
ilim0 = ilim; |
| 2519 |
ieps = 2; |
| 2520 |
if (k > 0) { |
| 2521 |
ds = tens[k&0xf]; |
| 2522 |
j = k >> 4; |
| 2523 |
if (j & Bletch) { |
| 2524 |
|
| 2525 |
j &= Bletch - 1; |
| 2526 |
dval(&u) /= bigtens[n_bigtens-1]; |
| 2527 |
ieps++; |
| 2528 |
} |
| 2529 |
for(; j; j >>= 1, i++) |
| 2530 |
if (j & 1) { |
| 2531 |
ieps++; |
| 2532 |
ds *= bigtens[i]; |
| 2533 |
} |
| 2534 |
dval(&u) /= ds; |
| 2535 |
} |
| 2536 |
else if ((j1 = -k)) { |
| 2537 |
dval(&u) *= tens[j1 & 0xf]; |
| 2538 |
for(j = j1 >> 4; j; j >>= 1, i++) |
| 2539 |
if (j & 1) { |
| 2540 |
ieps++; |
| 2541 |
dval(&u) *= bigtens[i]; |
| 2542 |
} |
| 2543 |
} |
| 2544 |
if (k_check && dval(&u) < 1. && ilim > 0) { |
| 2545 |
if (ilim1 <= 0) |
| 2546 |
goto fast_failed; |
| 2547 |
ilim = ilim1; |
| 2548 |
k--; |
| 2549 |
dval(&u) *= 10.; |
| 2550 |
ieps++; |
| 2551 |
} |
| 2552 |
dval(&eps) = ieps*dval(&u) + 7.; |
| 2553 |
word0(&eps) -= (P-1)*Exp_msk1; |
| 2554 |
if (ilim == 0) { |
| 2555 |
S = mhi = 0; |
| 2556 |
dval(&u) -= 5.; |
| 2557 |
if (dval(&u) > dval(&eps)) |
| 2558 |
goto one_digit; |
| 2559 |
if (dval(&u) < -dval(&eps)) |
| 2560 |
goto no_digits; |
| 2561 |
goto fast_failed; |
| 2562 |
} |
| 2563 |
if (leftright) { |
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
dval(&eps) = 0.5/tens[ilim-1] - dval(&eps); |
| 2568 |
for(i = 0;;) { |
| 2569 |
L = (Long)dval(&u); |
| 2570 |
dval(&u) -= L; |
| 2571 |
*s++ = '0' + (int)L; |
| 2572 |
if (dval(&u) < dval(&eps)) |
| 2573 |
goto ret1; |
| 2574 |
if (1. - dval(&u) < dval(&eps)) |
| 2575 |
goto bump_up; |
| 2576 |
if (++i >= ilim) |
| 2577 |
break; |
| 2578 |
dval(&eps) *= 10.; |
| 2579 |
dval(&u) *= 10.; |
| 2580 |
} |
| 2581 |
} |
| 2582 |
else { |
| 2583 |
|
| 2584 |
dval(&eps) *= tens[ilim-1]; |
| 2585 |
for(i = 1;; i++, dval(&u) *= 10.) { |
| 2586 |
L = (Long)(dval(&u)); |
| 2587 |
if (!(dval(&u) -= L)) |
| 2588 |
ilim = i; |
| 2589 |
*s++ = '0' + (int)L; |
| 2590 |
if (i == ilim) { |
| 2591 |
if (dval(&u) > 0.5 + dval(&eps)) |
| 2592 |
goto bump_up; |
| 2593 |
else if (dval(&u) < 0.5 - dval(&eps)) { |
| 2594 |
while(*--s == '0'); |
| 2595 |
s++; |
| 2596 |
goto ret1; |
| 2597 |
} |
| 2598 |
break; |
| 2599 |
} |
| 2600 |
} |
| 2601 |
} |
| 2602 |
fast_failed: |
| 2603 |
s = s0; |
| 2604 |
dval(&u) = dval(&d2); |
| 2605 |
k = k0; |
| 2606 |
ilim = ilim0; |
| 2607 |
} |
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
if (be >= 0 && k <= Int_max) { |
| 2612 |
|
| 2613 |
ds = tens[k]; |
| 2614 |
if (ndigits < 0 && ilim <= 0) { |
| 2615 |
S = mhi = 0; |
| 2616 |
if (ilim < 0 || dval(&u) <= 5*ds) |
| 2617 |
goto no_digits; |
| 2618 |
goto one_digit; |
| 2619 |
} |
| 2620 |
for(i = 1;; i++, dval(&u) *= 10.) { |
| 2621 |
L = (Long)(dval(&u) / ds); |
| 2622 |
dval(&u) -= L*ds; |
| 2623 |
*s++ = '0' + (int)L; |
| 2624 |
if (!dval(&u)) { |
| 2625 |
break; |
| 2626 |
} |
| 2627 |
if (i == ilim) { |
| 2628 |
dval(&u) += dval(&u); |
| 2629 |
if (dval(&u) > ds || (dval(&u) == ds && L & 1)) { |
| 2630 |
bump_up: |
| 2631 |
while(*--s == '9') |
| 2632 |
if (s == s0) { |
| 2633 |
k++; |
| 2634 |
*s = '0'; |
| 2635 |
break; |
| 2636 |
} |
| 2637 |
++*s++; |
| 2638 |
} |
| 2639 |
break; |
| 2640 |
} |
| 2641 |
} |
| 2642 |
goto ret1; |
| 2643 |
} |
| 2644 |
|
| 2645 |
m2 = b2; |
| 2646 |
m5 = b5; |
| 2647 |
if (leftright) { |
| 2648 |
i = |
| 2649 |
denorm ? be + (Bias + (P-1) - 1 + 1) : |
| 2650 |
1 + P - bbits; |
| 2651 |
b2 += i; |
| 2652 |
s2 += i; |
| 2653 |
mhi = i2b(1); |
| 2654 |
if (mhi == NULL) |
| 2655 |
goto failed_malloc; |
| 2656 |
} |
| 2657 |
if (m2 > 0 && s2 > 0) { |
| 2658 |
i = m2 < s2 ? m2 : s2; |
| 2659 |
b2 -= i; |
| 2660 |
m2 -= i; |
| 2661 |
s2 -= i; |
| 2662 |
} |
| 2663 |
if (b5 > 0) { |
| 2664 |
if (leftright) { |
| 2665 |
if (m5 > 0) { |
| 2666 |
mhi = pow5mult(mhi, m5); |
| 2667 |
if (mhi == NULL) |
| 2668 |
goto failed_malloc; |
| 2669 |
b1 = mult(mhi, b); |
| 2670 |
Bfree(b); |
| 2671 |
b = b1; |
| 2672 |
if (b == NULL) |
| 2673 |
goto failed_malloc; |
| 2674 |
} |
| 2675 |
if ((j = b5 - m5)) { |
| 2676 |
b = pow5mult(b, j); |
| 2677 |
if (b == NULL) |
| 2678 |
goto failed_malloc; |
| 2679 |
} |
| 2680 |
} |
| 2681 |
else { |
| 2682 |
b = pow5mult(b, b5); |
| 2683 |
if (b == NULL) |
| 2684 |
goto failed_malloc; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
S = i2b(1); |
| 2688 |
if (S == NULL) |
| 2689 |
goto failed_malloc; |
| 2690 |
if (s5 > 0) { |
| 2691 |
S = pow5mult(S, s5); |
| 2692 |
if (S == NULL) |
| 2693 |
goto failed_malloc; |
| 2694 |
} |
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
spec_case = 0; |
| 2699 |
if ((mode < 2 || leftright) |
| 2700 |
) { |
| 2701 |
if (!word1(&u) && !(word0(&u) & Bndry_mask) |
| 2702 |
&& word0(&u) & (Exp_mask & ~Exp_msk1) |
| 2703 |
) { |
| 2704 |
|
| 2705 |
b2 += Log2P; |
| 2706 |
s2 += Log2P; |
| 2707 |
spec_case = 1; |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
#define iInc 28 |
| 2719 |
i = dshift(S, s2); |
| 2720 |
b2 += i; |
| 2721 |
m2 += i; |
| 2722 |
s2 += i; |
| 2723 |
if (b2 > 0) { |
| 2724 |
b = lshift(b, b2); |
| 2725 |
if (b == NULL) |
| 2726 |
goto failed_malloc; |
| 2727 |
} |
| 2728 |
if (s2 > 0) { |
| 2729 |
S = lshift(S, s2); |
| 2730 |
if (S == NULL) |
| 2731 |
goto failed_malloc; |
| 2732 |
} |
| 2733 |
if (k_check) { |
| 2734 |
if (cmp(b,S) < 0) { |
| 2735 |
k--; |
| 2736 |
b = multadd(b, 10, 0); |
| 2737 |
if (b == NULL) |
| 2738 |
goto failed_malloc; |
| 2739 |
if (leftright) { |
| 2740 |
mhi = multadd(mhi, 10, 0); |
| 2741 |
if (mhi == NULL) |
| 2742 |
goto failed_malloc; |
| 2743 |
} |
| 2744 |
ilim = ilim1; |
| 2745 |
} |
| 2746 |
} |
| 2747 |
if (ilim <= 0 && (mode == 3 || mode == 5)) { |
| 2748 |
if (ilim < 0) { |
| 2749 |
|
| 2750 |
no_digits: |
| 2751 |
k = -1 - ndigits; |
| 2752 |
goto ret; |
| 2753 |
} |
| 2754 |
else { |
| 2755 |
S = multadd(S, 5, 0); |
| 2756 |
if (S == NULL) |
| 2757 |
goto failed_malloc; |
| 2758 |
if (cmp(b, S) <= 0) |
| 2759 |
goto no_digits; |
| 2760 |
} |
| 2761 |
one_digit: |
| 2762 |
*s++ = '1'; |
| 2763 |
k++; |
| 2764 |
goto ret; |
| 2765 |
} |
| 2766 |
if (leftright) { |
| 2767 |
if (m2 > 0) { |
| 2768 |
mhi = lshift(mhi, m2); |
| 2769 |
if (mhi == NULL) |
| 2770 |
goto failed_malloc; |
| 2771 |
} |
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
mlo = mhi; |
| 2778 |
if (spec_case) { |
| 2779 |
mhi = Balloc(mhi->k); |
| 2780 |
if (mhi == NULL) |
| 2781 |
goto failed_malloc; |
| 2782 |
Bcopy(mhi, mlo); |
| 2783 |
mhi = lshift(mhi, Log2P); |
| 2784 |
if (mhi == NULL) |
| 2785 |
goto failed_malloc; |
| 2786 |
} |
| 2787 |
|
| 2788 |
for(i = 1;;i++) { |
| 2789 |
dig = quorem(b,S) + '0'; |
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
j = cmp(b, mlo); |
| 2794 |
delta = diff(S, mhi); |
| 2795 |
if (delta == NULL) |
| 2796 |
goto failed_malloc; |
| 2797 |
j1 = delta->sign ? 1 : cmp(b, delta); |
| 2798 |
Bfree(delta); |
| 2799 |
if (j1 == 0 && mode != 1 && !(word1(&u) & 1) |
| 2800 |
) { |
| 2801 |
if (dig == '9') |
| 2802 |
goto round_9_up; |
| 2803 |
if (j > 0) |
| 2804 |
dig++; |
| 2805 |
*s++ = dig; |
| 2806 |
goto ret; |
| 2807 |
} |
| 2808 |
if (j < 0 || (j == 0 && mode != 1 |
| 2809 |
&& !(word1(&u) & 1) |
| 2810 |
)) { |
| 2811 |
if (!b->x[0] && b->wds <= 1) { |
| 2812 |
goto accept_dig; |
| 2813 |
} |
| 2814 |
if (j1 > 0) { |
| 2815 |
b = lshift(b, 1); |
| 2816 |
if (b == NULL) |
| 2817 |
goto failed_malloc; |
| 2818 |
j1 = cmp(b, S); |
| 2819 |
if ((j1 > 0 || (j1 == 0 && dig & 1)) |
| 2820 |
&& dig++ == '9') |
| 2821 |
goto round_9_up; |
| 2822 |
} |
| 2823 |
accept_dig: |
| 2824 |
*s++ = dig; |
| 2825 |
goto ret; |
| 2826 |
} |
| 2827 |
if (j1 > 0) { |
| 2828 |
if (dig == '9') { |
| 2829 |
round_9_up: |
| 2830 |
*s++ = '9'; |
| 2831 |
goto roundoff; |
| 2832 |
} |
| 2833 |
*s++ = dig + 1; |
| 2834 |
goto ret; |
| 2835 |
} |
| 2836 |
*s++ = dig; |
| 2837 |
if (i == ilim) |
| 2838 |
break; |
| 2839 |
b = multadd(b, 10, 0); |
| 2840 |
if (b == NULL) |
| 2841 |
goto failed_malloc; |
| 2842 |
if (mlo == mhi) { |
| 2843 |
mlo = mhi = multadd(mhi, 10, 0); |
| 2844 |
if (mlo == NULL) |
| 2845 |
goto failed_malloc; |
| 2846 |
} |
| 2847 |
else { |
| 2848 |
mlo = multadd(mlo, 10, 0); |
| 2849 |
if (mlo == NULL) |
| 2850 |
goto failed_malloc; |
| 2851 |
mhi = multadd(mhi, 10, 0); |
| 2852 |
if (mhi == NULL) |
| 2853 |
goto failed_malloc; |
| 2854 |
} |
| 2855 |
} |
| 2856 |
} |
| 2857 |
else |
| 2858 |
for(i = 1;; i++) { |
| 2859 |
*s++ = dig = quorem(b,S) + '0'; |
| 2860 |
if (!b->x[0] && b->wds <= 1) { |
| 2861 |
goto ret; |
| 2862 |
} |
| 2863 |
if (i >= ilim) |
| 2864 |
break; |
| 2865 |
b = multadd(b, 10, 0); |
| 2866 |
if (b == NULL) |
| 2867 |
goto failed_malloc; |
| 2868 |
} |
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
b = lshift(b, 1); |
| 2873 |
if (b == NULL) |
| 2874 |
goto failed_malloc; |
| 2875 |
j = cmp(b, S); |
| 2876 |
if (j > 0 || (j == 0 && dig & 1)) { |
| 2877 |
roundoff: |
| 2878 |
while(*--s == '9') |
| 2879 |
if (s == s0) { |
| 2880 |
k++; |
| 2881 |
*s++ = '1'; |
| 2882 |
goto ret; |
| 2883 |
} |
| 2884 |
++*s++; |
| 2885 |
} |
| 2886 |
else { |
| 2887 |
while(*--s == '0'); |
| 2888 |
s++; |
| 2889 |
} |
| 2890 |
ret: |
| 2891 |
Bfree(S); |
| 2892 |
if (mhi) { |
| 2893 |
if (mlo && mlo != mhi) |
| 2894 |
Bfree(mlo); |
| 2895 |
Bfree(mhi); |
| 2896 |
} |
| 2897 |
ret1: |
| 2898 |
Bfree(b); |
| 2899 |
*s = 0; |
| 2900 |
*decpt = k + 1; |
| 2901 |
if (rve) |
| 2902 |
*rve = s; |
| 2903 |
return s0; |
| 2904 |
failed_malloc: |
| 2905 |
if (S) |
| 2906 |
Bfree(S); |
| 2907 |
if (mlo && mlo != mhi) |
| 2908 |
Bfree(mlo); |
| 2909 |
if (mhi) |
| 2910 |
Bfree(mhi); |
| 2911 |
if (b) |
| 2912 |
Bfree(b); |
| 2913 |
if (s0) |
| 2914 |
_Py_dg_freedtoa(s0); |
| 2915 |
return NULL; |
| 2916 |
} |
| 2917 |
#ifdef __cplusplus |
| 2918 |
} |
| 2919 |
#endif |
| 2920 |
|
| 2921 |
#endif |