annotate Python/dtoa.c @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents dabe1f92ca27
children 8ca377853321
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
203 #ifdef DIGLIM_DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
204 extern int strtod_diglim;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
205 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
206 #define strtod_diglim STRTOD_DIGLIM
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
207 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
208
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
209 /* 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
210 * 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
211 * #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
212 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
213 #if defined(IEEE_8087)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
214 #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
215 ((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
216 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
217 #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
218 ((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
219 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
220
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
221 /* #define P DBL_MANT_DIG */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
222 /* 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
223 /* 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
224 /* 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
225 /* 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
226
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
227 #define Exp_shift 20
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
228 #define Exp_shift1 20
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
229 #define Exp_msk1 0x100000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
230 #define Exp_msk11 0x100000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
231 #define Exp_mask 0x7ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
232 #define P 53
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
233 #define Nbits 53
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
234 #define Bias 1023
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
235 #define Emax 1023
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
236 #define Emin (-1022)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
237 #define Exp_1 0x3ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
238 #define Exp_11 0x3ff00000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
239 #define Ebits 11
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
240 #define Frac_mask 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
241 #define Frac_mask1 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
242 #define Ten_pmax 22
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
243 #define Bletch 0x10
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
244 #define Bndry_mask 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
245 #define Bndry_mask1 0xfffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
246 #define LSB 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
247 #define Sign_bit 0x80000000
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
248 #define Log2P 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
249 #define Tiny0 0
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
250 #define Tiny1 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
251 #define Quick_max 14
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
252 #define Int_max 14
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
253
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
254 #ifndef Flt_Rounds
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
255 #ifdef FLT_ROUNDS
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
256 #define Flt_Rounds FLT_ROUNDS
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
257 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
258 #define Flt_Rounds 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
259 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
260 #endif /*Flt_Rounds*/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
261
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
262 #define Rounding Flt_Rounds
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
263
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
264 #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
265 #define Big1 0xffffffff
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
266
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
267 /* 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
268
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
269 typedef struct BCinfo BCinfo;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
270 struct
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
271 BCinfo {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
272 int dp0, dp1, dplen, dsign, e0, inexact;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
273 int nd, nd0, rounding, scale, uflchk;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
311 /* 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
312 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
313 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
314 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
315
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
316 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
317 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
318 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
319 in preference to using MALLOC.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
320
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
321 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
322 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
323 FREE.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
324
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
325 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
326 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
327 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
328 performance on impact. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
329
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
330 static Bigint *freelist[Kmax+1];
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 /* 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
333
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
334 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
335 Balloc(int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
336 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
337 int x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
338 Bigint *rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
339 unsigned int len;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
340
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
341 if (k <= Kmax && (rv = freelist[k]))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
342 freelist[k] = rv->next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
343 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
344 x = 1 << k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
345 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
346 /sizeof(double);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
347 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
348 rv = (Bigint*)pmem_next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
349 pmem_next += len;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
350 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
351 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
352 rv = (Bigint*)MALLOC(len*sizeof(double));
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
353 if (rv == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
354 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
355 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
356 rv->k = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
357 rv->maxwds = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
358 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
359 rv->sign = rv->wds = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
360 return rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
361 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
362
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
363 /* Free a Bigint allocated with Balloc */
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 static void
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
366 Bfree(Bigint *v)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
367 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
368 if (v) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
369 if (v->k > Kmax)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
370 FREE((void*)v);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
371 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
372 v->next = freelist[v->k];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
373 freelist[v->k] = v;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
374 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
375 }
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 #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
379 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
380
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
381 /* 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
382 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
383 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
384
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
385 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
386 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
387 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
388 int i, wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
389 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
390 ULong *x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
391 ULLong carry, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
392 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
393 ULong carry, *x, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
394 ULong xi, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
395 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
396 Bigint *b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
397
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
398 wds = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
399 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
400 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
401 carry = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
402 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
403 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
404 y = *x * (ULLong)m + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
405 carry = y >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
406 *x++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
407 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
408 xi = *x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
409 y = (xi & 0xffff) * m + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
410 z = (xi >> 16) * m + (y >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
411 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
412 *x++ = (z << 16) + (y & 0xffff);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
413 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
414 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
415 while(++i < wds);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
416 if (carry) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
417 if (wds >= b->maxwds) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
418 b1 = Balloc(b->k+1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
419 if (b1 == NULL){
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
420 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
421 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
422 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
423 Bcopy(b1, b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
424 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
425 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
426 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
427 b->x[wds++] = (ULong)carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
428 b->wds = wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
429 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
430 return b;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
433 /* 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
434 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
435 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
436 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
437 NULL on failure. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
438
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
439 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
440 s2b(const char *s, int nd0, int nd, ULong y9, int dplen)
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 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
443 int i, k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
444 Long x, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
445
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
446 x = (nd + 8) / 9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
447 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
448 b = Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
449 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
450 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
451 b->x[0] = y9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
452 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
453
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
454 i = 9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
455 if (9 < nd0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
456 s += 9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
457 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
458 b = multadd(b, 10, *s++ - '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
459 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
460 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
461 } while(++i < nd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
462 s += dplen;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
463 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
464 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
465 s += dplen + 9;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
466 for(; i < nd; i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
467 b = multadd(b, 10, *s++ - '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
468 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
469 return NULL;
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 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
472 }
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 /* 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
475
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
476 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
477 hi0bits(ULong x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
478 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
479 int k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
480
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
481 if (!(x & 0xffff0000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
482 k = 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
483 x <<= 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
484 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
485 if (!(x & 0xff000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
486 k += 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
487 x <<= 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
488 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
489 if (!(x & 0xf0000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
490 k += 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
491 x <<= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
492 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
493 if (!(x & 0xc0000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
494 k += 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
495 x <<= 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
496 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
497 if (!(x & 0x80000000)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
498 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
499 if (!(x & 0x40000000))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
500 return 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
501 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
502 return k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
503 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
504
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
505 /* 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
506 number of bits. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
507
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
508 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
509 lo0bits(ULong *y)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
510 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
511 int k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
512 ULong x = *y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
513
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
514 if (x & 7) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
515 if (x & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
516 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
517 if (x & 2) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
518 *y = x >> 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
519 return 1;
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 *y = x >> 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
522 return 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
523 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
524 k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
525 if (!(x & 0xffff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
526 k = 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
527 x >>= 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
528 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
529 if (!(x & 0xff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
530 k += 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
531 x >>= 8;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
532 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
533 if (!(x & 0xf)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
534 k += 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
535 x >>= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
536 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
537 if (!(x & 0x3)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
538 k += 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
539 x >>= 2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
540 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
541 if (!(x & 1)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
542 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
543 x >>= 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
544 if (!x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
545 return 32;
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 *y = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
548 return k;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
551 /* 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
552
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
553 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
554 i2b(int i)
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 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
557
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
558 b = Balloc(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
559 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
560 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
561 b->x[0] = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
562 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
563 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
564 }
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 /* 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
567 the signs of a and b. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
568
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
569 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
570 mult(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
571 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
572 Bigint *c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
573 int k, wa, wb, wc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
574 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
575 ULong y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
576 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
577 ULLong carry, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
578 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
579 ULong carry, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
580 ULong z2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
581 #endif
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 (a->wds < b->wds) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
584 c = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
585 a = b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
586 b = c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
587 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
588 k = a->k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
589 wa = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
590 wb = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
591 wc = wa + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
592 if (wc > a->maxwds)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
593 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
594 c = Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
595 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
596 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
597 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
598 *x = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
599 xa = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
600 xae = xa + wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
601 xb = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
602 xbe = xb + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
603 xc0 = c->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
604 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
605 for(; xb < xbe; xc0++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
606 if ((y = *xb++)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
607 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
608 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
609 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
610 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
611 z = *x++ * (ULLong)y + *xc + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
612 carry = z >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
613 *xc++ = (ULong)(z & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
614 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
615 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
616 *xc = (ULong)carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
617 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
618 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
619 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
620 for(; xb < xbe; xb++, xc0++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
621 if (y = *xb & 0xffff) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
622 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
623 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
624 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
625 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
626 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
627 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
628 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
629 carry = z2 >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
630 Storeinc(xc, z2, z);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
631 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
632 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
633 *xc = carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
634 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
635 if (y = *xb >> 16) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
636 x = xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
637 xc = xc0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
638 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
639 z2 = *xc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
640 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
641 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
642 carry = z >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
643 Storeinc(xc, z, z2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
644 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
645 carry = z2 >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
646 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
647 while(x < xae);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
648 *xc = z2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
649 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
650 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
651 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
652 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
653 c->wds = wc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
654 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
655 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
656
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
657 /* 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
658
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
659 static Bigint *p5s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
660
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
661 /* 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
662 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
663 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
664
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
665 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
666 pow5mult(Bigint *b, int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
667 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
668 Bigint *b1, *p5, *p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
669 int i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
670 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
671
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
672 if ((i = k & 3)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
673 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
674 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
675 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
676 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
677
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
678 if (!(k >>= 2))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
679 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
680 p5 = p5s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
681 if (!p5) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
682 /* first time */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
683 p5 = i2b(625);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
684 if (p5 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
685 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
686 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
687 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
688 p5s = p5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
689 p5->next = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
690 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
691 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
692 if (k & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
693 b1 = mult(b, p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
694 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
695 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
696 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
697 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
698 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
699 if (!(k >>= 1))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
700 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
701 p51 = p5->next;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
702 if (!p51) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
703 p51 = mult(p5,p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
704 if (p51 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
705 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
706 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
707 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
708 p51->next = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
709 p5->next = p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
710 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
711 p5 = p51;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
712 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
713 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
714 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
715
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
716 /* 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
717 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
718 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
719
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
720 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
721 lshift(Bigint *b, int k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
722 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
723 int i, k1, n, n1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
724 Bigint *b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
725 ULong *x, *x1, *xe, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
726
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
727 n = k >> 5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
728 k1 = b->k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
729 n1 = n + b->wds + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
730 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
731 k1++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
732 b1 = Balloc(k1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
733 if (b1 == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
734 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
735 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
736 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
737 x1 = b1->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
738 for(i = 0; i < n; i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
739 *x1++ = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
740 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
741 xe = x + b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
742 if (k &= 0x1f) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
743 k1 = 32 - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
744 z = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
745 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
746 *x1++ = *x << k | z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
747 z = *x++ >> k1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
748 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
749 while(x < xe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
750 if ((*x1 = z))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
751 ++n1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
752 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
753 else do
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
754 *x1++ = *x++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
755 while(x < xe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
756 b1->wds = n1 - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
757 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
758 return b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
759 }
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 /* 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
762 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
763
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
764 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
765 cmp(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
766 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
767 ULong *xa, *xa0, *xb, *xb0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
768 int i, j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
769
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
770 i = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
771 j = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
772 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
773 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
774 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
775 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
776 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
777 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
778 if (i -= j)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
779 return i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
780 xa0 = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
781 xa = xa0 + j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
782 xb0 = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
783 xb = xb0 + j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
784 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
785 if (*--xa != *--xb)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
786 return *xa < *xb ? -1 : 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
787 if (xa <= xa0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
788 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
789 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
790 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
791 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
792
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
793 /* 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
794 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
795 result is set appropriately. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
796
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
797 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
798 diff(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
799 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
800 Bigint *c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
801 int i, wa, wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
802 ULong *xa, *xae, *xb, *xbe, *xc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
803 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
804 ULLong borrow, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
805 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
806 ULong borrow, y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
807 ULong z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
808 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
809
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
810 i = cmp(a,b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
811 if (!i) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
812 c = Balloc(0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
813 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
814 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
815 c->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
816 c->x[0] = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
817 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
818 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
819 if (i < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
820 c = a;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
821 a = b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
822 b = c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
823 i = 1;
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 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
826 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
827 c = Balloc(a->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
828 if (c == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
829 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
830 c->sign = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
831 wa = a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
832 xa = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
833 xae = xa + wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
834 wb = b->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
835 xb = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
836 xbe = xb + wb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
837 xc = c->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
838 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
839 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
840 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
841 y = (ULLong)*xa++ - *xb++ - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
842 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
843 *xc++ = (ULong)(y & FFFFFFFF);
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 while(xb < xbe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
846 while(xa < xae) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
847 y = *xa++ - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
848 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
849 *xc++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
850 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
851 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
852 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
853 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
854 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
855 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
856 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
857 Storeinc(xc, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
858 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
859 while(xb < xbe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
860 while(xa < xae) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
861 y = (*xa & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
862 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
863 z = (*xa++ >> 16) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
864 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
865 Storeinc(xc, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
866 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
867 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
868 while(!*--xc)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
869 wa--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
870 c->wds = wa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
871 return c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
872 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
873
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
874 /* Given a positive normal double x, return the difference between x and the next
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
875 double up. Doesn't give correct results for subnormals. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
876
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
877 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
878 ulp(U *x)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
879 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
880 Long L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
881 U u;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
882
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
883 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
884 word0(&u) = L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
885 word1(&u) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
886 return dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
887 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
888
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
889 /* 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
890
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
891 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
892 b2d(Bigint *a, int *e)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
893 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
894 ULong *xa, *xa0, w, y, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
895 int k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
896 U d;
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 xa0 = a->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
899 xa = xa0 + a->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
900 y = *--xa;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
901 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
902 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
903 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
904 k = hi0bits(y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
905 *e = 32 - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
906 if (k < Ebits) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
907 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
908 w = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
909 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
910 goto ret_d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
911 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
912 z = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
913 if (k -= Ebits) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
914 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
915 y = xa > xa0 ? *--xa : 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
916 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
917 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
918 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
919 word0(&d) = Exp_1 | y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
920 word1(&d) = z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
921 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
922 ret_d:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
923 return dval(&d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
924 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
925
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
926 /* 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
927
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
928 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
929 such that fabs(d) = b * 2**e. On return, *bbits gives the number of
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
930 significant bits of e; that is, 2**(*bbits-1) <= b < 2**(*bbits).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
931
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
932 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
933 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
934
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
935
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
936 static Bigint *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
937 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
938 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
939 Bigint *b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
940 int de, k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
941 ULong *x, y, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
942 int i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
943
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
944 b = Balloc(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
945 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
946 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
947 x = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
948
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
949 z = word0(d) & Frac_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
950 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
951 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
952 z |= Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
953 if ((y = word1(d))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
954 if ((k = lo0bits(&y))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
955 x[0] = y | z << (32 - k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
956 z >>= k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
957 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
958 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
959 x[0] = y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
960 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
961 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
962 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
963 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
964 k = lo0bits(&z);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
965 x[0] = z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
966 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
967 b->wds = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
968 k += 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
969 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
970 if (de) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
971 *e = de - Bias - (P-1) + k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
972 *bits = P - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
973 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
974 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
975 *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
976 *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
977 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
978 return b;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
979 }
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 /* 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
982 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
983
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
984 static double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
985 ratio(Bigint *a, Bigint *b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
986 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
987 U da, db;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
988 int k, ka, kb;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
989
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
990 dval(&da) = b2d(a, &ka);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
991 dval(&db) = b2d(b, &kb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
992 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
993 if (k > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
994 word0(&da) += k*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
995 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
996 k = -k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
997 word0(&db) += k*Exp_msk1;
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 return dval(&da) / dval(&db);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1000 }
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 static const double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1003 tens[] = {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1004 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
1005 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
1006 1e20, 1e21, 1e22
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1007 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1008
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1009 static const double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1010 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1011 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
1012 9007199254740992.*9007199254740992.e-256
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1013 /* = 2^106 * 1e-256 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1014 };
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1015 /* 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
1016 /* 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
1017 #define Scale_Bit 0x10
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1018 #define n_bigtens 5
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 #define ULbits 32
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1021 #define kshift 5
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1022 #define kmask 31
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1023
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1024
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1025 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1026 dshift(Bigint *b, int p2)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1027 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1028 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
1029 if (p2 > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1030 rv -= p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1031 return rv & kmask;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1034 /* 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
1035 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
1036 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
1037
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1038 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1039 quorem(Bigint *b, Bigint *S)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1040 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1041 int n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1042 ULong *bx, *bxe, q, *sx, *sxe;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1043 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1044 ULLong borrow, carry, y, ys;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1045 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1046 ULong borrow, carry, y, ys;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1047 ULong si, z, zs;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1048 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1049
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1050 n = S->wds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1051 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1052 /*debug*/ if (b->wds > n)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1053 /*debug*/ Bug("oversize b in quorem");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1054 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1055 if (b->wds < n)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1056 return 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1057 sx = S->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1058 sxe = sx + --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1059 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1060 bxe = bx + n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1061 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
1062 #ifdef DEBUG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1063 /*debug*/ if (q > 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1064 /*debug*/ Bug("oversized quotient in quorem");
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1065 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1066 if (q) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1067 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1068 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1069 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1070 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1071 ys = *sx++ * (ULLong)q + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1072 carry = ys >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1073 y = *bx - (ys & FFFFFFFF) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1074 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1075 *bx++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1076 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1077 si = *sx++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1078 ys = (si & 0xffff) * q + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1079 zs = (si >> 16) * q + (ys >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1080 carry = zs >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1081 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1082 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1083 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1084 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1085 Storeinc(bx, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1086 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1087 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1088 while(sx <= sxe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1089 if (!*bxe) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1090 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1091 while(--bxe > bx && !*bxe)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1092 --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1093 b->wds = n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1094 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1095 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1096 if (cmp(b, S) >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1097 q++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1098 borrow = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1099 carry = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1100 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1101 sx = S->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1102 do {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1103 #ifdef ULLong
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1104 ys = *sx++ + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1105 carry = ys >> 32;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1106 y = *bx - (ys & FFFFFFFF) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1107 borrow = y >> 32 & (ULong)1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1108 *bx++ = (ULong)(y & FFFFFFFF);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1109 #else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1110 si = *sx++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1111 ys = (si & 0xffff) + carry;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1112 zs = (si >> 16) + (ys >> 16);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1113 carry = zs >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1114 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1115 borrow = (y & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1116 z = (*bx >> 16) - (zs & 0xffff) - borrow;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1117 borrow = (z & 0x10000) >> 16;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1118 Storeinc(bx, z, y);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1119 #endif
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 while(sx <= sxe);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1122 bx = b->x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1123 bxe = bx + n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1124 if (!*bxe) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1125 while(--bxe > bx && !*bxe)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1126 --n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1127 b->wds = n;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1128 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1129 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1130 return q;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1131 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1132
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1133
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1134 /* return 0 on success, -1 on failure */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1135
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1136 static int
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1137 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
1138 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1139 Bigint *b, *d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1140 int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1141
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1142 dsign = bc->dsign;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1143 nd = bc->nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1144 nd0 = bc->nd0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1145 p5 = nd + bc->e0 - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1146 speccase = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1147 if (rv->d == 0.) { /* special case: value near underflow-to-zero */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1148 /* threshold was rounded to zero */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1149 b = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1150 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1151 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1152 p2 = Emin - P + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1153 bbits = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1154 word0(rv) = (P+2) << Exp_shift;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1155 i = 0;
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 speccase = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1158 --p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1159 dsign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1160 goto have_i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1161 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1162 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1163 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1164 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1165 b = d2b(rv, &p2, &bbits);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1166 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1167 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1168 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1169 p2 -= bc->scale;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1170 /* floor(log2(rv)) == bbits - 1 + p2 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1171 /* Check for denormal case. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1172 i = P - bbits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1173 if (i > (j = P - Emin - 1 + p2)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1174 i = j;
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 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1177 b = lshift(b, ++i);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1178 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1179 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1180 b->x[0] |= 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1181 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1182 have_i:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1183 p2 -= p5 + i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1184 d = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1185 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1186 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1187 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1188 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1189 /* Arrange for convenient computation of quotients:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1190 * 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
1191 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1192 if (p5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1193 d = pow5mult(d, p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1194 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1195 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1196 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1197 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1198 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1199 else if (p5 < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1200 b = pow5mult(b, -p5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1201 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1202 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1203 return -1;
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 }
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 b2 = p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1208 d2 = 0;
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 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1211 b2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1212 d2 = -p2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1213 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1214 i = dshift(d, d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1215 if ((b2 += i) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1216 b = lshift(b, b2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1217 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1218 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1219 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1220 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1221 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1222 if ((d2 += i) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1223 d = lshift(d, d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1224 if (d == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1225 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1226 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1227 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1228 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1229
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1230 /* Now b/d = exactly half-way between the two floating-point values */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1231 /* on either side of the input string. Compute first digit of b/d. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1232
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1233 if (!(dig = quorem(b,d))) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1234 b = multadd(b, 10, 0); /* very unlikely */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1235 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1236 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1237 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1238 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1239 dig = quorem(b,d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1240 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1241
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1242 /* Compare b/d with s0 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1243
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1244 assert(nd > 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1245 dd = 9999; /* silence gcc compiler warning */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1246 for(i = 0; i < nd0; ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1247 if ((dd = s0[i++] - '0' - dig))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1248 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1249 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
1250 if (i < nd)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1251 dd = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1252 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1253 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1254 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1255 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1256 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1257 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1258 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1259 dig = quorem(b,d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1260 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1261 for(j = bc->dp1; i++ < nd;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1262 if ((dd = s0[j++] - '0' - dig))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1263 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1264 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
1265 if (i < nd)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1266 dd = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1267 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1268 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1269 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1270 if (b == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1271 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1272 return -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1273 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1274 dig = quorem(b,d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1275 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1276 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
1277 dd = -1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1278 ret:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1279 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1280 Bfree(d);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1281 if (speccase) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1282 if (dd <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1283 rv->d = 0.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1284 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1285 else if (dd < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1286 if (!dsign) /* does not happen for round-near */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1287 retlow1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1288 dval(rv) -= ulp(rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1289 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1290 else if (dd > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1291 if (dsign) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1292 rethi1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1293 dval(rv) += ulp(rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1294 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1295 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1296 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1297 /* Exact half-way case: apply round-even rule. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1298 if (word1(rv) & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1299 if (dsign)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1300 goto rethi1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1301 goto retlow1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1302 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1303 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1304
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1305 return 0;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1308 double
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1309 _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
1310 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1311 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1, error;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1312 int esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1313 const char *s, *s0, *s1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1314 double aadj, aadj1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1315 Long L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1316 U aadj2, adj, rv, rv0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1317 ULong y, z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1318 BCinfo bc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1319 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
1320
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1321 sign = nz0 = nz = bc.dplen = bc.uflchk = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1322 dval(&rv) = 0.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1323 for(s = s00;;s++) switch(*s) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1324 case '-':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1325 sign = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1326 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1327 case '+':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1328 if (*++s)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1329 goto break2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1330 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1331 case 0:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1332 goto ret0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1333 /* modify original dtoa.c so that it doesn't accept leading whitespace
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1334 case '\t':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1335 case '\n':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1336 case '\v':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1337 case '\f':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1338 case '\r':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1339 case ' ':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1340 continue;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1341 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1342 default:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1343 goto break2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1344 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1345 break2:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1346 if (*s == '0') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1347 nz0 = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1348 while(*++s == '0') ;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1349 if (!*s)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1350 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1351 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1352 s0 = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1353 y = z = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1354 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1355 if (nd < 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1356 y = 10*y + c - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1357 else if (nd < 16)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1358 z = 10*z + c - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1359 nd0 = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1360 bc.dp0 = bc.dp1 = s - s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1361 if (c == '.') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1362 c = *++s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1363 bc.dp1 = s - s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1364 bc.dplen = bc.dp1 - bc.dp0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1365 if (!nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1366 for(; c == '0'; c = *++s)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1367 nz++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1368 if (c > '0' && c <= '9') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1369 s0 = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1370 nf += nz;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1371 nz = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1372 goto have_dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1373 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1374 goto dig_done;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1375 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1376 for(; c >= '0' && c <= '9'; c = *++s) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1377 have_dig:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1378 nz++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1379 if (c -= '0') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1380 nf += nz;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1381 for(i = 1; i < nz; i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1382 if (nd++ < 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1383 y *= 10;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1384 else if (nd <= DBL_DIG + 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1385 z *= 10;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1386 if (nd++ < 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1387 y = 10*y + c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1388 else if (nd <= DBL_DIG + 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1389 z = 10*z + c;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1390 nz = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1391 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1392 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1393 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1394 dig_done:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1395 e = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1396 if (c == 'e' || c == 'E') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1397 if (!nd && !nz && !nz0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1398 goto ret0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1399 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1400 s00 = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1401 esign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1402 switch(c = *++s) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1403 case '-':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1404 esign = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1405 case '+':
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1406 c = *++s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1407 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1408 if (c >= '0' && c <= '9') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1409 while(c == '0')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1410 c = *++s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1411 if (c > '0' && c <= '9') {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1412 L = c - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1413 s1 = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1414 while((c = *++s) >= '0' && c <= '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1415 L = 10*L + c - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1416 if (s - s1 > 8 || L > 19999)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1417 /* Avoid confusion from exponents
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1418 * so large that e might overflow.
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 e = 19999; /* safe for 16 bit ints */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1421 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1422 e = (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1423 if (esign)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1424 e = -e;
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 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1427 e = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1428 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1429 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1430 s = s00;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1431 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1432 if (!nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1433 if (!nz && !nz0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1434 ret0:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1435 s = s00;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1436 sign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1437 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1438 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1439 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1440 bc.e0 = e1 = e -= nf;
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 /* Now we have nd0 digits, starting at s0, followed by a
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1443 * decimal point, followed by nd-nd0 digits. The number we're
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1444 * after is the integer represented by those digits times
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1445 * 10**e */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1446
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1447 if (!nd0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1448 nd0 = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1449 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
1450 dval(&rv) = y;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1451 if (k > 9) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1452 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
1453 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1454 bd0 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1455 if (nd <= DBL_DIG
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1456 && Flt_Rounds == 1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1457 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1458 if (!e)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1459 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1460 if (e > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1461 if (e <= Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1462 dval(&rv) *= tens[e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1463 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1464 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1465 i = DBL_DIG - nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1466 if (e <= Ten_pmax + i) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1467 /* 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
1468 * this for larger i values.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1469 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1470 e -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1471 dval(&rv) *= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1472 dval(&rv) *= tens[e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1473 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1474 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1475 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1476 else if (e >= -Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1477 dval(&rv) /= tens[-e];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1478 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1479 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1480 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1481 e1 += nd - k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1482
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1483 bc.scale = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1484
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1485 /* Get starting approximation = rv * 10**e1 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1486
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1487 if (e1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1488 if ((i = e1 & 15))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1489 dval(&rv) *= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1490 if (e1 &= ~15) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1491 if (e1 > DBL_MAX_10_EXP) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1492 ovfl:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1493 errno = ERANGE;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1494 /* Can't trust HUGE_VAL */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1495 word0(&rv) = Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1496 word1(&rv) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1497 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1498 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1499 e1 >>= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1500 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
1501 if (e1 & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1502 dval(&rv) *= bigtens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1503 /* The last multiplication could overflow. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1504 word0(&rv) -= P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1505 dval(&rv) *= bigtens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1506 if ((z = word0(&rv) & Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1507 > 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
1508 goto ovfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1509 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
1510 /* set to largest number */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1511 /* (Can't trust DBL_MAX) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1512 word0(&rv) = Big0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1513 word1(&rv) = Big1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1514 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1515 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1516 word0(&rv) += P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1517 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1518 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1519 else if (e1 < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1520 e1 = -e1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1521 if ((i = e1 & 15))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1522 dval(&rv) /= tens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1523 if (e1 >>= 4) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1524 if (e1 >= 1 << n_bigtens)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1525 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1526 if (e1 & Scale_Bit)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1527 bc.scale = 2*P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1528 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
1529 if (e1 & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1530 dval(&rv) *= tinytens[j];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1531 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
1532 >> Exp_shift)) > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1533 /* 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
1534 if (j >= 32) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1535 word1(&rv) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1536 if (j >= 53)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1537 word0(&rv) = (P+2)*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1538 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1539 word0(&rv) &= 0xffffffff << (j-32);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1540 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1541 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1542 word1(&rv) &= 0xffffffff << j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1543 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1544 if (!dval(&rv)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1545 undfl:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1546 dval(&rv) = 0.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1547 errno = ERANGE;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1548 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1549 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1550 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1551 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1552
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1553 /* 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
1554
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1555 /* 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
1556
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1557 bc.nd = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1558 bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1559 /* 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
1560 /* possibly not being initialized. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1561 if (nd > strtod_diglim) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1562 /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1563 /* 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
1564 /* in IEEE arithmetic. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1565 i = j = 18;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1566 if (i > nd0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1567 j += bc.dplen;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1568 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1569 if (--j <= bc.dp1 && j >= bc.dp0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1570 j = bc.dp0 - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1571 if (s0[j] != '0')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1572 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1573 --i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1574 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1575 e += nd - i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1576 nd = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1577 if (nd0 > nd)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1578 nd0 = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1579 if (nd < 9) { /* must recompute y */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1580 y = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1581 for(i = 0; i < nd0; ++i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1582 y = 10*y + s0[i] - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1583 for(j = bc.dp1; i < nd; ++i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1584 y = 10*y + s0[j++] - '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1585 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1586 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1587 bd0 = s2b(s0, nd0, nd, y, bc.dplen);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1588 if (bd0 == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1589 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1590
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1591 for(;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1592 bd = Balloc(bd0->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1593 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1594 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1595 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1596 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1597 Bcopy(bd, bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1598 bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1599 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1600 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1601 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1602 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1603 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1604 bs = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1605 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1606 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1607 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1608 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1609 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1610 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1611
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1612 if (e >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1613 bb2 = bb5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1614 bd2 = bd5 = e;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1615 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1616 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1617 bb2 = bb5 = -e;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1618 bd2 = bd5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1619 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1620 if (bbe >= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1621 bb2 += bbe;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1622 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1623 bd2 -= bbe;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1624 bs2 = bb2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1625 j = bbe - bc.scale;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1626 i = j + bbbits - 1; /* logb(rv) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1627 if (i < Emin) /* denormal */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1628 j += P - Emin;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1629 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1630 j = P + 1 - bbbits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1631 bb2 += j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1632 bd2 += j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1633 bd2 += bc.scale;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1634 i = bb2 < bd2 ? bb2 : bd2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1635 if (i > bs2)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1636 i = bs2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1637 if (i > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1638 bb2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1639 bd2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1640 bs2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1641 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1642 if (bb5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1643 bs = pow5mult(bs, bb5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1644 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1645 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1646 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1647 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1648 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1649 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1650 bb1 = mult(bs, bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1651 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1652 bb = bb1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1653 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1654 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1655 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1656 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1657 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1658 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1659 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1660 if (bb2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1661 bb = lshift(bb, bb2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1662 if (bb == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1663 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1664 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1665 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1666 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1667 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1668 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1669 if (bd5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1670 bd = pow5mult(bd, bd5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1671 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1672 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1673 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1674 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1675 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1676 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1677 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1678 if (bd2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1679 bd = lshift(bd, bd2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1680 if (bd == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1681 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1682 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1683 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1684 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1685 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1686 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1687 if (bs2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1688 bs = lshift(bs, bs2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1689 if (bs == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1690 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1691 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1692 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1693 goto failed_malloc;
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 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1696 delta = diff(bb, bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1697 if (delta == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1698 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1699 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1700 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1701 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1702 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1703 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1704 bc.dsign = delta->sign;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1705 delta->sign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1706 i = cmp(delta, bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1707 if (bc.nd > nd && i <= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1708 if (bc.dsign)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1709 break; /* Must use bigcomp(). */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1710 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1711 bc.nd = nd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1712 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
1713 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1714 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1715
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1716 if (i < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1717 /* 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
1718 * 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
1719 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1720 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1721 || (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
1722 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1723 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1724 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1725 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
1726 /* exact result */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1727 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1728 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1729 delta = lshift(delta,Log2P);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1730 if (delta == NULL) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1731 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1732 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1733 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1734 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1735 goto failed_malloc;
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 if (cmp(delta, bs) > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1738 goto drop_down;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1739 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1740 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1741 if (i == 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1742 /* exactly half-way between */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1743 if (bc.dsign) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1744 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
1745 && word1(&rv) == (
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1746 (bc.scale &&
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1747 (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
1748 (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
1749 0xffffffff)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1750 /*boundary case -- increment exponent*/
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1751 word0(&rv) = (word0(&rv) & Exp_mask)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1752 + Exp_msk1
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1753 ;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1754 word1(&rv) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1755 bc.dsign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1756 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1757 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1758 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1759 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
1760 drop_down:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1761 /* boundary case -- decrement exponent */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1762 if (bc.scale) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1763 L = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1764 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
1765 if (L > (P+2)*Exp_msk1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1766 /* round even ==> */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1767 /* accept rv */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1768 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1769 /* rv = smallest denormal */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1770 if (bc.nd >nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1771 bc.uflchk = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1772 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1773 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1774 goto undfl;
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 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
1778 word0(&rv) = L | Bndry_mask1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1779 word1(&rv) = 0xffffffff;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1780 break;
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 if (!(word1(&rv) & LSB))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1783 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1784 if (bc.dsign)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1785 dval(&rv) += ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1786 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1787 dval(&rv) -= ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1788 if (!dval(&rv)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1789 if (bc.nd >nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1790 bc.uflchk = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1791 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1792 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1793 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1794 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1795 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1796 bc.dsign = 1 - bc.dsign;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1797 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1798 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1799 if ((aadj = ratio(delta, bs)) <= 2.) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1800 if (bc.dsign)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1801 aadj = aadj1 = 1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1802 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
1803 if (word1(&rv) == Tiny1 && !word0(&rv)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1804 if (bc.nd >nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1805 bc.uflchk = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1806 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1807 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1808 goto undfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1809 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1810 aadj = 1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1811 aadj1 = -1.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1812 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1813 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1814 /* 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
1815 /* rounded down... */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1816
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1817 if (aadj < 2./FLT_RADIX)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1818 aadj = 1./FLT_RADIX;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1819 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1820 aadj *= 0.5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1821 aadj1 = -aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1822 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1823 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1824 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1825 aadj *= 0.5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1826 aadj1 = bc.dsign ? aadj : -aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1827 if (Flt_Rounds == 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1828 aadj1 += 0.5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1829 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1830 y = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1831
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1832 /* Check for overflow */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1833
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1834 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
1835 dval(&rv0) = dval(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1836 word0(&rv) -= P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1837 adj.d = aadj1 * ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1838 dval(&rv) += adj.d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1839 if ((word0(&rv) & Exp_mask) >=
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1840 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
1841 if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1842 goto ovfl;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1843 word0(&rv) = Big0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1844 word1(&rv) = Big1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1845 goto cont;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1846 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1847 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1848 word0(&rv) += P*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1849 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1850 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1851 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
1852 if (aadj <= 0x7fffffff) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1853 if ((z = (ULong)aadj) <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1854 z = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1855 aadj = z;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1856 aadj1 = bc.dsign ? aadj : -aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1857 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1858 dval(&aadj2) = aadj1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1859 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
1860 aadj1 = dval(&aadj2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1861 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1862 adj.d = aadj1 * ulp(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1863 dval(&rv) += adj.d;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1864 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1865 z = word0(&rv) & Exp_mask;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1866 if (bc.nd == nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1867 if (!bc.scale)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1868 if (y == z) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1869 /* Can we stop now? */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1870 L = (Long)aadj;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1871 aadj -= L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1872 /* The tolerances below are conservative. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1873 if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1874 if (aadj < .4999999 || aadj > .5000001)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1875 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1876 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1877 else if (aadj < .4999999/FLT_RADIX)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1878 break;
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 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1881 cont:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1882 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1883 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1884 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1885 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1886 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1887 Bfree(bb);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1888 Bfree(bd);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1889 Bfree(bs);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1890 Bfree(bd0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1891 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1892 if (bc.nd > nd) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1893 error = bigcomp(&rv, s0, &bc);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1894 if (error)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1895 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1896 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1897
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1898 if (bc.scale) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1899 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
1900 word1(&rv0) = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1901 dval(&rv) *= dval(&rv0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1902 /* try to avoid the bug of testing an 8087 register value */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1903 if (!(word0(&rv) & Exp_mask))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1904 errno = ERANGE;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1905 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1906 ret:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1907 if (se)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1908 *se = (char *)s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1909 return sign ? -dval(&rv) : dval(&rv);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1910
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1911 failed_malloc:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1912 if (se)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1913 *se = (char *)s00;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1914 errno = ENOMEM;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1915 return -1.0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1916 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1917
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1918 static char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1919 rv_alloc(int i)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1920 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1921 int j, k, *r;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1922
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1923 j = sizeof(ULong);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1924 for(k = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1925 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
1926 j <<= 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1927 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1928 r = (int*)Balloc(k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1929 if (r == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1930 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1931 *r = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1932 return (char *)(r+1);
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1935 static char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1936 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
1937 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1938 char *rv, *t;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1939
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1940 rv = rv_alloc(n);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1941 if (rv == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1942 return NULL;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1943 t = rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1944 while((*t = *s++)) t++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1945 if (rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1946 *rve = t;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1947 return rv;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1948 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1949
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1950 /* 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
1951 * 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
1952 * 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
1953 * when MULTIPLE_THREADS is not defined.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1954 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1955
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1956 void
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1957 _Py_dg_freedtoa(char *s)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1958 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1959 Bigint *b = (Bigint *)((int *)s - 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1960 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
1961 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1962 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1963
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1964 /* 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
1965 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1966 * 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
1967 * 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
1968 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1969 * Modifications:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1970 * 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
1971 * 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
1972 * 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
1973 * 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
1974 * 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
1975 * 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
1976 * 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
1977 * 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
1978 * 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
1979 * 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
1980 * 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
1981 * 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
1982 * inequality.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1983 * 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
1984 * quantities.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1985 * 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
1986 * 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
1987 * to multiple-precision integers.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1988 * 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
1989 * 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
1990 * 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
1991 * 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
1992 * 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
1993 * "uniformly" distributed input, the probability is
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1994 * 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
1995 * calculation.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1996 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1997
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
1998 /* 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
1999 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
2000 call to _Py_dg_freedtoa. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2001
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2002 char *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2003 _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
2004 int *decpt, int *sign, char **rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2005 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2006 /* 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
2007 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
2008 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
2009 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
2010 then *decpt is set to 9999.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2011
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2012 mode:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2013 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
2014 and rounded to nearest.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2015 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
2016 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
2017 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
2018 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
2019 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
2020 that trailing zeros are suppressed.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2021 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
2022 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
2023 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
2024 ndigits can be negative.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2025 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
2026 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
2027 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
2028 With IEEE arithmetic and compilation with
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2029 -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
2030 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
2031 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
2032 fast floating-point estimate (if applicable).
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2033
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2034 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
2035
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2036 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
2037 to hold the suppressed trailing zeros.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2038 */
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 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
2041 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
2042 spec_case, try_quick;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2043 Long L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2044 int denorm;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2045 ULong x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2046 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
2047 U d2, eps, u;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2048 double ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2049 char *s, *s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2050
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2051 /* 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
2052 cleanup easier on error */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2053 mlo = mhi = b = S = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2054 s0 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2055
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2056 u.d = dd;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2057 if (word0(&u) & Sign_bit) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2058 /* 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
2059 *sign = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2060 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
2061 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2062 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2063 *sign = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2064
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2065 /* 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
2066 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
2067 {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2068 /* Infinity or NaN */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2069 *decpt = 9999;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2070 if (!word1(&u) && !(word0(&u) & 0xfffff))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2071 return nrv_alloc("Infinity", rve, 8);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2072 return nrv_alloc("NaN", rve, 3);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2073 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2074 if (!dval(&u)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2075 *decpt = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2076 return nrv_alloc("0", rve, 1);
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2079 /* 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
2080 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
2081 b = d2b(&u, &be, &bbits);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2082 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2083 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2084 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
2085 dval(&d2) = dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2086 word0(&d2) &= Frac_mask1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2087 word0(&d2) |= Exp_11;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2088
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2089 /* 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
2090 * log10(x) = log(x) / log(10)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2091 * ~=~ 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
2092 * 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
2093 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2094 * 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
2095 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2096 * k = (i - Bias)*0.301029995663981
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2097 * + ( (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
2098 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2099 * 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
2100 * 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
2101 * 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
2102 * 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
2103 * (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
2104 * 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
2105 * 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
2106 * 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
2107 * (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
2108 * but this is probably not worthwhile.)
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2111 i -= Bias;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2112 denorm = 0;
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 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2115 /* d is denormalized */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2116
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2117 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
2118 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
2119 : word1(&u) << (32 - i);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2120 dval(&d2) = x;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2121 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
2122 i -= (Bias + (P-1) - 1) + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2123 denorm = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2124 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2125 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
2126 i*0.301029995663981;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2127 k = (int)ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2128 if (ds < 0. && ds != k)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2129 k--; /* want k = floor(ds) */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2130 k_check = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2131 if (k >= 0 && k <= Ten_pmax) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2132 if (dval(&u) < tens[k])
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2133 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2134 k_check = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2135 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2136 j = bbits - i - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2137 if (j >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2138 b2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2139 s2 = j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2140 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2141 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2142 b2 = -j;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2143 s2 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2144 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2145 if (k >= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2146 b5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2147 s5 = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2148 s2 += k;
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 b2 -= k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2152 b5 = -k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2153 s5 = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2154 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2155 if (mode < 0 || mode > 9)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2156 mode = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2157
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2158 try_quick = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2159
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2160 if (mode > 5) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2161 mode -= 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2162 try_quick = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2163 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2164 leftright = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2165 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
2166 /* silence erroneous "gcc -Wall" warning. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2167 switch(mode) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2168 case 0:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2169 case 1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2170 i = 18;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2171 ndigits = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2172 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2173 case 2:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2174 leftright = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2175 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2176 case 4:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2177 if (ndigits <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2178 ndigits = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2179 ilim = ilim1 = i = ndigits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2180 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2181 case 3:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2182 leftright = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2183 /* no break */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2184 case 5:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2185 i = ndigits + k + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2186 ilim = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2187 ilim1 = i - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2188 if (i <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2189 i = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2190 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2191 s0 = rv_alloc(i);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2192 if (s0 == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2193 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2194 s = s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2195
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2196
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2197 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
2198
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2199 /* 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
2200
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2201 i = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2202 dval(&d2) = dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2203 k0 = k;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2204 ilim0 = ilim;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2205 ieps = 2; /* conservative */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2206 if (k > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2207 ds = tens[k&0xf];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2208 j = k >> 4;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2209 if (j & Bletch) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2210 /* prevent overflows */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2211 j &= Bletch - 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2212 dval(&u) /= bigtens[n_bigtens-1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2213 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2214 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2215 for(; j; j >>= 1, i++)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2216 if (j & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2217 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2218 ds *= bigtens[i];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2219 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2220 dval(&u) /= ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2221 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2222 else if ((j1 = -k)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2223 dval(&u) *= tens[j1 & 0xf];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2224 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
2225 if (j & 1) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2226 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2227 dval(&u) *= bigtens[i];
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 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2230 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
2231 if (ilim1 <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2232 goto fast_failed;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2233 ilim = ilim1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2234 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2235 dval(&u) *= 10.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2236 ieps++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2237 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2238 dval(&eps) = ieps*dval(&u) + 7.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2239 word0(&eps) -= (P-1)*Exp_msk1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2240 if (ilim == 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2241 S = mhi = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2242 dval(&u) -= 5.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2243 if (dval(&u) > dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2244 goto one_digit;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2245 if (dval(&u) < -dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2246 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2247 goto fast_failed;
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 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2250 /* Use Steele & White method of only
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2251 * generating digits needed.
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2252 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2253 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
2254 for(i = 0;;) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2255 L = (Long)dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2256 dval(&u) -= L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2257 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2258 if (dval(&u) < dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2259 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2260 if (1. - dval(&u) < dval(&eps))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2261 goto bump_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2262 if (++i >= ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2263 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2264 dval(&eps) *= 10.;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2265 dval(&u) *= 10.;
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 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2268 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2269 /* 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
2270 dval(&eps) *= tens[ilim-1];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2271 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
2272 L = (Long)(dval(&u));
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2273 if (!(dval(&u) -= L))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2274 ilim = i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2275 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2276 if (i == ilim) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2277 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
2278 goto bump_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2279 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
2280 while(*--s == '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2281 s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2282 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2283 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2284 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2285 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2286 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2287 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2288 fast_failed:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2289 s = s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2290 dval(&u) = dval(&d2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2291 k = k0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2292 ilim = ilim0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2293 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2294
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2295 /* Do we have a "small" integer? */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2296
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2297 if (be >= 0 && k <= Int_max) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2298 /* Yes. */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2299 ds = tens[k];
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2300 if (ndigits < 0 && ilim <= 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2301 S = mhi = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2302 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
2303 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2304 goto one_digit;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2305 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2306 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
2307 L = (Long)(dval(&u) / ds);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2308 dval(&u) -= L*ds;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2309 *s++ = '0' + (int)L;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2310 if (!dval(&u)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2311 break;
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 if (i == ilim) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2314 dval(&u) += dval(&u);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2315 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
2316 bump_up:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2317 while(*--s == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2318 if (s == s0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2319 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2320 *s = '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2321 break;
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 ++*s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2324 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2325 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2326 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2327 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2328 goto ret1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2329 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2330
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2331 m2 = b2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2332 m5 = b5;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2333 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2334 i =
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2335 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
2336 1 + P - bbits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2337 b2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2338 s2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2339 mhi = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2340 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2341 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2342 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2343 if (m2 > 0 && s2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2344 i = m2 < s2 ? m2 : s2;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2345 b2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2346 m2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2347 s2 -= i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2348 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2349 if (b5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2350 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2351 if (m5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2352 mhi = pow5mult(mhi, m5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2353 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2354 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2355 b1 = mult(mhi, b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2356 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2357 b = b1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2358 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2359 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2360 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2361 if ((j = b5 - m5)) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2362 b = pow5mult(b, j);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2363 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2364 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2365 }
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 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2368 b = pow5mult(b, b5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2369 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2370 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2371 }
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 S = i2b(1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2374 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2375 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2376 if (s5 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2377 S = pow5mult(S, s5);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2378 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2379 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2380 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2381
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2382 /* 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
2383
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2384 spec_case = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2385 if ((mode < 2 || leftright)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2386 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2387 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
2388 && word0(&u) & (Exp_mask & ~Exp_msk1)
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 /* The special case */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2391 b2 += Log2P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2392 s2 += Log2P;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2393 spec_case = 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2394 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2395 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2396
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2397 /* Arrange for convenient computation of quotients:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2398 * 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
2399 *
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2400 * 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
2401 * 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
2402 * 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
2403 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2404 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2405 i = 32 - i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2406 #define iInc 28
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2407 i = dshift(S, s2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2408 b2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2409 m2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2410 s2 += i;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2411 if (b2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2412 b = lshift(b, b2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2413 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2414 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2415 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2416 if (s2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2417 S = lshift(S, s2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2418 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2419 goto failed_malloc;
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 if (k_check) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2422 if (cmp(b,S) < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2423 k--;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2424 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
2425 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2426 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2427 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2428 mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2429 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2430 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2431 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2432 ilim = ilim1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2433 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2434 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2435 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
2436 if (ilim < 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2437 /* no digits, fcvt style */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2438 no_digits:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2439 k = -1 - ndigits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2440 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2441 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2442 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2443 S = multadd(S, 5, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2444 if (S == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2445 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2446 if (cmp(b, S) <= 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2447 goto no_digits;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2448 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2449 one_digit:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2450 *s++ = '1';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2451 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2452 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2453 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2454 if (leftright) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2455 if (m2 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2456 mhi = lshift(mhi, m2);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2457 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2458 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2459 }
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 /* Compute mlo -- check for special case
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2462 * 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
2463 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2464
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2465 mlo = mhi;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2466 if (spec_case) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2467 mhi = Balloc(mhi->k);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2468 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2469 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2470 Bcopy(mhi, mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2471 mhi = lshift(mhi, Log2P);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2472 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2473 goto failed_malloc;
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
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2476 for(i = 1;;i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2477 dig = quorem(b,S) + '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2478 /* 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
2479 * that will round to d?
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2480 */
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2481 j = cmp(b, mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2482 delta = diff(S, mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2483 if (delta == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2484 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2485 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
2486 Bfree(delta);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2487 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
2488 ) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2489 if (dig == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2490 goto round_9_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2491 if (j > 0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2492 dig++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2493 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2494 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2495 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2496 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
2497 && !(word1(&u) & 1)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2498 )) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2499 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
2500 goto accept_dig;
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 if (j1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2503 b = lshift(b, 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2504 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2505 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2506 j1 = cmp(b, S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2507 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
2508 && dig++ == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2509 goto round_9_up;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2510 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2511 accept_dig:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2512 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2513 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2514 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2515 if (j1 > 0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2516 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
2517 round_9_up:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2518 *s++ = '9';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2519 goto roundoff;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2520 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2521 *s++ = dig + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2522 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2523 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2524 *s++ = dig;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2525 if (i == ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2526 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2527 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2528 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2529 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2530 if (mlo == mhi) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2531 mlo = mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2532 if (mlo == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2533 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2534 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2535 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2536 mlo = multadd(mlo, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2537 if (mlo == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2538 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2539 mhi = multadd(mhi, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2540 if (mhi == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2541 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2542 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2543 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2544 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2545 else
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2546 for(i = 1;; i++) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2547 *s++ = dig = quorem(b,S) + '0';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2548 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
2549 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2550 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2551 if (i >= ilim)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2552 break;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2553 b = multadd(b, 10, 0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2554 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2555 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2556 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2557
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2558 /* Round off last digit */
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 b = lshift(b, 1);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2561 if (b == NULL)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2562 goto failed_malloc;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2563 j = cmp(b, S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2564 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
2565 roundoff:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2566 while(*--s == '9')
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2567 if (s == s0) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2568 k++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2569 *s++ = '1';
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2570 goto ret;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2571 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2572 ++*s++;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2573 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2574 else {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2575 while(*--s == '0');
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2576 s++;
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 ret:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2579 Bfree(S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2580 if (mhi) {
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2581 if (mlo && mlo != mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2582 Bfree(mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2583 Bfree(mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2584 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2585 ret1:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2586 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2587 *s = 0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2588 *decpt = k + 1;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2589 if (rve)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2590 *rve = s;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2591 return s0;
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2592 failed_malloc:
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2593 if (S)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2594 Bfree(S);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2595 if (mlo && mlo != mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2596 Bfree(mlo);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2597 if (mhi)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2598 Bfree(mhi);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2599 if (b)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2600 Bfree(b);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2601 if (s0)
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2602 _Py_dg_freedtoa(s0);
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2603 return NULL;
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 #ifdef __cplusplus
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2606 }
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2607 #endif
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2608
2146bbdf4ed7 Issue #7117: Prepare for backport of py3k float repr.
Mark Dickinson <dickinsm@gmail.com>
parents:
diff changeset
2609 #endif /* PY_NO_SHORT_FLOAT_REPR */