annotate Python/dtoa.c @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5cea27275af1
children 6d6a018a3bb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1 /****************************************************************
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
3 * The author of this software is David M. Gay.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
4 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
6 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
7 * Permission to use, copy, modify, and distribute this software for any
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
8 * purpose without fee is hereby granted, provided that this entire notice
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
9 * is included in all copies of any software which is or includes a copy
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
10 * or modification of this software and in all copies of the supporting
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
11 * documentation for such software.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
12 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
17 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
18 ***************************************************************/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
19
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
20 /****************************************************************
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
21 * This is dtoa.c by David M. Gay, downloaded from
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
22 * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
23 * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
24 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
25 * Please remember to check http://www.netlib.org/fp regularly (and especially
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
26 * before any Python release) for bugfixes and updates.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
27 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
28 * The major modifications from Gay's original code are as follows:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
29 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
30 * 0. The original code has been specialized to Python's needs by removing
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
31 * many of the #ifdef'd sections. In particular, code to support VAX and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
32 * IBM floating-point formats, hex NaNs, hex floats, locale-aware
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
33 * treatment of the decimal point, and setting of the inexact flag have
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
34 * been removed.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
35 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
36 * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
37 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
38 * 2. The public functions strtod, dtoa and freedtoa all now have
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
39 * a _Py_dg_ prefix.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
40 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
41 * 3. Instead of assuming that PyMem_Malloc always succeeds, we thread
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
42 * PyMem_Malloc failures through the code. The functions
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
43 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
44 * Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
45 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
46 * of return type *Bigint all return NULL to indicate a malloc failure.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
47 * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
48 * failure. bigcomp now has return type int (it used to be void) and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
49 * returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
50 * on failure. _Py_dg_strtod indicates failure due to malloc failure
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
51 * by returning -1.0, setting errno=ENOMEM and *se to s00.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
52 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
53 * 4. The static variable dtoa_result has been removed. Callers of
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
54 * _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
55 * the memory allocated by _Py_dg_dtoa.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
56 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
57 * 5. The code has been reformatted to better fit with Python's
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
58 * C style guide (PEP 7).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
59 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
60 * 6. A bug in the memory allocation has been fixed: to avoid FREEing memory
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
61 * that hasn't been MALLOC'ed, private_mem should only be used when k <=
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
62 * Kmax.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
63 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
64 * 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
65 * leading whitespace.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
66 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
67 ***************************************************************/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
68
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
69 /* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
70 * at acm dot org, with " at " changed at "@" and " dot " changed to ".").
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
71 * Please report bugs for this modified version using the Python issue tracker
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
72 * (http://bugs.python.org). */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
73
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
74 /* On a machine with IEEE extended-precision registers, it is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
75 * necessary to specify double-precision (53-bit) rounding precision
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
76 * before invoking strtod or dtoa. If the machine uses (the equivalent
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
77 * of) Intel 80x87 arithmetic, the call
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
78 * _control87(PC_53, MCW_PC);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
79 * does this with many compilers. Whether this or another call is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
80 * appropriate depends on the compiler; for this to work, it may be
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
81 * necessary to #include "float.h" or another system-dependent header
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
82 * file.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
83 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
84
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
85 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
86 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
87 * This strtod returns a nearest machine number to the input decimal
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
88 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
89 * broken by the IEEE round-even rule. Otherwise ties are broken by
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
90 * biased rounding (add half and chop).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
91 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
92 * Inspired loosely by William D. Clinger's paper "How to Read Floating
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
93 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
94 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
95 * Modifications:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
96 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
97 * 1. We only require IEEE, IBM, or VAX double-precision
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
98 * arithmetic (not IEEE double-extended).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
99 * 2. We get by with floating-point arithmetic in a case that
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
100 * Clinger missed -- when we're computing d * 10^n
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
101 * for a small integer d and the integer n is not too
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
102 * much larger than 22 (the maximum integer k for which
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
103 * we can represent 10^k exactly), we may be able to
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
104 * compute (d*10^k) * 10^(e-k) with just one roundoff.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
105 * 3. Rather than a bit-at-a-time adjustment of the binary
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
106 * result in the hard case, we use floating-point
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
107 * arithmetic to determine the adjustment to within
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
108 * one bit; only in really hard cases do we need to
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
109 * compute a second residual.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
110 * 4. Because of 3., we don't need a large table of powers of 10
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
111 * for ten-to-e (just some small tables, e.g. of 10^k
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
112 * for 0 <= k <= 22).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
113 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
114
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
115 /* Linking of Python's #defines to Gay's #defines starts here. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
116
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
117 #include "Python.h"
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
118
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
119 /* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
120 the following code */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
121 #ifndef PY_NO_SHORT_FLOAT_REPR
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
122
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
123 #include "float.h"
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
124
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
125 #define MALLOC PyMem_Malloc
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
126 #define FREE PyMem_Free
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
127
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
128 /* This code should also work for ARM mixed-endian format on little-endian
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
129 machines, where doubles have byte order 45670123 (in increasing address
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
130 order, 0 being the least significant byte). */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
131 #ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
132 # define IEEE_8087
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
133 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
134 #if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
135 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
136 # define IEEE_MC68k
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
137 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
138 #if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
139 #error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
140 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
141
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
142 /* The code below assumes that the endianness of integers matches the
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
143 endianness of the two 32-bit words of a double. Check this. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
144 #if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
145 defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
146 #error "doubles and ints have incompatible endianness"
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
147 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
148
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
149 #if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
150 #error "doubles and ints have incompatible endianness"
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
151 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
152
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
153
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
154 #if defined(HAVE_UINT32_T) && defined(HAVE_INT32_T)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
155 typedef PY_UINT32_T ULong;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
156 typedef PY_INT32_T Long;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
157 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
158 #error "Failed to find an exact-width 32-bit integer type"
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
159 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
160
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
161 #if defined(HAVE_UINT64_T)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
162 #define ULLong PY_UINT64_T
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
163 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
164 #undef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
165 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
166
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
167 #undef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
168 #ifdef Py_DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
169 #define DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
170 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
171
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
172 /* End Python #define linking */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
173
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
174 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
175 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
176 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
177
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
178 #ifndef PRIVATE_MEM
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
179 #define PRIVATE_MEM 2304
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
180 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
181 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
182 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
183
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
184 #ifdef __cplusplus
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
185 extern "C" {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
186 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
187
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
188 typedef union { double d; ULong L[2]; } U;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
189
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
190 #ifdef IEEE_8087
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
191 #define word0(x) (x)->L[1]
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
192 #define word1(x) (x)->L[0]
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
193 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
194 #define word0(x) (x)->L[0]
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
195 #define word1(x) (x)->L[1]
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
196 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
197 #define dval(x) (x)->d
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
198
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
199 #ifndef STRTOD_DIGLIM
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
200 #define STRTOD_DIGLIM 40
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
201 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
202
58249
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
203 /* maximum permitted exponent value for strtod; exponents larger than
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
204 MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
205 should fit into an int. */
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
206 #ifndef MAX_ABS_EXP
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
207 #define MAX_ABS_EXP 19999U
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
208 #endif
33e402909cf2 Change a variable type to avoid signed overflow; replace repeated '19999' constant by a define.
Mark Dickinson <dickinsm@gmail.com>
parents: 58239
diff changeset
209
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
210 /* The following definition of Storeinc is appropriate for MIPS processors.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
211 * An alternative that might be better on some machines is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
212 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
213 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
214 #if defined(IEEE_8087)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
215 #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
216 ((unsigned short *)a)[0] = (unsigned short)c, a++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
217 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
218 #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
219 ((unsigned short *)a)[1] = (unsigned short)c, a++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
220 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
221
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
222 /* #define P DBL_MANT_DIG */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
223 /* Ten_pmax = floor(P*log(2)/log(5)) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
224 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
225 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
226 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
227
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
228 #define Exp_shift 20
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
229 #define Exp_shift1 20
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
230 #define Exp_msk1 0x100000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
231 #define Exp_msk11 0x100000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
232 #define Exp_mask 0x7ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
233 #define P 53
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
234 #define Nbits 53
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
235 #define Bias 1023
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
236 #define Emax 1023
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
237 #define Emin (-1022)
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
238 #define Etiny (-1074) /* smallest denormal is 2**Etiny */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
239 #define Exp_1 0x3ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
240 #define Exp_11 0x3ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
241 #define Ebits 11
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
242 #define Frac_mask 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
243 #define Frac_mask1 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
244 #define Ten_pmax 22
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
245 #define Bletch 0x10
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
246 #define Bndry_mask 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
247 #define Bndry_mask1 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
248 #define Sign_bit 0x80000000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
249 #define Log2P 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
250 #define Tiny0 0
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
251 #define Tiny1 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
252 #define Quick_max 14
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
253 #define Int_max 14
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
254
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
255 #ifndef Flt_Rounds
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
256 #ifdef FLT_ROUNDS
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
257 #define Flt_Rounds FLT_ROUNDS
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
258 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
259 #define Flt_Rounds 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
260 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
261 #endif /*Flt_Rounds*/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
262
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
263 #define Rounding Flt_Rounds
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
264
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
265 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
266 #define Big1 0xffffffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
267
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
268 /* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
269
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
270 typedef struct BCinfo BCinfo;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
271 struct
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
272 BCinfo {
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
273 int e0, nd, nd0, scale;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
274 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
275
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
276 #define FFFFFFFF 0xffffffffUL
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
277
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
278 #define Kmax 7
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
279
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
280 /* struct Bigint is used to represent arbitrary-precision integers. These
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
281 integers are stored in sign-magnitude format, with the magnitude stored as
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
282 an array of base 2**32 digits. Bigints are always normalized: if x is a
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
283 Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
284
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
285 The Bigint fields are as follows:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
286
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
287 - next is a header used by Balloc and Bfree to keep track of lists
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
288 of freed Bigints; it's also used for the linked list of
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
289 powers of 5 of the form 5**2**i used by pow5mult.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
290 - k indicates which pool this Bigint was allocated from
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
291 - maxwds is the maximum number of words space was allocated for
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
292 (usually maxwds == 2**k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
293 - sign is 1 for negative Bigints, 0 for positive. The sign is unused
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
294 (ignored on inputs, set to 0 on outputs) in almost all operations
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
295 involving Bigints: a notable exception is the diff function, which
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
296 ignores signs on inputs but sets the sign of the output correctly.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
297 - wds is the actual number of significant words
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
298 - x contains the vector of words (digits) for this Bigint, from least
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
299 significant (x[0]) to most significant (x[wds-1]).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
300 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
301
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
302 struct
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
303 Bigint {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
304 struct Bigint *next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
305 int k, maxwds, sign, wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
306 ULong x[1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
307 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
308
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
309 typedef struct Bigint Bigint;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
310
58401
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
311 #ifndef Py_USING_MEMORY_DEBUGGER
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
312
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
313 /* Memory management: memory is allocated from, and returned to, Kmax+1 pools
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
314 of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
315 1 << k. These pools are maintained as linked lists, with freelist[k]
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
316 pointing to the head of the list for pool k.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
317
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
318 On allocation, if there's no free slot in the appropriate pool, MALLOC is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
319 called to get more memory. This memory is not returned to the system until
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
320 Python quits. There's also a private memory pool that's allocated from
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
321 in preference to using MALLOC.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
322
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
323 For Bigints with more than (1 << Kmax) digits (which implies at least 1233
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
324 decimal digits), memory is directly allocated using MALLOC, and freed using
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
325 FREE.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
326
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
327 XXX: it would be easy to bypass this memory-management system and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
328 translate each call to Balloc into a call to PyMem_Malloc, and each
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
329 Bfree to PyMem_Free. Investigate whether this has any significant
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
330 performance on impact. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
331
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
332 static Bigint *freelist[Kmax+1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
333
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
334 /* Allocate space for a Bigint with up to 1<<k digits */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
335
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
336 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
337 Balloc(int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
338 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
339 int x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
340 Bigint *rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
341 unsigned int len;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
342
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
343 if (k <= Kmax && (rv = freelist[k]))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
344 freelist[k] = rv->next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
345 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
346 x = 1 << k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
347 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
348 /sizeof(double);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
349 if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
350 rv = (Bigint*)pmem_next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
351 pmem_next += len;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
352 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
353 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
354 rv = (Bigint*)MALLOC(len*sizeof(double));
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
355 if (rv == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
356 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
357 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
358 rv->k = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
359 rv->maxwds = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
360 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
361 rv->sign = rv->wds = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
362 return rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
363 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
364
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
365 /* Free a Bigint allocated with Balloc */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
366
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
367 static void
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
368 Bfree(Bigint *v)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
369 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
370 if (v) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
371 if (v->k > Kmax)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
372 FREE((void*)v);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
373 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
374 v->next = freelist[v->k];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
375 freelist[v->k] = v;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
376 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
377 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
378 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
379
58401
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
380 #else
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
381
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
382 /* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
383 PyMem_Free directly in place of the custom memory allocation scheme above.
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
384 These are provided for the benefit of memory debugging tools like
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
385 Valgrind. */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
386
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
387 /* Allocate space for a Bigint with up to 1<<k digits */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
388
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
389 static Bigint *
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
390 Balloc(int k)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
391 {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
392 int x;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
393 Bigint *rv;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
394 unsigned int len;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
395
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
396 x = 1 << k;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
397 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
398 /sizeof(double);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
399
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
400 rv = (Bigint*)MALLOC(len*sizeof(double));
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
401 if (rv == NULL)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
402 return NULL;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
403
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
404 rv->k = k;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
405 rv->maxwds = x;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
406 rv->sign = rv->wds = 0;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
407 return rv;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
408 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
409
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
410 /* Free a Bigint allocated with Balloc */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
411
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
412 static void
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
413 Bfree(Bigint *v)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
414 {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
415 if (v) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
416 FREE((void*)v);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
417 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
418 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
419
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
420 #endif /* Py_USING_MEMORY_DEBUGGER */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
421
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
422 #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
423 y->wds*sizeof(Long) + 2*sizeof(int))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
424
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
425 /* Multiply a Bigint b by m and add a. Either modifies b in place and returns
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
426 a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
427 On failure, return NULL. In this case, b will have been already freed. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
428
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
429 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
430 multadd(Bigint *b, int m, int a) /* multiply by m and add a */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
431 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
432 int i, wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
433 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
434 ULong *x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
435 ULLong carry, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
436 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
437 ULong carry, *x, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
438 ULong xi, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
439 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
440 Bigint *b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
441
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
442 wds = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
443 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
444 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
445 carry = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
446 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
447 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
448 y = *x * (ULLong)m + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
449 carry = y >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
450 *x++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
451 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
452 xi = *x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
453 y = (xi & 0xffff) * m + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
454 z = (xi >> 16) * m + (y >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
455 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
456 *x++ = (z << 16) + (y & 0xffff);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
457 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
458 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
459 while(++i < wds);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
460 if (carry) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
461 if (wds >= b->maxwds) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
462 b1 = Balloc(b->k+1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
463 if (b1 == NULL){
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
464 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
465 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
466 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
467 Bcopy(b1, b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
468 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
469 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
470 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
471 b->x[wds++] = (ULong)carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
472 b->wds = wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
473 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
474 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
475 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
476
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
477 /* convert a string s containing nd decimal digits (possibly containing a
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
478 decimal separator at position nd0, which is ignored) to a Bigint. This
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
479 function carries on where the parsing code in _Py_dg_strtod leaves off: on
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
480 entry, y9 contains the result of converting the first 9 digits. Returns
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
481 NULL on failure. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
482
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
483 static Bigint *
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
484 s2b(const char *s, int nd0, int nd, ULong y9)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
485 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
486 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
487 int i, k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
488 Long x, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
489
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
490 x = (nd + 8) / 9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
491 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
492 b = Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
493 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
494 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
495 b->x[0] = y9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
496 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
497
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
498 if (nd <= 9)
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
499 return b;
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
500
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
501 s += 9;
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
502 for (i = 9; i < nd0; i++) {
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
503 b = multadd(b, 10, *s++ - '0');
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
504 if (b == NULL)
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
505 return NULL;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
506 }
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
507 s++;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
508 for(; i < nd; i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
509 b = multadd(b, 10, *s++ - '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
510 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
511 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
512 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
513 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
514 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
515
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
516 /* count leading 0 bits in the 32-bit integer x. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
517
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
518 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
519 hi0bits(ULong x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
520 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
521 int k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
522
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
523 if (!(x & 0xffff0000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
524 k = 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
525 x <<= 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
526 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
527 if (!(x & 0xff000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
528 k += 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
529 x <<= 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
530 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
531 if (!(x & 0xf0000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
532 k += 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
533 x <<= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
534 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
535 if (!(x & 0xc0000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
536 k += 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
537 x <<= 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
538 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
539 if (!(x & 0x80000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
540 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
541 if (!(x & 0x40000000))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
542 return 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
543 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
544 return k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
545 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
546
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
547 /* count trailing 0 bits in the 32-bit integer y, and shift y right by that
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
548 number of bits. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
549
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
550 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
551 lo0bits(ULong *y)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
552 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
553 int k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
554 ULong x = *y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
555
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
556 if (x & 7) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
557 if (x & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
558 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
559 if (x & 2) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
560 *y = x >> 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
561 return 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
562 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
563 *y = x >> 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
564 return 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
565 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
566 k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
567 if (!(x & 0xffff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
568 k = 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
569 x >>= 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
570 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
571 if (!(x & 0xff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
572 k += 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
573 x >>= 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
574 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
575 if (!(x & 0xf)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
576 k += 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
577 x >>= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
578 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
579 if (!(x & 0x3)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
580 k += 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
581 x >>= 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
582 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
583 if (!(x & 1)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
584 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
585 x >>= 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
586 if (!x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
587 return 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
588 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
589 *y = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
590 return k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
591 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
592
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
593 /* convert a small nonnegative integer to a Bigint */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
594
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
595 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
596 i2b(int i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
597 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
598 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
599
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
600 b = Balloc(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
601 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
602 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
603 b->x[0] = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
604 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
605 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
606 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
607
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
608 /* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
609 the signs of a and b. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
610
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
611 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
612 mult(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
613 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
614 Bigint *c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
615 int k, wa, wb, wc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
616 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
617 ULong y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
618 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
619 ULLong carry, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
620 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
621 ULong carry, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
622 ULong z2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
623 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
624
58458
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
625 if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
626 c = Balloc(0);
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
627 if (c == NULL)
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
628 return NULL;
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
629 c->wds = 1;
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
630 c->x[0] = 0;
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
631 return c;
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
632 }
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
633
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
634 if (a->wds < b->wds) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
635 c = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
636 a = b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
637 b = c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
638 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
639 k = a->k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
640 wa = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
641 wb = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
642 wc = wa + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
643 if (wc > a->maxwds)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
644 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
645 c = Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
646 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
647 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
648 for(x = c->x, xa = x + wc; x < xa; x++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
649 *x = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
650 xa = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
651 xae = xa + wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
652 xb = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
653 xbe = xb + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
654 xc0 = c->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
655 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
656 for(; xb < xbe; xc0++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
657 if ((y = *xb++)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
658 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
659 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
660 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
661 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
662 z = *x++ * (ULLong)y + *xc + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
663 carry = z >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
664 *xc++ = (ULong)(z & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
665 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
666 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
667 *xc = (ULong)carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
668 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
669 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
670 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
671 for(; xb < xbe; xb++, xc0++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
672 if (y = *xb & 0xffff) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
673 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
674 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
675 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
676 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
677 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
678 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
679 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
680 carry = z2 >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
681 Storeinc(xc, z2, z);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
682 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
683 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
684 *xc = carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
685 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
686 if (y = *xb >> 16) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
687 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
688 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
689 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
690 z2 = *xc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
691 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
692 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
693 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
694 Storeinc(xc, z, z2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
695 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
696 carry = z2 >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
697 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
698 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
699 *xc = z2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
700 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
701 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
702 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
703 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
704 c->wds = wc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
705 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
706 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
707
58401
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
708 #ifndef Py_USING_MEMORY_DEBUGGER
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
709
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
710 /* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
711
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
712 static Bigint *p5s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
713
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
714 /* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
715 failure; if the returned pointer is distinct from b then the original
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
716 Bigint b will have been Bfree'd. Ignores the sign of b. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
717
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
718 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
719 pow5mult(Bigint *b, int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
720 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
721 Bigint *b1, *p5, *p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
722 int i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
723 static int p05[3] = { 5, 25, 125 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
724
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
725 if ((i = k & 3)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
726 b = multadd(b, p05[i-1], 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
727 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
728 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
729 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
730
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
731 if (!(k >>= 2))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
732 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
733 p5 = p5s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
734 if (!p5) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
735 /* first time */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
736 p5 = i2b(625);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
737 if (p5 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
738 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
739 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
740 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
741 p5s = p5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
742 p5->next = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
743 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
744 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
745 if (k & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
746 b1 = mult(b, p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
747 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
748 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
749 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
750 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
751 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
752 if (!(k >>= 1))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
753 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
754 p51 = p5->next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
755 if (!p51) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
756 p51 = mult(p5,p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
757 if (p51 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
758 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
759 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
760 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
761 p51->next = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
762 p5->next = p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
763 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
764 p5 = p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
765 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
766 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
767 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
768
58401
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
769 #else
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
770
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
771 /* Version of pow5mult that doesn't cache powers of 5. Provided for
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
772 the benefit of memory debugging tools like Valgrind. */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
773
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
774 static Bigint *
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
775 pow5mult(Bigint *b, int k)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
776 {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
777 Bigint *b1, *p5, *p51;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
778 int i;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
779 static int p05[3] = { 5, 25, 125 };
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
780
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
781 if ((i = k & 3)) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
782 b = multadd(b, p05[i-1], 0);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
783 if (b == NULL)
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
784 return NULL;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
785 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
786
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
787 if (!(k >>= 2))
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
788 return b;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
789 p5 = i2b(625);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
790 if (p5 == NULL) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
791 Bfree(b);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
792 return NULL;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
793 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
794
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
795 for(;;) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
796 if (k & 1) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
797 b1 = mult(b, p5);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
798 Bfree(b);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
799 b = b1;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
800 if (b == NULL) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
801 Bfree(p5);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
802 return NULL;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
803 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
804 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
805 if (!(k >>= 1))
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
806 break;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
807 p51 = mult(p5, p5);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
808 Bfree(p5);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
809 p5 = p51;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
810 if (p5 == NULL) {
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
811 Bfree(b);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
812 return NULL;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
813 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
814 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
815 Bfree(p5);
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
816 return b;
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
817 }
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
818
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
819 #endif /* Py_USING_MEMORY_DEBUGGER */
5c9c53a721a5 Issue #7632: When Py_USING_MEMORY_DEBUGGER is defined, disable the
Mark Dickinson <dickinsm@gmail.com>
parents: 58390
diff changeset
820
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
821 /* shift a Bigint b left by k bits. Return a pointer to the shifted result,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
822 or NULL on failure. If the returned pointer is distinct from b then the
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
823 original b will have been Bfree'd. Ignores the sign of b. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
824
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
825 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
826 lshift(Bigint *b, int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
827 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
828 int i, k1, n, n1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
829 Bigint *b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
830 ULong *x, *x1, *xe, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
831
58458
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
832 if (!k || (!b->x[0] && b->wds == 1))
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
833 return b;
d77c947ff7db Issue #7743: Add checks for zero inputs to the lshift and mult functions;
Mark Dickinson <dickinsm@gmail.com>
parents: 58444
diff changeset
834
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
835 n = k >> 5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
836 k1 = b->k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
837 n1 = n + b->wds + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
838 for(i = b->maxwds; n1 > i; i <<= 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
839 k1++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
840 b1 = Balloc(k1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
841 if (b1 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
842 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
843 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
844 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
845 x1 = b1->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
846 for(i = 0; i < n; i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
847 *x1++ = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
848 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
849 xe = x + b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
850 if (k &= 0x1f) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
851 k1 = 32 - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
852 z = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
853 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
854 *x1++ = *x << k | z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
855 z = *x++ >> k1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
856 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
857 while(x < xe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
858 if ((*x1 = z))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
859 ++n1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
860 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
861 else do
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
862 *x1++ = *x++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
863 while(x < xe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
864 b1->wds = n1 - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
865 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
866 return b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
867 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
868
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
869 /* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
870 1 if a > b. Ignores signs of a and b. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
871
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
872 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
873 cmp(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
874 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
875 ULong *xa, *xa0, *xb, *xb0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
876 int i, j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
877
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
878 i = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
879 j = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
880 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
881 if (i > 1 && !a->x[i-1])
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
882 Bug("cmp called with a->x[a->wds-1] == 0");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
883 if (j > 1 && !b->x[j-1])
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
884 Bug("cmp called with b->x[b->wds-1] == 0");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
885 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
886 if (i -= j)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
887 return i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
888 xa0 = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
889 xa = xa0 + j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
890 xb0 = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
891 xb = xb0 + j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
892 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
893 if (*--xa != *--xb)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
894 return *xa < *xb ? -1 : 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
895 if (xa <= xa0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
896 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
897 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
898 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
899 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
900
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
901 /* Take the difference of Bigints a and b, returning a new Bigint. Returns
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
902 NULL on failure. The signs of a and b are ignored, but the sign of the
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
903 result is set appropriately. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
904
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
905 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
906 diff(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
907 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
908 Bigint *c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
909 int i, wa, wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
910 ULong *xa, *xae, *xb, *xbe, *xc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
911 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
912 ULLong borrow, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
913 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
914 ULong borrow, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
915 ULong z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
916 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
917
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
918 i = cmp(a,b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
919 if (!i) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
920 c = Balloc(0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
921 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
922 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
923 c->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
924 c->x[0] = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
925 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
926 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
927 if (i < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
928 c = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
929 a = b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
930 b = c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
931 i = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
932 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
933 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
934 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
935 c = Balloc(a->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
936 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
937 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
938 c->sign = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
939 wa = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
940 xa = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
941 xae = xa + wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
942 wb = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
943 xb = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
944 xbe = xb + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
945 xc = c->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
946 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
947 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
948 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
949 y = (ULLong)*xa++ - *xb++ - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
950 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
951 *xc++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
952 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
953 while(xb < xbe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
954 while(xa < xae) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
955 y = *xa++ - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
956 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
957 *xc++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
958 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
959 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
960 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
961 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
962 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
963 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
964 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
965 Storeinc(xc, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
966 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
967 while(xb < xbe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
968 while(xa < xae) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
969 y = (*xa & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
970 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
971 z = (*xa++ >> 16) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
972 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
973 Storeinc(xc, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
974 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
975 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
976 while(!*--xc)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
977 wa--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
978 c->wds = wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
979 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
980 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
981
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
982 /* Given a positive normal double x, return the difference between x and the
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
983 next double up. Doesn't give correct results for subnormals. */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
984
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
985 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
986 ulp(U *x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
987 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
988 Long L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
989 U u;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
990
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
991 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
992 word0(&u) = L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
993 word1(&u) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
994 return dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
995 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
996
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
997 /* Convert a Bigint to a double plus an exponent */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
998
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
999 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1000 b2d(Bigint *a, int *e)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1001 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1002 ULong *xa, *xa0, w, y, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1003 int k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1004 U d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1005
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1006 xa0 = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1007 xa = xa0 + a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1008 y = *--xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1009 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1010 if (!y) Bug("zero y in b2d");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1011 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1012 k = hi0bits(y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1013 *e = 32 - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1014 if (k < Ebits) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1015 word0(&d) = Exp_1 | y >> (Ebits - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1016 w = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1017 word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1018 goto ret_d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1019 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1020 z = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1021 if (k -= Ebits) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1022 word0(&d) = Exp_1 | y << k | z >> (32 - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1023 y = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1024 word1(&d) = z << k | y >> (32 - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1025 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1026 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1027 word0(&d) = Exp_1 | y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1028 word1(&d) = z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1029 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1030 ret_d:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1031 return dval(&d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1032 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1033
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1034 /* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1035 except that it accepts the scale parameter used in _Py_dg_strtod (which
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1036 should be either 0 or 2*P), and the normalization for the return value is
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1037 different (see below). On input, d should be finite and nonnegative, and d
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1038 / 2**scale should be exactly representable as an IEEE 754 double.
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1039
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1040 Returns a Bigint b and an integer e such that
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1041
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1042 dval(d) / 2**scale = b * 2**e.
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1043
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1044 Unlike d2b, b is not necessarily odd: b and e are normalized so
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1045 that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1046 and e == Etiny. This applies equally to an input of 0.0: in that
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1047 case the return values are b = 0 and e = Etiny.
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1048
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1049 The above normalization ensures that for all possible inputs d,
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1050 2**e gives ulp(d/2**scale).
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1051
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1052 Returns NULL on failure.
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1053 */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1054
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1055 static Bigint *
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1056 sd2b(U *d, int scale, int *e)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1057 {
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1058 Bigint *b;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1059
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1060 b = Balloc(1);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1061 if (b == NULL)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1062 return NULL;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1063
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1064 /* First construct b and e assuming that scale == 0. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1065 b->wds = 2;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1066 b->x[0] = word1(d);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1067 b->x[1] = word0(d) & Frac_mask;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1068 *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1069 if (*e < Etiny)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1070 *e = Etiny;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1071 else
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1072 b->x[1] |= Exp_msk1;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1073
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1074 /* Now adjust for scale, provided that b != 0. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1075 if (scale && (b->x[0] || b->x[1])) {
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1076 *e -= scale;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1077 if (*e < Etiny) {
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1078 scale = Etiny - *e;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1079 *e = Etiny;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1080 /* We can't shift more than P-1 bits without shifting out a 1. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1081 assert(0 < scale && scale <= P - 1);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1082 if (scale >= 32) {
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1083 /* The bits shifted out should all be zero. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1084 assert(b->x[0] == 0);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1085 b->x[0] = b->x[1];
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1086 b->x[1] = 0;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1087 scale -= 32;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1088 }
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1089 if (scale) {
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1090 /* The bits shifted out should all be zero. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1091 assert(b->x[0] << (32 - scale) == 0);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1092 b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1093 b->x[1] >>= scale;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1094 }
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1095 }
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1096 }
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1097 /* Ensure b is normalized. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1098 if (!b->x[1])
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1099 b->wds = 1;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1100
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1101 return b;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1102 }
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1103
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1104 /* Convert a double to a Bigint plus an exponent. Return NULL on failure.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1105
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1106 Given a finite nonzero double d, return an odd Bigint b and exponent *e
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1107 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
58151
8ca377853321 Fix typo in comment.
Mark Dickinson <dickinsm@gmail.com>
parents: 56709
diff changeset
1108 significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1109
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1110 If d is zero, then b == 0, *e == -1010, *bbits = 0.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1111 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1112
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1113 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1114 d2b(U *d, int *e, int *bits)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1115 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1116 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1117 int de, k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1118 ULong *x, y, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1119 int i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1120
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1121 b = Balloc(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1122 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1123 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1124 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1125
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1126 z = word0(d) & Frac_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1127 word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1128 if ((de = (int)(word0(d) >> Exp_shift)))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1129 z |= Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1130 if ((y = word1(d))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1131 if ((k = lo0bits(&y))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1132 x[0] = y | z << (32 - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1133 z >>= k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1134 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1135 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1136 x[0] = y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1137 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1138 b->wds = (x[1] = z) ? 2 : 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1139 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1140 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1141 k = lo0bits(&z);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1142 x[0] = z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1143 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1144 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1145 k += 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1146 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1147 if (de) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1148 *e = de - Bias - (P-1) + k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1149 *bits = P - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1150 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1151 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1152 *e = de - Bias - (P-1) + 1 + k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1153 *bits = 32*i - hi0bits(x[i-1]);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1154 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1155 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1156 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1157
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1158 /* Compute the ratio of two Bigints, as a double. The result may have an
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1159 error of up to 2.5 ulps. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1160
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1161 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1162 ratio(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1163 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1164 U da, db;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1165 int k, ka, kb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1166
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1167 dval(&da) = b2d(a, &ka);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1168 dval(&db) = b2d(b, &kb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1169 k = ka - kb + 32*(a->wds - b->wds);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1170 if (k > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1171 word0(&da) += k*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1172 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1173 k = -k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1174 word0(&db) += k*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1175 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1176 return dval(&da) / dval(&db);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1177 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1178
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1179 static const double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1180 tens[] = {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1181 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1182 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1183 1e20, 1e21, 1e22
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1184 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1185
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1186 static const double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1187 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1188 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1189 9007199254740992.*9007199254740992.e-256
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1190 /* = 2^106 * 1e-256 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1191 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1192 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1193 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1194 #define Scale_Bit 0x10
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1195 #define n_bigtens 5
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1196
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1197 #define ULbits 32
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1198 #define kshift 5
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1199 #define kmask 31
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1200
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1201
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1202 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1203 dshift(Bigint *b, int p2)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1204 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1205 int rv = hi0bits(b->x[b->wds-1]) - 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1206 if (p2 > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1207 rv -= p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1208 return rv & kmask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1209 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1210
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1211 /* special case of Bigint division. The quotient is always in the range 0 <=
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1212 quotient < 10, and on entry the divisor S is normalized so that its top 4
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1213 bits (28--31) are zero and bit 27 is set. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1214
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1215 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1216 quorem(Bigint *b, Bigint *S)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1217 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1218 int n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1219 ULong *bx, *bxe, q, *sx, *sxe;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1220 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1221 ULLong borrow, carry, y, ys;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1222 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1223 ULong borrow, carry, y, ys;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1224 ULong si, z, zs;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1225 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1226
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1227 n = S->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1228 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1229 /*debug*/ if (b->wds > n)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1230 /*debug*/ Bug("oversize b in quorem");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1231 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1232 if (b->wds < n)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1233 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1234 sx = S->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1235 sxe = sx + --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1236 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1237 bxe = bx + n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1238 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1239 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1240 /*debug*/ if (q > 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1241 /*debug*/ Bug("oversized quotient in quorem");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1242 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1243 if (q) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1244 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1245 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1246 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1247 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1248 ys = *sx++ * (ULLong)q + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1249 carry = ys >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1250 y = *bx - (ys & FFFFFFFF) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1251 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1252 *bx++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1253 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1254 si = *sx++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1255 ys = (si & 0xffff) * q + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1256 zs = (si >> 16) * q + (ys >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1257 carry = zs >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1258 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1259 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1260 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1261 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1262 Storeinc(bx, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1263 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1264 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1265 while(sx <= sxe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1266 if (!*bxe) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1267 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1268 while(--bxe > bx && !*bxe)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1269 --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1270 b->wds = n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1271 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1272 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1273 if (cmp(b, S) >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1274 q++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1275 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1276 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1277 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1278 sx = S->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1279 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1280 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1281 ys = *sx++ + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1282 carry = ys >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1283 y = *bx - (ys & FFFFFFFF) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1284 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1285 *bx++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1286 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1287 si = *sx++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1288 ys = (si & 0xffff) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1289 zs = (si >> 16) + (ys >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1290 carry = zs >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1291 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1292 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1293 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1294 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1295 Storeinc(bx, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1296 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1297 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1298 while(sx <= sxe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1299 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1300 bxe = bx + n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1301 if (!*bxe) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1302 while(--bxe > bx && !*bxe)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1303 --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1304 b->wds = n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1305 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1306 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1307 return q;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1308 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1309
58305
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1310 /* sulp(x) is a version of ulp(x) that takes bc.scale into account.
58278
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1311
58305
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1312 Assuming that x is finite and nonnegative (positive zero is fine
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1313 here) and x / 2^bc.scale is exactly representable as a double,
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1314 sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
58278
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1315
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1316 static double
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1317 sulp(U *x, BCinfo *bc)
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1318 {
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1319 U u;
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1320
58309
c7f33d5216f9 Fix buggy comparison: LHS of comparison was being treated as unsigned.
Mark Dickinson <dickinsm@gmail.com>
parents: 58308
diff changeset
1321 if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
58278
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1322 /* rv/2^bc->scale is subnormal */
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1323 word0(&u) = (P+2)*Exp_msk1;
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1324 word1(&u) = 0;
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1325 return u.d;
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1326 }
58305
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1327 else {
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1328 assert(word0(x) || word1(x)); /* x != 0.0 */
58278
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1329 return ulp(x);
58305
c5a0a5d6acb0 Clarify that sulp expects a nonnegative input, but that +0.0 is fine.
Mark Dickinson <dickinsm@gmail.com>
parents: 58304
diff changeset
1330 }
58278
b4f301cea115 Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
Mark Dickinson <dickinsm@gmail.com>
parents: 58277
diff changeset
1331 }
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1332
58304
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1333 /* The bigcomp function handles some hard cases for strtod, for inputs
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1334 with more than STRTOD_DIGLIM digits. It's called once an initial
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1335 estimate for the double corresponding to the input string has
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1336 already been obtained by the code in _Py_dg_strtod.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1337
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1338 The bigcomp function is only called after _Py_dg_strtod has found a
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1339 double value rv such that either rv or rv + 1ulp represents the
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1340 correctly rounded value corresponding to the original string. It
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1341 determines which of these two values is the correct one by
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1342 computing the decimal digits of rv + 0.5ulp and comparing them with
58308
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1343 the corresponding digits of s0.
58304
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1344
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1345 In the following, write dv for the absolute value of the number represented
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1346 by the input string.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1347
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1348 Inputs:
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1349
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1350 s0 points to the first significant digit of the input string.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1351
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1352 rv is a (possibly scaled) estimate for the closest double value to the
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1353 value represented by the original input to _Py_dg_strtod. If
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1354 bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1355 the input value.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1356
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1357 bc is a struct containing information gathered during the parsing and
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1358 estimation steps of _Py_dg_strtod. Description of fields follows:
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1359
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1360 bc->e0 gives the exponent of the input value, such that dv = (integer
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1361 given by the bd->nd digits of s0) * 10**e0
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1362
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
1363 bc->nd gives the total number of significant digits of s0. It will
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
1364 be at least 1.
58304
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1365
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1366 bc->nd0 gives the number of significant digits of s0 before the
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1367 decimal separator. If there's no decimal separator, bc->nd0 ==
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1368 bc->nd.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1369
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1370 bc->scale is the value used to scale rv to avoid doing arithmetic with
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1371 subnormal values. It's either 0 or 2*P (=106).
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1372
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1373 Outputs:
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1374
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1375 On successful exit, rv/2^(bc->scale) is the closest double to dv.
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1376
57102283ab5e Add comments explaining the role of the bigcomp function in dtoa.c.
Mark Dickinson <dickinsm@gmail.com>
parents: 58278
diff changeset
1377 Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1378
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1379 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1380 bigcomp(U *rv, const char *s0, BCinfo *bc)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1381 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1382 Bigint *b, *d;
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1383 int b2, d2, dd, i, nd, nd0, odd, p2, p5;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1384
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1385 nd = bc->nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1386 nd0 = bc->nd0;
58277
2a9f66df4dc4 Issue #7632: Fix a problem with _Py_dg_strtod that could lead to
Mark Dickinson <dickinsm@gmail.com>
parents: 58249
diff changeset
1387 p5 = nd + bc->e0;
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1388 b = sd2b(rv, bc->scale, &p2);
58308
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1389 if (b == NULL)
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1390 return -1;
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1391
58317
76fb7dbd7388 Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow.
Mark Dickinson <dickinsm@gmail.com>
parents: 58316
diff changeset
1392 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
76fb7dbd7388 Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow.
Mark Dickinson <dickinsm@gmail.com>
parents: 58316
diff changeset
1393 case, this is used for round to even. */
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1394 odd = b->x[0] & 1;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1395
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1396 /* left shift b by 1 bit and or a 1 into the least significant bit;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1397 this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1398 b = lshift(b, 1);
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1399 if (b == NULL)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1400 return -1;
58308
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1401 b->x[0] |= 1;
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1402 p2--;
58308
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1403
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1404 p2 -= p5;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1405 d = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1406 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1407 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1408 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1409 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1410 /* Arrange for convenient computation of quotients:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1411 * shift left if necessary so divisor has 4 leading 0 bits.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1412 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1413 if (p5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1414 d = pow5mult(d, p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1415 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1416 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1417 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1418 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1419 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1420 else if (p5 < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1421 b = pow5mult(b, -p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1422 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1423 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1424 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1425 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1426 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1427 if (p2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1428 b2 = p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1429 d2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1430 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1431 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1432 b2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1433 d2 = -p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1434 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1435 i = dshift(d, d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1436 if ((b2 += i) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1437 b = lshift(b, b2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1438 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1439 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1440 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1441 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1442 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1443 if ((d2 += i) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1444 d = lshift(d, d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1445 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1446 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1447 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1448 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1449 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1450
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1451 /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1452 * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1453 * a number in the range [0.1, 1). */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1454 if (cmp(b, d) >= 0)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1455 /* b/d >= 1 */
58277
2a9f66df4dc4 Issue #7632: Fix a problem with _Py_dg_strtod that could lead to
Mark Dickinson <dickinsm@gmail.com>
parents: 58249
diff changeset
1456 dd = -1;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1457 else {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1458 i = 0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1459 for(;;) {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1460 b = multadd(b, 10, 0);
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1461 if (b == NULL) {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1462 Bfree(d);
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1463 return -1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1464 }
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1465 dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1466 i++;
58317
76fb7dbd7388 Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow.
Mark Dickinson <dickinsm@gmail.com>
parents: 58316
diff changeset
1467
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1468 if (dd)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1469 break;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1470 if (!b->x[0] && b->wds == 1) {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1471 /* b/d == 0 */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1472 dd = i < nd;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1473 break;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1474 }
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1475 if (!(i < nd)) {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1476 /* b/d != 0, but digits of s0 exhausted */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1477 dd = -1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1478 break;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1479 }
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1480 }
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
1481 }
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1482 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1483 Bfree(d);
58317
76fb7dbd7388 Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow.
Mark Dickinson <dickinsm@gmail.com>
parents: 58316
diff changeset
1484 if (dd > 0 || (dd == 0 && odd))
58308
0f974bb238ed Simplify and annotate the bigcomp function, removing unused special cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58305
diff changeset
1485 dval(rv) += sulp(rv, bc);
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1486 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1487 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1488
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1489 double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1490 _Py_dg_strtod(const char *s00, char **se)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1491 {
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1492 int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1493 int esign, i, j, k, lz, nd, nd0, odd, sign;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1494 const char *s, *s0, *s1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1495 double aadj, aadj1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1496 U aadj2, adj, rv, rv0;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1497 ULong y, z, abs_exp;
58358
5c32f3b65d21 Fix multiple uses of variable 'L' in _Py_dg_strtod, where one use requires an unsigned long and the other a signed long. See also r77421.
Mark Dickinson <dickinsm@gmail.com>
parents: 58356
diff changeset
1498 Long L;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1499 BCinfo bc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1500 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1501
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1502 dval(&rv) = 0.;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1503
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1504 /* Start parsing. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1505 c = *(s = s00);
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1506
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1507 /* Parse optional sign, if present. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1508 sign = 0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1509 switch (c) {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1510 case '-':
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1511 sign = 1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1512 /* no break */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1513 case '+':
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1514 c = *++s;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1515 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1516
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1517 /* Skip leading zeros: lz is true iff there were leading zeros. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1518 s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1519 while (c == '0')
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1520 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1521 lz = s != s1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1522
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1523 /* Point s0 at the first nonzero digit (if any). nd0 will be the position
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1524 of the point relative to s0. nd will be the total number of digits
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1525 ignoring leading zeros. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1526 s0 = s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1527 while ('0' <= c && c <= '9')
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1528 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1529 nd0 = nd = s - s1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1530
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1531 /* Parse decimal point and following digits. */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1532 if (c == '.') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1533 c = *++s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1534 if (!nd) {
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1535 s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1536 while (c == '0')
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1537 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1538 lz = lz || s != s1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1539 nd0 -= s - s1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1540 s0 = s;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1541 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1542 s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1543 while ('0' <= c && c <= '9')
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1544 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1545 nd += s - s1;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1546 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1547
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1548 /* Now lz is true if and only if there were leading zero digits, and nd
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1549 gives the total number of digits ignoring leading zeros. A valid input
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1550 must have at least one digit. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1551 if (!nd && !lz) {
58420
5dbbc43c591b Don't try to put a value into a NULL pointer.
Mark Dickinson <dickinsm@gmail.com>
parents: 58419
diff changeset
1552 if (se)
5dbbc43c591b Don't try to put a value into a NULL pointer.
Mark Dickinson <dickinsm@gmail.com>
parents: 58419
diff changeset
1553 *se = (char *)s00;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1554 goto parse_error;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1555 }
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1556
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1557 /* Parse exponent. */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1558 e = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1559 if (c == 'e' || c == 'E') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1560 s00 = s;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1561 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1562
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1563 /* Exponent sign. */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1564 esign = 0;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1565 switch (c) {
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1566 case '-':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1567 esign = 1;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1568 /* no break */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1569 case '+':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1570 c = *++s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1571 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1572
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1573 /* Skip zeros. lz is true iff there are leading zeros. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1574 s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1575 while (c == '0')
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1576 c = *++s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1577 lz = s != s1;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1578
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1579 /* Get absolute value of the exponent. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1580 s1 = s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1581 abs_exp = 0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1582 while ('0' <= c && c <= '9') {
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1583 abs_exp = 10*abs_exp + (c - '0');
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1584 c = *++s;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1585 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1586
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1587 /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1588 there are at most 9 significant exponent digits then overflow is
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1589 impossible. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1590 if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1591 e = (int)MAX_ABS_EXP;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1592 else
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1593 e = (int)abs_exp;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1594 if (esign)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1595 e = -e;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1596
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1597 /* A valid exponent must have at least one digit. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1598 if (s == s1 && !lz)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1599 s = s00;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1600 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1601
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1602 /* Adjust exponent to take into account position of the point. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1603 e -= nd - nd0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1604 if (nd0 <= 0)
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1605 nd0 = nd;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1606
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1607 /* Finished parsing. Set se to indicate how far we parsed */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1608 if (se)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1609 *se = (char *)s;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1610
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1611 /* If all digits were zero, exit with return value +-0.0. Otherwise,
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1612 strip trailing zeros: scan back until we hit a nonzero digit. */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1613 if (!nd)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1614 goto ret;
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1615 for (i = nd; i > 0; ) {
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1616 --i;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1617 if (s0[i < nd0 ? i : i+1] != '0') {
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1618 ++i;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1619 break;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1620 }
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1621 }
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1622 e += nd - i;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1623 nd = i;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1624 if (nd0 > nd)
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1625 nd0 = nd;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1626
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1627 /* Summary of parsing results. After parsing, and dealing with zero
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1628 * inputs, we have values s0, nd0, nd, e, sign, where:
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1629 *
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1630 * - s0 points to the first significant digit of the input string
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1631 *
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1632 * - nd is the total number of significant digits (here, and
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1633 * below, 'significant digits' means the set of digits of the
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1634 * significand of the input that remain after ignoring leading
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1635 * and trailing zeros).
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1636 *
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1637 * - nd0 indicates the position of the decimal point, if present; it
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1638 * satisfies 1 <= nd0 <= nd. The nd significant digits are in
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1639 * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1640 * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1641 * nd0 == nd, then s0[nd0] could be any non-digit character.)
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1642 *
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1643 * - e is the adjusted exponent: the absolute value of the number
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1644 * represented by the original input string is n * 10**e, where
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1645 * n is the integer represented by the concatenation of
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1646 * s0[0:nd0] and s0[nd0+1:nd+1]
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1647 *
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1648 * - sign gives the sign of the input: 1 for negative, 0 for positive
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1649 *
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1650 * - the first and last significant digits are nonzero
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1651 */
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1652
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1653 /* put first DBL_DIG+1 digits into integer y and z.
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1654 *
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1655 * - y contains the value represented by the first min(9, nd)
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1656 * significant digits
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1657 *
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1658 * - if nd > 9, z contains the value represented by significant digits
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1659 * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1660 * gives the value represented by the first min(16, nd) sig. digits.
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1661 */
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1662
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1663 bc.e0 = e1 = e;
58356
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1664 y = z = 0;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1665 for (i = 0; i < nd; i++) {
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1666 if (i < 9)
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1667 y = 10*y + s0[i < nd0 ? i : i+1] - '0';
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1668 else if (i < DBL_DIG+1)
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1669 z = 10*z + s0[i < nd0 ? i : i+1] - '0';
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1670 else
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1671 break;
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1672 }
8e179a0fcb5e Issue #7632: Fix one more case of incorrect rounding for str -> float
Mark Dickinson <dickinsm@gmail.com>
parents: 58345
diff changeset
1673
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1674 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1675 dval(&rv) = y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1676 if (k > 9) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1677 dval(&rv) = tens[k - 9] * dval(&rv) + z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1678 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1679 bd0 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1680 if (nd <= DBL_DIG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1681 && Flt_Rounds == 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1682 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1683 if (!e)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1684 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1685 if (e > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1686 if (e <= Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1687 dval(&rv) *= tens[e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1688 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1689 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1690 i = DBL_DIG - nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1691 if (e <= Ten_pmax + i) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1692 /* A fancier test would sometimes let us do
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1693 * this for larger i values.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1694 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1695 e -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1696 dval(&rv) *= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1697 dval(&rv) *= tens[e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1698 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1699 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1700 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1701 else if (e >= -Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1702 dval(&rv) /= tens[-e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1703 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1704 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1705 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1706 e1 += nd - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1707
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1708 bc.scale = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1709
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1710 /* Get starting approximation = rv * 10**e1 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1711
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1712 if (e1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1713 if ((i = e1 & 15))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1714 dval(&rv) *= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1715 if (e1 &= ~15) {
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1716 if (e1 > DBL_MAX_10_EXP)
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1717 goto ovfl;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1718 e1 >>= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1719 for(j = 0; e1 > 1; j++, e1 >>= 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1720 if (e1 & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1721 dval(&rv) *= bigtens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1722 /* The last multiplication could overflow. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1723 word0(&rv) -= P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1724 dval(&rv) *= bigtens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1725 if ((z = word0(&rv) & Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1726 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1727 goto ovfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1728 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1729 /* set to largest number */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1730 /* (Can't trust DBL_MAX) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1731 word0(&rv) = Big0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1732 word1(&rv) = Big1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1733 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1734 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1735 word0(&rv) += P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1736 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1737 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1738 else if (e1 < 0) {
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1739 /* The input decimal value lies in [10**e1, 10**(e1+16)).
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1740
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1741 If e1 <= -512, underflow immediately.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1742 If e1 <= -256, set bc.scale to 2*P.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1743
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1744 So for input value < 1e-256, bc.scale is always set;
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1745 for input value >= 1e-240, bc.scale is never set.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1746 For input values in [1e-256, 1e-240), bc.scale may or may
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1747 not be set. */
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1748
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1749 e1 = -e1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1750 if ((i = e1 & 15))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1751 dval(&rv) /= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1752 if (e1 >>= 4) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1753 if (e1 >= 1 << n_bigtens)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1754 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1755 if (e1 & Scale_Bit)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1756 bc.scale = 2*P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1757 for(j = 0; e1 > 0; j++, e1 >>= 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1758 if (e1 & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1759 dval(&rv) *= tinytens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1760 if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1761 >> Exp_shift)) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1762 /* scaled rv is denormal; clear j low bits */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1763 if (j >= 32) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1764 word1(&rv) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1765 if (j >= 53)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1766 word0(&rv) = (P+2)*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1767 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1768 word0(&rv) &= 0xffffffff << (j-32);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1769 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1770 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1771 word1(&rv) &= 0xffffffff << j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1772 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1773 if (!dval(&rv))
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1774 goto undfl;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1775 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1776 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1777
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1778 /* Now the hard part -- adjusting rv to the correct value.*/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1779
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1780 /* Put digits into bd: true value = bd * 10^e */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1781
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1782 bc.nd = nd;
58239
3e1712221fa6 Remove unused BCinfo fields and an unused macro.
Mark Dickinson <dickinsm@gmail.com>
parents: 58151
diff changeset
1783 bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1784 /* to silence an erroneous warning about bc.nd0 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1785 /* possibly not being initialized. */
58239
3e1712221fa6 Remove unused BCinfo fields and an unused macro.
Mark Dickinson <dickinsm@gmail.com>
parents: 58151
diff changeset
1786 if (nd > STRTOD_DIGLIM) {
3e1712221fa6 Remove unused BCinfo fields and an unused macro.
Mark Dickinson <dickinsm@gmail.com>
parents: 58151
diff changeset
1787 /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1788 /* minimum number of decimal digits to distinguish double values */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1789 /* in IEEE arithmetic. */
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1790
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1791 /* Truncate input to 18 significant digits, then discard any trailing
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1792 zeros on the result by updating nd, nd0, e and y suitably. (There's
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1793 no need to update z; it's not reused beyond this point.) */
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1794 for (i = 18; i > 0; ) {
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1795 /* scan back until we hit a nonzero digit. significant digit 'i'
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1796 is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1797 --i;
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1798 if (s0[i < nd0 ? i : i+1] != '0') {
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1799 ++i;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1800 break;
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1801 }
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1802 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1803 e += nd - i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1804 nd = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1805 if (nd0 > nd)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1806 nd0 = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1807 if (nd < 9) { /* must recompute y */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1808 y = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1809 for(i = 0; i < nd0; ++i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1810 y = 10*y + s0[i] - '0';
58345
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1811 for(; i < nd; ++i)
5673dc3b6432 Issue #7632: Fix a serious wrong output bug for string -> float conversion.
Mark Dickinson <dickinsm@gmail.com>
parents: 58318
diff changeset
1812 y = 10*y + s0[i+1] - '0';
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1813 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1814 }
58310
b39a0219f727 More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1.
Mark Dickinson <dickinsm@gmail.com>
parents: 58309
diff changeset
1815 bd0 = s2b(s0, nd0, nd, y);
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1816 if (bd0 == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1817 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1818
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1819 /* Notation for the comments below. Write:
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1820
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1821 - dv for the absolute value of the number represented by the original
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1822 decimal input string.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1823
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1824 - if we've truncated dv, write tdv for the truncated value.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1825 Otherwise, set tdv == dv.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1826
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1827 - srv for the quantity rv/2^bc.scale; so srv is the current binary
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1828 approximation to tdv (and dv). It should be exactly representable
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1829 in an IEEE 754 double.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1830 */
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1831
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1832 for(;;) {
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1833
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1834 /* This is the main correction loop for _Py_dg_strtod.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1835
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1836 We've got a decimal value tdv, and a floating-point approximation
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1837 srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1838 close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1839 approximation if not.
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1840
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1841 To determine whether srv is close enough to tdv, compute integers
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1842 bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1843 respectively, and then use integer arithmetic to determine whether
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1844 |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1845 */
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1846
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1847 bd = Balloc(bd0->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1848 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1849 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1850 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1851 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1852 Bcopy(bd, bd0);
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1853 bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1854 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1855 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1856 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1857 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1858 }
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1859 /* Record whether lsb of bb is odd, in case we need this
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1860 for the round-to-even step later. */
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1861 odd = bb->x[0] & 1;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1862
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1863 /* tdv = bd * 10**e; srv = bb * 2**bbe */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1864 bs = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1865 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1866 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1867 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1868 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1869 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1870 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1871
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1872 if (e >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1873 bb2 = bb5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1874 bd2 = bd5 = e;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1875 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1876 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1877 bb2 = bb5 = -e;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1878 bd2 = bd5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1879 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1880 if (bbe >= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1881 bb2 += bbe;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1882 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1883 bd2 -= bbe;
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1884 bs2 = bb2;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1885 bb2++;
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1886 bd2++;
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1887
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1888 /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1889 and bs == 1, so:
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1890
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1891 tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1892 srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1893 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1894
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1895 It follows that:
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1896
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1897 M * tdv = bd * 2**bd2 * 5**bd5
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1898 M * srv = bb * 2**bb2 * 5**bb5
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1899 M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1900
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1901 for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1902 this fact is not needed below.)
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1903 */
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1904
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
1905 /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1906 i = bb2 < bd2 ? bb2 : bd2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1907 if (i > bs2)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1908 i = bs2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1909 if (i > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1910 bb2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1911 bd2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1912 bs2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1913 }
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1914
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1915 /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1916 if (bb5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1917 bs = pow5mult(bs, bb5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1918 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1919 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1920 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1921 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1922 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1923 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1924 bb1 = mult(bs, bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1925 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1926 bb = bb1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1927 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1928 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1929 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1930 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1931 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1932 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1933 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1934 if (bb2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1935 bb = lshift(bb, bb2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1936 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1937 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1938 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1939 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1940 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1941 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1942 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1943 if (bd5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1944 bd = pow5mult(bd, bd5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1945 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1946 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1947 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1948 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1949 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1950 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1951 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1952 if (bd2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1953 bd = lshift(bd, bd2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1954 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1955 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1956 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1957 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1958 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1959 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1960 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1961 if (bs2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1962 bs = lshift(bs, bs2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1963 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1964 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1965 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1966 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1967 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1968 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1969 }
58421
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1970
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1971 /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1972 respectively. Compute the difference |tdv - srv|, and compare
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1973 with 0.5 ulp(srv). */
886320f1ba82 Additional explanatory comments for _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58420
diff changeset
1974
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1975 delta = diff(bb, bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1976 if (delta == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1977 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1978 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1979 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1980 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1981 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1982 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1983 dsign = delta->sign;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1984 delta->sign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1985 i = cmp(delta, bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1986 if (bc.nd > nd && i <= 0) {
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
1987 if (dsign)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1988 break; /* Must use bigcomp(). */
58318
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1989
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1990 /* Here rv overestimates the truncated decimal value by at most
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1991 0.5 ulp(rv). Hence rv either overestimates the true decimal
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1992 value by <= 0.5 ulp(rv), or underestimates it by some small
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1993 amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1994 the true decimal value, so it's possible to exit.
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1995
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1996 Exception: if scaled rv is a normal exact power of 2, but not
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1997 DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1998 next double, so the correctly rounded result is either rv - 0.5
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
1999 ulp(rv) or rv; in this case, use bigcomp to distinguish. */
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2000
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2001 if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2002 /* rv can't be 0, since it's an overestimate for some
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2003 nonzero value. So rv is a normal power of 2. */
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2004 j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2005 /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2006 rv / 2^bc.scale >= 2^-1021. */
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2007 if (j - bc.scale >= 2) {
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2008 dval(&rv) -= 0.5 * sulp(&rv, &bc);
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2009 break; /* Use bigcomp. */
58318
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2010 }
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2011 }
92384d7f425a Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.)
Mark Dickinson <dickinsm@gmail.com>
parents: 58317
diff changeset
2012
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2013 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2014 bc.nd = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2015 i = -1; /* Discarded digits make delta smaller. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2016 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2017 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2018
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2019 if (i < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2020 /* Error is less than half an ulp -- check for
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2021 * special case of mantissa a power of two.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2022 */
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2023 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2024 || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2025 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2026 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2027 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2028 if (!delta->x[0] && delta->wds <= 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2029 /* exact result */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2030 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2031 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2032 delta = lshift(delta,Log2P);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2033 if (delta == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2034 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2035 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2036 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2037 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2038 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2039 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2040 if (cmp(delta, bs) > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2041 goto drop_down;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2042 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2043 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2044 if (i == 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2045 /* exactly half-way between */
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2046 if (dsign) {
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2047 if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2048 && word1(&rv) == (
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2049 (bc.scale &&
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2050 (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2051 (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2052 0xffffffff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2053 /*boundary case -- increment exponent*/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2054 word0(&rv) = (word0(&rv) & Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2055 + Exp_msk1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2056 ;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2057 word1(&rv) = 0;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2058 dsign = 0;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2059 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2060 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2061 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2062 else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2063 drop_down:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2064 /* boundary case -- decrement exponent */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2065 if (bc.scale) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2066 L = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2067 if (L <= (2*P+1)*Exp_msk1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2068 if (L > (P+2)*Exp_msk1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2069 /* round even ==> */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2070 /* accept rv */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2071 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2072 /* rv = smallest denormal */
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2073 if (bc.nd > nd)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2074 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2075 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2076 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2077 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2078 L = (word0(&rv) & Exp_mask) - Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2079 word0(&rv) = L | Bndry_mask1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2080 word1(&rv) = 0xffffffff;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2081 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2082 }
58444
7a9f36cc6df1 Issue #7743: Fix a potential incorrect rounding bug in dtoa.c (2nd bug
Mark Dickinson <dickinsm@gmail.com>
parents: 58421
diff changeset
2083 if (!odd)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2084 break;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2085 if (dsign)
58459
b40a5a568e76 dtoa.c fix from upstream that fixes incorrectly rounded results for certain subnormals that are also halfway cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58458
diff changeset
2086 dval(&rv) += sulp(&rv, &bc);
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2087 else {
58459
b40a5a568e76 dtoa.c fix from upstream that fixes incorrectly rounded results for certain subnormals that are also halfway cases.
Mark Dickinson <dickinsm@gmail.com>
parents: 58458
diff changeset
2088 dval(&rv) -= sulp(&rv, &bc);
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2089 if (!dval(&rv)) {
58239
3e1712221fa6 Remove unused BCinfo fields and an unused macro.
Mark Dickinson <dickinsm@gmail.com>
parents: 58151
diff changeset
2090 if (bc.nd >nd)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2091 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2092 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2093 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2094 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2095 dsign = 1 - dsign;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2096 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2097 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2098 if ((aadj = ratio(delta, bs)) <= 2.) {
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2099 if (dsign)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2100 aadj = aadj1 = 1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2101 else if (word1(&rv) || word0(&rv) & Bndry_mask) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2102 if (word1(&rv) == Tiny1 && !word0(&rv)) {
58239
3e1712221fa6 Remove unused BCinfo fields and an unused macro.
Mark Dickinson <dickinsm@gmail.com>
parents: 58151
diff changeset
2103 if (bc.nd >nd)
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2104 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2105 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2106 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2107 aadj = 1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2108 aadj1 = -1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2109 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2110 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2111 /* special case -- power of FLT_RADIX to be */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2112 /* rounded down... */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2113
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2114 if (aadj < 2./FLT_RADIX)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2115 aadj = 1./FLT_RADIX;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2116 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2117 aadj *= 0.5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2118 aadj1 = -aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2119 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2120 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2121 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2122 aadj *= 0.5;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2123 aadj1 = dsign ? aadj : -aadj;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2124 if (Flt_Rounds == 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2125 aadj1 += 0.5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2126 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2127 y = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2128
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2129 /* Check for overflow */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2130
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2131 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2132 dval(&rv0) = dval(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2133 word0(&rv) -= P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2134 adj.d = aadj1 * ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2135 dval(&rv) += adj.d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2136 if ((word0(&rv) & Exp_mask) >=
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2137 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
58390
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2138 if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2139 Bfree(bb);
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2140 Bfree(bd);
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2141 Bfree(bs);
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2142 Bfree(bd0);
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2143 Bfree(delta);
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2144 goto ovfl;
58390
7322c5691294 Issue #7632: Fix a memory leak in _Py_dg_strtod.
Mark Dickinson <dickinsm@gmail.com>
parents: 58358
diff changeset
2145 }
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2146 word0(&rv) = Big0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2147 word1(&rv) = Big1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2148 goto cont;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2149 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2150 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2151 word0(&rv) += P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2152 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2153 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2154 if (bc.scale && y <= 2*P*Exp_msk1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2155 if (aadj <= 0x7fffffff) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2156 if ((z = (ULong)aadj) <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2157 z = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2158 aadj = z;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2159 aadj1 = dsign ? aadj : -aadj;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2160 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2161 dval(&aadj2) = aadj1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2162 word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2163 aadj1 = dval(&aadj2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2164 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2165 adj.d = aadj1 * ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2166 dval(&rv) += adj.d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2167 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2168 z = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2169 if (bc.nd == nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2170 if (!bc.scale)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2171 if (y == z) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2172 /* Can we stop now? */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2173 L = (Long)aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2174 aadj -= L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2175 /* The tolerances below are conservative. */
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2176 if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2177 if (aadj < .4999999 || aadj > .5000001)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2178 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2179 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2180 else if (aadj < .4999999/FLT_RADIX)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2181 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2182 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2183 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2184 cont:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2185 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2186 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2187 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2188 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2189 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2190 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2191 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2192 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2193 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2194 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2195 if (bc.nd > nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2196 error = bigcomp(&rv, s0, &bc);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2197 if (error)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2198 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2199 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2200
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2201 if (bc.scale) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2202 word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2203 word1(&rv0) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2204 dval(&rv) *= dval(&rv0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2205 }
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2206
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2207 ret:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2208 return sign ? -dval(&rv) : dval(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2209
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2210 parse_error:
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2211 return 0.0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2212
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2213 failed_malloc:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2214 errno = ENOMEM;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2215 return -1.0;
58419
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2216
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2217 undfl:
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2218 return sign ? -0.0 : 0.0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2219
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2220 ovfl:
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2221 errno = ERANGE;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2222 /* Can't trust HUGE_VAL */
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2223 word0(&rv) = Exp_mask;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2224 word1(&rv) = 0;
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2225 return sign ? -dval(&rv) : dval(&rv);
bac821dffea8 Various dtoa.c cleanups. 1. Despagghetify _Py_dg_strtod parsing code
Mark Dickinson <dickinsm@gmail.com>
parents: 58401
diff changeset
2226
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2227 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2228
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2229 static char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2230 rv_alloc(int i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2231 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2232 int j, k, *r;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2233
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2234 j = sizeof(ULong);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2235 for(k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2236 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2237 j <<= 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2238 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2239 r = (int*)Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2240 if (r == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2241 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2242 *r = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2243 return (char *)(r+1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2244 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2245
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2246 static char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2247 nrv_alloc(char *s, char **rve, int n)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2248 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2249 char *rv, *t;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2250
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2251 rv = rv_alloc(n);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2252 if (rv == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2253 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2254 t = rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2255 while((*t = *s++)) t++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2256 if (rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2257 *rve = t;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2258 return rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2259 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2260
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2261 /* freedtoa(s) must be used to free values s returned by dtoa
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2262 * when MULTIPLE_THREADS is #defined. It should be used in all cases,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2263 * but for consistency with earlier versions of dtoa, it is optional
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2264 * when MULTIPLE_THREADS is not defined.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2265 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2266
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2267 void
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2268 _Py_dg_freedtoa(char *s)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2269 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2270 Bigint *b = (Bigint *)((int *)s - 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2271 b->maxwds = 1 << (b->k = *(int*)b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2272 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2273 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2274
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2275 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2276 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2277 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2278 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2279 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2280 * Modifications:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2281 * 1. Rather than iterating, we use a simple numeric overestimate
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2282 * to determine k = floor(log10(d)). We scale relevant
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2283 * quantities using O(log2(k)) rather than O(k) multiplications.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2284 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2285 * try to generate digits strictly left to right. Instead, we
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2286 * compute with fewer bits and propagate the carry if necessary
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2287 * when rounding the final digit up. This is often faster.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2288 * 3. Under the assumption that input will be rounded nearest,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2289 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2290 * That is, we allow equality in stopping tests when the
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2291 * round-nearest rule will give the same floating-point value
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2292 * as would satisfaction of the stopping test with strict
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2293 * inequality.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2294 * 4. We remove common factors of powers of 2 from relevant
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2295 * quantities.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2296 * 5. When converting floating-point integers less than 1e16,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2297 * we use floating-point arithmetic rather than resorting
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2298 * to multiple-precision integers.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2299 * 6. When asked to produce fewer than 15 digits, we first try
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2300 * to get by with floating-point arithmetic; we resort to
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2301 * multiple-precision integer arithmetic only if we cannot
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2302 * guarantee that the floating-point calculation has given
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2303 * the correctly rounded result. For k requested digits and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2304 * "uniformly" distributed input, the probability is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2305 * something like 10^(k-15) that we must resort to the Long
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2306 * calculation.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2307 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2308
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2309 /* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2310 leakage, a successful call to _Py_dg_dtoa should always be matched by a
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2311 call to _Py_dg_freedtoa. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2312
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2313 char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2314 _Py_dg_dtoa(double dd, int mode, int ndigits,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2315 int *decpt, int *sign, char **rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2316 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2317 /* Arguments ndigits, decpt, sign are similar to those
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2318 of ecvt and fcvt; trailing zeros are suppressed from
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2319 the returned string. If not null, *rve is set to point
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2320 to the end of the return value. If d is +-Infinity or NaN,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2321 then *decpt is set to 9999.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2322
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2323 mode:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2324 0 ==> shortest string that yields d when read in
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2325 and rounded to nearest.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2326 1 ==> like 0, but with Steele & White stopping rule;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2327 e.g. with IEEE P754 arithmetic , mode 0 gives
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2328 1e23 whereas mode 1 gives 9.999999999999999e22.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2329 2 ==> max(1,ndigits) significant digits. This gives a
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2330 return value similar to that of ecvt, except
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2331 that trailing zeros are suppressed.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2332 3 ==> through ndigits past the decimal point. This
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2333 gives a return value similar to that from fcvt,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2334 except that trailing zeros are suppressed, and
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2335 ndigits can be negative.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2336 4,5 ==> similar to 2 and 3, respectively, but (in
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2337 round-nearest mode) with the tests of mode 0 to
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2338 possibly return a shorter string that rounds to d.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2339 With IEEE arithmetic and compilation with
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2340 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2341 as modes 2 and 3 when FLT_ROUNDS != 1.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2342 6-9 ==> Debugging modes similar to mode - 4: don't try
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2343 fast floating-point estimate (if applicable).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2344
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2345 Values of mode other than 0-9 are treated as mode 0.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2346
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2347 Sufficient space is allocated to the return value
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2348 to hold the suppressed trailing zeros.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2349 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2350
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2351 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2352 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2353 spec_case, try_quick;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2354 Long L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2355 int denorm;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2356 ULong x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2357 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2358 U d2, eps, u;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2359 double ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2360 char *s, *s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2361
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2362 /* set pointers to NULL, to silence gcc compiler warnings and make
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2363 cleanup easier on error */
61267
5cea27275af1 Remove three unneeded variable assignments.
Brett Cannon <bcannon@gmail.com>
parents: 58459
diff changeset
2364 mlo = mhi = S = 0;
56700
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2365 s0 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2366
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2367 u.d = dd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2368 if (word0(&u) & Sign_bit) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2369 /* set sign for everything, including 0's and NaNs */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2370 *sign = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2371 word0(&u) &= ~Sign_bit; /* clear sign bit */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2372 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2373 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2374 *sign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2375
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2376 /* quick return for Infinities, NaNs and zeros */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2377 if ((word0(&u) & Exp_mask) == Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2378 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2379 /* Infinity or NaN */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2380 *decpt = 9999;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2381 if (!word1(&u) && !(word0(&u) & 0xfffff))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2382 return nrv_alloc("Infinity", rve, 8);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2383 return nrv_alloc("NaN", rve, 3);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2384 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2385 if (!dval(&u)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2386 *decpt = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2387 return nrv_alloc("0", rve, 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2388 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2389
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2390 /* compute k = floor(log10(d)). The computation may leave k
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2391 one too large, but should never leave k too small. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2392 b = d2b(&u, &be, &bbits);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2393 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2394 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2395 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2396 dval(&d2) = dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2397 word0(&d2) &= Frac_mask1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2398 word0(&d2) |= Exp_11;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2399
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2400 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2401 * log10(x) = log(x) / log(10)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2402 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2403 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2404 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2405 * This suggests computing an approximation k to log10(d) by
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2406 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2407 * k = (i - Bias)*0.301029995663981
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2408 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2409 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2410 * We want k to be too large rather than too small.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2411 * The error in the first-order Taylor series approximation
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2412 * is in our favor, so we just round up the constant enough
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2413 * to compensate for any error in the multiplication of
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2414 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2415 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2416 * adding 1e-13 to the constant term more than suffices.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2417 * Hence we adjust the constant term to 0.1760912590558.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2418 * (We could get a more accurate k by invoking log10,
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2419 * but this is probably not worthwhile.)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2420 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2421
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2422 i -= Bias;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2423 denorm = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2424 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2425 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2426 /* d is denormalized */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2427
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2428 i = bbits + be + (Bias + (P-1) - 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2429 x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2430 : word1(&u) << (32 - i);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2431 dval(&d2) = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2432 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2433 i -= (Bias + (P-1) - 1) + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2434 denorm = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2435 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2436 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2437 i*0.301029995663981;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2438 k = (int)ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2439 if (ds < 0. && ds != k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2440 k--; /* want k = floor(ds) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2441 k_check = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2442 if (k >= 0 && k <= Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2443 if (dval(&u) < tens[k])
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2444 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2445 k_check = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2446 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2447 j = bbits - i - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2448 if (j >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2449 b2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2450 s2 = j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2451 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2452 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2453 b2 = -j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2454 s2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2455 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2456 if (k >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2457 b5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2458 s5 = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2459 s2 += k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2460 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2461 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2462 b2 -= k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2463 b5 = -k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2464 s5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2465 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2466 if (mode < 0 || mode > 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2467 mode = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2468
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2469 try_quick = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2470
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2471 if (mode > 5) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2472 mode -= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2473 try_quick = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2474 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2475 leftright = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2476 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2477 /* silence erroneous "gcc -Wall" warning. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2478 switch(mode) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2479 case 0:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2480 case 1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2481 i = 18;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2482 ndigits = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2483 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2484 case 2:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2485 leftright = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2486 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2487 case 4:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2488 if (ndigits <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2489 ndigits = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2490 ilim = ilim1 = i = ndigits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2491 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2492 case 3:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2493 leftright = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2494 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2495 case 5:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2496 i = ndigits + k + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2497 ilim = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2498 ilim1 = i - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2499 if (i <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2500 i = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2501 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2502 s0 = rv_alloc(i);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2503 if (s0 == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2504 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2505 s = s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2506
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2507
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2508 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2509
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2510 /* Try to get by with floating-point arithmetic. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2511
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2512 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2513 dval(&d2) = dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2514 k0 = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2515 ilim0 = ilim;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2516 ieps = 2; /* conservative */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2517 if (k > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2518 ds = tens[k&0xf];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2519 j = k >> 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2520 if (j & Bletch) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2521 /* prevent overflows */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2522 j &= Bletch - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2523 dval(&u) /= bigtens[n_bigtens-1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2524 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2525 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2526 for(; j; j >>= 1, i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2527 if (j & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2528 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2529 ds *= bigtens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2530 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2531 dval(&u) /= ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2532 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2533 else if ((j1 = -k)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2534 dval(&u) *= tens[j1 & 0xf];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2535 for(j = j1 >> 4; j; j >>= 1, i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2536 if (j & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2537 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2538 dval(&u) *= bigtens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2539 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2540 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2541 if (k_check && dval(&u) < 1. && ilim > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2542 if (ilim1 <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2543 goto fast_failed;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2544 ilim = ilim1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2545 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2546 dval(&u) *= 10.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2547 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2548 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2549 dval(&eps) = ieps*dval(&u) + 7.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2550 word0(&eps) -= (P-1)*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2551 if (ilim == 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2552 S = mhi = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2553 dval(&u) -= 5.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2554 if (dval(&u) > dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2555 goto one_digit;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2556 if (dval(&u) < -dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2557 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2558 goto fast_failed;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2559 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2560 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2561 /* Use Steele & White method of only
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2562 * generating digits needed.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2563 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2564 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2565 for(i = 0;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2566 L = (Long)dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2567 dval(&u) -= L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2568 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2569 if (dval(&u) < dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2570 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2571 if (1. - dval(&u) < dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2572 goto bump_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2573 if (++i >= ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2574 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2575 dval(&eps) *= 10.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2576 dval(&u) *= 10.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2577 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2578 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2579 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2580 /* Generate ilim digits, then fix them up. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2581 dval(&eps) *= tens[ilim-1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2582 for(i = 1;; i++, dval(&u) *= 10.) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2583 L = (Long)(dval(&u));
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2584 if (!(dval(&u) -= L))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2585 ilim = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2586 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2587 if (i == ilim) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2588 if (dval(&u) > 0.5 + dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2589 goto bump_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2590 else if (dval(&u) < 0.5 - dval(&eps)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2591 while(*--s == '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2592 s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2593 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2594 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2595 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2596 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2597 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2598 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2599 fast_failed:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2600 s = s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2601 dval(&u) = dval(&d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2602 k = k0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2603 ilim = ilim0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2604 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2605
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2606 /* Do we have a "small" integer? */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2607
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2608 if (be >= 0 && k <= Int_max) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2609 /* Yes. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2610 ds = tens[k];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2611 if (ndigits < 0 && ilim <= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2612 S = mhi = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2613 if (ilim < 0 || dval(&u) <= 5*ds)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2614 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2615 goto one_digit;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2616 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2617 for(i = 1;; i++, dval(&u) *= 10.) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2618 L = (Long)(dval(&u) / ds);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2619 dval(&u) -= L*ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2620 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2621 if (!dval(&u)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2622 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2623 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2624 if (i == ilim) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2625 dval(&u) += dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2626 if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2627 bump_up:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2628 while(*--s == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2629 if (s == s0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2630 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2631 *s = '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2632 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2633 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2634 ++*s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2635 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2636 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2637 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2638 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2639 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2640 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2641
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2642 m2 = b2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2643 m5 = b5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2644 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2645 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2646 denorm ? be + (Bias + (P-1) - 1 + 1) :
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2647 1 + P - bbits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2648 b2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2649 s2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2650 mhi = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2651 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2652 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2653 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2654 if (m2 > 0 && s2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2655 i = m2 < s2 ? m2 : s2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2656 b2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2657 m2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2658 s2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2659 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2660 if (b5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2661 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2662 if (m5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2663 mhi = pow5mult(mhi, m5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2664 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2665 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2666 b1 = mult(mhi, b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2667 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2668 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2669 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2670 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2671 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2672 if ((j = b5 - m5)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2673 b = pow5mult(b, j);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2674 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2675 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2676 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2677 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2678 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2679 b = pow5mult(b, b5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2680 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2681 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2682 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2683 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2684 S = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2685 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2686 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2687 if (s5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2688 S = pow5mult(S, s5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2689 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2690 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2691 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2692
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2693 /* Check for special case that d is a normalized power of 2. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2694
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2695 spec_case = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2696 if ((mode < 2 || leftright)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2697 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2698 if (!word1(&u) && !(word0(&u) & Bndry_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2699 && word0(&u) & (Exp_mask & ~Exp_msk1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2700 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2701 /* The special case */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2702 b2 += Log2P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2703 s2 += Log2P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2704 spec_case = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2705 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2706 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2707
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2708 /* Arrange for convenient computation of quotients:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2709 * shift left if necessary so divisor has 4 leading 0 bits.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2710 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2711 * Perhaps we should just compute leading 28 bits of S once
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2712 * and for all and pass them and a shift to quorem, so it
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2713 * can do shifts and ors to compute the numerator for q.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2714 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2715 #define iInc 28
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2716 i = dshift(S, s2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2717 b2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2718 m2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2719 s2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2720 if (b2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2721 b = lshift(b, b2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2722 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2723 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2724 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2725 if (s2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2726 S = lshift(S, s2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2727 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2728 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2729 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2730 if (k_check) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2731 if (cmp(b,S) < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2732 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2733 b = multadd(b, 10, 0); /* we botched the k estimate */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2734 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2735 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2736 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2737 mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2738 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2739 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2740 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2741 ilim = ilim1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2742 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2743 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2744 if (ilim <= 0 && (mode == 3 || mode == 5)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2745 if (ilim < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2746 /* no digits, fcvt style */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2747 no_digits:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2748 k = -1 - ndigits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2749 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2750 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2751 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2752 S = multadd(S, 5, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2753 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2754 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2755 if (cmp(b, S) <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2756 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2757 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2758 one_digit:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2759 *s++ = '1';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2760 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2761 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2762 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2763 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2764 if (m2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2765 mhi = lshift(mhi, m2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2766 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2767 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2768 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2769
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2770 /* Compute mlo -- check for special case
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2771 * that d is a normalized power of 2.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2772 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2773
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2774 mlo = mhi;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2775 if (spec_case) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2776 mhi = Balloc(mhi->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2777 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2778 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2779 Bcopy(mhi, mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2780 mhi = lshift(mhi, Log2P);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2781 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2782 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2783 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2784
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2785 for(i = 1;;i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2786 dig = quorem(b,S) + '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2787 /* Do we yet have the shortest decimal string
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2788 * that will round to d?
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2789 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2790 j = cmp(b, mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2791 delta = diff(S, mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2792 if (delta == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2793 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2794 j1 = delta->sign ? 1 : cmp(b, delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2795 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2796 if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2797 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2798 if (dig == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2799 goto round_9_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2800 if (j > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2801 dig++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2802 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2803 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2804 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2805 if (j < 0 || (j == 0 && mode != 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2806 && !(word1(&u) & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2807 )) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2808 if (!b->x[0] && b->wds <= 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2809 goto accept_dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2810 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2811 if (j1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2812 b = lshift(b, 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2813 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2814 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2815 j1 = cmp(b, S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2816 if ((j1 > 0 || (j1 == 0 && dig & 1))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2817 && dig++ == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2818 goto round_9_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2819 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2820 accept_dig:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2821 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2822 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2823 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2824 if (j1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2825 if (dig == '9') { /* possible if i == 1 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2826 round_9_up:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2827 *s++ = '9';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2828 goto roundoff;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2829 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2830 *s++ = dig + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2831 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2832 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2833 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2834 if (i == ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2835 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2836 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2837 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2838 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2839 if (mlo == mhi) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2840 mlo = mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2841 if (mlo == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2842 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2843 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2844 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2845 mlo = multadd(mlo, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2846 if (mlo == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2847 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2848 mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2849 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2850 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2851 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2852 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2853 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2854 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2855 for(i = 1;; i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2856 *s++ = dig = quorem(b,S) + '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2857 if (!b->x[0] && b->wds <= 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2858 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2859 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2860 if (i >= ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2861 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2862 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2863 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2864 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2865 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2866
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2867 /* Round off last digit */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2868
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2869 b = lshift(b, 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2870 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2871 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2872 j = cmp(b, S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2873 if (j > 0 || (j == 0 && dig & 1)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2874 roundoff:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2875 while(*--s == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2876 if (s == s0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2877 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2878 *s++ = '1';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2879 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2880 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2881 ++*s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2882 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2883 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2884 while(*--s == '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2885 s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2886 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2887 ret:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2888 Bfree(S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2889 if (mhi) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2890 if (mlo && mlo != mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2891 Bfree(mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2892 Bfree(mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2893 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2894 ret1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2895 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2896 *s = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2897 *decpt = k + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2898 if (rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2899 *rve = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2900 return s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2901 failed_malloc:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2902 if (S)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2903 Bfree(S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2904 if (mlo && mlo != mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2905 Bfree(mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2906 if (mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2907 Bfree(mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2908 if (b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2909 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2910 if (s0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2911 _Py_dg_freedtoa(s0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2912 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2913 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2914 #ifdef __cplusplus
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2915 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2916 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2917
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2918 #endif /* PY_NO_SHORT_FLOAT_REPR */