PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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