PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/pypy/translator/c/src/dtoa.c

https://bitbucket.org/dac_io/pypy
C | 2977 lines | 2124 code | 236 blank | 617 comment | 610 complexity | 9e76f87701a80de5fba7f52887ae84f0 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /****************************************************************
  2. *
  3. * The author of this software is David M. Gay.
  4. *
  5. * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose without fee is hereby granted, provided that this entire notice
  9. * is included in all copies of any software which is or includes a copy
  10. * or modification of this software and in all copies of the supporting
  11. * documentation for such software.
  12. *
  13. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  14. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
  15. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  16. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  17. *
  18. ***************************************************************/
  19. /****************************************************************
  20. * This is dtoa.c by David M. Gay, downloaded from
  21. * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
  22. * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
  23. *
  24. * Please remember to check http://www.netlib.org/fp regularly (and especially
  25. * before any Python release) for bugfixes and updates.
  26. *
  27. * The major modifications from Gay's original code are as follows:
  28. *
  29. * 0. The original code has been specialized to Python's needs by removing
  30. * many of the #ifdef'd sections. In particular, code to support VAX and
  31. * IBM floating-point formats, hex NaNs, hex floats, locale-aware
  32. * treatment of the decimal point, and setting of the inexact flag have
  33. * been removed.
  34. *
  35. * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
  36. *
  37. * 2. The public functions strtod, dtoa and freedtoa all now have
  38. * a _Py_dg_ prefix.
  39. *
  40. * 3. Instead of assuming that PyMem_Malloc always succeeds, we thread
  41. * PyMem_Malloc failures through the code. The functions
  42. *
  43. * Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
  44. *
  45. * of return type *Bigint all return NULL to indicate a malloc failure.
  46. * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
  47. * failure. bigcomp now has return type int (it used to be void) and
  48. * returns -1 on failure and 0 otherwise. __Py_dg_dtoa returns NULL
  49. * on failure. __Py_dg_strtod indicates failure due to malloc failure
  50. * by returning -1.0, setting errno=ENOMEM and *se to s00.
  51. *
  52. * 4. The static variable dtoa_result has been removed. Callers of
  53. * __Py_dg_dtoa are expected to call __Py_dg_freedtoa to free
  54. * the memory allocated by __Py_dg_dtoa.
  55. *
  56. * 5. The code has been reformatted to better fit with Python's
  57. * C style guide (PEP 7).
  58. *
  59. * 6. A bug in the memory allocation has been fixed: to avoid FREEing memory
  60. * that hasn't been MALLOC'ed, private_mem should only be used when k <=
  61. * Kmax.
  62. *
  63. * 7. __Py_dg_strtod has been modified so that it doesn't accept strings with
  64. * leading whitespace.
  65. *
  66. ***************************************************************/
  67. /* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
  68. * at acm dot org, with " at " changed at "@" and " dot " changed to ".").
  69. * Please report bugs for this modified version using the Python issue tracker
  70. * (http://bugs.python.org). */
  71. /* On a machine with IEEE extended-precision registers, it is
  72. * necessary to specify double-precision (53-bit) rounding precision
  73. * before invoking strtod or dtoa. If the machine uses (the equivalent
  74. * of) Intel 80x87 arithmetic, the call
  75. * _control87(PC_53, MCW_PC);
  76. * does this with many compilers. Whether this or another call is
  77. * appropriate depends on the compiler; for this to work, it may be
  78. * necessary to #include "float.h" or another system-dependent header
  79. * file.
  80. */
  81. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  82. *
  83. * This strtod returns a nearest machine number to the input decimal
  84. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  85. * broken by the IEEE round-even rule. Otherwise ties are broken by
  86. * biased rounding (add half and chop).
  87. *
  88. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  89. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  90. *
  91. * Modifications:
  92. *
  93. * 1. We only require IEEE, IBM, or VAX double-precision
  94. * arithmetic (not IEEE double-extended).
  95. * 2. We get by with floating-point arithmetic in a case that
  96. * Clinger missed -- when we're computing d * 10^n
  97. * for a small integer d and the integer n is not too
  98. * much larger than 22 (the maximum integer k for which
  99. * we can represent 10^k exactly), we may be able to
  100. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  101. * 3. Rather than a bit-at-a-time adjustment of the binary
  102. * result in the hard case, we use floating-point
  103. * arithmetic to determine the adjustment to within
  104. * one bit; only in really hard cases do we need to
  105. * compute a second residual.
  106. * 4. Because of 3., we don't need a large table of powers of 10
  107. * for ten-to-e (just some small tables, e.g. of 10^k
  108. * for 0 <= k <= 22).
  109. */
  110. /* Linking of Python's #defines to Gay's #defines starts here. */
  111. /* Begin PYPY hacks */
  112. /* #include "Python.h" */
  113. #define HAVE_UINT32_T
  114. #define HAVE_INT32_T
  115. #define HAVE_UINT64_T
  116. #define PY_UINT32_T unsigned int
  117. #define PY_INT32_T int
  118. #define PY_UINT64_T unsigned long long
  119. #include <stdlib.h>
  120. #include <errno.h>
  121. #include <assert.h>
  122. #include <stdio.h>
  123. #include <string.h>
  124. #define PYPY_NOT_MAIN_FILE
  125. #include "src/asm.h"
  126. #define PyMem_Malloc malloc
  127. #define PyMem_Free free
  128. /* End PYPY hacks */
  129. /* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
  130. the following code */
  131. #ifndef PY_NO_SHORT_FLOAT_REPR
  132. #include <float.h>
  133. #define MALLOC PyMem_Malloc
  134. #define FREE PyMem_Free
  135. /* This code should also work for ARM mixed-endian format on little-endian
  136. machines, where doubles have byte order 45670123 (in increasing address
  137. order, 0 being the least significant byte). */
  138. #ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
  139. # define IEEE_8087
  140. #endif
  141. #if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
  142. defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
  143. # define IEEE_MC68k
  144. #endif
  145. #if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
  146. #error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
  147. #endif
  148. /* The code below assumes that the endianness of integers matches the
  149. endianness of the two 32-bit words of a double. Check this. */
  150. #if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
  151. defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
  152. #error "doubles and ints have incompatible endianness"
  153. #endif
  154. #if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
  155. #error "doubles and ints have incompatible endianness"
  156. #endif
  157. #if defined(HAVE_UINT32_T) && defined(HAVE_INT32_T)
  158. typedef PY_UINT32_T ULong;
  159. typedef PY_INT32_T Long;
  160. #else
  161. #error "Failed to find an exact-width 32-bit integer type"
  162. #endif
  163. #if defined(HAVE_UINT64_T)
  164. #define ULLong PY_UINT64_T
  165. #else
  166. #undef ULLong
  167. #endif
  168. #undef DEBUG
  169. #ifdef Py_DEBUG
  170. #define DEBUG
  171. #endif
  172. /* End Python #define linking */
  173. #ifdef DEBUG
  174. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  175. #endif
  176. #ifndef PRIVATE_MEM
  177. #define PRIVATE_MEM 2304
  178. #endif
  179. #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
  180. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  181. #ifdef __cplusplus
  182. extern "C" {
  183. #endif
  184. typedef union { double d; ULong L[2]; } U;
  185. #ifdef IEEE_8087
  186. #define word0(x) (x)->L[1]
  187. #define word1(x) (x)->L[0]
  188. #else
  189. #define word0(x) (x)->L[0]
  190. #define word1(x) (x)->L[1]
  191. #endif
  192. #define dval(x) (x)->d
  193. #ifndef STRTOD_DIGLIM
  194. #define STRTOD_DIGLIM 40
  195. #endif
  196. /* maximum permitted exponent value for strtod; exponents larger than
  197. MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
  198. should fit into an int. */
  199. #ifndef MAX_ABS_EXP
  200. #define MAX_ABS_EXP 19999U
  201. #endif
  202. /* The following definition of Storeinc is appropriate for MIPS processors.
  203. * An alternative that might be better on some machines is
  204. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  205. */
  206. #if defined(IEEE_8087)
  207. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  208. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  209. #else
  210. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  211. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  212. #endif
  213. /* #define P DBL_MANT_DIG */
  214. /* Ten_pmax = floor(P*log(2)/log(5)) */
  215. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  216. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  217. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  218. #define Exp_shift 20
  219. #define Exp_shift1 20
  220. #define Exp_msk1 0x100000
  221. #define Exp_msk11 0x100000
  222. #define Exp_mask 0x7ff00000
  223. #define P 53
  224. #define Nbits 53
  225. #define Bias 1023
  226. #define Emax 1023
  227. #define Emin (-1022)
  228. #define Etiny (-1074) /* smallest denormal is 2**Etiny */
  229. #define Exp_1 0x3ff00000
  230. #define Exp_11 0x3ff00000
  231. #define Ebits 11
  232. #define Frac_mask 0xfffff
  233. #define Frac_mask1 0xfffff
  234. #define Ten_pmax 22
  235. #define Bletch 0x10
  236. #define Bndry_mask 0xfffff
  237. #define Bndry_mask1 0xfffff
  238. #define Sign_bit 0x80000000
  239. #define Log2P 1
  240. #define Tiny0 0
  241. #define Tiny1 1
  242. #define Quick_max 14
  243. #define Int_max 14
  244. #ifndef Flt_Rounds
  245. #ifdef FLT_ROUNDS
  246. #define Flt_Rounds FLT_ROUNDS
  247. #else
  248. #define Flt_Rounds 1
  249. #endif
  250. #endif /*Flt_Rounds*/
  251. #define Rounding Flt_Rounds
  252. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  253. #define Big1 0xffffffff
  254. /* struct BCinfo is used to pass information from __Py_dg_strtod to bigcomp */
  255. typedef struct BCinfo BCinfo;
  256. struct
  257. BCinfo {
  258. int e0, nd, nd0, scale;
  259. };
  260. #define FFFFFFFF 0xffffffffUL
  261. #define Kmax 7
  262. /* struct Bigint is used to represent arbitrary-precision integers. These
  263. integers are stored in sign-magnitude format, with the magnitude stored as
  264. an array of base 2**32 digits. Bigints are always normalized: if x is a
  265. Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
  266. The Bigint fields are as follows:
  267. - next is a header used by Balloc and Bfree to keep track of lists
  268. of freed Bigints; it's also used for the linked list of
  269. powers of 5 of the form 5**2**i used by pow5mult.
  270. - k indicates which pool this Bigint was allocated from
  271. - maxwds is the maximum number of words space was allocated for
  272. (usually maxwds == 2**k)
  273. - sign is 1 for negative Bigints, 0 for positive. The sign is unused
  274. (ignored on inputs, set to 0 on outputs) in almost all operations
  275. involving Bigints: a notable exception is the diff function, which
  276. ignores signs on inputs but sets the sign of the output correctly.
  277. - wds is the actual number of significant words
  278. - x contains the vector of words (digits) for this Bigint, from least
  279. significant (x[0]) to most significant (x[wds-1]).
  280. */
  281. struct
  282. Bigint {
  283. struct Bigint *next;
  284. int k, maxwds, sign, wds;
  285. ULong x[1];
  286. };
  287. typedef struct Bigint Bigint;
  288. #ifndef Py_USING_MEMORY_DEBUGGER
  289. /* Memory management: memory is allocated from, and returned to, Kmax+1 pools
  290. of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
  291. 1 << k. These pools are maintained as linked lists, with freelist[k]
  292. pointing to the head of the list for pool k.
  293. On allocation, if there's no free slot in the appropriate pool, MALLOC is
  294. called to get more memory. This memory is not returned to the system until
  295. Python quits. There's also a private memory pool that's allocated from
  296. in preference to using MALLOC.
  297. For Bigints with more than (1 << Kmax) digits (which implies at least 1233
  298. decimal digits), memory is directly allocated using MALLOC, and freed using
  299. FREE.
  300. XXX: it would be easy to bypass this memory-management system and
  301. translate each call to Balloc into a call to PyMem_Malloc, and each
  302. Bfree to PyMem_Free. Investigate whether this has any significant
  303. performance on impact. */
  304. static Bigint *freelist[Kmax+1];
  305. /* Allocate space for a Bigint with up to 1<<k digits */
  306. static Bigint *
  307. Balloc(int k)
  308. {
  309. int x;
  310. Bigint *rv;
  311. unsigned int len;
  312. if (k <= Kmax && (rv = freelist[k]))
  313. freelist[k] = rv->next;
  314. else {
  315. x = 1 << k;
  316. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  317. /sizeof(double);
  318. if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
  319. rv = (Bigint*)pmem_next;
  320. pmem_next += len;
  321. }
  322. else {
  323. rv = (Bigint*)MALLOC(len*sizeof(double));
  324. if (rv == NULL)
  325. return NULL;
  326. }
  327. rv->k = k;
  328. rv->maxwds = x;
  329. }
  330. rv->sign = rv->wds = 0;
  331. return rv;
  332. }
  333. /* Free a Bigint allocated with Balloc */
  334. static void
  335. Bfree(Bigint *v)
  336. {
  337. if (v) {
  338. if (v->k > Kmax)
  339. FREE((void*)v);
  340. else {
  341. v->next = freelist[v->k];
  342. freelist[v->k] = v;
  343. }
  344. }
  345. }
  346. #else
  347. /* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
  348. PyMem_Free directly in place of the custom memory allocation scheme above.
  349. These are provided for the benefit of memory debugging tools like
  350. Valgrind. */
  351. /* Allocate space for a Bigint with up to 1<<k digits */
  352. static Bigint *
  353. Balloc(int k)
  354. {
  355. int x;
  356. Bigint *rv;
  357. unsigned int len;
  358. x = 1 << k;
  359. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  360. /sizeof(double);
  361. rv = (Bigint*)MALLOC(len*sizeof(double));
  362. if (rv == NULL)
  363. return NULL;
  364. rv->k = k;
  365. rv->maxwds = x;
  366. rv->sign = rv->wds = 0;
  367. return rv;
  368. }
  369. /* Free a Bigint allocated with Balloc */
  370. static void
  371. Bfree(Bigint *v)
  372. {
  373. if (v) {
  374. FREE((void*)v);
  375. }
  376. }
  377. #endif /* Py_USING_MEMORY_DEBUGGER */
  378. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  379. y->wds*sizeof(Long) + 2*sizeof(int))
  380. /* Multiply a Bigint b by m and add a. Either modifies b in place and returns
  381. a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
  382. On failure, return NULL. In this case, b will have been already freed. */
  383. static Bigint *
  384. multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  385. {
  386. int i, wds;
  387. #ifdef ULLong
  388. ULong *x;
  389. ULLong carry, y;
  390. #else
  391. ULong carry, *x, y;
  392. ULong xi, z;
  393. #endif
  394. Bigint *b1;
  395. wds = b->wds;
  396. x = b->x;
  397. i = 0;
  398. carry = a;
  399. do {
  400. #ifdef ULLong
  401. y = *x * (ULLong)m + carry;
  402. carry = y >> 32;
  403. *x++ = (ULong)(y & FFFFFFFF);
  404. #else
  405. xi = *x;
  406. y = (xi & 0xffff) * m + carry;
  407. z = (xi >> 16) * m + (y >> 16);
  408. carry = z >> 16;
  409. *x++ = (z << 16) + (y & 0xffff);
  410. #endif
  411. }
  412. while(++i < wds);
  413. if (carry) {
  414. if (wds >= b->maxwds) {
  415. b1 = Balloc(b->k+1);
  416. if (b1 == NULL){
  417. Bfree(b);
  418. return NULL;
  419. }
  420. Bcopy(b1, b);
  421. Bfree(b);
  422. b = b1;
  423. }
  424. b->x[wds++] = (ULong)carry;
  425. b->wds = wds;
  426. }
  427. return b;
  428. }
  429. /* convert a string s containing nd decimal digits (possibly containing a
  430. decimal separator at position nd0, which is ignored) to a Bigint. This
  431. function carries on where the parsing code in __Py_dg_strtod leaves off: on
  432. entry, y9 contains the result of converting the first 9 digits. Returns
  433. NULL on failure. */
  434. static Bigint *
  435. s2b(const char *s, int nd0, int nd, ULong y9)
  436. {
  437. Bigint *b;
  438. int i, k;
  439. Long x, y;
  440. x = (nd + 8) / 9;
  441. for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  442. b = Balloc(k);
  443. if (b == NULL)
  444. return NULL;
  445. b->x[0] = y9;
  446. b->wds = 1;
  447. if (nd <= 9)
  448. return b;
  449. s += 9;
  450. for (i = 9; i < nd0; i++) {
  451. b = multadd(b, 10, *s++ - '0');
  452. if (b == NULL)
  453. return NULL;
  454. }
  455. s++;
  456. for(; i < nd; i++) {
  457. b = multadd(b, 10, *s++ - '0');
  458. if (b == NULL)
  459. return NULL;
  460. }
  461. return b;
  462. }
  463. /* count leading 0 bits in the 32-bit integer x. */
  464. static int
  465. hi0bits(ULong x)
  466. {
  467. int k = 0;
  468. if (!(x & 0xffff0000)) {
  469. k = 16;
  470. x <<= 16;
  471. }
  472. if (!(x & 0xff000000)) {
  473. k += 8;
  474. x <<= 8;
  475. }
  476. if (!(x & 0xf0000000)) {
  477. k += 4;
  478. x <<= 4;
  479. }
  480. if (!(x & 0xc0000000)) {
  481. k += 2;
  482. x <<= 2;
  483. }
  484. if (!(x & 0x80000000)) {
  485. k++;
  486. if (!(x & 0x40000000))
  487. return 32;
  488. }
  489. return k;
  490. }
  491. /* count trailing 0 bits in the 32-bit integer y, and shift y right by that
  492. number of bits. */
  493. static int
  494. lo0bits(ULong *y)
  495. {
  496. int k;
  497. ULong x = *y;
  498. if (x & 7) {
  499. if (x & 1)
  500. return 0;
  501. if (x & 2) {
  502. *y = x >> 1;
  503. return 1;
  504. }
  505. *y = x >> 2;
  506. return 2;
  507. }
  508. k = 0;
  509. if (!(x & 0xffff)) {
  510. k = 16;
  511. x >>= 16;
  512. }
  513. if (!(x & 0xff)) {
  514. k += 8;
  515. x >>= 8;
  516. }
  517. if (!(x & 0xf)) {
  518. k += 4;
  519. x >>= 4;
  520. }
  521. if (!(x & 0x3)) {
  522. k += 2;
  523. x >>= 2;
  524. }
  525. if (!(x & 1)) {
  526. k++;
  527. x >>= 1;
  528. if (!x)
  529. return 32;
  530. }
  531. *y = x;
  532. return k;
  533. }
  534. /* convert a small nonnegative integer to a Bigint */
  535. static Bigint *
  536. i2b(int i)
  537. {
  538. Bigint *b;
  539. b = Balloc(1);
  540. if (b == NULL)
  541. return NULL;
  542. b->x[0] = i;
  543. b->wds = 1;
  544. return b;
  545. }
  546. /* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
  547. the signs of a and b. */
  548. static Bigint *
  549. mult(Bigint *a, Bigint *b)
  550. {
  551. Bigint *c;
  552. int k, wa, wb, wc;
  553. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  554. ULong y;
  555. #ifdef ULLong
  556. ULLong carry, z;
  557. #else
  558. ULong carry, z;
  559. ULong z2;
  560. #endif
  561. if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
  562. c = Balloc(0);
  563. if (c == NULL)
  564. return NULL;
  565. c->wds = 1;
  566. c->x[0] = 0;
  567. return c;
  568. }
  569. if (a->wds < b->wds) {
  570. c = a;
  571. a = b;
  572. b = c;
  573. }
  574. k = a->k;
  575. wa = a->wds;
  576. wb = b->wds;
  577. wc = wa + wb;
  578. if (wc > a->maxwds)
  579. k++;
  580. c = Balloc(k);
  581. if (c == NULL)
  582. return NULL;
  583. for(x = c->x, xa = x + wc; x < xa; x++)
  584. *x = 0;
  585. xa = a->x;
  586. xae = xa + wa;
  587. xb = b->x;
  588. xbe = xb + wb;
  589. xc0 = c->x;
  590. #ifdef ULLong
  591. for(; xb < xbe; xc0++) {
  592. if ((y = *xb++)) {
  593. x = xa;
  594. xc = xc0;
  595. carry = 0;
  596. do {
  597. z = *x++ * (ULLong)y + *xc + carry;
  598. carry = z >> 32;
  599. *xc++ = (ULong)(z & FFFFFFFF);
  600. }
  601. while(x < xae);
  602. *xc = (ULong)carry;
  603. }
  604. }
  605. #else
  606. for(; xb < xbe; xb++, xc0++) {
  607. if (y = *xb & 0xffff) {
  608. x = xa;
  609. xc = xc0;
  610. carry = 0;
  611. do {
  612. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  613. carry = z >> 16;
  614. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  615. carry = z2 >> 16;
  616. Storeinc(xc, z2, z);
  617. }
  618. while(x < xae);
  619. *xc = carry;
  620. }
  621. if (y = *xb >> 16) {
  622. x = xa;
  623. xc = xc0;
  624. carry = 0;
  625. z2 = *xc;
  626. do {
  627. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  628. carry = z >> 16;
  629. Storeinc(xc, z, z2);
  630. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  631. carry = z2 >> 16;
  632. }
  633. while(x < xae);
  634. *xc = z2;
  635. }
  636. }
  637. #endif
  638. for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  639. c->wds = wc;
  640. return c;
  641. }
  642. #ifndef Py_USING_MEMORY_DEBUGGER
  643. /* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
  644. static Bigint *p5s;
  645. /* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
  646. failure; if the returned pointer is distinct from b then the original
  647. Bigint b will have been Bfree'd. Ignores the sign of b. */
  648. static Bigint *
  649. pow5mult(Bigint *b, int k)
  650. {
  651. Bigint *b1, *p5, *p51;
  652. int i;
  653. static int p05[3] = { 5, 25, 125 };
  654. if ((i = k & 3)) {
  655. b = multadd(b, p05[i-1], 0);
  656. if (b == NULL)
  657. return NULL;
  658. }
  659. if (!(k >>= 2))
  660. return b;
  661. p5 = p5s;
  662. if (!p5) {
  663. /* first time */
  664. p5 = i2b(625);
  665. if (p5 == NULL) {
  666. Bfree(b);
  667. return NULL;
  668. }
  669. p5s = p5;
  670. p5->next = 0;
  671. }
  672. for(;;) {
  673. if (k & 1) {
  674. b1 = mult(b, p5);
  675. Bfree(b);
  676. b = b1;
  677. if (b == NULL)
  678. return NULL;
  679. }
  680. if (!(k >>= 1))
  681. break;
  682. p51 = p5->next;
  683. if (!p51) {
  684. p51 = mult(p5,p5);
  685. if (p51 == NULL) {
  686. Bfree(b);
  687. return NULL;
  688. }
  689. p51->next = 0;
  690. p5->next = p51;
  691. }
  692. p5 = p51;
  693. }
  694. return b;
  695. }
  696. #else
  697. /* Version of pow5mult that doesn't cache powers of 5. Provided for
  698. the benefit of memory debugging tools like Valgrind. */
  699. static Bigint *
  700. pow5mult(Bigint *b, int k)
  701. {
  702. Bigint *b1, *p5, *p51;
  703. int i;
  704. static int p05[3] = { 5, 25, 125 };
  705. if ((i = k & 3)) {
  706. b = multadd(b, p05[i-1], 0);
  707. if (b == NULL)
  708. return NULL;
  709. }
  710. if (!(k >>= 2))
  711. return b;
  712. p5 = i2b(625);
  713. if (p5 == NULL) {
  714. Bfree(b);
  715. return NULL;
  716. }
  717. for(;;) {
  718. if (k & 1) {
  719. b1 = mult(b, p5);
  720. Bfree(b);
  721. b = b1;
  722. if (b == NULL) {
  723. Bfree(p5);
  724. return NULL;
  725. }
  726. }
  727. if (!(k >>= 1))
  728. break;
  729. p51 = mult(p5, p5);
  730. Bfree(p5);
  731. p5 = p51;
  732. if (p5 == NULL) {
  733. Bfree(b);
  734. return NULL;
  735. }
  736. }
  737. Bfree(p5);
  738. return b;
  739. }
  740. #endif /* Py_USING_MEMORY_DEBUGGER */
  741. /* shift a Bigint b left by k bits. Return a pointer to the shifted result,
  742. or NULL on failure. If the returned pointer is distinct from b then the
  743. original b will have been Bfree'd. Ignores the sign of b. */
  744. static Bigint *
  745. lshift(Bigint *b, int k)
  746. {
  747. int i, k1, n, n1;
  748. Bigint *b1;
  749. ULong *x, *x1, *xe, z;
  750. if (!k || (!b->x[0] && b->wds == 1))
  751. return b;
  752. n = k >> 5;
  753. k1 = b->k;
  754. n1 = n + b->wds + 1;
  755. for(i = b->maxwds; n1 > i; i <<= 1)
  756. k1++;
  757. b1 = Balloc(k1);
  758. if (b1 == NULL) {
  759. Bfree(b);
  760. return NULL;
  761. }
  762. x1 = b1->x;
  763. for(i = 0; i < n; i++)
  764. *x1++ = 0;
  765. x = b->x;
  766. xe = x + b->wds;
  767. if (k &= 0x1f) {
  768. k1 = 32 - k;
  769. z = 0;
  770. do {
  771. *x1++ = *x << k | z;
  772. z = *x++ >> k1;
  773. }
  774. while(x < xe);
  775. if ((*x1 = z))
  776. ++n1;
  777. }
  778. else do
  779. *x1++ = *x++;
  780. while(x < xe);
  781. b1->wds = n1 - 1;
  782. Bfree(b);
  783. return b1;
  784. }
  785. /* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
  786. 1 if a > b. Ignores signs of a and b. */
  787. static int
  788. cmp(Bigint *a, Bigint *b)
  789. {
  790. ULong *xa, *xa0, *xb, *xb0;
  791. int i, j;
  792. i = a->wds;
  793. j = b->wds;
  794. #ifdef DEBUG
  795. if (i > 1 && !a->x[i-1])
  796. Bug("cmp called with a->x[a->wds-1] == 0");
  797. if (j > 1 && !b->x[j-1])
  798. Bug("cmp called with b->x[b->wds-1] == 0");
  799. #endif
  800. if (i -= j)
  801. return i;
  802. xa0 = a->x;
  803. xa = xa0 + j;
  804. xb0 = b->x;
  805. xb = xb0 + j;
  806. for(;;) {
  807. if (*--xa != *--xb)
  808. return *xa < *xb ? -1 : 1;
  809. if (xa <= xa0)
  810. break;
  811. }
  812. return 0;
  813. }
  814. /* Take the difference of Bigints a and b, returning a new Bigint. Returns
  815. NULL on failure. The signs of a and b are ignored, but the sign of the
  816. result is set appropriately. */
  817. static Bigint *
  818. diff(Bigint *a, Bigint *b)
  819. {
  820. Bigint *c;
  821. int i, wa, wb;
  822. ULong *xa, *xae, *xb, *xbe, *xc;
  823. #ifdef ULLong
  824. ULLong borrow, y;
  825. #else
  826. ULong borrow, y;
  827. ULong z;
  828. #endif
  829. i = cmp(a,b);
  830. if (!i) {
  831. c = Balloc(0);
  832. if (c == NULL)
  833. return NULL;
  834. c->wds = 1;
  835. c->x[0] = 0;
  836. return c;
  837. }
  838. if (i < 0) {
  839. c = a;
  840. a = b;
  841. b = c;
  842. i = 1;
  843. }
  844. else
  845. i = 0;
  846. c = Balloc(a->k);
  847. if (c == NULL)
  848. return NULL;
  849. c->sign = i;
  850. wa = a->wds;
  851. xa = a->x;
  852. xae = xa + wa;
  853. wb = b->wds;
  854. xb = b->x;
  855. xbe = xb + wb;
  856. xc = c->x;
  857. borrow = 0;
  858. #ifdef ULLong
  859. do {
  860. y = (ULLong)*xa++ - *xb++ - borrow;
  861. borrow = y >> 32 & (ULong)1;
  862. *xc++ = (ULong)(y & FFFFFFFF);
  863. }
  864. while(xb < xbe);
  865. while(xa < xae) {
  866. y = *xa++ - borrow;
  867. borrow = y >> 32 & (ULong)1;
  868. *xc++ = (ULong)(y & FFFFFFFF);
  869. }
  870. #else
  871. do {
  872. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  873. borrow = (y & 0x10000) >> 16;
  874. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  875. borrow = (z & 0x10000) >> 16;
  876. Storeinc(xc, z, y);
  877. }
  878. while(xb < xbe);
  879. while(xa < xae) {
  880. y = (*xa & 0xffff) - borrow;
  881. borrow = (y & 0x10000) >> 16;
  882. z = (*xa++ >> 16) - borrow;
  883. borrow = (z & 0x10000) >> 16;
  884. Storeinc(xc, z, y);
  885. }
  886. #endif
  887. while(!*--xc)
  888. wa--;
  889. c->wds = wa;
  890. return c;
  891. }
  892. /* Given a positive normal double x, return the difference between x and the
  893. next double up. Doesn't give correct results for subnormals. */
  894. static double
  895. ulp(U *x)
  896. {
  897. Long L;
  898. U u;
  899. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  900. word0(&u) = L;
  901. word1(&u) = 0;
  902. return dval(&u);
  903. }
  904. /* Convert a Bigint to a double plus an exponent */
  905. static double
  906. b2d(Bigint *a, int *e)
  907. {
  908. ULong *xa, *xa0, w, y, z;
  909. int k;
  910. U d;
  911. xa0 = a->x;
  912. xa = xa0 + a->wds;
  913. y = *--xa;
  914. #ifdef DEBUG
  915. if (!y) Bug("zero y in b2d");
  916. #endif
  917. k = hi0bits(y);
  918. *e = 32 - k;
  919. if (k < Ebits) {
  920. word0(&d) = Exp_1 | y >> (Ebits - k);
  921. w = xa > xa0 ? *--xa : 0;
  922. word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
  923. goto ret_d;
  924. }
  925. z = xa > xa0 ? *--xa : 0;
  926. if (k -= Ebits) {
  927. word0(&d) = Exp_1 | y << k | z >> (32 - k);
  928. y = xa > xa0 ? *--xa : 0;
  929. word1(&d) = z << k | y >> (32 - k);
  930. }
  931. else {
  932. word0(&d) = Exp_1 | y;
  933. word1(&d) = z;
  934. }
  935. ret_d:
  936. return dval(&d);
  937. }
  938. /* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
  939. except that it accepts the scale parameter used in __Py_dg_strtod (which
  940. should be either 0 or 2*P), and the normalization for the return value is
  941. different (see below). On input, d should be finite and nonnegative, and d
  942. / 2**scale should be exactly representable as an IEEE 754 double.
  943. Returns a Bigint b and an integer e such that
  944. dval(d) / 2**scale = b * 2**e.
  945. Unlike d2b, b is not necessarily odd: b and e are normalized so
  946. that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
  947. and e == Etiny. This applies equally to an input of 0.0: in that
  948. case the return values are b = 0 and e = Etiny.
  949. The above normalization ensures that for all possible inputs d,
  950. 2**e gives ulp(d/2**scale).
  951. Returns NULL on failure.
  952. */
  953. static Bigint *
  954. sd2b(U *d, int scale, int *e)
  955. {
  956. Bigint *b;
  957. b = Balloc(1);
  958. if (b == NULL)
  959. return NULL;
  960. /* First construct b and e assuming that scale == 0. */
  961. b->wds = 2;
  962. b->x[0] = word1(d);
  963. b->x[1] = word0(d) & Frac_mask;
  964. *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
  965. if (*e < Etiny)
  966. *e = Etiny;
  967. else
  968. b->x[1] |= Exp_msk1;
  969. /* Now adjust for scale, provided that b != 0. */
  970. if (scale && (b->x[0] || b->x[1])) {
  971. *e -= scale;
  972. if (*e < Etiny) {
  973. scale = Etiny - *e;
  974. *e = Etiny;
  975. /* We can't shift more than P-1 bits without shifting out a 1. */
  976. assert(0 < scale && scale <= P - 1);
  977. if (scale >= 32) {
  978. /* The bits shifted out should all be zero. */
  979. assert(b->x[0] == 0);
  980. b->x[0] = b->x[1];
  981. b->x[1] = 0;
  982. scale -= 32;
  983. }
  984. if (scale) {
  985. /* The bits shifted out should all be zero. */
  986. assert(b->x[0] << (32 - scale) == 0);
  987. b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
  988. b->x[1] >>= scale;
  989. }
  990. }
  991. }
  992. /* Ensure b is normalized. */
  993. if (!b->x[1])
  994. b->wds = 1;
  995. return b;
  996. }
  997. /* Convert a double to a Bigint plus an exponent. Return NULL on failure.
  998. Given a finite nonzero double d, return an odd Bigint b and exponent *e
  999. such that fabs(d) = b * 2**e. On return, *bbits gives the number of
  1000. significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
  1001. If d is zero, then b == 0, *e == -1010, *bbits = 0.
  1002. */
  1003. static Bigint *
  1004. d2b(U *d, int *e, int *bits)
  1005. {
  1006. Bigint *b;
  1007. int de, k;
  1008. ULong *x, y, z;
  1009. int i;
  1010. b = Balloc(1);
  1011. if (b == NULL)
  1012. return NULL;
  1013. x = b->x;
  1014. z = word0(d) & Frac_mask;
  1015. word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
  1016. if ((de = (int)(word0(d) >> Exp_shift)))
  1017. z |= Exp_msk1;
  1018. if ((y = word1(d))) {
  1019. if ((k = lo0bits(&y))) {
  1020. x[0] = y | z << (32 - k);
  1021. z >>= k;
  1022. }
  1023. else
  1024. x[0] = y;
  1025. i =
  1026. b->wds = (x[1] = z) ? 2 : 1;
  1027. }
  1028. else {
  1029. k = lo0bits(&z);
  1030. x[0] = z;
  1031. i =
  1032. b->wds = 1;
  1033. k += 32;
  1034. }
  1035. if (de) {
  1036. *e = de - Bias - (P-1) + k;
  1037. *bits = P - k;
  1038. }
  1039. else {
  1040. *e = de - Bias - (P-1) + 1 + k;
  1041. *bits = 32*i - hi0bits(x[i-1]);
  1042. }
  1043. return b;
  1044. }
  1045. /* Compute the ratio of two Bigints, as a double. The result may have an
  1046. error of up to 2.5 ulps. */
  1047. static double
  1048. ratio(Bigint *a, Bigint *b)
  1049. {
  1050. U da, db;
  1051. int k, ka, kb;
  1052. dval(&da) = b2d(a, &ka);
  1053. dval(&db) = b2d(b, &kb);
  1054. k = ka - kb + 32*(a->wds - b->wds);
  1055. if (k > 0)
  1056. word0(&da) += k*Exp_msk1;
  1057. else {
  1058. k = -k;
  1059. word0(&db) += k*Exp_msk1;
  1060. }
  1061. return dval(&da) / dval(&db);
  1062. }
  1063. static const double
  1064. tens[] = {
  1065. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1066. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1067. 1e20, 1e21, 1e22
  1068. };
  1069. static const double
  1070. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1071. static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1072. 9007199254740992.*9007199254740992.e-256
  1073. /* = 2^106 * 1e-256 */
  1074. };
  1075. /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  1076. /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
  1077. #define Scale_Bit 0x10
  1078. #define n_bigtens 5
  1079. #define ULbits 32
  1080. #define kshift 5
  1081. #define kmask 31
  1082. static int
  1083. dshift(Bigint *b, int p2)
  1084. {
  1085. int rv = hi0bits(b->x[b->wds-1]) - 4;
  1086. if (p2 > 0)
  1087. rv -= p2;
  1088. return rv & kmask;
  1089. }
  1090. /* special case of Bigint division. The quotient is always in the range 0 <=
  1091. quotient < 10, and on entry the divisor S is normalized so that its top 4
  1092. bits (28--31) are zero and bit 27 is set. */
  1093. static int
  1094. quorem(Bigint *b, Bigint *S)
  1095. {
  1096. int n;
  1097. ULong *bx, *bxe, q, *sx, *sxe;
  1098. #ifdef ULLong
  1099. ULLong borrow, carry, y, ys;
  1100. #else
  1101. ULong borrow, carry, y, ys;
  1102. ULong si, z, zs;
  1103. #endif
  1104. n = S->wds;
  1105. #ifdef DEBUG
  1106. /*debug*/ if (b->wds > n)
  1107. /*debug*/ Bug("oversize b in quorem");
  1108. #endif
  1109. if (b->wds < n)
  1110. return 0;
  1111. sx = S->x;
  1112. sxe = sx + --n;
  1113. bx = b->x;
  1114. bxe = bx + n;
  1115. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  1116. #ifdef DEBUG
  1117. /*debug*/ if (q > 9)
  1118. /*debug*/ Bug("oversized quotient in quorem");
  1119. #endif
  1120. if (q) {
  1121. borrow = 0;
  1122. carry = 0;
  1123. do {
  1124. #ifdef ULLong
  1125. ys = *sx++ * (ULLong)q + carry;
  1126. carry = ys >> 32;
  1127. y = *bx - (ys & FFFFFFFF) - borrow;
  1128. borrow = y >> 32 & (ULong)1;
  1129. *bx++ = (ULong)(y & FFFFFFFF);
  1130. #else
  1131. si = *sx++;
  1132. ys = (si & 0xffff) * q + carry;
  1133. zs = (si >> 16) * q + (ys >> 16);
  1134. carry = zs >> 16;
  1135. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1136. borrow = (y & 0x10000) >> 16;
  1137. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1138. borrow = (z & 0x10000) >> 16;
  1139. Storeinc(bx, z, y);
  1140. #endif
  1141. }
  1142. while(sx <= sxe);
  1143. if (!*bxe) {
  1144. bx = b->x;
  1145. while(--bxe > bx && !*bxe)
  1146. --n;
  1147. b->wds = n;
  1148. }
  1149. }
  1150. if (cmp(b, S) >= 0) {
  1151. q++;
  1152. borrow = 0;
  1153. carry = 0;
  1154. bx = b->x;
  1155. sx = S->x;
  1156. do {
  1157. #ifdef ULLong
  1158. ys = *sx++ + carry;
  1159. carry = ys >> 32;
  1160. y = *bx - (ys & FFFFFFFF) - borrow;
  1161. borrow = y >> 32 & (ULong)1;
  1162. *bx++ = (ULong)(y & FFFFFFFF);
  1163. #else
  1164. si = *sx++;
  1165. ys = (si & 0xffff) + carry;
  1166. zs = (si >> 16) + (ys >> 16);
  1167. carry = zs >> 16;
  1168. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1169. borrow = (y & 0x10000) >> 16;
  1170. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1171. borrow = (z & 0x10000) >> 16;
  1172. Storeinc(bx, z, y);
  1173. #endif
  1174. }
  1175. while(sx <= sxe);
  1176. bx = b->x;
  1177. bxe = bx + n;
  1178. if (!*bxe) {
  1179. while(--bxe > bx && !*bxe)
  1180. --n;
  1181. b->wds = n;
  1182. }
  1183. }
  1184. return q;
  1185. }
  1186. /* sulp(x) is a version of ulp(x) that takes bc.scale into account.
  1187. Assuming that x is finite and nonnegative (positive zero is fine
  1188. here) and x / 2^bc.scale is exactly representable as a double,
  1189. sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
  1190. static double
  1191. sulp(U *x, BCinfo *bc)
  1192. {
  1193. U u;
  1194. if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
  1195. /* rv/2^bc->scale is subnormal */
  1196. word0(&u) = (P+2)*Exp_msk1;
  1197. word1(&u) = 0;
  1198. return u.d;
  1199. }
  1200. else {
  1201. assert(word0(x) || word1(x)); /* x != 0.0 */
  1202. return ulp(x);
  1203. }
  1204. }
  1205. /* The bigcomp function handles some hard cases for strtod, for inputs
  1206. with more than STRTOD_DIGLIM digits. It's called once an initial
  1207. estimate for the double corresponding to the input string has
  1208. already been obtained by the code in __Py_dg_strtod.
  1209. The bigcomp function is only called after __Py_dg_strtod has found a
  1210. double value rv such that either rv or rv + 1ulp represents the
  1211. correctly rounded value corresponding to the original string. It
  1212. determines which of these two values is the correct one by
  1213. computing the decimal digits of rv + 0.5ulp and comparing them with
  1214. the corresponding digits of s0.
  1215. In the following, write dv for the absolute value of the number represented
  1216. by the input string.
  1217. Inputs:
  1218. s0 points to the first significant digit of the input string.
  1219. rv is a (possibly scaled) estimate for the closest double value to the
  1220. value represented by the original input to __Py_dg_strtod. If
  1221. bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
  1222. the input value.
  1223. bc is a struct containing information gathered during the parsing and
  1224. estimation steps of __Py_dg_strtod. Description of fields follows:
  1225. bc->e0 gives the exponent of the input value, such that dv = (integer
  1226. given by the bd->nd digits of s0) * 10**e0
  1227. bc->nd gives the total number of significant digits of s0. It will
  1228. be at least 1.
  1229. bc->nd0 gives the number of significant digits of s0 before the
  1230. decimal separator. If there's no decimal separator, bc->nd0 ==
  1231. bc->nd.
  1232. bc->scale is the value used to scale rv to avoid doing arithmetic with
  1233. subnormal values. It's either 0 or 2*P (=106).
  1234. Outputs:
  1235. On successful exit, rv/2^(bc->scale) is the closest double to dv.
  1236. Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
  1237. static int
  1238. bigcomp(U *rv, const char *s0, BCinfo *bc)
  1239. {
  1240. Bigint *b, *d;
  1241. int b2, d2, dd, i, nd, nd0, odd, p2, p5;
  1242. nd = bc->nd;
  1243. nd0 = bc->nd0;
  1244. p5 = nd + bc->e0;
  1245. b = sd2b(rv, bc->scale, &p2);
  1246. if (b == NULL)
  1247. return -1;
  1248. /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
  1249. case, this is used for round to even. */
  1250. odd = b->x[0] & 1;
  1251. /* left shift b by 1 bit and or a 1 into the least significant bit;
  1252. this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
  1253. b = lshift(b, 1);
  1254. if (b == NULL)
  1255. return -1;
  1256. b->x[0] |= 1;
  1257. p2--;
  1258. p2 -= p5;
  1259. d = i2b(1);
  1260. if (d == NULL) {
  1261. Bfree(b);
  1262. return -1;
  1263. }
  1264. /* Arrange for convenient computation of quotients:
  1265. * shift left if necessary so divisor has 4 leading 0 bits.
  1266. */
  1267. if (p5 > 0) {
  1268. d = pow5mult(d, p5);
  1269. if (d == NULL) {
  1270. Bfree(b);
  1271. return -1;
  1272. }
  1273. }
  1274. else if (p5 < 0) {
  1275. b = pow5mult(b, -p5);
  1276. if (b == NULL) {
  1277. Bfree(d);
  1278. return -1;
  1279. }
  1280. }
  1281. if (p2 > 0) {
  1282. b2 = p2;
  1283. d2 = 0;
  1284. }
  1285. else {
  1286. b2 = 0;
  1287. d2 = -p2;
  1288. }
  1289. i = dshift(d, d2);
  1290. if ((b2 += i) > 0) {
  1291. b = lshift(b, b2);
  1292. if (b == NULL) {
  1293. Bfree(d);
  1294. return -1;
  1295. }
  1296. }
  1297. if ((d2 += i) > 0) {
  1298. d = lshift(d, d2);
  1299. if (d == NULL) {
  1300. Bfree(b);
  1301. return -1;
  1302. }
  1303. }
  1304. /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
  1305. * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
  1306. * a number in the range [0.1, 1). */
  1307. if (cmp(b, d) >= 0)
  1308. /* b/d >= 1 */
  1309. dd = -1;
  1310. else {
  1311. i = 0;
  1312. for(;;) {
  1313. b = multadd(b, 10, 0);
  1314. if (b == NULL) {
  1315. Bfree(d);
  1316. return -1;
  1317. }
  1318. dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
  1319. i++;
  1320. if (dd)
  1321. break;
  1322. if (!b->x[0] && b->wds == 1) {
  1323. /* b/d == 0 */
  1324. dd = i < nd;
  1325. break;
  1326. }
  1327. if (!(i < nd)) {
  1328. /* b/d != 0, but digits of s0 exhausted */
  1329. dd = -1;
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. Bfree(b);
  1335. Bfree(d);
  1336. if (dd > 0 || (dd == 0 && odd))
  1337. dval(rv) += sulp(rv, bc);
  1338. return 0;
  1339. }
  1340. static double
  1341. __Py_dg_strtod(const char *s00, char **se)
  1342. {
  1343. int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
  1344. int esign, i, j, k, lz, nd, nd0, odd, sign;
  1345. const char *s, *s0, *s1;
  1346. double aadj, aadj1;
  1347. U aadj2, adj, rv, rv0;
  1348. ULong y, z, abs_exp;
  1349. Long L;
  1350. BCinfo bc;
  1351. Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  1352. dval(&rv) = 0.;
  1353. /* Start parsing. */
  1354. c = *(s = s00);
  1355. /* Parse optional sign, if present. */
  1356. sign = 0;
  1357. switch (c) {
  1358. case '-':
  1359. sign = 1;
  1360. /* no break */
  1361. case '+':
  1362. c = *++s;
  1363. }
  1364. /* Skip leading zeros: lz is true iff there were leading zeros. */
  1365. s1 = s;
  1366. while (c == '0')
  1367. c = *++s;
  1368. lz = s != s1;
  1369. /* Point s0 at the first nonzero digit (if any). nd0 will be the position
  1370. of the point relative to s0. nd will be the total number of digits
  1371. ignoring leading zeros. */
  1372. s0 = s1 = s;
  1373. while ('0' <= c && c <= '9')
  1374. c = *++s;
  1375. nd0 = nd = s - s1;
  1376. /* Parse decimal point and following digits. */
  1377. if (c == '.') {
  1378. c = *++s;
  1379. if (!nd) {
  1380. s1 = s;
  1381. while (c == '0')
  1382. c = *++s;
  1383. lz = lz || s != s1;
  1384. nd0 -= s - s1;
  1385. s0 = s;
  1386. }
  1387. s1 = s;
  1388. while ('0' <= c && c <= '9')
  1389. c = *++s;
  1390. nd += s - s1;
  1391. }
  1392. /* Now lz is true if and only if there were leading zero digits, and nd
  1393. gives the total number of digits ignoring leading zeros. A valid input
  1394. must have at least one digit. */
  1395. if (!nd && !lz) {
  1396. if (se)
  1397. *se = (char *)s00;
  1398. goto parse_error;
  1399. }
  1400. /* Parse exponent. */
  1401. e = 0;
  1402. if (c == 'e' || c == 'E') {
  1403. s00 = s;
  1404. c = *++s;
  1405. /* Exponent sign. */
  1406. esign = 0;
  1407. switch (c) {
  1408. case '-':
  1409. esign = 1;
  1410. /* no break */
  1411. case '+':
  1412. c = *++s;
  1413. }
  1414. /* Skip zeros. lz is true iff there are leading zeros. */
  1415. s1 = s;
  1416. while (c == '0')
  1417. c = *++s;
  1418. lz = s != s1;
  1419. /* Get absolute value of the exponent. */
  1420. s1 = s;
  1421. abs_exp = 0;
  1422. while ('0' <= c && c <= '9') {
  1423. abs_exp = 10*abs_exp + (c - '0');
  1424. c = *++s;
  1425. }
  1426. /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
  1427. there are at most 9 significant exponent digits then overflow is
  1428. impossible. */
  1429. if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
  1430. e = (int)MAX_ABS_EXP;
  1431. else
  1432. e = (int)abs_exp;
  1433. if (esign)
  1434. e = -e;
  1435. /* A valid exponent must have at least one digit. */
  1436. if (s == s1 && !lz)
  1437. s = s00;
  1438. }
  1439. /* Adjust exponent to take into account position of the point. */
  1440. e -= nd - nd0;
  1441. if (nd0 <= 0)
  1442. nd0 = nd;
  1443. /* Finished parsing. Set se to indicate how far we parsed */
  1444. if (se)
  1445. *se = (char *)s;
  1446. /* If all digits were zero, exit with return value +-0.0. Otherwise,
  1447. strip trailing zeros: scan back until we hit a nonzero digit. */
  1448. if (!nd)
  1449. goto ret;
  1450. for (i = nd; i > 0; ) {
  1451. --i;
  1452. if (s0[i < nd0 ? i : i+1] != '0') {
  1453. ++i;
  1454. break;
  1455. }
  1456. }
  1457. e += nd - i;
  1458. nd = i;
  1459. if (nd0 > nd)
  1460. nd0 = nd;
  1461. /* Summary of parsing results. After parsing, and dealing with zero
  1462. * inputs, we have values s0, nd0, nd, e, sign, where:
  1463. *
  1464. * - s0 points to the first significant digit of the input string
  1465. *
  1466. * - nd is the total number of significant digits (here, and
  1467. * below, 'significant digits' means the set of digits of the
  1468. * significand of the input that remain after ignoring leading
  1469. * and trailing zeros).
  1470. *
  1471. * - nd0 indicates the position of the decimal point, if present; it
  1472. * satisfies 1 <= nd0 <= nd. The nd significant digits are in
  1473. * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
  1474. * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
  1475. * nd0 == nd, then s0[nd0] could be any non-digit character.)
  1476. *
  1477. * - e is the adjusted exponent: the absolute value of the number
  1478. * represented by the original input string is n * 10**e, where
  1479. * n is the integer represented by the concatenation of
  1480. * s0[0:nd0] and s0[nd0+1:nd+1]
  1481. *
  1482. * - sign gives the sign of the input: 1 for negative, 0 for positive
  1483. *
  1484. * - the first and last significant digits are nonzero
  1485. */
  1486. /* put first DBL_DIG+1 digits into integer y and z.
  1487. *
  1488. * - y contains the value represented by the first min(9, nd)
  1489. * significant digits
  1490. *
  1491. * - if nd > 9, z contains the value represented by significant digits
  1492. * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
  1493. * gives the value represented by the first min(16, nd) sig. digits.
  1494. */
  1495. bc.e0 = e1 = e;
  1496. y = z = 0;
  1497. for (i = 0; i < nd; i++) {
  1498. if (i < 9)
  1499. y = 10*y + s0[i < nd0 ? i : i+1] - '0';
  1500. else if (i < DBL_DIG+1)
  1501. z = 10*z + s0[i < nd0 ? i : i+1] - '0';
  1502. else
  1503. break;
  1504. }
  1505. k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1506. dval(&rv) = y;
  1507. if (k > 9) {
  1508. dval(&rv) = tens[k - 9] * dval(&rv) + z;
  1509. }
  1510. bd0 = 0;
  1511. if (nd <= DBL_DIG
  1512. && Flt_Rounds == 1
  1513. ) {
  1514. if (!e)
  1515. goto ret;
  1516. if (e > 0) {
  1517. if (e <= Ten_pmax) {
  1518. dval(&rv) *= tens[e];
  1519. goto ret;
  1520. }
  1521. i = DBL_DIG - nd;
  1522. if (e <= Ten_pmax + i) {
  1523. /* A fancier test would sometimes let us do
  1524. * this for larger i values.
  1525. */
  1526. e -= i;
  1527. dval(&rv) *= tens[i];
  1528. dval(&rv) *= tens[e];
  1529. goto ret;
  1530. }
  1531. }
  1532. else if (e >= -Ten_pmax) {
  1533. dval(&rv) /= tens[-e];
  1534. goto ret;
  1535. }
  1536. }
  1537. e1 += nd - k;
  1538. bc.scale = 0;
  1539. /* Get starting approximation = rv * 10**e1 */
  1540. if (e1 > 0) {
  1541. if ((i = e1 & 15))
  1542. dval(&rv) *= tens[i];
  1543. if (e1 &= ~15) {
  1544. if (e1 > DBL_MAX_10_EXP)
  1545. goto ovfl;
  1546. e1 >>= 4;
  1547. for(j = 0; e1 > 1; j++, e1 >>= 1)
  1548. if (e1 & 1)
  1549. dval(&rv) *= bigtens[j];
  1550. /* The last multiplication could overflow. */
  1551. word0(&rv) -= P*Exp_msk1;
  1552. dval(&rv) *= bigtens[j];
  1553. if ((z = word0(&rv) & Exp_mask)
  1554. > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1555. goto ovfl;
  1556. if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1557. /* set to largest number */
  1558. /* (Can't trust DBL_MAX) */
  1559. word0(&rv) = Big0;
  1560. word1(&rv) = Big1;
  1561. }
  1562. else
  1563. word0(&rv) += P*Exp_msk1;
  1564. }
  1565. }
  1566. else if (e1 < 0) {
  1567. /* The input decimal value lies in [10**e1, 10**(e1+16)).
  1568. If e1 <= -512, underflow immediately.
  1569. If e1 <= -256, set bc.scale to 2*P.
  1570. So for input value < 1e-256, bc.scale is always set;
  1571. for input value >= 1e-240, bc.scale is never set.
  1572. For input values in [1e-256, 1e-240), bc.scale may or may
  1573. not be set. */
  1574. e1 = -e1;
  1575. if ((i = e1 & 15))
  1576. dval(&rv) /= tens[i];
  1577. if (e1 >>= 4) {
  1578. if (e1 >= 1 << n_bigtens)
  1579. goto undfl;
  1580. if (e1 & Scale_Bit)
  1581. bc.scale = 2*P;
  1582. for(j = 0; e1 > 0; j++, e1 >>= 1)
  1583. if (e1 & 1)
  1584. dval(&rv) *= tinytens[j];
  1585. if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
  1586. >> Exp_shift)) > 0) {
  1587. /* scaled rv is denormal; clear j low bits */
  1588. if (j >= 32) {
  1589. word1(&rv) = 0;
  1590. if (j >= 53)
  1591. word0(&rv) = (P+2)*Exp_msk1;
  1592. else
  1593. word0(&rv) &= 0xffffffff << (j-32);
  1594. }
  1595. else
  1596. word1(&rv) &= 0xffffffff << j;
  1597. }
  1598. if (!dval(&rv))
  1599. goto undfl;
  1600. }
  1601. }
  1602. /* Now the hard part -- adjusting rv to the correct value.*/
  1603. /* Put digits into bd: true value = bd * 10^e */
  1604. bc.nd = nd;
  1605. bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
  1606. /* to silence an erroneous warning about bc.nd0 */
  1607. /* possibly not being initialized. */
  1608. if (nd > STRTOD_DIGLIM) {
  1609. /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
  1610. /* minimum number of decimal digits to distinguish double values */
  1611. /* in IEEE arithmetic. */
  1612. /* Truncate input to 18 significant digits, then discard any trailing
  1613. zeros on the result by updating nd, nd0, e and y suitably. (There's
  1614. no need to update z; it's not reused beyond this point.) */
  1615. for (i = 18; i > 0; ) {
  1616. /* scan back until we hit a nonzero digit. significant digit 'i'
  1617. is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
  1618. --i;
  1619. if (s0[i < nd0 ? i : i+1] != '0') {
  1620. ++i;
  1621. break;
  1622. }
  1623. }
  1624. e += nd - i;
  1625. nd = i;
  1626. if (nd0 > nd)
  1627. nd0 = nd;
  1628. if (nd < 9) { /* must recompute y */
  1629. y = 0;
  1630. for(i = 0; i < nd0; ++i)
  1631. y = 10*y + s0[i] - '0';
  1632. for(; i < nd; ++i)
  1633. y = 10*y + s0[i+1] - '0';
  1634. }
  1635. }
  1636. bd0 = s2b(s0, nd0, nd, y);
  1637. if (bd0 == NULL)
  1638. goto failed_malloc;
  1639. /* Notation for the comments below. Write:
  1640. - dv for the absolute value of the number represented by the original
  1641. decimal input string.
  1642. - if we've truncated dv, write tdv for the truncated value.
  1643. Otherwise, set tdv == dv.
  1644. - srv for the quantity rv/2^bc.scale; so srv is the current binary
  1645. approximation to tdv (and dv). It should be exactly representable
  1646. in an IEEE 754 double.
  1647. */
  1648. for(;;) {
  1649. /* This is the main correction loop for __Py_dg_strtod.
  1650. We've got a decimal value tdv, and a floating-point approximation
  1651. srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
  1652. close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
  1653. approximation if not.
  1654. To determine whether srv is close enough to tdv, compute integ…

Large files files are truncated, but you can click here to view the full file