Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Python/dtoa.c
12 views
1
/****************************************************************
2
*
3
* The author of this software is David M. Gay.
4
*
5
* Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6
*
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose without fee is hereby granted, provided that this entire notice
9
* is included in all copies of any software which is or includes a copy
10
* or modification of this software and in all copies of the supporting
11
* documentation for such software.
12
*
13
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17
*
18
***************************************************************/
19
20
/****************************************************************
21
* This is dtoa.c by David M. Gay, downloaded from
22
* http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
23
* inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
24
*
25
* Please remember to check http://www.netlib.org/fp regularly (and especially
26
* before any Python release) for bugfixes and updates.
27
*
28
* The major modifications from Gay's original code are as follows:
29
*
30
* 0. The original code has been specialized to Python's needs by removing
31
* many of the #ifdef'd sections. In particular, code to support VAX and
32
* IBM floating-point formats, hex NaNs, hex floats, locale-aware
33
* treatment of the decimal point, and setting of the inexact flag have
34
* been removed.
35
*
36
* 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
37
*
38
* 2. The public functions strtod, dtoa and freedtoa all now have
39
* a _Py_dg_ prefix.
40
*
41
* 3. Instead of assuming that PyMem_Malloc always succeeds, we thread
42
* PyMem_Malloc failures through the code. The functions
43
*
44
* Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
45
*
46
* of return type *Bigint all return NULL to indicate a malloc failure.
47
* Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
48
* failure. bigcomp now has return type int (it used to be void) and
49
* returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL
50
* on failure. _Py_dg_strtod indicates failure due to malloc failure
51
* by returning -1.0, setting errno=ENOMEM and *se to s00.
52
*
53
* 4. The static variable dtoa_result has been removed. Callers of
54
* _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free
55
* the memory allocated by _Py_dg_dtoa.
56
*
57
* 5. The code has been reformatted to better fit with Python's
58
* C style guide (PEP 7).
59
*
60
* 6. A bug in the memory allocation has been fixed: to avoid FREEing memory
61
* that hasn't been MALLOC'ed, private_mem should only be used when k <=
62
* Kmax.
63
*
64
* 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
65
* leading whitespace.
66
*
67
* 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
68
* fixed. (bugs.python.org/issue40780)
69
*
70
***************************************************************/
71
72
/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
73
* at acm dot org, with " at " changed at "@" and " dot " changed to ".").
74
* Please report bugs for this modified version using the Python issue tracker
75
* (http://bugs.python.org). */
76
77
/* On a machine with IEEE extended-precision registers, it is
78
* necessary to specify double-precision (53-bit) rounding precision
79
* before invoking strtod or dtoa. If the machine uses (the equivalent
80
* of) Intel 80x87 arithmetic, the call
81
* _control87(PC_53, MCW_PC);
82
* does this with many compilers. Whether this or another call is
83
* appropriate depends on the compiler; for this to work, it may be
84
* necessary to #include "float.h" or another system-dependent header
85
* file.
86
*/
87
88
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
89
*
90
* This strtod returns a nearest machine number to the input decimal
91
* string (or sets errno to ERANGE). With IEEE arithmetic, ties are
92
* broken by the IEEE round-even rule. Otherwise ties are broken by
93
* biased rounding (add half and chop).
94
*
95
* Inspired loosely by William D. Clinger's paper "How to Read Floating
96
* Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
97
*
98
* Modifications:
99
*
100
* 1. We only require IEEE, IBM, or VAX double-precision
101
* arithmetic (not IEEE double-extended).
102
* 2. We get by with floating-point arithmetic in a case that
103
* Clinger missed -- when we're computing d * 10^n
104
* for a small integer d and the integer n is not too
105
* much larger than 22 (the maximum integer k for which
106
* we can represent 10^k exactly), we may be able to
107
* compute (d*10^k) * 10^(e-k) with just one roundoff.
108
* 3. Rather than a bit-at-a-time adjustment of the binary
109
* result in the hard case, we use floating-point
110
* arithmetic to determine the adjustment to within
111
* one bit; only in really hard cases do we need to
112
* compute a second residual.
113
* 4. Because of 3., we don't need a large table of powers of 10
114
* for ten-to-e (just some small tables, e.g. of 10^k
115
* for 0 <= k <= 22).
116
*/
117
118
/* Linking of Python's #defines to Gay's #defines starts here. */
119
120
#include "Python.h"
121
#include "pycore_dtoa.h" // _PY_SHORT_FLOAT_REPR
122
#include "pycore_pystate.h" // _PyInterpreterState_GET()
123
#include <stdlib.h> // exit()
124
125
/* if _PY_SHORT_FLOAT_REPR == 0, then don't even try to compile
126
the following code */
127
#if _PY_SHORT_FLOAT_REPR == 1
128
129
#include "float.h"
130
131
#define MALLOC PyMem_Malloc
132
#define FREE PyMem_Free
133
134
/* This code should also work for ARM mixed-endian format on little-endian
135
machines, where doubles have byte order 45670123 (in increasing address
136
order, 0 being the least significant byte). */
137
#ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
138
# define IEEE_8087
139
#endif
140
#if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
141
defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
142
# define IEEE_MC68k
143
#endif
144
#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
145
#error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
146
#endif
147
148
/* The code below assumes that the endianness of integers matches the
149
endianness of the two 32-bit words of a double. Check this. */
150
#if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
151
defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
152
#error "doubles and ints have incompatible endianness"
153
#endif
154
155
#if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
156
#error "doubles and ints have incompatible endianness"
157
#endif
158
159
160
// ULong is defined in pycore_dtoa.h.
161
typedef int32_t Long;
162
typedef uint64_t ULLong;
163
164
#undef DEBUG
165
#ifdef Py_DEBUG
166
#define DEBUG
167
#endif
168
169
/* End Python #define linking */
170
171
#ifdef DEBUG
172
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
173
#endif
174
175
#ifdef __cplusplus
176
extern "C" {
177
#endif
178
179
typedef union { double d; ULong L[2]; } U;
180
181
#ifdef IEEE_8087
182
#define word0(x) (x)->L[1]
183
#define word1(x) (x)->L[0]
184
#else
185
#define word0(x) (x)->L[0]
186
#define word1(x) (x)->L[1]
187
#endif
188
#define dval(x) (x)->d
189
190
#ifndef STRTOD_DIGLIM
191
#define STRTOD_DIGLIM 40
192
#endif
193
194
/* maximum permitted exponent value for strtod; exponents larger than
195
MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
196
should fit into an int. */
197
#ifndef MAX_ABS_EXP
198
#define MAX_ABS_EXP 1100000000U
199
#endif
200
/* Bound on length of pieces of input strings in _Py_dg_strtod; specifically,
201
this is used to bound the total number of digits ignoring leading zeros and
202
the number of digits that follow the decimal point. Ideally, MAX_DIGITS
203
should satisfy MAX_DIGITS + 400 < MAX_ABS_EXP; that ensures that the
204
exponent clipping in _Py_dg_strtod can't affect the value of the output. */
205
#ifndef MAX_DIGITS
206
#define MAX_DIGITS 1000000000U
207
#endif
208
209
/* Guard against trying to use the above values on unusual platforms with ints
210
* of width less than 32 bits. */
211
#if MAX_ABS_EXP > INT_MAX
212
#error "MAX_ABS_EXP should fit in an int"
213
#endif
214
#if MAX_DIGITS > INT_MAX
215
#error "MAX_DIGITS should fit in an int"
216
#endif
217
218
/* The following definition of Storeinc is appropriate for MIPS processors.
219
* An alternative that might be better on some machines is
220
* #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
221
*/
222
#if defined(IEEE_8087)
223
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
224
((unsigned short *)a)[0] = (unsigned short)c, a++)
225
#else
226
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
227
((unsigned short *)a)[1] = (unsigned short)c, a++)
228
#endif
229
230
/* #define P DBL_MANT_DIG */
231
/* Ten_pmax = floor(P*log(2)/log(5)) */
232
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
233
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
234
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
235
236
#define Exp_shift 20
237
#define Exp_shift1 20
238
#define Exp_msk1 0x100000
239
#define Exp_msk11 0x100000
240
#define Exp_mask 0x7ff00000
241
#define P 53
242
#define Nbits 53
243
#define Bias 1023
244
#define Emax 1023
245
#define Emin (-1022)
246
#define Etiny (-1074) /* smallest denormal is 2**Etiny */
247
#define Exp_1 0x3ff00000
248
#define Exp_11 0x3ff00000
249
#define Ebits 11
250
#define Frac_mask 0xfffff
251
#define Frac_mask1 0xfffff
252
#define Ten_pmax 22
253
#define Bletch 0x10
254
#define Bndry_mask 0xfffff
255
#define Bndry_mask1 0xfffff
256
#define Sign_bit 0x80000000
257
#define Log2P 1
258
#define Tiny0 0
259
#define Tiny1 1
260
#define Quick_max 14
261
#define Int_max 14
262
263
#ifndef Flt_Rounds
264
#ifdef FLT_ROUNDS
265
#define Flt_Rounds FLT_ROUNDS
266
#else
267
#define Flt_Rounds 1
268
#endif
269
#endif /*Flt_Rounds*/
270
271
#define Rounding Flt_Rounds
272
273
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
274
#define Big1 0xffffffff
275
276
/* Bits of the representation of positive infinity. */
277
278
#define POSINF_WORD0 0x7ff00000
279
#define POSINF_WORD1 0
280
281
/* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
282
283
typedef struct BCinfo BCinfo;
284
struct
285
BCinfo {
286
int e0, nd, nd0, scale;
287
};
288
289
#define FFFFFFFF 0xffffffffUL
290
291
/* struct Bigint is used to represent arbitrary-precision integers. These
292
integers are stored in sign-magnitude format, with the magnitude stored as
293
an array of base 2**32 digits. Bigints are always normalized: if x is a
294
Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
295
296
The Bigint fields are as follows:
297
298
- next is a header used by Balloc and Bfree to keep track of lists
299
of freed Bigints; it's also used for the linked list of
300
powers of 5 of the form 5**2**i used by pow5mult.
301
- k indicates which pool this Bigint was allocated from
302
- maxwds is the maximum number of words space was allocated for
303
(usually maxwds == 2**k)
304
- sign is 1 for negative Bigints, 0 for positive. The sign is unused
305
(ignored on inputs, set to 0 on outputs) in almost all operations
306
involving Bigints: a notable exception is the diff function, which
307
ignores signs on inputs but sets the sign of the output correctly.
308
- wds is the actual number of significant words
309
- x contains the vector of words (digits) for this Bigint, from least
310
significant (x[0]) to most significant (x[wds-1]).
311
*/
312
313
// struct Bigint is defined in pycore_dtoa.h.
314
typedef struct Bigint Bigint;
315
316
#ifndef Py_USING_MEMORY_DEBUGGER
317
318
/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
319
of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
320
1 << k. These pools are maintained as linked lists, with freelist[k]
321
pointing to the head of the list for pool k.
322
323
On allocation, if there's no free slot in the appropriate pool, MALLOC is
324
called to get more memory. This memory is not returned to the system until
325
Python quits. There's also a private memory pool that's allocated from
326
in preference to using MALLOC.
327
328
For Bigints with more than (1 << Kmax) digits (which implies at least 1233
329
decimal digits), memory is directly allocated using MALLOC, and freed using
330
FREE.
331
332
XXX: it would be easy to bypass this memory-management system and
333
translate each call to Balloc into a call to PyMem_Malloc, and each
334
Bfree to PyMem_Free. Investigate whether this has any significant
335
performance on impact. */
336
337
#define freelist interp->dtoa.freelist
338
#define private_mem interp->dtoa.preallocated
339
#define pmem_next interp->dtoa.preallocated_next
340
341
/* Allocate space for a Bigint with up to 1<<k digits */
342
343
static Bigint *
344
Balloc(int k)
345
{
346
int x;
347
Bigint *rv;
348
unsigned int len;
349
PyInterpreterState *interp = _PyInterpreterState_GET();
350
351
if (k <= Bigint_Kmax && (rv = freelist[k]))
352
freelist[k] = rv->next;
353
else {
354
x = 1 << k;
355
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
356
/sizeof(double);
357
if (k <= Bigint_Kmax &&
358
pmem_next - private_mem + len <= (Py_ssize_t)Bigint_PREALLOC_SIZE
359
) {
360
rv = (Bigint*)pmem_next;
361
pmem_next += len;
362
}
363
else {
364
rv = (Bigint*)MALLOC(len*sizeof(double));
365
if (rv == NULL)
366
return NULL;
367
}
368
rv->k = k;
369
rv->maxwds = x;
370
}
371
rv->sign = rv->wds = 0;
372
return rv;
373
}
374
375
/* Free a Bigint allocated with Balloc */
376
377
static void
378
Bfree(Bigint *v)
379
{
380
if (v) {
381
if (v->k > Bigint_Kmax)
382
FREE((void*)v);
383
else {
384
PyInterpreterState *interp = _PyInterpreterState_GET();
385
v->next = freelist[v->k];
386
freelist[v->k] = v;
387
}
388
}
389
}
390
391
#undef pmem_next
392
#undef private_mem
393
#undef freelist
394
395
#else
396
397
/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
398
PyMem_Free directly in place of the custom memory allocation scheme above.
399
These are provided for the benefit of memory debugging tools like
400
Valgrind. */
401
402
/* Allocate space for a Bigint with up to 1<<k digits */
403
404
static Bigint *
405
Balloc(int k)
406
{
407
int x;
408
Bigint *rv;
409
unsigned int len;
410
411
x = 1 << k;
412
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
413
/sizeof(double);
414
415
rv = (Bigint*)MALLOC(len*sizeof(double));
416
if (rv == NULL)
417
return NULL;
418
419
rv->k = k;
420
rv->maxwds = x;
421
rv->sign = rv->wds = 0;
422
return rv;
423
}
424
425
/* Free a Bigint allocated with Balloc */
426
427
static void
428
Bfree(Bigint *v)
429
{
430
if (v) {
431
FREE((void*)v);
432
}
433
}
434
435
#endif /* Py_USING_MEMORY_DEBUGGER */
436
437
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
438
y->wds*sizeof(Long) + 2*sizeof(int))
439
440
/* Multiply a Bigint b by m and add a. Either modifies b in place and returns
441
a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
442
On failure, return NULL. In this case, b will have been already freed. */
443
444
static Bigint *
445
multadd(Bigint *b, int m, int a) /* multiply by m and add a */
446
{
447
int i, wds;
448
ULong *x;
449
ULLong carry, y;
450
Bigint *b1;
451
452
wds = b->wds;
453
x = b->x;
454
i = 0;
455
carry = a;
456
do {
457
y = *x * (ULLong)m + carry;
458
carry = y >> 32;
459
*x++ = (ULong)(y & FFFFFFFF);
460
}
461
while(++i < wds);
462
if (carry) {
463
if (wds >= b->maxwds) {
464
b1 = Balloc(b->k+1);
465
if (b1 == NULL){
466
Bfree(b);
467
return NULL;
468
}
469
Bcopy(b1, b);
470
Bfree(b);
471
b = b1;
472
}
473
b->x[wds++] = (ULong)carry;
474
b->wds = wds;
475
}
476
return b;
477
}
478
479
/* convert a string s containing nd decimal digits (possibly containing a
480
decimal separator at position nd0, which is ignored) to a Bigint. This
481
function carries on where the parsing code in _Py_dg_strtod leaves off: on
482
entry, y9 contains the result of converting the first 9 digits. Returns
483
NULL on failure. */
484
485
static Bigint *
486
s2b(const char *s, int nd0, int nd, ULong y9)
487
{
488
Bigint *b;
489
int i, k;
490
Long x, y;
491
492
x = (nd + 8) / 9;
493
for(k = 0, y = 1; x > y; y <<= 1, k++) ;
494
b = Balloc(k);
495
if (b == NULL)
496
return NULL;
497
b->x[0] = y9;
498
b->wds = 1;
499
500
if (nd <= 9)
501
return b;
502
503
s += 9;
504
for (i = 9; i < nd0; i++) {
505
b = multadd(b, 10, *s++ - '0');
506
if (b == NULL)
507
return NULL;
508
}
509
s++;
510
for(; i < nd; i++) {
511
b = multadd(b, 10, *s++ - '0');
512
if (b == NULL)
513
return NULL;
514
}
515
return b;
516
}
517
518
/* count leading 0 bits in the 32-bit integer x. */
519
520
static int
521
hi0bits(ULong x)
522
{
523
int k = 0;
524
525
if (!(x & 0xffff0000)) {
526
k = 16;
527
x <<= 16;
528
}
529
if (!(x & 0xff000000)) {
530
k += 8;
531
x <<= 8;
532
}
533
if (!(x & 0xf0000000)) {
534
k += 4;
535
x <<= 4;
536
}
537
if (!(x & 0xc0000000)) {
538
k += 2;
539
x <<= 2;
540
}
541
if (!(x & 0x80000000)) {
542
k++;
543
if (!(x & 0x40000000))
544
return 32;
545
}
546
return k;
547
}
548
549
/* count trailing 0 bits in the 32-bit integer y, and shift y right by that
550
number of bits. */
551
552
static int
553
lo0bits(ULong *y)
554
{
555
int k;
556
ULong x = *y;
557
558
if (x & 7) {
559
if (x & 1)
560
return 0;
561
if (x & 2) {
562
*y = x >> 1;
563
return 1;
564
}
565
*y = x >> 2;
566
return 2;
567
}
568
k = 0;
569
if (!(x & 0xffff)) {
570
k = 16;
571
x >>= 16;
572
}
573
if (!(x & 0xff)) {
574
k += 8;
575
x >>= 8;
576
}
577
if (!(x & 0xf)) {
578
k += 4;
579
x >>= 4;
580
}
581
if (!(x & 0x3)) {
582
k += 2;
583
x >>= 2;
584
}
585
if (!(x & 1)) {
586
k++;
587
x >>= 1;
588
if (!x)
589
return 32;
590
}
591
*y = x;
592
return k;
593
}
594
595
/* convert a small nonnegative integer to a Bigint */
596
597
static Bigint *
598
i2b(int i)
599
{
600
Bigint *b;
601
602
b = Balloc(1);
603
if (b == NULL)
604
return NULL;
605
b->x[0] = i;
606
b->wds = 1;
607
return b;
608
}
609
610
/* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
611
the signs of a and b. */
612
613
static Bigint *
614
mult(Bigint *a, Bigint *b)
615
{
616
Bigint *c;
617
int k, wa, wb, wc;
618
ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
619
ULong y;
620
ULLong carry, z;
621
622
if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
623
c = Balloc(0);
624
if (c == NULL)
625
return NULL;
626
c->wds = 1;
627
c->x[0] = 0;
628
return c;
629
}
630
631
if (a->wds < b->wds) {
632
c = a;
633
a = b;
634
b = c;
635
}
636
k = a->k;
637
wa = a->wds;
638
wb = b->wds;
639
wc = wa + wb;
640
if (wc > a->maxwds)
641
k++;
642
c = Balloc(k);
643
if (c == NULL)
644
return NULL;
645
for(x = c->x, xa = x + wc; x < xa; x++)
646
*x = 0;
647
xa = a->x;
648
xae = xa + wa;
649
xb = b->x;
650
xbe = xb + wb;
651
xc0 = c->x;
652
for(; xb < xbe; xc0++) {
653
if ((y = *xb++)) {
654
x = xa;
655
xc = xc0;
656
carry = 0;
657
do {
658
z = *x++ * (ULLong)y + *xc + carry;
659
carry = z >> 32;
660
*xc++ = (ULong)(z & FFFFFFFF);
661
}
662
while(x < xae);
663
*xc = (ULong)carry;
664
}
665
}
666
for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
667
c->wds = wc;
668
return c;
669
}
670
671
#ifndef Py_USING_MEMORY_DEBUGGER
672
673
/* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
674
failure; if the returned pointer is distinct from b then the original
675
Bigint b will have been Bfree'd. Ignores the sign of b. */
676
677
static Bigint *
678
pow5mult(Bigint *b, int k)
679
{
680
Bigint *b1, *p5, *p51;
681
int i;
682
static const int p05[3] = { 5, 25, 125 };
683
684
if ((i = k & 3)) {
685
b = multadd(b, p05[i-1], 0);
686
if (b == NULL)
687
return NULL;
688
}
689
690
if (!(k >>= 2))
691
return b;
692
PyInterpreterState *interp = _PyInterpreterState_GET();
693
p5 = interp->dtoa.p5s;
694
if (!p5) {
695
/* first time */
696
p5 = i2b(625);
697
if (p5 == NULL) {
698
Bfree(b);
699
return NULL;
700
}
701
interp->dtoa.p5s = p5;
702
p5->next = 0;
703
}
704
for(;;) {
705
if (k & 1) {
706
b1 = mult(b, p5);
707
Bfree(b);
708
b = b1;
709
if (b == NULL)
710
return NULL;
711
}
712
if (!(k >>= 1))
713
break;
714
p51 = p5->next;
715
if (!p51) {
716
p51 = mult(p5,p5);
717
if (p51 == NULL) {
718
Bfree(b);
719
return NULL;
720
}
721
p51->next = 0;
722
p5->next = p51;
723
}
724
p5 = p51;
725
}
726
return b;
727
}
728
729
#else
730
731
/* Version of pow5mult that doesn't cache powers of 5. Provided for
732
the benefit of memory debugging tools like Valgrind. */
733
734
static Bigint *
735
pow5mult(Bigint *b, int k)
736
{
737
Bigint *b1, *p5, *p51;
738
int i;
739
static const int p05[3] = { 5, 25, 125 };
740
741
if ((i = k & 3)) {
742
b = multadd(b, p05[i-1], 0);
743
if (b == NULL)
744
return NULL;
745
}
746
747
if (!(k >>= 2))
748
return b;
749
p5 = i2b(625);
750
if (p5 == NULL) {
751
Bfree(b);
752
return NULL;
753
}
754
755
for(;;) {
756
if (k & 1) {
757
b1 = mult(b, p5);
758
Bfree(b);
759
b = b1;
760
if (b == NULL) {
761
Bfree(p5);
762
return NULL;
763
}
764
}
765
if (!(k >>= 1))
766
break;
767
p51 = mult(p5, p5);
768
Bfree(p5);
769
p5 = p51;
770
if (p5 == NULL) {
771
Bfree(b);
772
return NULL;
773
}
774
}
775
Bfree(p5);
776
return b;
777
}
778
779
#endif /* Py_USING_MEMORY_DEBUGGER */
780
781
/* shift a Bigint b left by k bits. Return a pointer to the shifted result,
782
or NULL on failure. If the returned pointer is distinct from b then the
783
original b will have been Bfree'd. Ignores the sign of b. */
784
785
static Bigint *
786
lshift(Bigint *b, int k)
787
{
788
int i, k1, n, n1;
789
Bigint *b1;
790
ULong *x, *x1, *xe, z;
791
792
if (!k || (!b->x[0] && b->wds == 1))
793
return b;
794
795
n = k >> 5;
796
k1 = b->k;
797
n1 = n + b->wds + 1;
798
for(i = b->maxwds; n1 > i; i <<= 1)
799
k1++;
800
b1 = Balloc(k1);
801
if (b1 == NULL) {
802
Bfree(b);
803
return NULL;
804
}
805
x1 = b1->x;
806
for(i = 0; i < n; i++)
807
*x1++ = 0;
808
x = b->x;
809
xe = x + b->wds;
810
if (k &= 0x1f) {
811
k1 = 32 - k;
812
z = 0;
813
do {
814
*x1++ = *x << k | z;
815
z = *x++ >> k1;
816
}
817
while(x < xe);
818
if ((*x1 = z))
819
++n1;
820
}
821
else do
822
*x1++ = *x++;
823
while(x < xe);
824
b1->wds = n1 - 1;
825
Bfree(b);
826
return b1;
827
}
828
829
/* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
830
1 if a > b. Ignores signs of a and b. */
831
832
static int
833
cmp(Bigint *a, Bigint *b)
834
{
835
ULong *xa, *xa0, *xb, *xb0;
836
int i, j;
837
838
i = a->wds;
839
j = b->wds;
840
#ifdef DEBUG
841
if (i > 1 && !a->x[i-1])
842
Bug("cmp called with a->x[a->wds-1] == 0");
843
if (j > 1 && !b->x[j-1])
844
Bug("cmp called with b->x[b->wds-1] == 0");
845
#endif
846
if (i -= j)
847
return i;
848
xa0 = a->x;
849
xa = xa0 + j;
850
xb0 = b->x;
851
xb = xb0 + j;
852
for(;;) {
853
if (*--xa != *--xb)
854
return *xa < *xb ? -1 : 1;
855
if (xa <= xa0)
856
break;
857
}
858
return 0;
859
}
860
861
/* Take the difference of Bigints a and b, returning a new Bigint. Returns
862
NULL on failure. The signs of a and b are ignored, but the sign of the
863
result is set appropriately. */
864
865
static Bigint *
866
diff(Bigint *a, Bigint *b)
867
{
868
Bigint *c;
869
int i, wa, wb;
870
ULong *xa, *xae, *xb, *xbe, *xc;
871
ULLong borrow, y;
872
873
i = cmp(a,b);
874
if (!i) {
875
c = Balloc(0);
876
if (c == NULL)
877
return NULL;
878
c->wds = 1;
879
c->x[0] = 0;
880
return c;
881
}
882
if (i < 0) {
883
c = a;
884
a = b;
885
b = c;
886
i = 1;
887
}
888
else
889
i = 0;
890
c = Balloc(a->k);
891
if (c == NULL)
892
return NULL;
893
c->sign = i;
894
wa = a->wds;
895
xa = a->x;
896
xae = xa + wa;
897
wb = b->wds;
898
xb = b->x;
899
xbe = xb + wb;
900
xc = c->x;
901
borrow = 0;
902
do {
903
y = (ULLong)*xa++ - *xb++ - borrow;
904
borrow = y >> 32 & (ULong)1;
905
*xc++ = (ULong)(y & FFFFFFFF);
906
}
907
while(xb < xbe);
908
while(xa < xae) {
909
y = *xa++ - borrow;
910
borrow = y >> 32 & (ULong)1;
911
*xc++ = (ULong)(y & FFFFFFFF);
912
}
913
while(!*--xc)
914
wa--;
915
c->wds = wa;
916
return c;
917
}
918
919
/* Given a positive normal double x, return the difference between x and the
920
next double up. Doesn't give correct results for subnormals. */
921
922
static double
923
ulp(U *x)
924
{
925
Long L;
926
U u;
927
928
L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
929
word0(&u) = L;
930
word1(&u) = 0;
931
return dval(&u);
932
}
933
934
/* Convert a Bigint to a double plus an exponent */
935
936
static double
937
b2d(Bigint *a, int *e)
938
{
939
ULong *xa, *xa0, w, y, z;
940
int k;
941
U d;
942
943
xa0 = a->x;
944
xa = xa0 + a->wds;
945
y = *--xa;
946
#ifdef DEBUG
947
if (!y) Bug("zero y in b2d");
948
#endif
949
k = hi0bits(y);
950
*e = 32 - k;
951
if (k < Ebits) {
952
word0(&d) = Exp_1 | y >> (Ebits - k);
953
w = xa > xa0 ? *--xa : 0;
954
word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
955
goto ret_d;
956
}
957
z = xa > xa0 ? *--xa : 0;
958
if (k -= Ebits) {
959
word0(&d) = Exp_1 | y << k | z >> (32 - k);
960
y = xa > xa0 ? *--xa : 0;
961
word1(&d) = z << k | y >> (32 - k);
962
}
963
else {
964
word0(&d) = Exp_1 | y;
965
word1(&d) = z;
966
}
967
ret_d:
968
return dval(&d);
969
}
970
971
/* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
972
except that it accepts the scale parameter used in _Py_dg_strtod (which
973
should be either 0 or 2*P), and the normalization for the return value is
974
different (see below). On input, d should be finite and nonnegative, and d
975
/ 2**scale should be exactly representable as an IEEE 754 double.
976
977
Returns a Bigint b and an integer e such that
978
979
dval(d) / 2**scale = b * 2**e.
980
981
Unlike d2b, b is not necessarily odd: b and e are normalized so
982
that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
983
and e == Etiny. This applies equally to an input of 0.0: in that
984
case the return values are b = 0 and e = Etiny.
985
986
The above normalization ensures that for all possible inputs d,
987
2**e gives ulp(d/2**scale).
988
989
Returns NULL on failure.
990
*/
991
992
static Bigint *
993
sd2b(U *d, int scale, int *e)
994
{
995
Bigint *b;
996
997
b = Balloc(1);
998
if (b == NULL)
999
return NULL;
1000
1001
/* First construct b and e assuming that scale == 0. */
1002
b->wds = 2;
1003
b->x[0] = word1(d);
1004
b->x[1] = word0(d) & Frac_mask;
1005
*e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
1006
if (*e < Etiny)
1007
*e = Etiny;
1008
else
1009
b->x[1] |= Exp_msk1;
1010
1011
/* Now adjust for scale, provided that b != 0. */
1012
if (scale && (b->x[0] || b->x[1])) {
1013
*e -= scale;
1014
if (*e < Etiny) {
1015
scale = Etiny - *e;
1016
*e = Etiny;
1017
/* We can't shift more than P-1 bits without shifting out a 1. */
1018
assert(0 < scale && scale <= P - 1);
1019
if (scale >= 32) {
1020
/* The bits shifted out should all be zero. */
1021
assert(b->x[0] == 0);
1022
b->x[0] = b->x[1];
1023
b->x[1] = 0;
1024
scale -= 32;
1025
}
1026
if (scale) {
1027
/* The bits shifted out should all be zero. */
1028
assert(b->x[0] << (32 - scale) == 0);
1029
b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
1030
b->x[1] >>= scale;
1031
}
1032
}
1033
}
1034
/* Ensure b is normalized. */
1035
if (!b->x[1])
1036
b->wds = 1;
1037
1038
return b;
1039
}
1040
1041
/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
1042
1043
Given a finite nonzero double d, return an odd Bigint b and exponent *e
1044
such that fabs(d) = b * 2**e. On return, *bbits gives the number of
1045
significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
1046
1047
If d is zero, then b == 0, *e == -1010, *bbits = 0.
1048
*/
1049
1050
static Bigint *
1051
d2b(U *d, int *e, int *bits)
1052
{
1053
Bigint *b;
1054
int de, k;
1055
ULong *x, y, z;
1056
int i;
1057
1058
b = Balloc(1);
1059
if (b == NULL)
1060
return NULL;
1061
x = b->x;
1062
1063
z = word0(d) & Frac_mask;
1064
word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
1065
if ((de = (int)(word0(d) >> Exp_shift)))
1066
z |= Exp_msk1;
1067
if ((y = word1(d))) {
1068
if ((k = lo0bits(&y))) {
1069
x[0] = y | z << (32 - k);
1070
z >>= k;
1071
}
1072
else
1073
x[0] = y;
1074
i =
1075
b->wds = (x[1] = z) ? 2 : 1;
1076
}
1077
else {
1078
k = lo0bits(&z);
1079
x[0] = z;
1080
i =
1081
b->wds = 1;
1082
k += 32;
1083
}
1084
if (de) {
1085
*e = de - Bias - (P-1) + k;
1086
*bits = P - k;
1087
}
1088
else {
1089
*e = de - Bias - (P-1) + 1 + k;
1090
*bits = 32*i - hi0bits(x[i-1]);
1091
}
1092
return b;
1093
}
1094
1095
/* Compute the ratio of two Bigints, as a double. The result may have an
1096
error of up to 2.5 ulps. */
1097
1098
static double
1099
ratio(Bigint *a, Bigint *b)
1100
{
1101
U da, db;
1102
int k, ka, kb;
1103
1104
dval(&da) = b2d(a, &ka);
1105
dval(&db) = b2d(b, &kb);
1106
k = ka - kb + 32*(a->wds - b->wds);
1107
if (k > 0)
1108
word0(&da) += k*Exp_msk1;
1109
else {
1110
k = -k;
1111
word0(&db) += k*Exp_msk1;
1112
}
1113
return dval(&da) / dval(&db);
1114
}
1115
1116
static const double
1117
tens[] = {
1118
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1119
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1120
1e20, 1e21, 1e22
1121
};
1122
1123
static const double
1124
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1125
static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1126
9007199254740992.*9007199254740992.e-256
1127
/* = 2^106 * 1e-256 */
1128
};
1129
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1130
/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1131
#define Scale_Bit 0x10
1132
#define n_bigtens 5
1133
1134
#define ULbits 32
1135
#define kshift 5
1136
#define kmask 31
1137
1138
1139
static int
1140
dshift(Bigint *b, int p2)
1141
{
1142
int rv = hi0bits(b->x[b->wds-1]) - 4;
1143
if (p2 > 0)
1144
rv -= p2;
1145
return rv & kmask;
1146
}
1147
1148
/* special case of Bigint division. The quotient is always in the range 0 <=
1149
quotient < 10, and on entry the divisor S is normalized so that its top 4
1150
bits (28--31) are zero and bit 27 is set. */
1151
1152
static int
1153
quorem(Bigint *b, Bigint *S)
1154
{
1155
int n;
1156
ULong *bx, *bxe, q, *sx, *sxe;
1157
ULLong borrow, carry, y, ys;
1158
1159
n = S->wds;
1160
#ifdef DEBUG
1161
/*debug*/ if (b->wds > n)
1162
/*debug*/ Bug("oversize b in quorem");
1163
#endif
1164
if (b->wds < n)
1165
return 0;
1166
sx = S->x;
1167
sxe = sx + --n;
1168
bx = b->x;
1169
bxe = bx + n;
1170
q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1171
#ifdef DEBUG
1172
/*debug*/ if (q > 9)
1173
/*debug*/ Bug("oversized quotient in quorem");
1174
#endif
1175
if (q) {
1176
borrow = 0;
1177
carry = 0;
1178
do {
1179
ys = *sx++ * (ULLong)q + carry;
1180
carry = ys >> 32;
1181
y = *bx - (ys & FFFFFFFF) - borrow;
1182
borrow = y >> 32 & (ULong)1;
1183
*bx++ = (ULong)(y & FFFFFFFF);
1184
}
1185
while(sx <= sxe);
1186
if (!*bxe) {
1187
bx = b->x;
1188
while(--bxe > bx && !*bxe)
1189
--n;
1190
b->wds = n;
1191
}
1192
}
1193
if (cmp(b, S) >= 0) {
1194
q++;
1195
borrow = 0;
1196
carry = 0;
1197
bx = b->x;
1198
sx = S->x;
1199
do {
1200
ys = *sx++ + carry;
1201
carry = ys >> 32;
1202
y = *bx - (ys & FFFFFFFF) - borrow;
1203
borrow = y >> 32 & (ULong)1;
1204
*bx++ = (ULong)(y & FFFFFFFF);
1205
}
1206
while(sx <= sxe);
1207
bx = b->x;
1208
bxe = bx + n;
1209
if (!*bxe) {
1210
while(--bxe > bx && !*bxe)
1211
--n;
1212
b->wds = n;
1213
}
1214
}
1215
return q;
1216
}
1217
1218
/* sulp(x) is a version of ulp(x) that takes bc.scale into account.
1219
1220
Assuming that x is finite and nonnegative (positive zero is fine
1221
here) and x / 2^bc.scale is exactly representable as a double,
1222
sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
1223
1224
static double
1225
sulp(U *x, BCinfo *bc)
1226
{
1227
U u;
1228
1229
if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
1230
/* rv/2^bc->scale is subnormal */
1231
word0(&u) = (P+2)*Exp_msk1;
1232
word1(&u) = 0;
1233
return u.d;
1234
}
1235
else {
1236
assert(word0(x) || word1(x)); /* x != 0.0 */
1237
return ulp(x);
1238
}
1239
}
1240
1241
/* The bigcomp function handles some hard cases for strtod, for inputs
1242
with more than STRTOD_DIGLIM digits. It's called once an initial
1243
estimate for the double corresponding to the input string has
1244
already been obtained by the code in _Py_dg_strtod.
1245
1246
The bigcomp function is only called after _Py_dg_strtod has found a
1247
double value rv such that either rv or rv + 1ulp represents the
1248
correctly rounded value corresponding to the original string. It
1249
determines which of these two values is the correct one by
1250
computing the decimal digits of rv + 0.5ulp and comparing them with
1251
the corresponding digits of s0.
1252
1253
In the following, write dv for the absolute value of the number represented
1254
by the input string.
1255
1256
Inputs:
1257
1258
s0 points to the first significant digit of the input string.
1259
1260
rv is a (possibly scaled) estimate for the closest double value to the
1261
value represented by the original input to _Py_dg_strtod. If
1262
bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
1263
the input value.
1264
1265
bc is a struct containing information gathered during the parsing and
1266
estimation steps of _Py_dg_strtod. Description of fields follows:
1267
1268
bc->e0 gives the exponent of the input value, such that dv = (integer
1269
given by the bd->nd digits of s0) * 10**e0
1270
1271
bc->nd gives the total number of significant digits of s0. It will
1272
be at least 1.
1273
1274
bc->nd0 gives the number of significant digits of s0 before the
1275
decimal separator. If there's no decimal separator, bc->nd0 ==
1276
bc->nd.
1277
1278
bc->scale is the value used to scale rv to avoid doing arithmetic with
1279
subnormal values. It's either 0 or 2*P (=106).
1280
1281
Outputs:
1282
1283
On successful exit, rv/2^(bc->scale) is the closest double to dv.
1284
1285
Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
1286
1287
static int
1288
bigcomp(U *rv, const char *s0, BCinfo *bc)
1289
{
1290
Bigint *b, *d;
1291
int b2, d2, dd, i, nd, nd0, odd, p2, p5;
1292
1293
nd = bc->nd;
1294
nd0 = bc->nd0;
1295
p5 = nd + bc->e0;
1296
b = sd2b(rv, bc->scale, &p2);
1297
if (b == NULL)
1298
return -1;
1299
1300
/* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
1301
case, this is used for round to even. */
1302
odd = b->x[0] & 1;
1303
1304
/* left shift b by 1 bit and or a 1 into the least significant bit;
1305
this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1306
b = lshift(b, 1);
1307
if (b == NULL)
1308
return -1;
1309
b->x[0] |= 1;
1310
p2--;
1311
1312
p2 -= p5;
1313
d = i2b(1);
1314
if (d == NULL) {
1315
Bfree(b);
1316
return -1;
1317
}
1318
/* Arrange for convenient computation of quotients:
1319
* shift left if necessary so divisor has 4 leading 0 bits.
1320
*/
1321
if (p5 > 0) {
1322
d = pow5mult(d, p5);
1323
if (d == NULL) {
1324
Bfree(b);
1325
return -1;
1326
}
1327
}
1328
else if (p5 < 0) {
1329
b = pow5mult(b, -p5);
1330
if (b == NULL) {
1331
Bfree(d);
1332
return -1;
1333
}
1334
}
1335
if (p2 > 0) {
1336
b2 = p2;
1337
d2 = 0;
1338
}
1339
else {
1340
b2 = 0;
1341
d2 = -p2;
1342
}
1343
i = dshift(d, d2);
1344
if ((b2 += i) > 0) {
1345
b = lshift(b, b2);
1346
if (b == NULL) {
1347
Bfree(d);
1348
return -1;
1349
}
1350
}
1351
if ((d2 += i) > 0) {
1352
d = lshift(d, d2);
1353
if (d == NULL) {
1354
Bfree(b);
1355
return -1;
1356
}
1357
}
1358
1359
/* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
1360
* b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
1361
* a number in the range [0.1, 1). */
1362
if (cmp(b, d) >= 0)
1363
/* b/d >= 1 */
1364
dd = -1;
1365
else {
1366
i = 0;
1367
for(;;) {
1368
b = multadd(b, 10, 0);
1369
if (b == NULL) {
1370
Bfree(d);
1371
return -1;
1372
}
1373
dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
1374
i++;
1375
1376
if (dd)
1377
break;
1378
if (!b->x[0] && b->wds == 1) {
1379
/* b/d == 0 */
1380
dd = i < nd;
1381
break;
1382
}
1383
if (!(i < nd)) {
1384
/* b/d != 0, but digits of s0 exhausted */
1385
dd = -1;
1386
break;
1387
}
1388
}
1389
}
1390
Bfree(b);
1391
Bfree(d);
1392
if (dd > 0 || (dd == 0 && odd))
1393
dval(rv) += sulp(rv, bc);
1394
return 0;
1395
}
1396
1397
1398
double
1399
_Py_dg_strtod(const char *s00, char **se)
1400
{
1401
int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
1402
int esign, i, j, k, lz, nd, nd0, odd, sign;
1403
const char *s, *s0, *s1;
1404
double aadj, aadj1;
1405
U aadj2, adj, rv, rv0;
1406
ULong y, z, abs_exp;
1407
Long L;
1408
BCinfo bc;
1409
Bigint *bb = NULL, *bd = NULL, *bd0 = NULL, *bs = NULL, *delta = NULL;
1410
size_t ndigits, fraclen;
1411
double result;
1412
1413
dval(&rv) = 0.;
1414
1415
/* Start parsing. */
1416
c = *(s = s00);
1417
1418
/* Parse optional sign, if present. */
1419
sign = 0;
1420
switch (c) {
1421
case '-':
1422
sign = 1;
1423
/* fall through */
1424
case '+':
1425
c = *++s;
1426
}
1427
1428
/* Skip leading zeros: lz is true iff there were leading zeros. */
1429
s1 = s;
1430
while (c == '0')
1431
c = *++s;
1432
lz = s != s1;
1433
1434
/* Point s0 at the first nonzero digit (if any). fraclen will be the
1435
number of digits between the decimal point and the end of the
1436
digit string. ndigits will be the total number of digits ignoring
1437
leading zeros. */
1438
s0 = s1 = s;
1439
while ('0' <= c && c <= '9')
1440
c = *++s;
1441
ndigits = s - s1;
1442
fraclen = 0;
1443
1444
/* Parse decimal point and following digits. */
1445
if (c == '.') {
1446
c = *++s;
1447
if (!ndigits) {
1448
s1 = s;
1449
while (c == '0')
1450
c = *++s;
1451
lz = lz || s != s1;
1452
fraclen += (s - s1);
1453
s0 = s;
1454
}
1455
s1 = s;
1456
while ('0' <= c && c <= '9')
1457
c = *++s;
1458
ndigits += s - s1;
1459
fraclen += s - s1;
1460
}
1461
1462
/* Now lz is true if and only if there were leading zero digits, and
1463
ndigits gives the total number of digits ignoring leading zeros. A
1464
valid input must have at least one digit. */
1465
if (!ndigits && !lz) {
1466
if (se)
1467
*se = (char *)s00;
1468
goto parse_error;
1469
}
1470
1471
/* Range check ndigits and fraclen to make sure that they, and values
1472
computed with them, can safely fit in an int. */
1473
if (ndigits > MAX_DIGITS || fraclen > MAX_DIGITS) {
1474
if (se)
1475
*se = (char *)s00;
1476
goto parse_error;
1477
}
1478
nd = (int)ndigits;
1479
nd0 = (int)ndigits - (int)fraclen;
1480
1481
/* Parse exponent. */
1482
e = 0;
1483
if (c == 'e' || c == 'E') {
1484
s00 = s;
1485
c = *++s;
1486
1487
/* Exponent sign. */
1488
esign = 0;
1489
switch (c) {
1490
case '-':
1491
esign = 1;
1492
/* fall through */
1493
case '+':
1494
c = *++s;
1495
}
1496
1497
/* Skip zeros. lz is true iff there are leading zeros. */
1498
s1 = s;
1499
while (c == '0')
1500
c = *++s;
1501
lz = s != s1;
1502
1503
/* Get absolute value of the exponent. */
1504
s1 = s;
1505
abs_exp = 0;
1506
while ('0' <= c && c <= '9') {
1507
abs_exp = 10*abs_exp + (c - '0');
1508
c = *++s;
1509
}
1510
1511
/* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
1512
there are at most 9 significant exponent digits then overflow is
1513
impossible. */
1514
if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
1515
e = (int)MAX_ABS_EXP;
1516
else
1517
e = (int)abs_exp;
1518
if (esign)
1519
e = -e;
1520
1521
/* A valid exponent must have at least one digit. */
1522
if (s == s1 && !lz)
1523
s = s00;
1524
}
1525
1526
/* Adjust exponent to take into account position of the point. */
1527
e -= nd - nd0;
1528
if (nd0 <= 0)
1529
nd0 = nd;
1530
1531
/* Finished parsing. Set se to indicate how far we parsed */
1532
if (se)
1533
*se = (char *)s;
1534
1535
/* If all digits were zero, exit with return value +-0.0. Otherwise,
1536
strip trailing zeros: scan back until we hit a nonzero digit. */
1537
if (!nd)
1538
goto ret;
1539
for (i = nd; i > 0; ) {
1540
--i;
1541
if (s0[i < nd0 ? i : i+1] != '0') {
1542
++i;
1543
break;
1544
}
1545
}
1546
e += nd - i;
1547
nd = i;
1548
if (nd0 > nd)
1549
nd0 = nd;
1550
1551
/* Summary of parsing results. After parsing, and dealing with zero
1552
* inputs, we have values s0, nd0, nd, e, sign, where:
1553
*
1554
* - s0 points to the first significant digit of the input string
1555
*
1556
* - nd is the total number of significant digits (here, and
1557
* below, 'significant digits' means the set of digits of the
1558
* significand of the input that remain after ignoring leading
1559
* and trailing zeros).
1560
*
1561
* - nd0 indicates the position of the decimal point, if present; it
1562
* satisfies 1 <= nd0 <= nd. The nd significant digits are in
1563
* s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
1564
* notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
1565
* nd0 == nd, then s0[nd0] could be any non-digit character.)
1566
*
1567
* - e is the adjusted exponent: the absolute value of the number
1568
* represented by the original input string is n * 10**e, where
1569
* n is the integer represented by the concatenation of
1570
* s0[0:nd0] and s0[nd0+1:nd+1]
1571
*
1572
* - sign gives the sign of the input: 1 for negative, 0 for positive
1573
*
1574
* - the first and last significant digits are nonzero
1575
*/
1576
1577
/* put first DBL_DIG+1 digits into integer y and z.
1578
*
1579
* - y contains the value represented by the first min(9, nd)
1580
* significant digits
1581
*
1582
* - if nd > 9, z contains the value represented by significant digits
1583
* with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
1584
* gives the value represented by the first min(16, nd) sig. digits.
1585
*/
1586
1587
bc.e0 = e1 = e;
1588
y = z = 0;
1589
for (i = 0; i < nd; i++) {
1590
if (i < 9)
1591
y = 10*y + s0[i < nd0 ? i : i+1] - '0';
1592
else if (i < DBL_DIG+1)
1593
z = 10*z + s0[i < nd0 ? i : i+1] - '0';
1594
else
1595
break;
1596
}
1597
1598
k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1599
dval(&rv) = y;
1600
if (k > 9) {
1601
dval(&rv) = tens[k - 9] * dval(&rv) + z;
1602
}
1603
if (nd <= DBL_DIG
1604
&& Flt_Rounds == 1
1605
) {
1606
if (!e)
1607
goto ret;
1608
if (e > 0) {
1609
if (e <= Ten_pmax) {
1610
dval(&rv) *= tens[e];
1611
goto ret;
1612
}
1613
i = DBL_DIG - nd;
1614
if (e <= Ten_pmax + i) {
1615
/* A fancier test would sometimes let us do
1616
* this for larger i values.
1617
*/
1618
e -= i;
1619
dval(&rv) *= tens[i];
1620
dval(&rv) *= tens[e];
1621
goto ret;
1622
}
1623
}
1624
else if (e >= -Ten_pmax) {
1625
dval(&rv) /= tens[-e];
1626
goto ret;
1627
}
1628
}
1629
e1 += nd - k;
1630
1631
bc.scale = 0;
1632
1633
/* Get starting approximation = rv * 10**e1 */
1634
1635
if (e1 > 0) {
1636
if ((i = e1 & 15))
1637
dval(&rv) *= tens[i];
1638
if (e1 &= ~15) {
1639
if (e1 > DBL_MAX_10_EXP)
1640
goto ovfl;
1641
e1 >>= 4;
1642
for(j = 0; e1 > 1; j++, e1 >>= 1)
1643
if (e1 & 1)
1644
dval(&rv) *= bigtens[j];
1645
/* The last multiplication could overflow. */
1646
word0(&rv) -= P*Exp_msk1;
1647
dval(&rv) *= bigtens[j];
1648
if ((z = word0(&rv) & Exp_mask)
1649
> Exp_msk1*(DBL_MAX_EXP+Bias-P))
1650
goto ovfl;
1651
if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1652
/* set to largest number */
1653
/* (Can't trust DBL_MAX) */
1654
word0(&rv) = Big0;
1655
word1(&rv) = Big1;
1656
}
1657
else
1658
word0(&rv) += P*Exp_msk1;
1659
}
1660
}
1661
else if (e1 < 0) {
1662
/* The input decimal value lies in [10**e1, 10**(e1+16)).
1663
1664
If e1 <= -512, underflow immediately.
1665
If e1 <= -256, set bc.scale to 2*P.
1666
1667
So for input value < 1e-256, bc.scale is always set;
1668
for input value >= 1e-240, bc.scale is never set.
1669
For input values in [1e-256, 1e-240), bc.scale may or may
1670
not be set. */
1671
1672
e1 = -e1;
1673
if ((i = e1 & 15))
1674
dval(&rv) /= tens[i];
1675
if (e1 >>= 4) {
1676
if (e1 >= 1 << n_bigtens)
1677
goto undfl;
1678
if (e1 & Scale_Bit)
1679
bc.scale = 2*P;
1680
for(j = 0; e1 > 0; j++, e1 >>= 1)
1681
if (e1 & 1)
1682
dval(&rv) *= tinytens[j];
1683
if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
1684
>> Exp_shift)) > 0) {
1685
/* scaled rv is denormal; clear j low bits */
1686
if (j >= 32) {
1687
word1(&rv) = 0;
1688
if (j >= 53)
1689
word0(&rv) = (P+2)*Exp_msk1;
1690
else
1691
word0(&rv) &= 0xffffffff << (j-32);
1692
}
1693
else
1694
word1(&rv) &= 0xffffffff << j;
1695
}
1696
if (!dval(&rv))
1697
goto undfl;
1698
}
1699
}
1700
1701
/* Now the hard part -- adjusting rv to the correct value.*/
1702
1703
/* Put digits into bd: true value = bd * 10^e */
1704
1705
bc.nd = nd;
1706
bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
1707
/* to silence an erroneous warning about bc.nd0 */
1708
/* possibly not being initialized. */
1709
if (nd > STRTOD_DIGLIM) {
1710
/* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
1711
/* minimum number of decimal digits to distinguish double values */
1712
/* in IEEE arithmetic. */
1713
1714
/* Truncate input to 18 significant digits, then discard any trailing
1715
zeros on the result by updating nd, nd0, e and y suitably. (There's
1716
no need to update z; it's not reused beyond this point.) */
1717
for (i = 18; i > 0; ) {
1718
/* scan back until we hit a nonzero digit. significant digit 'i'
1719
is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
1720
--i;
1721
if (s0[i < nd0 ? i : i+1] != '0') {
1722
++i;
1723
break;
1724
}
1725
}
1726
e += nd - i;
1727
nd = i;
1728
if (nd0 > nd)
1729
nd0 = nd;
1730
if (nd < 9) { /* must recompute y */
1731
y = 0;
1732
for(i = 0; i < nd0; ++i)
1733
y = 10*y + s0[i] - '0';
1734
for(; i < nd; ++i)
1735
y = 10*y + s0[i+1] - '0';
1736
}
1737
}
1738
bd0 = s2b(s0, nd0, nd, y);
1739
if (bd0 == NULL)
1740
goto failed_malloc;
1741
1742
/* Notation for the comments below. Write:
1743
1744
- dv for the absolute value of the number represented by the original
1745
decimal input string.
1746
1747
- if we've truncated dv, write tdv for the truncated value.
1748
Otherwise, set tdv == dv.
1749
1750
- srv for the quantity rv/2^bc.scale; so srv is the current binary
1751
approximation to tdv (and dv). It should be exactly representable
1752
in an IEEE 754 double.
1753
*/
1754
1755
for(;;) {
1756
1757
/* This is the main correction loop for _Py_dg_strtod.
1758
1759
We've got a decimal value tdv, and a floating-point approximation
1760
srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
1761
close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
1762
approximation if not.
1763
1764
To determine whether srv is close enough to tdv, compute integers
1765
bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
1766
respectively, and then use integer arithmetic to determine whether
1767
|tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
1768
*/
1769
1770
bd = Balloc(bd0->k);
1771
if (bd == NULL) {
1772
goto failed_malloc;
1773
}
1774
Bcopy(bd, bd0);
1775
bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
1776
if (bb == NULL) {
1777
goto failed_malloc;
1778
}
1779
/* Record whether lsb of bb is odd, in case we need this
1780
for the round-to-even step later. */
1781
odd = bb->x[0] & 1;
1782
1783
/* tdv = bd * 10**e; srv = bb * 2**bbe */
1784
bs = i2b(1);
1785
if (bs == NULL) {
1786
goto failed_malloc;
1787
}
1788
1789
if (e >= 0) {
1790
bb2 = bb5 = 0;
1791
bd2 = bd5 = e;
1792
}
1793
else {
1794
bb2 = bb5 = -e;
1795
bd2 = bd5 = 0;
1796
}
1797
if (bbe >= 0)
1798
bb2 += bbe;
1799
else
1800
bd2 -= bbe;
1801
bs2 = bb2;
1802
bb2++;
1803
bd2++;
1804
1805
/* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
1806
and bs == 1, so:
1807
1808
tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
1809
srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
1810
0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
1811
1812
It follows that:
1813
1814
M * tdv = bd * 2**bd2 * 5**bd5
1815
M * srv = bb * 2**bb2 * 5**bb5
1816
M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
1817
1818
for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1819
this fact is not needed below.)
1820
*/
1821
1822
/* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
1823
i = bb2 < bd2 ? bb2 : bd2;
1824
if (i > bs2)
1825
i = bs2;
1826
if (i > 0) {
1827
bb2 -= i;
1828
bd2 -= i;
1829
bs2 -= i;
1830
}
1831
1832
/* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
1833
if (bb5 > 0) {
1834
bs = pow5mult(bs, bb5);
1835
if (bs == NULL) {
1836
goto failed_malloc;
1837
}
1838
Bigint *bb1 = mult(bs, bb);
1839
Bfree(bb);
1840
bb = bb1;
1841
if (bb == NULL) {
1842
goto failed_malloc;
1843
}
1844
}
1845
if (bb2 > 0) {
1846
bb = lshift(bb, bb2);
1847
if (bb == NULL) {
1848
goto failed_malloc;
1849
}
1850
}
1851
if (bd5 > 0) {
1852
bd = pow5mult(bd, bd5);
1853
if (bd == NULL) {
1854
goto failed_malloc;
1855
}
1856
}
1857
if (bd2 > 0) {
1858
bd = lshift(bd, bd2);
1859
if (bd == NULL) {
1860
goto failed_malloc;
1861
}
1862
}
1863
if (bs2 > 0) {
1864
bs = lshift(bs, bs2);
1865
if (bs == NULL) {
1866
goto failed_malloc;
1867
}
1868
}
1869
1870
/* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
1871
respectively. Compute the difference |tdv - srv|, and compare
1872
with 0.5 ulp(srv). */
1873
1874
delta = diff(bb, bd);
1875
if (delta == NULL) {
1876
goto failed_malloc;
1877
}
1878
dsign = delta->sign;
1879
delta->sign = 0;
1880
i = cmp(delta, bs);
1881
if (bc.nd > nd && i <= 0) {
1882
if (dsign)
1883
break; /* Must use bigcomp(). */
1884
1885
/* Here rv overestimates the truncated decimal value by at most
1886
0.5 ulp(rv). Hence rv either overestimates the true decimal
1887
value by <= 0.5 ulp(rv), or underestimates it by some small
1888
amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
1889
the true decimal value, so it's possible to exit.
1890
1891
Exception: if scaled rv is a normal exact power of 2, but not
1892
DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
1893
next double, so the correctly rounded result is either rv - 0.5
1894
ulp(rv) or rv; in this case, use bigcomp to distinguish. */
1895
1896
if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
1897
/* rv can't be 0, since it's an overestimate for some
1898
nonzero value. So rv is a normal power of 2. */
1899
j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
1900
/* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
1901
rv / 2^bc.scale >= 2^-1021. */
1902
if (j - bc.scale >= 2) {
1903
dval(&rv) -= 0.5 * sulp(&rv, &bc);
1904
break; /* Use bigcomp. */
1905
}
1906
}
1907
1908
{
1909
bc.nd = nd;
1910
i = -1; /* Discarded digits make delta smaller. */
1911
}
1912
}
1913
1914
if (i < 0) {
1915
/* Error is less than half an ulp -- check for
1916
* special case of mantissa a power of two.
1917
*/
1918
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
1919
|| (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
1920
) {
1921
break;
1922
}
1923
if (!delta->x[0] && delta->wds <= 1) {
1924
/* exact result */
1925
break;
1926
}
1927
delta = lshift(delta,Log2P);
1928
if (delta == NULL) {
1929
goto failed_malloc;
1930
}
1931
if (cmp(delta, bs) > 0)
1932
goto drop_down;
1933
break;
1934
}
1935
if (i == 0) {
1936
/* exactly half-way between */
1937
if (dsign) {
1938
if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
1939
&& word1(&rv) == (
1940
(bc.scale &&
1941
(y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
1942
(0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1943
0xffffffff)) {
1944
/*boundary case -- increment exponent*/
1945
word0(&rv) = (word0(&rv) & Exp_mask)
1946
+ Exp_msk1
1947
;
1948
word1(&rv) = 0;
1949
/* dsign = 0; */
1950
break;
1951
}
1952
}
1953
else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1954
drop_down:
1955
/* boundary case -- decrement exponent */
1956
if (bc.scale) {
1957
L = word0(&rv) & Exp_mask;
1958
if (L <= (2*P+1)*Exp_msk1) {
1959
if (L > (P+2)*Exp_msk1)
1960
/* round even ==> */
1961
/* accept rv */
1962
break;
1963
/* rv = smallest denormal */
1964
if (bc.nd > nd)
1965
break;
1966
goto undfl;
1967
}
1968
}
1969
L = (word0(&rv) & Exp_mask) - Exp_msk1;
1970
word0(&rv) = L | Bndry_mask1;
1971
word1(&rv) = 0xffffffff;
1972
break;
1973
}
1974
if (!odd)
1975
break;
1976
if (dsign)
1977
dval(&rv) += sulp(&rv, &bc);
1978
else {
1979
dval(&rv) -= sulp(&rv, &bc);
1980
if (!dval(&rv)) {
1981
if (bc.nd >nd)
1982
break;
1983
goto undfl;
1984
}
1985
}
1986
/* dsign = 1 - dsign; */
1987
break;
1988
}
1989
if ((aadj = ratio(delta, bs)) <= 2.) {
1990
if (dsign)
1991
aadj = aadj1 = 1.;
1992
else if (word1(&rv) || word0(&rv) & Bndry_mask) {
1993
if (word1(&rv) == Tiny1 && !word0(&rv)) {
1994
if (bc.nd >nd)
1995
break;
1996
goto undfl;
1997
}
1998
aadj = 1.;
1999
aadj1 = -1.;
2000
}
2001
else {
2002
/* special case -- power of FLT_RADIX to be */
2003
/* rounded down... */
2004
2005
if (aadj < 2./FLT_RADIX)
2006
aadj = 1./FLT_RADIX;
2007
else
2008
aadj *= 0.5;
2009
aadj1 = -aadj;
2010
}
2011
}
2012
else {
2013
aadj *= 0.5;
2014
aadj1 = dsign ? aadj : -aadj;
2015
if (Flt_Rounds == 0)
2016
aadj1 += 0.5;
2017
}
2018
y = word0(&rv) & Exp_mask;
2019
2020
/* Check for overflow */
2021
2022
if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2023
dval(&rv0) = dval(&rv);
2024
word0(&rv) -= P*Exp_msk1;
2025
adj.d = aadj1 * ulp(&rv);
2026
dval(&rv) += adj.d;
2027
if ((word0(&rv) & Exp_mask) >=
2028
Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2029
if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
2030
goto ovfl;
2031
}
2032
word0(&rv) = Big0;
2033
word1(&rv) = Big1;
2034
goto cont;
2035
}
2036
else
2037
word0(&rv) += P*Exp_msk1;
2038
}
2039
else {
2040
if (bc.scale && y <= 2*P*Exp_msk1) {
2041
if (aadj <= 0x7fffffff) {
2042
if ((z = (ULong)aadj) <= 0)
2043
z = 1;
2044
aadj = z;
2045
aadj1 = dsign ? aadj : -aadj;
2046
}
2047
dval(&aadj2) = aadj1;
2048
word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
2049
aadj1 = dval(&aadj2);
2050
}
2051
adj.d = aadj1 * ulp(&rv);
2052
dval(&rv) += adj.d;
2053
}
2054
z = word0(&rv) & Exp_mask;
2055
if (bc.nd == nd) {
2056
if (!bc.scale)
2057
if (y == z) {
2058
/* Can we stop now? */
2059
L = (Long)aadj;
2060
aadj -= L;
2061
/* The tolerances below are conservative. */
2062
if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
2063
if (aadj < .4999999 || aadj > .5000001)
2064
break;
2065
}
2066
else if (aadj < .4999999/FLT_RADIX)
2067
break;
2068
}
2069
}
2070
cont:
2071
Bfree(bb); bb = NULL;
2072
Bfree(bd); bd = NULL;
2073
Bfree(bs); bs = NULL;
2074
Bfree(delta); delta = NULL;
2075
}
2076
if (bc.nd > nd) {
2077
error = bigcomp(&rv, s0, &bc);
2078
if (error)
2079
goto failed_malloc;
2080
}
2081
2082
if (bc.scale) {
2083
word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
2084
word1(&rv0) = 0;
2085
dval(&rv) *= dval(&rv0);
2086
}
2087
2088
ret:
2089
result = sign ? -dval(&rv) : dval(&rv);
2090
goto done;
2091
2092
parse_error:
2093
result = 0.0;
2094
goto done;
2095
2096
failed_malloc:
2097
errno = ENOMEM;
2098
result = -1.0;
2099
goto done;
2100
2101
undfl:
2102
result = sign ? -0.0 : 0.0;
2103
goto done;
2104
2105
ovfl:
2106
errno = ERANGE;
2107
/* Can't trust HUGE_VAL */
2108
word0(&rv) = Exp_mask;
2109
word1(&rv) = 0;
2110
result = sign ? -dval(&rv) : dval(&rv);
2111
goto done;
2112
2113
done:
2114
Bfree(bb);
2115
Bfree(bd);
2116
Bfree(bs);
2117
Bfree(bd0);
2118
Bfree(delta);
2119
return result;
2120
2121
}
2122
2123
static char *
2124
rv_alloc(int i)
2125
{
2126
int j, k, *r;
2127
2128
j = sizeof(ULong);
2129
for(k = 0;
2130
sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
2131
j <<= 1)
2132
k++;
2133
r = (int*)Balloc(k);
2134
if (r == NULL)
2135
return NULL;
2136
*r = k;
2137
return (char *)(r+1);
2138
}
2139
2140
static char *
2141
nrv_alloc(const char *s, char **rve, int n)
2142
{
2143
char *rv, *t;
2144
2145
rv = rv_alloc(n);
2146
if (rv == NULL)
2147
return NULL;
2148
t = rv;
2149
while((*t = *s++)) t++;
2150
if (rve)
2151
*rve = t;
2152
return rv;
2153
}
2154
2155
/* freedtoa(s) must be used to free values s returned by dtoa
2156
* when MULTIPLE_THREADS is #defined. It should be used in all cases,
2157
* but for consistency with earlier versions of dtoa, it is optional
2158
* when MULTIPLE_THREADS is not defined.
2159
*/
2160
2161
void
2162
_Py_dg_freedtoa(char *s)
2163
{
2164
Bigint *b = (Bigint *)((int *)s - 1);
2165
b->maxwds = 1 << (b->k = *(int*)b);
2166
Bfree(b);
2167
}
2168
2169
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2170
*
2171
* Inspired by "How to Print Floating-Point Numbers Accurately" by
2172
* Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2173
*
2174
* Modifications:
2175
* 1. Rather than iterating, we use a simple numeric overestimate
2176
* to determine k = floor(log10(d)). We scale relevant
2177
* quantities using O(log2(k)) rather than O(k) multiplications.
2178
* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2179
* try to generate digits strictly left to right. Instead, we
2180
* compute with fewer bits and propagate the carry if necessary
2181
* when rounding the final digit up. This is often faster.
2182
* 3. Under the assumption that input will be rounded nearest,
2183
* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2184
* That is, we allow equality in stopping tests when the
2185
* round-nearest rule will give the same floating-point value
2186
* as would satisfaction of the stopping test with strict
2187
* inequality.
2188
* 4. We remove common factors of powers of 2 from relevant
2189
* quantities.
2190
* 5. When converting floating-point integers less than 1e16,
2191
* we use floating-point arithmetic rather than resorting
2192
* to multiple-precision integers.
2193
* 6. When asked to produce fewer than 15 digits, we first try
2194
* to get by with floating-point arithmetic; we resort to
2195
* multiple-precision integer arithmetic only if we cannot
2196
* guarantee that the floating-point calculation has given
2197
* the correctly rounded result. For k requested digits and
2198
* "uniformly" distributed input, the probability is
2199
* something like 10^(k-15) that we must resort to the Long
2200
* calculation.
2201
*/
2202
2203
/* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
2204
leakage, a successful call to _Py_dg_dtoa should always be matched by a
2205
call to _Py_dg_freedtoa. */
2206
2207
char *
2208
_Py_dg_dtoa(double dd, int mode, int ndigits,
2209
int *decpt, int *sign, char **rve)
2210
{
2211
/* Arguments ndigits, decpt, sign are similar to those
2212
of ecvt and fcvt; trailing zeros are suppressed from
2213
the returned string. If not null, *rve is set to point
2214
to the end of the return value. If d is +-Infinity or NaN,
2215
then *decpt is set to 9999.
2216
2217
mode:
2218
0 ==> shortest string that yields d when read in
2219
and rounded to nearest.
2220
1 ==> like 0, but with Steele & White stopping rule;
2221
e.g. with IEEE P754 arithmetic , mode 0 gives
2222
1e23 whereas mode 1 gives 9.999999999999999e22.
2223
2 ==> max(1,ndigits) significant digits. This gives a
2224
return value similar to that of ecvt, except
2225
that trailing zeros are suppressed.
2226
3 ==> through ndigits past the decimal point. This
2227
gives a return value similar to that from fcvt,
2228
except that trailing zeros are suppressed, and
2229
ndigits can be negative.
2230
4,5 ==> similar to 2 and 3, respectively, but (in
2231
round-nearest mode) with the tests of mode 0 to
2232
possibly return a shorter string that rounds to d.
2233
With IEEE arithmetic and compilation with
2234
-DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2235
as modes 2 and 3 when FLT_ROUNDS != 1.
2236
6-9 ==> Debugging modes similar to mode - 4: don't try
2237
fast floating-point estimate (if applicable).
2238
2239
Values of mode other than 0-9 are treated as mode 0.
2240
2241
Sufficient space is allocated to the return value
2242
to hold the suppressed trailing zeros.
2243
*/
2244
2245
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2246
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2247
spec_case, try_quick;
2248
Long L;
2249
int denorm;
2250
ULong x;
2251
Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2252
U d2, eps, u;
2253
double ds;
2254
char *s, *s0;
2255
2256
/* set pointers to NULL, to silence gcc compiler warnings and make
2257
cleanup easier on error */
2258
mlo = mhi = S = 0;
2259
s0 = 0;
2260
2261
u.d = dd;
2262
if (word0(&u) & Sign_bit) {
2263
/* set sign for everything, including 0's and NaNs */
2264
*sign = 1;
2265
word0(&u) &= ~Sign_bit; /* clear sign bit */
2266
}
2267
else
2268
*sign = 0;
2269
2270
/* quick return for Infinities, NaNs and zeros */
2271
if ((word0(&u) & Exp_mask) == Exp_mask)
2272
{
2273
/* Infinity or NaN */
2274
*decpt = 9999;
2275
if (!word1(&u) && !(word0(&u) & 0xfffff))
2276
return nrv_alloc("Infinity", rve, 8);
2277
return nrv_alloc("NaN", rve, 3);
2278
}
2279
if (!dval(&u)) {
2280
*decpt = 1;
2281
return nrv_alloc("0", rve, 1);
2282
}
2283
2284
/* compute k = floor(log10(d)). The computation may leave k
2285
one too large, but should never leave k too small. */
2286
b = d2b(&u, &be, &bbits);
2287
if (b == NULL)
2288
goto failed_malloc;
2289
if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
2290
dval(&d2) = dval(&u);
2291
word0(&d2) &= Frac_mask1;
2292
word0(&d2) |= Exp_11;
2293
2294
/* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2295
* log10(x) = log(x) / log(10)
2296
* ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2297
* log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2298
*
2299
* This suggests computing an approximation k to log10(d) by
2300
*
2301
* k = (i - Bias)*0.301029995663981
2302
* + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2303
*
2304
* We want k to be too large rather than too small.
2305
* The error in the first-order Taylor series approximation
2306
* is in our favor, so we just round up the constant enough
2307
* to compensate for any error in the multiplication of
2308
* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2309
* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2310
* adding 1e-13 to the constant term more than suffices.
2311
* Hence we adjust the constant term to 0.1760912590558.
2312
* (We could get a more accurate k by invoking log10,
2313
* but this is probably not worthwhile.)
2314
*/
2315
2316
i -= Bias;
2317
denorm = 0;
2318
}
2319
else {
2320
/* d is denormalized */
2321
2322
i = bbits + be + (Bias + (P-1) - 1);
2323
x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2324
: word1(&u) << (32 - i);
2325
dval(&d2) = x;
2326
word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
2327
i -= (Bias + (P-1) - 1) + 1;
2328
denorm = 1;
2329
}
2330
ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2331
i*0.301029995663981;
2332
k = (int)ds;
2333
if (ds < 0. && ds != k)
2334
k--; /* want k = floor(ds) */
2335
k_check = 1;
2336
if (k >= 0 && k <= Ten_pmax) {
2337
if (dval(&u) < tens[k])
2338
k--;
2339
k_check = 0;
2340
}
2341
j = bbits - i - 1;
2342
if (j >= 0) {
2343
b2 = 0;
2344
s2 = j;
2345
}
2346
else {
2347
b2 = -j;
2348
s2 = 0;
2349
}
2350
if (k >= 0) {
2351
b5 = 0;
2352
s5 = k;
2353
s2 += k;
2354
}
2355
else {
2356
b2 -= k;
2357
b5 = -k;
2358
s5 = 0;
2359
}
2360
if (mode < 0 || mode > 9)
2361
mode = 0;
2362
2363
try_quick = 1;
2364
2365
if (mode > 5) {
2366
mode -= 4;
2367
try_quick = 0;
2368
}
2369
leftright = 1;
2370
ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
2371
/* silence erroneous "gcc -Wall" warning. */
2372
switch(mode) {
2373
case 0:
2374
case 1:
2375
i = 18;
2376
ndigits = 0;
2377
break;
2378
case 2:
2379
leftright = 0;
2380
/* fall through */
2381
case 4:
2382
if (ndigits <= 0)
2383
ndigits = 1;
2384
ilim = ilim1 = i = ndigits;
2385
break;
2386
case 3:
2387
leftright = 0;
2388
/* fall through */
2389
case 5:
2390
i = ndigits + k + 1;
2391
ilim = i;
2392
ilim1 = i - 1;
2393
if (i <= 0)
2394
i = 1;
2395
}
2396
s0 = rv_alloc(i);
2397
if (s0 == NULL)
2398
goto failed_malloc;
2399
s = s0;
2400
2401
2402
if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2403
2404
/* Try to get by with floating-point arithmetic. */
2405
2406
i = 0;
2407
dval(&d2) = dval(&u);
2408
k0 = k;
2409
ilim0 = ilim;
2410
ieps = 2; /* conservative */
2411
if (k > 0) {
2412
ds = tens[k&0xf];
2413
j = k >> 4;
2414
if (j & Bletch) {
2415
/* prevent overflows */
2416
j &= Bletch - 1;
2417
dval(&u) /= bigtens[n_bigtens-1];
2418
ieps++;
2419
}
2420
for(; j; j >>= 1, i++)
2421
if (j & 1) {
2422
ieps++;
2423
ds *= bigtens[i];
2424
}
2425
dval(&u) /= ds;
2426
}
2427
else if ((j1 = -k)) {
2428
dval(&u) *= tens[j1 & 0xf];
2429
for(j = j1 >> 4; j; j >>= 1, i++)
2430
if (j & 1) {
2431
ieps++;
2432
dval(&u) *= bigtens[i];
2433
}
2434
}
2435
if (k_check && dval(&u) < 1. && ilim > 0) {
2436
if (ilim1 <= 0)
2437
goto fast_failed;
2438
ilim = ilim1;
2439
k--;
2440
dval(&u) *= 10.;
2441
ieps++;
2442
}
2443
dval(&eps) = ieps*dval(&u) + 7.;
2444
word0(&eps) -= (P-1)*Exp_msk1;
2445
if (ilim == 0) {
2446
S = mhi = 0;
2447
dval(&u) -= 5.;
2448
if (dval(&u) > dval(&eps))
2449
goto one_digit;
2450
if (dval(&u) < -dval(&eps))
2451
goto no_digits;
2452
goto fast_failed;
2453
}
2454
if (leftright) {
2455
/* Use Steele & White method of only
2456
* generating digits needed.
2457
*/
2458
dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
2459
for(i = 0;;) {
2460
L = (Long)dval(&u);
2461
dval(&u) -= L;
2462
*s++ = '0' + (int)L;
2463
if (dval(&u) < dval(&eps))
2464
goto ret1;
2465
if (1. - dval(&u) < dval(&eps))
2466
goto bump_up;
2467
if (++i >= ilim)
2468
break;
2469
dval(&eps) *= 10.;
2470
dval(&u) *= 10.;
2471
}
2472
}
2473
else {
2474
/* Generate ilim digits, then fix them up. */
2475
dval(&eps) *= tens[ilim-1];
2476
for(i = 1;; i++, dval(&u) *= 10.) {
2477
L = (Long)(dval(&u));
2478
if (!(dval(&u) -= L))
2479
ilim = i;
2480
*s++ = '0' + (int)L;
2481
if (i == ilim) {
2482
if (dval(&u) > 0.5 + dval(&eps))
2483
goto bump_up;
2484
else if (dval(&u) < 0.5 - dval(&eps)) {
2485
while(*--s == '0');
2486
s++;
2487
goto ret1;
2488
}
2489
break;
2490
}
2491
}
2492
}
2493
fast_failed:
2494
s = s0;
2495
dval(&u) = dval(&d2);
2496
k = k0;
2497
ilim = ilim0;
2498
}
2499
2500
/* Do we have a "small" integer? */
2501
2502
if (be >= 0 && k <= Int_max) {
2503
/* Yes. */
2504
ds = tens[k];
2505
if (ndigits < 0 && ilim <= 0) {
2506
S = mhi = 0;
2507
if (ilim < 0 || dval(&u) <= 5*ds)
2508
goto no_digits;
2509
goto one_digit;
2510
}
2511
for(i = 1;; i++, dval(&u) *= 10.) {
2512
L = (Long)(dval(&u) / ds);
2513
dval(&u) -= L*ds;
2514
*s++ = '0' + (int)L;
2515
if (!dval(&u)) {
2516
break;
2517
}
2518
if (i == ilim) {
2519
dval(&u) += dval(&u);
2520
if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2521
bump_up:
2522
while(*--s == '9')
2523
if (s == s0) {
2524
k++;
2525
*s = '0';
2526
break;
2527
}
2528
++*s++;
2529
}
2530
else {
2531
/* Strip trailing zeros. This branch was missing from the
2532
original dtoa.c, leading to surplus trailing zeros in
2533
some cases. See bugs.python.org/issue40780. */
2534
while (s > s0 && s[-1] == '0') {
2535
--s;
2536
}
2537
}
2538
break;
2539
}
2540
}
2541
goto ret1;
2542
}
2543
2544
m2 = b2;
2545
m5 = b5;
2546
if (leftright) {
2547
i =
2548
denorm ? be + (Bias + (P-1) - 1 + 1) :
2549
1 + P - bbits;
2550
b2 += i;
2551
s2 += i;
2552
mhi = i2b(1);
2553
if (mhi == NULL)
2554
goto failed_malloc;
2555
}
2556
if (m2 > 0 && s2 > 0) {
2557
i = m2 < s2 ? m2 : s2;
2558
b2 -= i;
2559
m2 -= i;
2560
s2 -= i;
2561
}
2562
if (b5 > 0) {
2563
if (leftright) {
2564
if (m5 > 0) {
2565
mhi = pow5mult(mhi, m5);
2566
if (mhi == NULL)
2567
goto failed_malloc;
2568
b1 = mult(mhi, b);
2569
Bfree(b);
2570
b = b1;
2571
if (b == NULL)
2572
goto failed_malloc;
2573
}
2574
if ((j = b5 - m5)) {
2575
b = pow5mult(b, j);
2576
if (b == NULL)
2577
goto failed_malloc;
2578
}
2579
}
2580
else {
2581
b = pow5mult(b, b5);
2582
if (b == NULL)
2583
goto failed_malloc;
2584
}
2585
}
2586
S = i2b(1);
2587
if (S == NULL)
2588
goto failed_malloc;
2589
if (s5 > 0) {
2590
S = pow5mult(S, s5);
2591
if (S == NULL)
2592
goto failed_malloc;
2593
}
2594
2595
/* Check for special case that d is a normalized power of 2. */
2596
2597
spec_case = 0;
2598
if ((mode < 2 || leftright)
2599
) {
2600
if (!word1(&u) && !(word0(&u) & Bndry_mask)
2601
&& word0(&u) & (Exp_mask & ~Exp_msk1)
2602
) {
2603
/* The special case */
2604
b2 += Log2P;
2605
s2 += Log2P;
2606
spec_case = 1;
2607
}
2608
}
2609
2610
/* Arrange for convenient computation of quotients:
2611
* shift left if necessary so divisor has 4 leading 0 bits.
2612
*
2613
* Perhaps we should just compute leading 28 bits of S once
2614
* and for all and pass them and a shift to quorem, so it
2615
* can do shifts and ors to compute the numerator for q.
2616
*/
2617
#define iInc 28
2618
i = dshift(S, s2);
2619
b2 += i;
2620
m2 += i;
2621
s2 += i;
2622
if (b2 > 0) {
2623
b = lshift(b, b2);
2624
if (b == NULL)
2625
goto failed_malloc;
2626
}
2627
if (s2 > 0) {
2628
S = lshift(S, s2);
2629
if (S == NULL)
2630
goto failed_malloc;
2631
}
2632
if (k_check) {
2633
if (cmp(b,S) < 0) {
2634
k--;
2635
b = multadd(b, 10, 0); /* we botched the k estimate */
2636
if (b == NULL)
2637
goto failed_malloc;
2638
if (leftright) {
2639
mhi = multadd(mhi, 10, 0);
2640
if (mhi == NULL)
2641
goto failed_malloc;
2642
}
2643
ilim = ilim1;
2644
}
2645
}
2646
if (ilim <= 0 && (mode == 3 || mode == 5)) {
2647
if (ilim < 0) {
2648
/* no digits, fcvt style */
2649
no_digits:
2650
k = -1 - ndigits;
2651
goto ret;
2652
}
2653
else {
2654
S = multadd(S, 5, 0);
2655
if (S == NULL)
2656
goto failed_malloc;
2657
if (cmp(b, S) <= 0)
2658
goto no_digits;
2659
}
2660
one_digit:
2661
*s++ = '1';
2662
k++;
2663
goto ret;
2664
}
2665
if (leftright) {
2666
if (m2 > 0) {
2667
mhi = lshift(mhi, m2);
2668
if (mhi == NULL)
2669
goto failed_malloc;
2670
}
2671
2672
/* Compute mlo -- check for special case
2673
* that d is a normalized power of 2.
2674
*/
2675
2676
mlo = mhi;
2677
if (spec_case) {
2678
mhi = Balloc(mhi->k);
2679
if (mhi == NULL)
2680
goto failed_malloc;
2681
Bcopy(mhi, mlo);
2682
mhi = lshift(mhi, Log2P);
2683
if (mhi == NULL)
2684
goto failed_malloc;
2685
}
2686
2687
for(i = 1;;i++) {
2688
dig = quorem(b,S) + '0';
2689
/* Do we yet have the shortest decimal string
2690
* that will round to d?
2691
*/
2692
j = cmp(b, mlo);
2693
delta = diff(S, mhi);
2694
if (delta == NULL)
2695
goto failed_malloc;
2696
j1 = delta->sign ? 1 : cmp(b, delta);
2697
Bfree(delta);
2698
if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2699
) {
2700
if (dig == '9')
2701
goto round_9_up;
2702
if (j > 0)
2703
dig++;
2704
*s++ = dig;
2705
goto ret;
2706
}
2707
if (j < 0 || (j == 0 && mode != 1
2708
&& !(word1(&u) & 1)
2709
)) {
2710
if (!b->x[0] && b->wds <= 1) {
2711
goto accept_dig;
2712
}
2713
if (j1 > 0) {
2714
b = lshift(b, 1);
2715
if (b == NULL)
2716
goto failed_malloc;
2717
j1 = cmp(b, S);
2718
if ((j1 > 0 || (j1 == 0 && dig & 1))
2719
&& dig++ == '9')
2720
goto round_9_up;
2721
}
2722
accept_dig:
2723
*s++ = dig;
2724
goto ret;
2725
}
2726
if (j1 > 0) {
2727
if (dig == '9') { /* possible if i == 1 */
2728
round_9_up:
2729
*s++ = '9';
2730
goto roundoff;
2731
}
2732
*s++ = dig + 1;
2733
goto ret;
2734
}
2735
*s++ = dig;
2736
if (i == ilim)
2737
break;
2738
b = multadd(b, 10, 0);
2739
if (b == NULL)
2740
goto failed_malloc;
2741
if (mlo == mhi) {
2742
mlo = mhi = multadd(mhi, 10, 0);
2743
if (mlo == NULL)
2744
goto failed_malloc;
2745
}
2746
else {
2747
mlo = multadd(mlo, 10, 0);
2748
if (mlo == NULL)
2749
goto failed_malloc;
2750
mhi = multadd(mhi, 10, 0);
2751
if (mhi == NULL)
2752
goto failed_malloc;
2753
}
2754
}
2755
}
2756
else
2757
for(i = 1;; i++) {
2758
*s++ = dig = quorem(b,S) + '0';
2759
if (!b->x[0] && b->wds <= 1) {
2760
goto ret;
2761
}
2762
if (i >= ilim)
2763
break;
2764
b = multadd(b, 10, 0);
2765
if (b == NULL)
2766
goto failed_malloc;
2767
}
2768
2769
/* Round off last digit */
2770
2771
b = lshift(b, 1);
2772
if (b == NULL)
2773
goto failed_malloc;
2774
j = cmp(b, S);
2775
if (j > 0 || (j == 0 && dig & 1)) {
2776
roundoff:
2777
while(*--s == '9')
2778
if (s == s0) {
2779
k++;
2780
*s++ = '1';
2781
goto ret;
2782
}
2783
++*s++;
2784
}
2785
else {
2786
while(*--s == '0');
2787
s++;
2788
}
2789
ret:
2790
Bfree(S);
2791
if (mhi) {
2792
if (mlo && mlo != mhi)
2793
Bfree(mlo);
2794
Bfree(mhi);
2795
}
2796
ret1:
2797
Bfree(b);
2798
*s = 0;
2799
*decpt = k + 1;
2800
if (rve)
2801
*rve = s;
2802
return s0;
2803
failed_malloc:
2804
if (S)
2805
Bfree(S);
2806
if (mlo && mlo != mhi)
2807
Bfree(mlo);
2808
if (mhi)
2809
Bfree(mhi);
2810
if (b)
2811
Bfree(b);
2812
if (s0)
2813
_Py_dg_freedtoa(s0);
2814
return NULL;
2815
}
2816
#ifdef __cplusplus
2817
}
2818
#endif
2819
2820
#endif // _PY_SHORT_FLOAT_REPR == 1
2821
2822