Source code

Revision control

Other Tools

1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
/****************************************************************
3
*
4
* The author of this software is David M. Gay.
5
*
6
* Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
7
*
8
* Permission to use, copy, modify, and distribute this software for any
9
* purpose without fee is hereby granted, provided that this entire notice
10
* is included in all copies of any software which is or includes a copy
11
* or modification of this software and in all copies of the supporting
12
* documentation for such software.
13
*
14
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
15
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
16
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
17
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18
*
19
***************************************************************/
20
21
/* Please send bug reports to David M. Gay (dmg at acm dot org,
22
* with " at " changed at "@" and " dot " changed to "."). */
23
24
/* On a machine with IEEE extended-precision registers, it is
25
* necessary to specify double-precision (53-bit) rounding precision
26
* before invoking strtod or dtoa. If the machine uses (the equivalent
27
* of) Intel 80x87 arithmetic, the call
28
* _control87(PC_53, MCW_PC);
29
* does this with many compilers. Whether this or another call is
30
* appropriate depends on the compiler; for this to work, it may be
31
* necessary to #include "float.h" or another system-dependent header
32
* file.
33
*/
34
35
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
36
*
37
* This strtod returns a nearest machine number to the input decimal
38
* string (or sets errno to ERANGE). With IEEE arithmetic, ties are
39
* broken by the IEEE round-even rule. Otherwise ties are broken by
40
* biased rounding (add half and chop).
41
*
42
* Inspired loosely by William D. Clinger's paper "How to Read Floating
43
* Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
44
*
45
* Modifications:
46
*
47
* 1. We only require IEEE, IBM, or VAX double-precision
48
* arithmetic (not IEEE double-extended).
49
* 2. We get by with floating-point arithmetic in a case that
50
* Clinger missed -- when we're computing d * 10^n
51
* for a small integer d and the integer n is not too
52
* much larger than 22 (the maximum integer k for which
53
* we can represent 10^k exactly), we may be able to
54
* compute (d*10^k) * 10^(e-k) with just one roundoff.
55
* 3. Rather than a bit-at-a-time adjustment of the binary
56
* result in the hard case, we use floating-point
57
* arithmetic to determine the adjustment to within
58
* one bit; only in really hard cases do we need to
59
* compute a second residual.
60
* 4. Because of 3., we don't need a large table of powers of 10
61
* for ten-to-e (just some small tables, e.g. of 10^k
62
* for 0 <= k <= 22).
63
*/
64
65
/*
66
* #define IEEE_8087 for IEEE-arithmetic machines where the least
67
* significant byte has the lowest address.
68
* #define IEEE_MC68k for IEEE-arithmetic machines where the most
69
* significant byte has the lowest address.
70
* #define Long int on machines with 32-bit ints and 64-bit longs.
71
* #define IBM for IBM mainframe-style floating-point arithmetic.
72
* #define VAX for VAX-style floating-point arithmetic (D_floating).
73
* #define No_leftright to omit left-right logic in fast floating-point
74
* computation of dtoa.
75
* #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
76
* and strtod and dtoa should round accordingly.
77
* #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
78
* and Honor_FLT_ROUNDS is not #defined.
79
* #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
80
* that use extended-precision instructions to compute rounded
81
* products and quotients) with IBM.
82
* #define ROUND_BIASED for IEEE-format with biased rounding.
83
* #define Inaccurate_Divide for IEEE-format with correctly rounded
84
* products but inaccurate quotients, e.g., for Intel i860.
85
* #define NO_LONG_LONG on machines that do not have a "long long"
86
* integer type (of >= 64 bits). On such machines, you can
87
* #define Just_16 to store 16 bits per 32-bit Long when doing
88
* high-precision integer arithmetic. Whether this speeds things
89
* up or slows things down depends on the machine and the number
90
* being converted. If long long is available and the name is
91
* something other than "long long", #define Llong to be the name,
92
* and if "unsigned Llong" does not work as an unsigned version of
93
* Llong, #define #ULLong to be the corresponding unsigned type.
94
* #define KR_headers for old-style C function headers.
95
* #define Bad_float_h if your system lacks a float.h or if it does not
96
* define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
97
* FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
98
* #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
99
* if memory is available and otherwise does something you deem
100
* appropriate. If MALLOC is undefined, malloc will be invoked
101
* directly -- and assumed always to succeed. Similarly, if you
102
* want something other than the system's free() to be called to
103
* recycle memory acquired from MALLOC, #define FREE to be the
104
* name of the alternate routine. (Unless you #define
105
* NO_GLOBAL_STATE and call destroydtoa, FREE or free is only
106
* called in pathological cases, e.g., in a dtoa call after a dtoa
107
* return in mode 3 with thousands of digits requested.)
108
* #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
109
* memory allocations from a private pool of memory when possible.
110
* When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
111
* unless #defined to be a different length. This default length
112
* suffices to get rid of MALLOC calls except for unusual cases,
113
* such as decimal-to-binary conversion of a very long string of
114
* digits. The longest string dtoa can return is about 751 bytes
115
* long. For conversions by strtod of strings of 800 digits and
116
* all dtoa conversions in single-threaded executions with 8-byte
117
* pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
118
* pointers, PRIVATE_MEM >= 7112 appears adequate.
119
* #define MULTIPLE_THREADS if the system offers preemptively scheduled
120
* multiple threads. In this case, you must provide (or suitably
121
* #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
122
* by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
123
* in pow5mult, ensures lazy evaluation of only one copy of high
124
* powers of 5; omitting this lock would introduce a small
125
* probability of wasting memory, but would otherwise be harmless.)
126
* You must also invoke freedtoa(s) to free the value s returned by
127
* dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
128
* #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
129
* avoids underflows on inputs whose result does not underflow.
130
* If you #define NO_IEEE_Scale on a machine that uses IEEE-format
131
* floating-point numbers and flushes underflows to zero rather
132
* than implementing gradual underflow, then you must also #define
133
* Sudden_Underflow.
134
* #define USE_LOCALE to use the current locale's decimal_point value.
135
* #define SET_INEXACT if IEEE arithmetic is being used and extra
136
* computation should be done to set the inexact flag when the
137
* result is inexact and avoid setting inexact when the result
138
* is exact. In this case, dtoa.c must be compiled in
139
* an environment, perhaps provided by #include "dtoa.c" in a
140
* suitable wrapper, that defines two functions,
141
* int get_inexact(void);
142
* void clear_inexact(void);
143
* such that get_inexact() returns a nonzero value if the
144
* inexact bit is already set, and clear_inexact() sets the
145
* inexact bit to 0. When SET_INEXACT is #defined, strtod
146
* also does extra computations to set the underflow and overflow
147
* flags when appropriate (i.e., when the result is tiny and
148
* inexact or when it is a numeric value rounded to +-infinity).
149
* #define NO_ERRNO if strtod should not assign errno = ERANGE when
150
* the result overflows to +-Infinity or underflows to 0.
151
* #define NO_GLOBAL_STATE to avoid defining any non-const global or
152
* static variables. Instead the necessary state is stored in an
153
* opaque struct, DtoaState, a pointer to which must be passed to
154
* every entry point. Two new functions are added to the API:
155
* DtoaState *newdtoa(void);
156
* void destroydtoa(DtoaState *);
157
*/
158
159
#ifndef Long
160
#define Long long
161
#endif
162
#ifndef ULong
163
typedef unsigned Long ULong;
164
#endif
165
166
#ifdef DEBUG
167
#include <stdio.h>
168
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
169
#endif
170
171
#include <stdlib.h>
172
#include <string.h>
173
174
#ifdef USE_LOCALE
175
#include <locale.h>
176
#endif
177
178
#ifdef MALLOC
179
#ifdef KR_headers
180
extern char *MALLOC();
181
#else
182
extern void *MALLOC(size_t);
183
#endif
184
#else
185
#define MALLOC malloc
186
#endif
187
188
#ifndef FREE
189
#define FREE free
190
#endif
191
192
#ifndef Omit_Private_Memory
193
#ifndef PRIVATE_MEM
194
#define PRIVATE_MEM 2304
195
#endif
196
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
197
#endif
198
199
#undef IEEE_Arith
200
#undef Avoid_Underflow
201
#ifdef IEEE_MC68k
202
#define IEEE_Arith
203
#endif
204
#ifdef IEEE_8087
205
#define IEEE_Arith
206
#endif
207
208
#include <errno.h>
209
210
#ifdef Bad_float_h
211
212
#ifdef IEEE_Arith
213
#define DBL_DIG 15
214
#define DBL_MAX_10_EXP 308
215
#define DBL_MAX_EXP 1024
216
#define FLT_RADIX 2
217
#endif /*IEEE_Arith*/
218
219
#ifdef IBM
220
#define DBL_DIG 16
221
#define DBL_MAX_10_EXP 75
222
#define DBL_MAX_EXP 63
223
#define FLT_RADIX 16
224
#define DBL_MAX 7.2370055773322621e+75
225
#endif
226
227
#ifdef VAX
228
#define DBL_DIG 16
229
#define DBL_MAX_10_EXP 38
230
#define DBL_MAX_EXP 127
231
#define FLT_RADIX 2
232
#define DBL_MAX 1.7014118346046923e+38
233
#endif
234
235
#ifndef LONG_MAX
236
#define LONG_MAX 2147483647
237
#endif
238
239
#else /* ifndef Bad_float_h */
240
#include <float.h>
241
#endif /* Bad_float_h */
242
243
#ifndef __MATH_H__
244
#include <math.h>
245
#endif
246
247
#ifndef CONST
248
#ifdef KR_headers
249
#define CONST /* blank */
250
#else
251
#define CONST const
252
#endif
253
#endif
254
255
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
256
#error "Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined."
257
#endif
258
259
typedef union { double d; ULong L[2]; } U;
260
261
#define dval(x) ((x).d)
262
#ifdef IEEE_8087
263
#define word0(x) ((x).L[1])
264
#define word1(x) ((x).L[0])
265
#else
266
#define word0(x) ((x).L[0])
267
#define word1(x) ((x).L[1])
268
#endif
269
270
/* The following definition of Storeinc is appropriate for MIPS processors.
271
* An alternative that might be better on some machines is
272
* #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
273
*/
274
#if defined(IEEE_8087) + defined(VAX)
275
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
276
((unsigned short *)a)[0] = (unsigned short)c, a++)
277
#else
278
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
279
((unsigned short *)a)[1] = (unsigned short)c, a++)
280
#endif
281
282
/* #define P DBL_MANT_DIG */
283
/* Ten_pmax = floor(P*log(2)/log(5)) */
284
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
285
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
286
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
287
288
#ifdef IEEE_Arith
289
#define Exp_shift 20
290
#define Exp_shift1 20
291
#define Exp_msk1 0x100000
292
#define Exp_msk11 0x100000
293
#define Exp_mask 0x7ff00000
294
#define P 53
295
#define Bias 1023
296
#define Emin (-1022)
297
#define Exp_1 0x3ff00000
298
#define Exp_11 0x3ff00000
299
#define Ebits 11
300
#define Frac_mask 0xfffff
301
#define Frac_mask1 0xfffff
302
#define Ten_pmax 22
303
#define Bletch 0x10
304
#define Bndry_mask 0xfffff
305
#define Bndry_mask1 0xfffff
306
#define LSB 1
307
#define Sign_bit 0x80000000
308
#define Log2P 1
309
#define Tiny0 0
310
#define Tiny1 1
311
#define Quick_max 14
312
#define Int_max 14
313
#ifndef NO_IEEE_Scale
314
#define Avoid_Underflow
315
#ifdef Flush_Denorm /* debugging option */
316
#undef Sudden_Underflow
317
#endif
318
#endif
319
320
#ifndef Flt_Rounds
321
#ifdef FLT_ROUNDS
322
#define Flt_Rounds FLT_ROUNDS
323
#else
324
#define Flt_Rounds 1
325
#endif
326
#endif /*Flt_Rounds*/
327
328
#ifdef Honor_FLT_ROUNDS
329
#define Rounding rounding
330
#undef Check_FLT_ROUNDS
331
#define Check_FLT_ROUNDS
332
#else
333
#define Rounding Flt_Rounds
334
#endif
335
336
#else /* ifndef IEEE_Arith */
337
#undef Check_FLT_ROUNDS
338
#undef Honor_FLT_ROUNDS
339
#undef SET_INEXACT
340
#undef Sudden_Underflow
341
#define Sudden_Underflow
342
#ifdef IBM
343
#undef Flt_Rounds
344
#define Flt_Rounds 0
345
#define Exp_shift 24
346
#define Exp_shift1 24
347
#define Exp_msk1 0x1000000
348
#define Exp_msk11 0x1000000
349
#define Exp_mask 0x7f000000
350
#define P 14
351
#define Bias 65
352
#define Exp_1 0x41000000
353
#define Exp_11 0x41000000
354
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
355
#define Frac_mask 0xffffff
356
#define Frac_mask1 0xffffff
357
#define Bletch 4
358
#define Ten_pmax 22
359
#define Bndry_mask 0xefffff
360
#define Bndry_mask1 0xffffff
361
#define LSB 1
362
#define Sign_bit 0x80000000
363
#define Log2P 4
364
#define Tiny0 0x100000
365
#define Tiny1 0
366
#define Quick_max 14
367
#define Int_max 15
368
#else /* VAX */
369
#undef Flt_Rounds
370
#define Flt_Rounds 1
371
#define Exp_shift 23
372
#define Exp_shift1 7
373
#define Exp_msk1 0x80
374
#define Exp_msk11 0x800000
375
#define Exp_mask 0x7f80
376
#define P 56
377
#define Bias 129
378
#define Exp_1 0x40800000
379
#define Exp_11 0x4080
380
#define Ebits 8
381
#define Frac_mask 0x7fffff
382
#define Frac_mask1 0xffff007f
383
#define Ten_pmax 24
384
#define Bletch 2
385
#define Bndry_mask 0xffff007f
386
#define Bndry_mask1 0xffff007f
387
#define LSB 0x10000
388
#define Sign_bit 0x8000
389
#define Log2P 1
390
#define Tiny0 0x80
391
#define Tiny1 0
392
#define Quick_max 15
393
#define Int_max 15
394
#endif /* IBM, VAX */
395
#endif /* IEEE_Arith */
396
397
#ifndef IEEE_Arith
398
#define ROUND_BIASED
399
#endif
400
401
#ifdef RND_PRODQUOT
402
#define rounded_product(a,b) a = rnd_prod(a, b)
403
#define rounded_quotient(a,b) a = rnd_quot(a, b)
404
#ifdef KR_headers
405
extern double rnd_prod(), rnd_quot();
406
#else
407
extern double rnd_prod(double, double), rnd_quot(double, double);
408
#endif
409
#else
410
#define rounded_product(a,b) a *= b
411
#define rounded_quotient(a,b) a /= b
412
#endif
413
414
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
415
#define Big1 0xffffffff
416
417
#ifndef Pack_32
418
#define Pack_32
419
#endif
420
421
#ifdef KR_headers
422
#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
423
#else
424
#define FFFFFFFF 0xffffffffUL
425
#endif
426
427
#ifdef NO_LONG_LONG
428
#undef ULLong
429
#ifdef Just_16
430
#undef Pack_32
431
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
432
* This makes some inner loops simpler and sometimes saves work
433
* during multiplications, but it often seems to make things slightly
434
* slower. Hence the default is now to store 32 bits per Long.
435
*/
436
#endif
437
#else /* long long available */
438
#ifndef Llong
439
#define Llong long long
440
#endif
441
#ifndef ULLong
442
#define ULLong unsigned Llong
443
#endif
444
#endif /* NO_LONG_LONG */
445
446
#ifndef MULTIPLE_THREADS
447
#define ACQUIRE_DTOA_LOCK(n) /*nothing*/
448
#define FREE_DTOA_LOCK(n) /*nothing*/
449
#endif
450
451
#define Kmax 7
452
453
struct
454
Bigint {
455
struct Bigint *next;
456
int k, maxwds, sign, wds;
457
ULong x[1];
458
};
459
460
typedef struct Bigint Bigint;
461
462
#ifdef NO_GLOBAL_STATE
463
#ifdef MULTIPLE_THREADS
464
#error "cannot have both NO_GLOBAL_STATE and MULTIPLE_THREADS"
465
#endif
466
struct
467
DtoaState {
468
#define DECLARE_GLOBAL_STATE /* nothing */
469
#else
470
#define DECLARE_GLOBAL_STATE static
471
#endif
472
473
DECLARE_GLOBAL_STATE Bigint *freelist[Kmax+1];
474
DECLARE_GLOBAL_STATE Bigint *p5s;
475
#ifndef Omit_Private_Memory
476
DECLARE_GLOBAL_STATE double private_mem[PRIVATE_mem];
477
DECLARE_GLOBAL_STATE double *pmem_next
478
#ifndef NO_GLOBAL_STATE
479
= private_mem
480
#endif
481
;
482
#endif
483
#ifdef NO_GLOBAL_STATE
484
};
485
typedef struct DtoaState DtoaState;
486
#ifdef KR_headers
487
#define STATE_PARAM state,
488
#define STATE_PARAM_DECL DtoaState *state;
489
#else
490
#define STATE_PARAM DtoaState *state,
491
#endif
492
#define PASS_STATE state,
493
#define GET_STATE(field) (state->field)
494
495
static DtoaState *
496
newdtoa(void)
497
{
498
DtoaState *state = (DtoaState *) MALLOC(sizeof(DtoaState));
499
if (state) {
500
memset(state, 0, sizeof(DtoaState));
501
#ifndef Omit_Private_Memory
502
state->pmem_next = state->private_mem;
503
#endif
504
}
505
return state;
506
}
507
508
static void
509
destroydtoa
510
#ifdef KR_headers
511
(state) STATE_PARAM_DECL
512
#else
513
(DtoaState *state)
514
#endif
515
{
516
int i;
517
Bigint *v, *next;
518
519
for (i = 0; i <= Kmax; i++) {
520
for (v = GET_STATE(freelist)[i]; v; v = next) {
521
next = v->next;
522
#ifndef Omit_Private_Memory
523
if ((double*)v < GET_STATE(private_mem) ||
524
(double*)v >= GET_STATE(private_mem) + PRIVATE_mem)
525
#endif
526
FREE((void*)v);
527
}
528
}
529
#ifdef Omit_Private_Memory
530
Bigint* p5 = GET_STATE(p5s);
531
while (p5) {
532
Bigint* tmp = p5;
533
p5 = p5->next;
534
FREE(tmp);
535
}
536
#endif
537
FREE((void *)state);
538
}
539
540
#else
541
#define STATE_PARAM /* nothing */
542
#define STATE_PARAM_DECL /* nothing */
543
#define PASS_STATE /* nothing */
544
#define GET_STATE(name) name
545
#endif
546
547
static Bigint *
548
Balloc
549
#ifdef KR_headers
550
(STATE_PARAM k) STATE_PARAM_DECL int k;
551
#else
552
(STATE_PARAM int k)
553
#endif
554
{
555
int x;
556
Bigint *rv;
557
#ifndef Omit_Private_Memory
558
size_t len;
559
#endif
560
561
ACQUIRE_DTOA_LOCK(0);
562
/* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
563
/* but this case seems very unlikely. */
564
if (k <= Kmax && (rv = GET_STATE(freelist)[k]))
565
GET_STATE(freelist)[k] = rv->next;
566
else {
567
x = 1 << k;
568
#ifdef Omit_Private_Memory
569
rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
570
#else
571
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
572
/sizeof(double);
573
if (k <= Kmax && GET_STATE(pmem_next) - GET_STATE(private_mem) + len <= PRIVATE_mem) {
574
rv = (Bigint*)GET_STATE(pmem_next);
575
GET_STATE(pmem_next) += len;
576
}
577
else
578
rv = (Bigint*)MALLOC(len*sizeof(double));
579
#endif
580
rv->k = k;
581
rv->maxwds = x;
582
}
583
FREE_DTOA_LOCK(0);
584
rv->sign = rv->wds = 0;
585
return rv;
586
}
587
588
static void
589
Bfree
590
#ifdef KR_headers
591
(STATE_PARAM v) STATE_PARAM_DECL Bigint *v;
592
#else
593
(STATE_PARAM Bigint *v)
594
#endif
595
{
596
if (v) {
597
if (v->k > Kmax)
598
FREE((void*)v);
599
else {
600
ACQUIRE_DTOA_LOCK(0);
601
v->next = GET_STATE(freelist)[v->k];
602
GET_STATE(freelist)[v->k] = v;
603
FREE_DTOA_LOCK(0);
604
}
605
}
606
}
607
608
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
609
y->wds*sizeof(Long) + 2*sizeof(int))
610
611
static Bigint *
612
multadd
613
#ifdef KR_headers
614
(STATE_PARAM b, m, a) STATE_PARAM_DECL Bigint *b; int m, a;
615
#else
616
(STATE_PARAM Bigint *b, int m, int a) /* multiply by m and add a */
617
#endif
618
{
619
int i, wds;
620
#ifdef ULLong
621
ULong *x;
622
ULLong carry, y;
623
#else
624
ULong carry, *x, y;
625
#ifdef Pack_32
626
ULong xi, z;
627
#endif
628
#endif
629
Bigint *b1;
630
631
wds = b->wds;
632
x = b->x;
633
i = 0;
634
carry = a;
635
do {
636
#ifdef ULLong
637
y = *x * (ULLong)m + carry;
638
carry = y >> 32;
639
*x++ = (ULong) y & FFFFFFFF;
640
#else
641
#ifdef Pack_32
642
xi = *x;
643
y = (xi & 0xffff) * m + carry;
644
z = (xi >> 16) * m + (y >> 16);
645
carry = z >> 16;
646
*x++ = (z << 16) + (y & 0xffff);
647
#else
648
y = *x * m + carry;
649
carry = y >> 16;
650
*x++ = y & 0xffff;
651
#endif
652
#endif
653
}
654
while(++i < wds);
655
if (carry) {
656
if (wds >= b->maxwds) {
657
b1 = Balloc(PASS_STATE b->k+1);
658
Bcopy(b1, b);
659
Bfree(PASS_STATE b);
660
b = b1;
661
}
662
b->x[wds++] = (ULong) carry;
663
b->wds = wds;
664
}
665
return b;
666
}
667
668
static Bigint *
669
s2b
670
#ifdef KR_headers
671
(STATE_PARAM s, nd0, nd, y9) STATE_PARAM_DECL CONST char *s; int nd0, nd; ULong y9;
672
#else
673
(STATE_PARAM CONST char *s, int nd0, int nd, ULong y9)
674
#endif
675
{
676
Bigint *b;
677
int i, k;
678
Long x, y;
679
680
x = (nd + 8) / 9;
681
for(k = 0, y = 1; x > y; y <<= 1, k++) ;
682
#ifdef Pack_32
683
b = Balloc(PASS_STATE k);
684
b->x[0] = y9;
685
b->wds = 1;
686
#else
687
b = Balloc(PASS_STATE k+1);
688
b->x[0] = y9 & 0xffff;
689
b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
690
#endif
691
692
i = 9;
693
if (9 < nd0) {
694
s += 9;
695
do b = multadd(PASS_STATE b, 10, *s++ - '0');
696
while(++i < nd0);
697
s++;
698
}
699
else
700
s += 10;
701
for(; i < nd; i++)
702
b = multadd(PASS_STATE b, 10, *s++ - '0');
703
return b;
704
}
705
706
static int
707
hi0bits
708
#ifdef KR_headers
709
(x) ULong x;
710
#else
711
(ULong x)
712
#endif
713
{
714
int k = 0;
715
716
if (!(x & 0xffff0000)) {
717
k = 16;
718
x <<= 16;
719
}
720
if (!(x & 0xff000000)) {
721
k += 8;
722
x <<= 8;
723
}
724
if (!(x & 0xf0000000)) {
725
k += 4;
726
x <<= 4;
727
}
728
if (!(x & 0xc0000000)) {
729
k += 2;
730
x <<= 2;
731
}
732
if (!(x & 0x80000000)) {
733
k++;
734
if (!(x & 0x40000000))
735
return 32;
736
}
737
return k;
738
}
739
740
static int
741
lo0bits
742
#ifdef KR_headers
743
(y) ULong *y;
744
#else
745
(ULong *y)
746
#endif
747
{
748
int k;
749
ULong x = *y;
750
751
if (x & 7) {
752
if (x & 1)
753
return 0;
754
if (x & 2) {
755
*y = x >> 1;
756
return 1;
757
}
758
*y = x >> 2;
759
return 2;
760
}
761
k = 0;
762
if (!(x & 0xffff)) {
763
k = 16;
764
x >>= 16;
765
}
766
if (!(x & 0xff)) {
767
k += 8;
768
x >>= 8;
769
}
770
if (!(x & 0xf)) {
771
k += 4;
772
x >>= 4;
773
}
774
if (!(x & 0x3)) {
775
k += 2;
776
x >>= 2;
777
}
778
if (!(x & 1)) {
779
k++;
780
x >>= 1;
781
if (!x)
782
return 32;
783
}
784
*y = x;
785
return k;
786
}
787
788
static Bigint *
789
i2b
790
#ifdef KR_headers
791
(STATE_PARAM i) STATE_PARAM_DECL int i;
792
#else
793
(STATE_PARAM int i)
794
#endif
795
{
796
Bigint *b;
797
798
b = Balloc(PASS_STATE 1);
799
b->x[0] = i;
800
b->wds = 1;
801
return b;
802
}
803
804
static Bigint *
805
mult
806
#ifdef KR_headers
807
(STATE_PARAM a, b) STATE_PARAM_DECL Bigint *a, *b;
808
#else
809
(STATE_PARAM Bigint *a, Bigint *b)
810
#endif
811
{
812
Bigint *c;
813
int k, wa, wb, wc;
814
ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
815
ULong y;
816
#ifdef ULLong
817
ULLong carry, z;
818
#else
819
ULong carry, z;
820
#ifdef Pack_32
821
ULong z2;
822
#endif
823
#endif
824
825
if (a->wds < b->wds) {
826
c = a;
827
a = b;
828
b = c;
829
}
830
k = a->k;
831
wa = a->wds;
832
wb = b->wds;
833
wc = wa + wb;
834
if (wc > a->maxwds)
835
k++;
836
c = Balloc(PASS_STATE k);
837
for(x = c->x, xa = x + wc; x < xa; x++)
838
*x = 0;
839
xa = a->x;
840
xae = xa + wa;
841
xb = b->x;
842
xbe = xb + wb;
843
xc0 = c->x;
844
#ifdef ULLong
845
for(; xb < xbe; xc0++) {
846
if ((y = *xb++)) {
847
x = xa;
848
xc = xc0;
849
carry = 0;
850
do {
851
z = *x++ * (ULLong)y + *xc + carry;
852
carry = z >> 32;
853
*xc++ = (ULong) z & FFFFFFFF;
854
}
855
while(x < xae);
856
*xc = (ULong) carry;
857
}
858
}
859
#else
860
#ifdef Pack_32
861
for(; xb < xbe; xb++, xc0++) {
862
if (y = *xb & 0xffff) {
863
x = xa;
864
xc = xc0;
865
carry = 0;
866
do {
867
z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
868
carry = z >> 16;
869
z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
870
carry = z2 >> 16;
871
Storeinc(xc, z2, z);
872
}
873
while(x < xae);
874
*xc = carry;
875
}
876
if (y = *xb >> 16) {
877
x = xa;
878
xc = xc0;
879
carry = 0;
880
z2 = *xc;
881
do {
882
z = (*x & 0xffff) * y + (*xc >> 16) + carry;
883
carry = z >> 16;
884
Storeinc(xc, z, z2);
885
z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
886
carry = z2 >> 16;
887
}
888
while(x < xae);
889
*xc = z2;
890
}
891
}
892
#else
893
for(; xb < xbe; xc0++) {
894
if (y = *xb++) {
895
x = xa;
896
xc = xc0;
897
carry = 0;
898
do {
899
z = *x++ * y + *xc + carry;
900
carry = z >> 16;
901
*xc++ = z & 0xffff;
902
}
903
while(x < xae);
904
*xc = carry;
905
}
906
}
907
#endif
908
#endif
909
for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
910
c->wds = wc;
911
return c;
912
}
913
914
static Bigint *
915
pow5mult
916
#ifdef KR_headers
917
(STATE_PARAM b, k) STATE_PARAM_DECL Bigint *b; int k;
918
#else
919
(STATE_PARAM Bigint *b, int k)
920
#endif
921
{
922
Bigint *b1, *p5, *p51;
923
int i;
924
static CONST int p05[3] = { 5, 25, 125 };
925
926
if ((i = k & 3))
927
b = multadd(PASS_STATE b, p05[i-1], 0);
928
929
if (!(k >>= 2))
930
return b;
931
if (!(p5 = GET_STATE(p5s))) {
932
/* first time */
933
#ifdef MULTIPLE_THREADS
934
ACQUIRE_DTOA_LOCK(1);
935
if (!(p5 = p5s)) {
936
p5 = p5s = i2b(625);
937
p5->next = 0;
938
}
939
FREE_DTOA_LOCK(1);
940
#else
941
p5 = GET_STATE(p5s) = i2b(PASS_STATE 625);
942
p5->next = 0;
943
#endif
944
}
945
for(;;) {
946
if (k & 1) {
947
b1 = mult(PASS_STATE b, p5);
948
Bfree(PASS_STATE b);
949
b = b1;
950
}
951
if (!(k >>= 1))
952
break;
953
if (!(p51 = p5->next)) {
954
#ifdef MULTIPLE_THREADS
955
ACQUIRE_DTOA_LOCK(1);
956
if (!(p51 = p5->next)) {
957
p51 = p5->next = mult(p5,p5);
958
p51->next = 0;
959
}
960
FREE_DTOA_LOCK(1);
961
#else
962
p51 = p5->next = mult(PASS_STATE p5,p5);
963
p51->next = 0;
964
#endif
965
}
966
p5 = p51;
967
}
968
return b;
969
}
970
971
static Bigint *
972
lshift
973
#ifdef KR_headers
974
(STATE_PARAM b, k) STATE_PARAM_DECL Bigint *b; int k;
975
#else
976
(STATE_PARAM Bigint *b, int k)
977
#endif
978
{
979
int i, k1, n, n1;
980
Bigint *b1;
981
ULong *x, *x1, *xe, z;
982
983
#ifdef Pack_32
984
n = k >> 5;
985
#else
986
n = k >> 4;
987
#endif
988
k1 = b->k;
989
n1 = n + b->wds + 1;
990
for(i = b->maxwds; n1 > i; i <<= 1)
991
k1++;
992
b1 = Balloc(PASS_STATE k1);
993
x1 = b1->x;
994
for(i = 0; i < n; i++)
995
*x1++ = 0;
996
x = b->x;
997
xe = x + b->wds;
998
#ifdef Pack_32
999
if (k &= 0x1f) {
1000
k1 = 32 - k;
1001
z = 0;
1002
do {
1003
*x1++ = *x << k | z;
1004
z = *x++ >> k1;
1005
}
1006
while(x < xe);
1007
if ((*x1 = z))
1008
++n1;
1009
}
1010
#else
1011
if (k &= 0xf) {
1012
k1 = 16 - k;
1013
z = 0;
1014
do {
1015
*x1++ = *x << k & 0xffff | z;
1016
z = *x++ >> k1;
1017
}
1018
while(x < xe);
1019
if (*x1 = z)
1020
++n1;
1021
}
1022
#endif
1023
else do
1024
*x1++ = *x++;
1025
while(x < xe);
1026
b1->wds = n1 - 1;
1027
Bfree(PASS_STATE b);
1028
return b1;
1029
}
1030
1031
static int
1032
cmp
1033
#ifdef KR_headers
1034
(a, b) Bigint *a, *b;
1035
#else
1036
(Bigint *a, Bigint *b)
1037
#endif
1038
{
1039
ULong *xa, *xa0, *xb, *xb0;
1040
int i, j;
1041
1042
i = a->wds;
1043
j = b->wds;
1044
#ifdef DEBUG
1045
if (i > 1 && !a->x[i-1])
1046
Bug("cmp called with a->x[a->wds-1] == 0");
1047
if (j > 1 && !b->x[j-1])
1048
Bug("cmp called with b->x[b->wds-1] == 0");
1049
#endif
1050
if (i -= j)
1051
return i;
1052
xa0 = a->x;
1053
xa = xa0 + j;
1054
xb0 = b->x;
1055
xb = xb0 + j;
1056
for(;;) {
1057
if (*--xa != *--xb)
1058
return *xa < *xb ? -1 : 1;
1059
if (xa <= xa0)
1060
break;
1061
}
1062
return 0;
1063
}
1064
1065
static Bigint *
1066
diff
1067
#ifdef KR_headers
1068
(STATE_PARAM a, b) STATE_PARAM_DECL Bigint *a, *b;
1069
#else
1070
(STATE_PARAM Bigint *a, Bigint *b)
1071
#endif
1072
{
1073
Bigint *c;
1074
int i, wa, wb;
1075
ULong *xa, *xae, *xb, *xbe, *xc;
1076
#ifdef ULLong
1077
ULLong borrow, y;
1078
#else
1079
ULong borrow, y;
1080
#ifdef Pack_32
1081
ULong z;
1082
#endif
1083
#endif
1084
1085
i = cmp(a,b);
1086
if (!i) {
1087
c = Balloc(PASS_STATE 0);
1088
c->wds = 1;
1089
c->x[0] = 0;
1090
return c;
1091
}
1092
if (i < 0) {
1093
c = a;
1094
a = b;
1095
b = c;
1096
i = 1;
1097
}
1098
else
1099
i = 0;
1100
c = Balloc(PASS_STATE a->k);
1101
c->sign = i;
1102
wa = a->wds;
1103
xa = a->x;
1104
xae = xa + wa;
1105
wb = b->wds;
1106
xb = b->x;
1107
xbe = xb + wb;
1108
xc = c->x;
1109
borrow = 0;
1110
#ifdef ULLong
1111
do {
1112
y = (ULLong)*xa++ - *xb++ - borrow;
1113
borrow = y >> 32 & (ULong)1;
1114
*xc++ = (ULong) y & FFFFFFFF;
1115
}
1116
while(xb < xbe);
1117
while(xa < xae) {
1118
y = *xa++ - borrow;
1119
borrow = y >> 32 & (ULong)1;
1120
*xc++ = (ULong) y & FFFFFFFF;
1121
}
1122
#else
1123
#ifdef Pack_32
1124
do {
1125
y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1126
borrow = (y & 0x10000) >> 16;
1127
z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1128
borrow = (z & 0x10000) >> 16;
1129
Storeinc(xc, z, y);
1130
}
1131
while(xb < xbe);
1132
while(xa < xae) {
1133
y = (*xa & 0xffff) - borrow;
1134
borrow = (y & 0x10000) >> 16;
1135
z = (*xa++ >> 16) - borrow;
1136
borrow = (z & 0x10000) >> 16;
1137
Storeinc(xc, z, y);
1138
}
1139
#else
1140
do {
1141
y = *xa++ - *xb++ - borrow;
1142
borrow = (y & 0x10000) >> 16;
1143
*xc++ = y & 0xffff;
1144
}
1145
while(xb < xbe);
1146
while(xa < xae) {
1147
y = *xa++ - borrow;
1148
borrow = (y & 0x10000) >> 16;
1149
*xc++ = y & 0xffff;
1150
}
1151
#endif
1152
#endif
1153
while(!*--xc)
1154
wa--;
1155
c->wds = wa;
1156
return c;
1157
}
1158
1159
static double
1160
ulp
1161
#ifdef KR_headers
1162
(x) U x;
1163
#else
1164
(U x)
1165
#endif
1166
{
1167
Long L;
1168
U a;
1169
1170
L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1171
#ifndef Avoid_Underflow
1172
#ifndef Sudden_Underflow
1173
if (L > 0) {
1174
#endif
1175
#endif
1176
#ifdef IBM
1177
L |= Exp_msk1 >> 4;
1178
#endif
1179
word0(a) = L;
1180
word1(a) = 0;
1181
#ifndef Avoid_Underflow
1182
#ifndef Sudden_Underflow
1183
}
1184
else {
1185
L = -L >> Exp_shift;
1186
if (L < Exp_shift) {
1187
word0(a) = 0x80000 >> L;
1188
word1(a) = 0;
1189
}
1190
else {
1191
word0(a) = 0;
1192
L -= Exp_shift;
1193
word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1194
}
1195
}
1196
#endif
1197
#endif
1198
return dval(a);
1199
}
1200
1201
static double
1202
b2d
1203
#ifdef KR_headers
1204
(a, e) Bigint *a; int *e;
1205
#else
1206
(Bigint *a, int *e)
1207
#endif
1208
{
1209
ULong *xa, *xa0, w, y, z;
1210
int k;
1211
U d;
1212
#ifdef VAX
1213
ULong d0, d1;
1214
#else
1215
#define d0 word0(d)
1216
#define d1 word1(d)
1217
#endif
1218
1219
xa0 = a->x;
1220
xa = xa0 + a->wds;
1221
y = *--xa;
1222
#ifdef DEBUG
1223
if (!y) Bug("zero y in b2d");
1224
#endif
1225
k = hi0bits(y);
1226
*e = 32 - k;
1227
#ifdef Pack_32
1228
if (k < Ebits) {
1229
d0 = Exp_1 | y >> (Ebits - k);
1230
w = xa > xa0 ? *--xa : 0;
1231
d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1232
goto ret_d;
1233
}
1234
z = xa > xa0 ? *--xa : 0;
1235
if (k -= Ebits) {
1236
d0 = Exp_1 | y << k | z >> (32 - k);
1237
y = xa > xa0 ? *--xa : 0;
1238
d1 = z << k | y >> (32 - k);
1239
}
1240
else {
1241
d0 = Exp_1 | y;
1242
d1 = z;
1243
}
1244
#else
1245
if (k < Ebits + 16) {
1246
z = xa > xa0 ? *--xa : 0;
1247
d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1248
w = xa > xa0 ? *--xa : 0;
1249
y = xa > xa0 ? *--xa : 0;
1250
d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1251
goto ret_d;
1252
}
1253
z = xa > xa0 ? *--xa : 0;
1254
w = xa > xa0 ? *--xa : 0;
1255
k -= Ebits + 16;
1256
d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1257
y = xa > xa0 ? *--xa : 0;
1258
d1 = w << k + 16 | y << k;
1259
#endif
1260
ret_d:
1261
#ifdef VAX
1262
word0(d) = d0 >> 16 | d0 << 16;
1263
word1(d) = d1 >> 16 | d1 << 16;
1264
#else
1265
#undef d0
1266
#undef d1
1267
#endif
1268
return dval(d);
1269
}
1270
1271
static Bigint *
1272
d2b
1273
#ifdef KR_headers
1274
(STATE_PARAM d, e, bits) STATE_PARAM_DECL U d; int *e, *bits;
1275
#else
1276
(STATE_PARAM U d, int *e, int *bits)
1277
#endif
1278
{
1279
Bigint *b;
1280
int de, k;
1281
ULong *x, y, z;
1282
#ifndef Sudden_Underflow
1283
int i;
1284
#endif
1285
#ifdef VAX
1286
ULong d0, d1;
1287
d0 = word0(d) >> 16 | word0(d) << 16;
1288
d1 = word1(d) >> 16 | word1(d) << 16;
1289
#else
1290
#define d0 word0(d)
1291
#define d1 word1(d)
1292
#endif
1293
1294
#ifdef Pack_32
1295
b = Balloc(PASS_STATE 1);
1296
#else
1297
b = Balloc(PASS_STATE 2);
1298
#endif
1299
x = b->x;
1300
1301
z = d0 & Frac_mask;
1302
d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1303
#ifdef Sudden_Underflow
1304
de = (int)(d0 >> Exp_shift);
1305
#ifndef IBM
1306
z |= Exp_msk11;
1307
#endif
1308
#else
1309
if ((de = (int)(d0 >> Exp_shift)))
1310
z |= Exp_msk1;
1311
#endif
1312
#ifdef Pack_32
1313
if ((y = d1)) {
1314
if ((k = lo0bits(&y))) {
1315
x[0] = y | z << (32 - k);
1316
z >>= k;
1317
}
1318
else
1319
x[0] = y;
1320
#ifndef Sudden_Underflow
1321
i =
1322
#endif
1323
b->wds = (x[1] = z) ? 2 : 1;
1324
}
1325
else {
1326
k = lo0bits(&z);
1327
x[0] = z;
1328
#ifndef Sudden_Underflow
1329
i =
1330
#endif
1331
b->wds = 1;
1332
k += 32;
1333
}
1334
#else
1335
if (y = d1) {
1336
if (k = lo0bits(&y))
1337
if (k >= 16) {
1338
x[0] = y | z << 32 - k & 0xffff;
1339
x[1] = z >> k - 16 & 0xffff;
1340
x[2] = z >> k;
1341
i = 2;
1342
}
1343
else {
1344
x[0] = y & 0xffff;
1345
x[1] = y >> 16 | z << 16 - k & 0xffff;
1346
x[2] = z >> k & 0xffff;
1347
x[3] = z >> k+16;
1348
i = 3;
1349
}
1350
else {
1351
x[0] = y & 0xffff;
1352
x[1] = y >> 16;
1353
x[2] = z & 0xffff;
1354
x[3] = z >> 16;
1355
i = 3;
1356
}
1357
}
1358
else {
1359
#ifdef DEBUG
1360
if (!z)
1361
Bug("Zero passed to d2b");
1362
#endif
1363
k = lo0bits(&z);
1364
if (k >= 16) {
1365
x[0] = z;
1366
i = 0;
1367
}
1368
else {
1369
x[0] = z & 0xffff;
1370
x[1] = z >> 16;
1371
i = 1;
1372
}
1373
k += 32;
1374
}
1375
while(!x[i])
1376
--i;
1377
b->wds = i + 1;
1378
#endif
1379
#ifndef Sudden_Underflow
1380
if (de) {
1381
#endif
1382
#ifdef IBM
1383
*e = (de - Bias - (P-1) << 2) + k;
1384
*bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1385
#else
1386
*e = de - Bias - (P-1) + k;
1387
*bits = P - k;
1388
#endif
1389
#ifndef Sudden_Underflow
1390
}
1391
else {
1392
*e = de - Bias - (P-1) + 1 + k;
1393
#ifdef Pack_32
1394
*bits = 32*i - hi0bits(x[i-1]);
1395
#else
1396
*bits = (i+2)*16 - hi0bits(x[i]);
1397
#endif
1398
}
1399
#endif
1400
return b;
1401
}
1402
#undef d0
1403
#undef d1
1404
1405
static double
1406
ratio
1407
#ifdef KR_headers
1408
(a, b) Bigint *a, *b;
1409
#else
1410
(Bigint *a, Bigint *b)
1411
#endif
1412
{
1413
U da, db;
1414
int k, ka, kb;
1415
1416
dval(da) = b2d(a, &ka);
1417
dval(db) = b2d(b, &kb);
1418
#ifdef Pack_32
1419
k = ka - kb + 32*(a->wds - b->wds);
1420
#else
1421
k = ka - kb + 16*(a->wds - b->wds);
1422
#endif
1423
#ifdef IBM
1424
if (k > 0) {
1425
word0(da) += (k >> 2)*Exp_msk1;
1426
if (k &= 3)
1427
dval(da) *= 1 << k;
1428
}
1429
else {
1430
k = -k;
1431
word0(db) += (k >> 2)*Exp_msk1;
1432
if (k &= 3)
1433
dval(db) *= 1 << k;
1434
}
1435
#else
1436
if (k > 0)
1437
word0(da) += k*Exp_msk1;
1438
else {
1439
k = -k;
1440
word0(db) += k*Exp_msk1;
1441
}
1442
#endif
1443
return dval(da) / dval(db);
1444
}
1445
1446
static CONST double
1447
tens[] = {
1448
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1449
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1450
1e20, 1e21, 1e22
1451
#ifdef VAX
1452
, 1e23, 1e24
1453
#endif
1454
};
1455
1456
static CONST double
1457
#ifdef IEEE_Arith
1458
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1459
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1460
#ifdef Avoid_Underflow
1461
9007199254740992.*9007199254740992.e-256
1462
/* = 2^106 * 1e-53 */
1463
#else
1464
1e-256
1465
#endif
1466
};
1467
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1468
/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1469
#define Scale_Bit 0x10
1470
#define n_bigtens 5
1471
#else
1472
#ifdef IBM
1473
bigtens[] = { 1e16, 1e32, 1e64 };
1474
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1475
#define n_bigtens 3
1476
#else
1477
bigtens[] = { 1e16, 1e32 };
1478
static CONST double tinytens[] = { 1e-16, 1e-32 };
1479
#define n_bigtens 2
1480
#endif
1481
#endif
1482
1483
static double
1484
_strtod
1485
#ifdef KR_headers
1486
(STATE_PARAM s00, se) STATE_PARAM_DECL CONST char *s00; char **se;
1487
#else
1488
(STATE_PARAM CONST char *s00, char **se)
1489
#endif
1490
{
1491
#ifdef Avoid_Underflow
1492
int scale;
1493
#endif
1494
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1495
e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1496
CONST char *s, *s0, *s1;
1497
double aadj, adj;
1498
U aadj1, rv, rv0;
1499
Long L;
1500
ULong y, z;
1501
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1502
#ifdef SET_INEXACT
1503
int inexact, oldinexact;
1504
#endif
1505
#ifdef Honor_FLT_ROUNDS
1506
int rounding;
1507
#endif
1508
#ifdef USE_LOCALE
1509
CONST char *s2;
1510
#endif
1511
1512
#ifdef __GNUC__
1513
delta = bb = bd = bs = 0;
1514
#endif
1515
1516
sign = nz0 = nz = 0;
1517
dval(rv) = 0.;
1518
for(s = s00;;s++) switch(*s) {
1519
case '-':
1520
sign = 1;
1521
/* no break */
1522
case '+':
1523
if (*++s)
1524
goto break2;
1525
/* no break */
1526
case 0:
1527
goto ret0;
1528
case '\t':
1529
case '\n':
1530
case '\v':
1531
case '\f':
1532
case '\r':
1533
case ' ':
1534
continue;
1535
default:
1536
goto break2;
1537
}
1538
break2:
1539
if (*s == '0') {
1540
nz0 = 1;
1541
while(*++s == '0') ;
1542
if (!*s)
1543
goto ret;
1544
}
1545
s0 = s;
1546
y = z = 0;
1547
for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1548
if (nd < 9)
1549
y = 10*y + c - '0';
1550
else if (nd < 16)
1551
z = 10*z + c - '0';
1552
nd0 = nd;
1553
#ifdef USE_LOCALE
1554
s1 = localeconv()->decimal_point;
1555
if (c == *s1) {
1556
c = '.';
1557
if (*++s1) {
1558
s2 = s;
1559
for(;;) {
1560
if (*++s2 != *s1) {
1561
c = 0;
1562
break;
1563
}
1564
if (!*++s1) {
1565
s = s2;
1566
break;
1567
}
1568
}
1569
}
1570
}
1571
#endif
1572
if (c == '.') {
1573
c = *++s;
1574
if (!nd) {
1575
for(; c == '0'; c = *++s)
1576
nz++;
1577
if (c > '0' && c <= '9') {
1578
s0 = s;
1579
nf += nz;
1580
nz = 0;
1581
goto have_dig;
1582
}
1583
goto dig_done;
1584
}
1585
for(; c >= '0' && c <= '9'; c = *++s) {
1586
have_dig:
1587
nz++;
1588
if (c -= '0') {
1589
nf += nz;
1590
for(i = 1; i < nz; i++)
1591
if (nd++ < 9)
1592
y *= 10;
1593
else if (nd <= DBL_DIG + 1)
1594
z *= 10;
1595
if (nd++ < 9)
1596
y = 10*y + c;
1597
else if (nd <= DBL_DIG + 1)
1598
z = 10*z + c;
1599
nz = 0;
1600
}
1601
}
1602
}
1603
dig_done:
1604
e = 0;
1605
if (c == 'e' || c == 'E') {
1606
if (!nd && !nz && !nz0) {
1607
goto ret0;
1608
}
1609
s00 = s;
1610
esign = 0;
1611
switch(c = *++s) {
1612
case '-':
1613
esign = 1;
1614
case '+':
1615
c = *++s;
1616
}
1617
if (c >= '0' && c <= '9') {
1618
while(c == '0')
1619
c = *++s;
1620
if (c > '0' && c <= '9') {
1621
L = c - '0';
1622
s1 = s;
1623
while((c = *++s) >= '0' && c <= '9')
1624
L = 10*L + c - '0';
1625
if (s - s1 > 8 || L > 19999)
1626
/* Avoid confusion from exponents
1627
* so large that e might overflow.
1628
*/
1629
e = 19999; /* safe for 16 bit ints */
1630
else
1631
e = (int)L;
1632
if (esign)
1633
e = -e;
1634
}
1635
else
1636
e = 0;
1637
}
1638
else
1639
s = s00;
1640
}
1641
if (!nd) {
1642
if (!nz && !nz0) {
1643
ret0:
1644
s = s00;
1645
sign = 0;
1646
}
1647
goto ret;
1648
}
1649
e1 = e -= nf;
1650
1651
/* Now we have nd0 digits, starting at s0, followed by a
1652
* decimal point, followed by nd-nd0 digits. The number we're
1653
* after is the integer represented by those digits times
1654
* 10**e */
1655
1656
if (!nd0)
1657
nd0 = nd;
1658
k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1659
dval(rv) = y;
1660
if (k > 9) {
1661
#ifdef SET_INEXACT
1662
if (k > DBL_DIG)
1663
oldinexact = get_inexact();
1664
#endif
1665
dval(rv) = tens[k - 9] * dval(rv) + z;
1666
}
1667
bd0 = 0;
1668
if (nd <= DBL_DIG
1669
#ifndef RND_PRODQUOT
1670
#ifndef Honor_FLT_ROUNDS
1671
&& Flt_Rounds == 1
1672
#endif
1673
#endif
1674
) {
1675
if (!e)
1676
goto ret;
1677
if (e > 0) {
1678
if (e <= Ten_pmax) {
1679
#ifdef VAX
1680
goto vax_ovfl_check;
1681
#else
1682
#ifdef Honor_FLT_ROUNDS
1683
/* round correctly FLT_ROUNDS = 2 or 3 */
1684
if (sign) {
1685
rv = -rv;
1686
sign = 0;
1687
}
1688
#endif
1689
/* rv = */ rounded_product(dval(rv), tens[e]);
1690
goto ret;
1691
#endif
1692
}
1693
i = DBL_DIG - nd;
1694
if (e <= Ten_pmax + i) {
1695
/* A fancier test would sometimes let us do
1696
* this for larger i values.
1697
*/
1698
#ifdef Honor_FLT_ROUNDS
1699
/* round correctly FLT_ROUNDS = 2 or 3 */
1700
if (sign) {
1701
rv = -rv;
1702
sign = 0;
1703
}
1704
#endif
1705
e -= i;
1706
dval(rv) *= tens[i];
1707
#ifdef VAX
1708
/* VAX exponent range is so narrow we must
1709
* worry about overflow here...
1710
*/
1711
vax_ovfl_check:
1712
word0(rv) -= P*Exp_msk1;
1713
/* rv = */ rounded_product(dval(rv), tens[e]);
1714
if ((word0(rv) & Exp_mask)
1715
> Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1716
goto ovfl;
1717
word0(rv) += P*Exp_msk1;
1718
#else
1719
/* rv = */ rounded_product(dval(rv), tens[e]);
1720
#endif
1721
goto ret;
1722
}
1723
}
1724
#ifndef Inaccurate_Divide
1725
else if (e >= -Ten_pmax) {
1726
#ifdef Honor_FLT_ROUNDS
1727
/* round correctly FLT_ROUNDS = 2 or 3 */
1728
if (sign) {
1729
rv = -rv;
1730
sign = 0;
1731
}
1732
#endif
1733
/* rv = */ rounded_quotient(dval(rv), tens[-e]);
1734
goto ret;
1735
}
1736
#endif
1737
}
1738
e1 += nd - k;
1739
1740
#ifdef IEEE_Arith
1741
#ifdef SET_INEXACT
1742
inexact = 1;
1743
if (k <= DBL_DIG)
1744
oldinexact = get_inexact();
1745
#endif
1746
#ifdef Avoid_Underflow
1747
scale = 0;
1748
#endif
1749
#ifdef Honor_FLT_ROUNDS
1750
if ((rounding = Flt_Rounds) >= 2) {
1751
if (sign)
1752
rounding = rounding == 2 ? 0 : 2;
1753
else
1754
if (rounding != 2)
1755
rounding = 0;
1756
}
1757
#endif
1758
#endif /*IEEE_Arith*/
1759
1760
/* Get starting approximation = rv * 10**e1 */
1761
1762
if (e1 > 0) {
1763
if ((i = e1 & 15))
1764
dval(rv) *= tens[i];
1765
if (e1 &= ~15) {
1766
if (e1 > DBL_MAX_10_EXP) {
1767
ovfl:
1768
#ifndef NO_ERRNO
1769
errno = ERANGE;
1770
#endif
1771
/* Can't trust HUGE_VAL */
1772
#ifdef IEEE_Arith
1773
#ifdef Honor_FLT_ROUNDS
1774
switch(rounding) {
1775
case 0: /* toward 0 */
1776
case 3: /* toward -infinity */
1777
word0(rv) = Big0;
1778
word1(rv) = Big1;
1779
break;
1780
default:
1781
word0(rv) = Exp_mask;
1782
word1(rv) = 0;
1783
}
1784
#else /*Honor_FLT_ROUNDS*/
1785
word0(rv) = Exp_mask;
1786
word1(rv) = 0;
1787
#endif /*Honor_FLT_ROUNDS*/
1788
#ifdef SET_INEXACT
1789
/* set overflow bit */
1790
dval(rv0) = 1e300;
1791
dval(rv0) *= dval(rv0);
1792
#endif
1793
#else /*IEEE_Arith*/
1794
word0(rv) = Big0;
1795
word1(rv) = Big1;
1796
#endif /*IEEE_Arith*/
1797
if (bd0)
1798
goto retfree;
1799
goto ret;
1800
}
1801
e1 >>= 4;
1802
for(j = 0; e1 > 1; j++, e1 >>= 1)
1803
if (e1 & 1)
1804
dval(rv) *= bigtens[j];
1805
/* The last multiplication could overflow. */
1806
word0(rv) -= P*Exp_msk1;
1807
dval(rv) *= bigtens[j];
1808
if ((z = word0(rv) & Exp_mask)
1809
> Exp_msk1*(DBL_MAX_EXP+Bias-P))
1810
goto ovfl;
1811
if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1812
/* set to largest number */
1813
/* (Can't trust DBL_MAX) */
1814
word0(rv) = Big0;
1815
word1(rv) = Big1;
1816
}
1817
else
1818
word0(rv) += P*Exp_msk1;
1819
}
1820
}
1821
else if (e1 < 0) {
1822
e1 = -e1;
1823
if ((i = e1 & 15))
1824
dval(rv) /= tens[i];
1825
if (e1 >>= 4) {
1826
if (e1 >= 1 << n_bigtens)
1827
goto undfl;
1828
#ifdef Avoid_Underflow
1829
if (e1 & Scale_Bit)
1830
scale = 2*P;
1831
for(j = 0; e1 > 0; j++, e1 >>= 1)
1832
if (e1 & 1)
1833
dval(rv) *= tinytens[j];
1834
if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
1835
>> Exp_shift)) > 0) {
1836
/* scaled rv is denormal; zap j low bits */
1837
if (j >= 32) {
1838
word1(rv) = 0;
1839
if (j >= 53)
1840
word0(rv) = (P+2)*Exp_msk1;
1841
else
1842
word0(rv) &= 0xffffffff << (j-32);
1843
}
1844
else
1845
word1(rv) &= 0xffffffff << j;
1846
}
1847
#else
1848
for(j = 0; e1 > 1; j++, e1 >>= 1)
1849
if (e1 & 1)
1850
dval(rv) *= tinytens[j];
1851
/* The last multiplication could underflow. */
1852
dval(rv0) = dval(rv);
1853
dval(rv) *= tinytens[j];
1854
if (!dval(rv)) {
1855
dval(rv) = 2.*dval(rv0);
1856
dval(rv) *= tinytens[j];
1857
#endif
1858
if (!dval(rv)) {
1859
undfl:
1860
dval(rv) = 0.;
1861
#ifndef NO_ERRNO
1862
errno = ERANGE;
1863
#endif
1864
if (bd0)
1865
goto retfree;
1866
goto ret;
1867
}
1868
#ifndef Avoid_Underflow
1869
word0(rv) = Tiny0;
1870
word1(rv) = Tiny1;
1871
/* The refinement below will clean
1872
* this approximation up.
1873
*/
1874
}
1875
#endif
1876
}
1877
}
1878
1879
/* Now the hard part -- adjusting rv to the correct value.*/
1880
1881
/* Put digits into bd: true value = bd * 10^e */
1882
1883
bd0 = s2b(PASS_STATE s0, nd0, nd, y);
1884
1885
for(;;) {
1886
bd = Balloc(PASS_STATE bd0->k);
1887
Bcopy(bd, bd0);
1888
bb = d2b(PASS_STATE rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1889
bs = i2b(PASS_STATE 1);
1890
1891
if (e >= 0) {
1892
bb2 = bb5 = 0;
1893
bd2 = bd5 = e;
1894
}
1895
else {
1896
bb2 = bb5 = -e;
1897
bd2 = bd5 = 0;
1898
}
1899
if (bbe >= 0)
1900
bb2 += bbe;
1901
else
1902
bd2 -= bbe;
1903
bs2 = bb2;
1904
#ifdef Honor_FLT_ROUNDS
1905
if (rounding != 1)
1906
bs2++;
1907
#endif
1908
#ifdef Avoid_Underflow
1909
j = bbe - scale;
1910
i = j + bbbits - 1; /* logb(rv) */
1911
if (i < Emin) /* denormal */
1912
j += P - Emin;
1913
else
1914
j = P + 1 - bbbits;
1915
#else /*Avoid_Underflow*/
1916
#ifdef Sudden_Underflow
1917
#ifdef IBM
1918
j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1919
#else
1920
j = P + 1 - bbbits;
1921
#endif
1922
#else /*Sudden_Underflow*/
1923
j = bbe;
1924
i = j + bbbits - 1; /* logb(rv) */
1925
if (i < Emin) /* denormal */
1926
j += P - Emin;
1927
else
1928
j = P + 1 - bbbits;
1929
#endif /*Sudden_Underflow*/
1930
#endif /*Avoid_Underflow*/
1931
bb2 += j;
1932
bd2 += j;
1933
#ifdef Avoid_Underflow
1934
bd2 += scale;
1935
#endif
1936
i = bb2 < bd2 ? bb2 : bd2;
1937
if (i > bs2)
1938
i = bs2;
1939
if (i > 0) {
1940
bb2 -= i;
1941
bd2 -= i;
1942
bs2 -= i;
1943
}
1944
if (bb5 > 0) {
1945
bs = pow5mult(PASS_STATE bs, bb5);
1946
bb1 = mult(PASS_STATE bs, bb);
1947
Bfree(PASS_STATE bb);
1948
bb = bb1;
1949
}
1950
if (bb2 > 0)
1951
bb = lshift(PASS_STATE bb, bb2);
1952
if (bd5 > 0)
1953
bd = pow5mult(PASS_STATE bd, bd5);
1954
if (bd2 > 0)
1955
bd = lshift(PASS_STATE bd, bd2);
1956
if (bs2 > 0)
1957
bs = lshift(PASS_STATE bs, bs2);
1958
delta = diff(PASS_STATE bb, bd);
1959
dsign = delta->sign;
1960
delta->sign = 0;
1961
i = cmp(delta, bs);
1962
#ifdef Honor_FLT_ROUNDS
1963
if (rounding != 1) {
1964
if (i < 0) {
1965
/* Error is less than an ulp */
1966
if (!delta->x[0] && delta->wds <= 1) {
1967
/* exact */
1968
#ifdef SET_INEXACT
1969
inexact = 0;
1970
#endif
1971
break;
1972
}
1973
if (rounding) {
1974
if (dsign) {
1975
adj = 1.;
1976
goto apply_adj;
1977
}
1978
}
1979
else if (!dsign) {
1980
adj = -1.;
1981
if (!word1(rv)
1982
&& !(word0(rv) & Frac_mask)) {
1983
y = word0(rv) & Exp_mask;
1984
#ifdef Avoid_Underflow
1985
if (!scale || y > 2*P*Exp_msk1)
1986
#else
1987
if (y)
1988
#endif
1989
{
1990
delta = lshift(PASS_STATE delta,Log2P);
1991
if (cmp(delta, bs) <= 0)
1992
adj = -0.5;
1993
}
1994
}
1995
apply_adj:
1996
#ifdef Avoid_Underflow
1997
if (scale && (y = word0(rv) & Exp_mask)
1998
<= 2*P*Exp_msk1)
1999
word0(adj) += (2*P+1)*Exp_msk1 - y;
2000
#else
2001
#ifdef Sudden_Underflow
2002
if ((word0(rv) & Exp_mask) <=
2003
P*Exp_msk1) {
2004
word0(rv) += P*Exp_msk1;
2005
dval(rv) += adj*ulp(rv);
2006
word0(rv) -= P*Exp_msk1;
2007
}
2008
else
2009
#endif /*Sudden_Underflow*/
2010
#endif /*Avoid_Underflow*/
2011
dval(rv) += adj*ulp(rv);
2012
}
2013
break;
2014
}
2015
adj = ratio(delta, bs);
2016
if (adj < 1.)
2017
adj = 1.;
2018
if (adj <= 0x7ffffffe) {
2019
/* adj = rounding ? ceil(adj) : floor(adj); */
2020
y = adj;
2021
if (y != adj) {
2022
if (!((rounding>>1) ^ dsign))
2023
y++;
2024
adj = y;
2025
}
2026
}
2027
#ifdef Avoid_Underflow
2028
if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2029
word0(adj) += (2*P+1)*Exp_msk1 - y;
2030
#else
2031
#ifdef Sudden_Underflow
2032
if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2033
word0(rv) += P*Exp_msk1;
2034
adj *= ulp(rv);
2035
if (dsign)
2036
dval(rv) += adj;
2037
else
2038
dval(rv) -= adj;
2039
word0(rv) -= P*Exp_msk1;
2040
goto cont;
2041
}
2042
#endif /*Sudden_Underflow*/
2043
#endif /*Avoid_Underflow*/
2044
adj *= ulp(rv);
2045
if (dsign)
2046
dval(rv) += adj;
2047
else
2048
dval(rv) -= adj;
2049
goto cont;
2050
}
2051
#endif /*Honor_FLT_ROUNDS*/
2052
2053
if (i < 0) {
2054
/* Error is less than half an ulp -- check for
2055
* special case of mantissa a power of two.
2056
*/
2057
if (dsign || word1(rv) || word0(rv) & Bndry_mask
2058
#ifdef IEEE_Arith
2059
#ifdef Avoid_Underflow
2060
|| (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2061
#else
2062
|| (word0(rv) & Exp_mask) <= Exp_msk1
2063
#endif
2064
#endif
2065
) {
2066
#ifdef SET_INEXACT
2067
if (!delta->x[0] && delta->wds <= 1)
2068
inexact = 0;
2069
#endif
2070
break;
2071
}
2072
if (!delta->x[0] && delta->wds <= 1) {
2073
/* exact result */
2074
#ifdef SET_INEXACT
2075
inexact = 0;
2076
#endif
2077
break;
2078
}
2079
delta = lshift(PASS_STATE delta,Log2P);
2080
if (cmp(delta, bs) > 0)
2081
goto drop_down;
2082
break;
2083
}
2084
if (i == 0) {
2085
/* exactly half-way between */
2086
if (dsign) {
2087
if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2088
&& word1(rv) == (
2089
#ifdef Avoid_Underflow
2090
(scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2091
? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2092
#endif
2093
0xffffffff)) {
2094
/*boundary case -- increment exponent*/
2095
word0(rv) = (word0(rv) & Exp_mask)
2096
+ Exp_msk1
2097
#ifdef IBM
2098
| Exp_msk1 >> 4
2099
#endif
2100
;
2101
word1(rv) = 0;
2102
#ifdef Avoid_Underflow
2103
dsign = 0;
2104
#endif
2105
break;
2106
}
2107
}
2108
else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2109
drop_down:
2110
/* boundary case -- decrement exponent */
2111
#ifdef Sudden_Underflow /*{{*/
2112
L = word0(rv) & Exp_mask;
2113
#ifdef IBM
2114
if (L < Exp_msk1)
2115
#else
2116
#ifdef Avoid_Underflow
2117
if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2118
#else
2119
if (L <= Exp_msk1)
2120
#endif /*Avoid_Underflow*/
2121
#endif /*IBM*/
2122
goto undfl;
2123
L -= Exp_msk1;
2124
#else /*Sudden_Underflow}{*/
2125
#ifdef Avoid_Underflow
2126
if (scale) {
2127
L = word0(rv) & Exp_mask;
2128
if (L <= (2*P+1)*Exp_msk1) {
2129
if (L > (P+2)*Exp_msk1)
2130
/* round even ==> */
2131
/* accept rv */
2132
break;
2133
/* rv = smallest denormal */
2134
goto undfl;
2135
}
2136
}
2137
#endif /*Avoid_Underflow*/
2138
L = (word0(rv) & Exp_mask) - Exp_msk1;
2139
#endif /*Sudden_Underflow}}*/
2140
word0(rv) = L | Bndry_mask1;
2141
word1(rv) = 0xffffffff;
2142
#ifdef IBM
2143
goto cont;
2144
#else
2145
break;
2146
#endif
2147
}
2148
#ifndef ROUND_BIASED
2149
if (!(word1(rv) & LSB))
2150
break;
2151
#endif
2152
if (dsign)
2153
dval(rv) += ulp(rv);
2154
#ifndef ROUND_BIASED
2155
else {
2156
dval(rv) -= ulp(rv);
2157
#ifndef Sudden_Underflow
2158
if (!dval(rv))
2159
goto undfl;
2160
#endif
2161
}
2162
#ifdef Avoid_Underflow
2163
dsign = 1 - dsign;
2164
#endif
2165
#endif
2166
break;
2167
}
2168
if ((aadj = ratio(delta, bs)) <= 2.) {
2169
if (dsign)
2170
aadj = dval(aadj1) = 1.;
2171
else if (word1(rv) || word0(rv) & Bndry_mask) {
2172
#ifndef Sudden_Underflow
2173
if (word1(rv) == Tiny1 && !word0(rv))
2174
goto undfl;