-
Notifications
You must be signed in to change notification settings - Fork 6
/
dtoaLoc.c
3497 lines (3324 loc) · 80.8 KB
/
dtoaLoc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
*
* The author of this software is David M. Gay.
*
* Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
* Copyright (c) 2017-2023 GAMS Software GmbH <[email protected]>
* Copyright (c) 2017-2023 GAMS Development Corp. <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
***************************************************************/
/* On a machine with IEEE extended-precision registers, it is
* necessary to specify double-precision (53-bit) rounding precision
* before invoking strtod or dtoa. If the machine uses (the equivalent
* of) Intel 80x87 arithmetic, the call
* _control87(PC_53, MCW_PC);
* does this with many compilers. Whether this or another call is
* appropriate depends on the compiler; for this to work, it may be
* necessary to #include "float.h" or another system-dependent header
* file.
*/
/* strtod for IEEE-arithmetic machines.
* (Note that IEEE arithmetic is disabled by gcc's -ffast-math flag.)
*
* This strtod returns a nearest machine number to the input decimal
* string (or sets errno to ERANGE). With IEEE arithmetic, ties are
* broken by the IEEE round-even rule. Otherwise ties are broken by
* biased rounding (add half and chop).
*
* Inspired loosely by William D. Clinger's paper "How to Read Floating
* Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
*
* Modifications:
*
* 1. We only require IEEE double-precision
* arithmetic (not IEEE double-extended).
* 2. We get by with floating-point arithmetic in a case that
* Clinger missed -- when we're computing d * 10^n
* for a small integer d and the integer n is not too
* much larger than 22 (the maximum integer k for which
* we can represent 10^k exactly), we may be able to
* compute (d*10^k) * 10^(e-k) with just one roundoff.
* 3. Rather than a bit-at-a-time adjustment of the binary
* result in the hard case, we use floating-point
* arithmetic to determine the adjustment to within
* one bit; only in really hard cases do we need to
* compute a second residual.
* 4. Because of 3., we don't need a large table of powers of 10
* for ten-to-e (just some small tables, e.g. of 10^k
* for 0 <= k <= 22).
*/
/*
* #define IEEE_8087 for IEEE-arithmetic machines where the least
* significant byte has the lowest address.
* #define IEEE_MC68k for IEEE-arithmetic machines where the most
* significant byte has the lowest address.
* #define No_leftright to omit left-right logic in fast floating-point
* computation of dtoa. This will cause dtoa modes 4 and 5 to be
* treated the same as modes 2 and 3 for some inputs.
* #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
* and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS
* is also #defined, fegetround() will be queried for the rounding mode.
* Note that both FLT_ROUNDS and fegetround() are specified by the C99
* standard (and are specified to be consistent, with fesetround()
* affecting the value of FLT_ROUNDS), but that some (Linux) systems
* do not work correctly in this regard, so using fegetround() is more
* portable than using FLT_ROUNDS directly.
* #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
* and Honor_FLT_ROUNDS is not #defined.
* #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
* that rounds toward +Infinity.
* #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
* rounding when the underlying floating-point arithmetic uses
* unbiased rounding. This prevent using ordinary floating-point
* arithmetic when the result could be computed with one rounding error.
* #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
* if memory is available and otherwise does something you deem
* appropriate. If MALLOC is undefined, malloc will be invoked
* directly -- and assumed always to succeed.
* #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
* #defined automatically on IEEE systems. On such systems,
* when INFNAN_CHECK is #defined, strtod checks
* for Infinity and NaN (case insensitively). On some systems
* (e.g., some HP systems), it may be necessary to #define NAN_WORD0
* appropriately -- to the most significant word of a quiet NaN.
* (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
* When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
* strtod also accepts (case insensitively) strings of the form
* NaN(x), where x is a string of hexadecimal digits and spaces;
* if there is only one string of hexadecimal digits, it is taken
* for the 52 fraction bits of the resulting NaN; if there are two
* or more strings of hex digits, the first is for the high 20 bits,
* the second and subsequent for the low 32 bits, with intervening
* white space ignored; but if this results in none of the 52
* fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
* and NAN_WORD1 are used instead.
* #define USE_LOCALE to use the current locale's decimal_point value.
* #define SET_INEXACT if IEEE arithmetic is being used and extra
* computation should be done to set the inexact flag when the
* result is inexact and avoid setting inexact when the result
* is exact. In this case, dtoa.c must be compiled in
* an environment, perhaps provided by #include "dtoa.c" in a
* suitable wrapper, that defines two functions,
* int get_inexact(void);
* void clear_inexact(void);
* such that get_inexact() returns a nonzero value if the
* inexact bit is already set, and clear_inexact() sets the
* inexact bit to 0. When SET_INEXACT is #defined, strtod
* also does extra computations to set the underflow and overflow
* flags when appropriate (i.e., when the result is tiny and
* inexact or when it is a numeric value rounded to +-infinity).
* #define NO_HEX_FP to omit recognition of hexadecimal floating-point
* values by strtod.
* #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
* to disable logic for "fast" testing of very long input strings
* to strtod. This testing proceeds by initially truncating the
* input string, then if necessary comparing the whole string with
* a decimal expansion to decide close cases. This logic is only
* used for input more than STRTOD_DIGLIM digits long (default 40).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#ifdef DEBUG
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
#endif
/* sometimes we put in a quick hack, sometimes we really mean to assert */
#define HACKASSERT assert
#define GOODASSERT assert
/* should never get compiled in */
#define MALLOC noMalloc
#ifdef USE_LOCALE
#include <locale.h>
#endif
#ifdef Honor_FLT_ROUNDS
#ifndef Trust_FLT_ROUNDS
#include <fenv.h>
#endif
#endif
#define NO_STRTOD_BIGCOMP /* IMHO the bigcomp stuff is buggy */
/* we will set these automagically */
#undef IEEE_8087
#undef IEEE_MC68k
#undef Sudden_Underflow
#if defined(_WIN32)
# define IEEE_8087
# if defined(__INTEL_COMPILER_BUILD_DATE)
# define Sudden_Underflow
# endif
#elif defined(__linux) || defined(__linux__) || defined(__APPLE__)
# define IEEE_8087
#elif defined(__sparc) || (__HOS_AIX__)
# define IEEE_MC68k
#elif defined(__sun)
# define IEEE_8087
#else
# error This environment not recognized as little- or big-endian
#endif
#if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
# error Exactly one of IEEE_8087 or IEEE_MC68k should be defined.
#endif
#ifdef IEEE_MC68k
#endif
#ifdef IEEE_8087
#endif
#ifndef NO_INFNAN_CHECK
# undef INFNAN_CHECK
# define INFNAN_CHECK
#endif
#include "errno.h"
#include "float.h"
#include "math.h"
#if defined(DTOA_EXPORTS)
# include "dtoaLib.h"
#else
# include "dtoaLoc.h"
#endif
typedef union { double d; uint32_t L[2]; } U;
#ifdef IEEE_8087
# define word0(x) (x)->L[1]
# define word1(x) (x)->L[0]
#else
# define word0(x) (x)->L[0]
# define word1(x) (x)->L[1]
#endif
#define dval(x) (x)->d
#ifndef STRTOD_DIGLIM
#define STRTOD_DIGLIM 40
#endif
#ifdef DIGLIM_DEBUG
extern int strtod_diglim;
#else
#define strtod_diglim STRTOD_DIGLIM
#endif
/* DTOOA_USE_ND_BOUND will always get defined: treat it as a boolean value
* that enables a bound on the number of digits treated as significant.
* This is handy to get a bound on the amount of space required to convert
* long strings to doubles.
* Excess digits are treated as if they were zeros.
* Why 72? The heap makes a jump going to 73, but we save little
* space by making the limit less than 72.
*/
#if defined(DTOA_ND_BOUND) /* implies we will use it */
# if 0 == DTOA_USE_ND_BOUND
# error "Confusing: DTOA_USE_ND_BOUND is 0 but DTOA_ND_BOUND is specified"
# endif
# undef DTOA_USE_ND_BOUND
# define DTOA_USE_ND_BOUND 1
#else
# if (! defined(DTOA_USE_ND_BOUND)) || (0 != DTOA_USE_ND_BOUND)
# undef DTOA_USE_ND_BOUND
# define DTOA_USE_ND_BOUND 1
# define DTOA_ND_BOUND 72
# endif
#endif
/* #define P DBL_MANT_DIG */
/* Ten_pmax = floor(P*log(2)/log(5)) */
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
#define Exp_shift 20
#define Exp_shift1 20
#define Exp_msk1 0x100000
#define Exp_msk11 0x100000
#define Exp_mask 0x7ff00000
#define P 53
#define Nbits 53
#define Bias 1023
#define Emax 1023
#define Emin (-1022)
#define Exp_1 0x3ff00000
#define Exp_11 0x3ff00000
#define Ebits 11
#define Frac_mask 0xfffff
#define Frac_mask1 0xfffff
#define Ten_pmax 22
#define Bletch 0x10
#define Bndry_mask 0xfffff
#define Bndry_mask1 0xfffff
#define LSB 1
#define Sign_bit 0x80000000
#define Log2P 1
#define Tiny0 0
#define Tiny1 1
#define Quick_max 14
#define Int_max 14
#ifdef Flush_Denorm /* debugging option */
# undef Sudden_Underflow
#endif
#ifndef Flt_Rounds
# ifdef FLT_ROUNDS
# define Flt_Rounds FLT_ROUNDS
# else
# define Flt_Rounds 1
# endif
#endif /*Flt_Rounds*/
#ifdef Honor_FLT_ROUNDS
# undef Check_FLT_ROUNDS
# define Check_FLT_ROUNDS
#else
# define Rounding Flt_Rounds
#endif
#ifdef ROUND_BIASED_without_Round_Up
# undef ROUND_BIASED
# define ROUND_BIASED
#endif
#define rounded_product(a,b) a *= b
#define rounded_quotient(a,b) a /= b
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
#define Big1 0xffffffff
typedef struct BCinfo {
int dp0, dp1, dplen, dsign;
int e0, inexact, nd, nd0, rounding, scale, uflchk;
} BCinfo_t;
#define FFFFFFFF 0xffffffffUL
#define Kmax 7
struct Bigint {
struct Bigint *next;
int k, maxwds, sign, wds;
uint32_t x[1];
};
typedef struct Bigint Bigint;
#if ! defined(HEAP_SZ)
# define HEAP_SZ 200
/* roughly, HEAP_SZ * 8 = PRIVATE_MEM, but we can use less now that
* pow5mult is using static memory: the original value of
* PRIVATE_MEM = 288 * 8 is excessively conservative
* FYI, PRIVATE_MEM is the memory used in the original.
*/
#endif
typedef struct bigHeap {
double base[HEAP_SZ];
double *next;
Bigint *freeList[Kmax+1];
} bigHeap_t;
#if defined(DTOA_INFO_LOC)
static unsigned int heapHW; /* highwater mark for heap */
static int kHW; /* highwater mark for k */
#endif
static void
heapInit (bigHeap_t *hp)
{
// (void) memset(hp, 0, sizeof(bigHeap_t));
(void) memset(hp->freeList, 0, sizeof(Bigint *) * (Kmax+1));
hp->next = hp->base;
} /* heapInit */
static Bigint *
Balloc (bigHeap_t *hp, int k)
{
int x;
Bigint *rv;
unsigned int len;
GOODASSERT(k <= Kmax);
if ((rv = hp->freeList[k]))
hp->freeList[k] = rv->next;
else {
x = 1 << k;
len = (sizeof(Bigint) + (x-1)*sizeof(uint32_t) + sizeof(double) - 1)
/ sizeof(double);
GOODASSERT(HEAP_SZ - (hp->next - hp->base) >= len); /* should never overflow the heap */
rv = (Bigint *) hp->next;
hp->next += len;
#if defined(DTOA_INFO_LOC)
if (k > kHW)
kHW = k;
if ((len = (hp->next - hp->base)) > heapHW)
heapHW = len;
#endif
rv->k = k;
rv->maxwds = x;
}
rv->sign = rv->wds = 0;
return rv;
} /* Balloc */
static void
Bfree (bigHeap_t *hp, Bigint *v)
{
if (v) {
GOODASSERT(v->k <= Kmax);
v->next = hp->freeList[v->k];
hp->freeList[v->k] = v;
}
} /* Bfree */
#define BCOPY(x,y) memcpy((void *)&x->sign, (void *)&y->sign, \
y->wds*sizeof(int32_t) + 2*sizeof(int))
static Bigint *
multadd (bigHeap_t *hp, Bigint *b, int m, int a) /* multiply by m and add a */
{
int i, wds;
uint32_t *x;
uint64_t carry, y;
Bigint *b1;
wds = b->wds;
x = b->x;
i = 0;
carry = a;
do {
y = *x * (uint64_t)m + carry;
carry = y >> 32;
*x++ = y & FFFFFFFF;
} while(++i < wds);
if (carry) {
if (wds >= b->maxwds) {
b1 = Balloc(hp, b->k+1);
BCOPY(b1, b);
Bfree(hp, b);
b = b1;
}
b->x[wds++] = (uint32_t)carry;
b->wds = wds;
}
return b;
} /* multadd */
static Bigint *
s2b (bigHeap_t *hp, const char *s, int nd0, int nd, uint32_t y9, int dplen)
{
Bigint *b;
int i, k;
int32_t x, y;
x = (nd + 8) / 9;
for (k = 0, y = 1; x > y; y <<= 1, k++);
b = Balloc(hp, k);
b->x[0] = y9;
b->wds = 1;
i = 9;
if (9 < nd0) {
s += 9;
do
b = multadd(hp, b, 10, *s++ - '0');
while(++i < nd0);
s += dplen;
}
else
s += dplen + 9;
for ( ; i < nd; i++)
b = multadd(hp, b, 10, *s++ - '0');
return b;
} /* s2b */
static int
hi0bits (uint32_t x)
{
int k = 0;
if (!(x & 0xffff0000)) {
k = 16;
x <<= 16;
}
if (!(x & 0xff000000)) {
k += 8;
x <<= 8;
}
if (!(x & 0xf0000000)) {
k += 4;
x <<= 4;
}
if (!(x & 0xc0000000)) {
k += 2;
x <<= 2;
}
if (!(x & 0x80000000)) {
k++;
if (!(x & 0x40000000))
return 32;
}
return k;
} /* hi0bits */
static int
lo0bits (uint32_t *y)
{
int k;
uint32_t x = *y;
if (x & 7) {
if (x & 1)
return 0;
if (x & 2) {
*y = x >> 1;
return 1;
}
*y = x >> 2;
return 2;
}
k = 0;
if (!(x & 0xffff)) {
k = 16;
x >>= 16;
}
if (!(x & 0xff)) {
k += 8;
x >>= 8;
}
if (!(x & 0xf)) {
k += 4;
x >>= 4;
}
if (!(x & 0x3)) {
k += 2;
x >>= 2;
}
if (!(x & 1)) {
k++;
x >>= 1;
if (!x)
return 32;
}
*y = x;
return k;
} /* lo0bits */
static Bigint *
i2b (bigHeap_t *hp, int i)
{
Bigint *b;
b = Balloc(hp, 1);
b->x[0] = i;
b->wds = 1;
return b;
} /* i2b */
static Bigint *
mult (bigHeap_t *hp, const Bigint *a, const Bigint *b)
{
Bigint *c;
int k, wa, wb, wc;
const uint32_t *x, *xa, *xae, *xb, *xbe;
uint32_t *xc, *xc0;
uint32_t y;
uint64_t carry, z;
if (a->wds < b->wds) {
const Bigint *swp = a;
a = b;
b = swp;
}
k = a->k;
wa = a->wds;
wb = b->wds;
wc = wa + wb;
if (wc > a->maxwds)
k++;
c = Balloc(hp, k);
for (xc = c->x, xc0 = xc + wc; xc < xc0; xc++)
*xc = 0;
xa = a->x;
xae = xa + wa;
xb = b->x;
xbe = xb + wb;
xc0 = c->x;
for ( ; xb < xbe; xc0++) {
if ((y = *xb++)) {
x = xa;
xc = xc0;
carry = 0;
do {
z = *x++ * (uint64_t)y + *xc + carry;
carry = z >> 32;
*xc++ = z & FFFFFFFF;
} while(x < xae);
*xc = (uint32_t)carry;
}
}
for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc);
c->wds = wc;
return c;
} /* mult */
struct Bigint5 {
struct Bigint *next;
int k, maxwds, sign, wds;
uint32_t x[5];
};
typedef struct Bigint5 Bigint5_t;
struct Bigint20 {
struct Bigint *next;
int k, maxwds, sign, wds;
uint32_t x[20];
};
typedef struct Bigint20 Bigint20_t;
static Bigint *
pow5mult (bigHeap_t *hp, Bigint *b, int k)
{
Bigint *bb;
const Bigint *pw, *prev;
int i;
static const int p05[] = { 5, 25, 125, 625, 3125, 15625, 78125 };
static const Bigint20_t p256 = {NULL, /* next */
5, /* k */
32, /* maxwds */
0, /* sign */
19, /* wds */
{0x982e7c01, 0xbed3875b, 0xd8d99f72, 0x12152f87, 0x6bde50c6,
0xcf4a6e70, 0xd595d80f, 0x26b2716e, 0xadc666b0, 0x1d153624,
0x3c42d35a, 0x63ff540e, 0xcc5573c0, 0x65f9ef17, 0x55bc28f2,
0x80dcc7f7, 0xf46eeddc, 0x5fdcefce, 0x000553f7}
}; /* 5^256 */
static const Bigint20_t p128 = {(Bigint *)&p256, /* next */
4, /* k */
16, /* maxwds */
0, /* sign */
10, /* wds */
{0x2e953e01, 0x03df9909, 0x0f1538fd, 0x2374e42f, 0xd3cff5ec,
0xc404dc08, 0xbccdb0da, 0xa6337f19, 0xe91f2603, 0x0000024e}
}; /* 5^128 */
static const Bigint5_t p64 = {(Bigint *)&p128, /* next */
3, /* k */
8, /* maxwds */
0, /* sign */
5, /* wds */
{0xbf6a1f01, 0x6e38ed64, 0xdaa797ed, 0xe93ff9f4, 0x00184f03}
}; /* 5^64 */
static const Bigint5_t p32 = {(Bigint *)&p64, /* next */
2, /* k */
4, /* maxwds */
0, /* sign */
3, /* wds */
{0x85acef81, 0x2d6d415b, 0x000004ee}
}; /* 5^32 */
static const Bigint5_t p16 = {(Bigint *)&p32, /* next */
1, /* k */
2, /* maxwds */
0, /* sign */
2, /* wds */
{0x86f26fc1, 0x00000023}
}; /* 5^16 */
if ((i = k & 7))
b = multadd(hp, b, p05[i-1], 0);
if (k & 8)
b = multadd(hp, b, 390625, 0);
if (!(k >>= 4))
return b;
/* if we get here, we want b *= (390625^2)^k */
#if 0
/* do it quick and dirty */
for (i = 0; i < k; i++) {
b = multadd(hp, b, 390625, 0);
b = multadd(hp, b, 390625, 0);
}
#else
for (pw = (Bigint * ) &p16; pw; pw = pw->next) {
if (k & 1) {
bb = mult(hp, b, pw);
Bfree(hp, b);
b = bb;
}
if (!(k >>= 1))
return b;
prev = pw;
}
/* if we get here, the table wasn't big enough */
k <<= 1;
for (i = 0; i < k; i++) {
bb = mult(hp, b, prev);
Bfree(hp, b);
b = bb;
}
#endif
return b;
} /* pow5mult */
static Bigint *
lshift (bigHeap_t *hp, Bigint *b, int k)
{
int i, k1, n, n1;
Bigint *b1;
uint32_t *x, *x1, *xe, z;
n = k >> 5;
k1 = b->k;
n1 = n + b->wds + 1;
for (i = b->maxwds; n1 > i; i <<= 1)
k1++;
b1 = Balloc(hp, k1);
x1 = b1->x;
for (i = 0; i < n; i++)
*x1++ = 0;
x = b->x;
xe = x + b->wds;
if (k &= 0x1f) {
k1 = 32 - k;
z = 0;
do {
*x1++ = *x << k | z;
z = *x++ >> k1;
} while (x < xe);
if ((*x1 = z))
++n1;
}
else
do
*x1++ = *x++;
while (x < xe);
b1->wds = n1 - 1;
Bfree(hp, b);
return b1;
} /* lshift */
static int
cmp (const Bigint *a, const Bigint *b)
{
const uint32_t *xa, *xa0, *xb, *xb0;
int i, j;
i = a->wds;
j = b->wds;
#ifdef DEBUG
if (i > 1 && !a->x[i-1])
Bug("cmp called with a->x[a->wds-1] == 0");
if (j > 1 && !b->x[j-1])
Bug("cmp called with b->x[b->wds-1] == 0");
#endif
if (i -= j)
return i;
xa0 = a->x;
xa = xa0 + j;
xb0 = b->x;
xb = xb0 + j;
for ( ; ; ) {
if (*--xa != *--xb)
return *xa < *xb ? -1 : 1;
if (xa <= xa0)
break;
}
return 0;
} /* cmp */
static Bigint *
diff (bigHeap_t *hp, const Bigint *a, const Bigint *b)
{
Bigint *c;
int i, wa, wb;
const uint32_t *xa, *xae, *xb, *xbe;
uint32_t *xc;
uint64_t borrow, y;
i = cmp(a,b);
if (!i) {
c = Balloc(hp, 0);
c->wds = 1;
c->x[0] = 0;
return c;
}
if (i < 0) {
const Bigint *swp = a;
a = b;
b = swp;
i = 1;
}
else
i = 0;
c = Balloc(hp, a->k);
c->sign = i;
wa = a->wds;
xa = a->x;
xae = xa + wa;
wb = b->wds;
xb = b->x;
xbe = xb + wb;
xc = c->x;
borrow = 0;
do {
y = (uint64_t)*xa++ - *xb++ - borrow;
borrow = y >> 32 & (uint32_t)1;
*xc++ = y & FFFFFFFF;
}
while(xb < xbe);
while(xa < xae) {
y = *xa++ - borrow;
borrow = y >> 32 & (uint32_t)1;
*xc++ = y & FFFFFFFF;
}
while(!*--xc)
wa--;
c->wds = wa;
return c;
} /* diff */
static double
ulp (U *x)
{
int32_t L;
U u;
L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
word0(&u) = L;
word1(&u) = 0;
return dval(&u);
} /* ulp */
static double
b2d (Bigint *a, int *e)
{
uint32_t *xa, *xa0, w, y, z;
int k;
U d;
#define d0 word0(&d)
#define d1 word1(&d)
xa0 = a->x;
xa = xa0 + a->wds;
y = *--xa;
#ifdef DEBUG
if (!y) Bug("zero y in b2d");
#endif
k = hi0bits(y);
*e = 32 - k;
if (k < Ebits) {
d0 = Exp_1 | y >> (Ebits - k);
w = xa > xa0 ? *--xa : 0;
d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
goto ret_d;
}
z = xa > xa0 ? *--xa : 0;
if (k -= Ebits) {
d0 = Exp_1 | y << k | z >> (32 - k);
y = xa > xa0 ? *--xa : 0;
d1 = z << k | y >> (32 - k);
}
else {
d0 = Exp_1 | y;
d1 = z;
}
ret_d:
#undef d0
#undef d1
return dval(&d);
} /* b2d */
static Bigint *
d2b (bigHeap_t *hp, U *d, int *e, int *bits)
{
Bigint *b;
int de, k;
uint32_t *x, y, z;
#ifndef Sudden_Underflow
int i;
#endif
#define d0 word0(d)
#define d1 word1(d)
b = Balloc(hp, 1);
x = b->x;
z = d0 & Frac_mask;
d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
#ifdef Sudden_Underflow
de = (int)(d0 >> Exp_shift);
z |= Exp_msk11;
#else
if ((de = (int)(d0 >> Exp_shift)))
z |= Exp_msk1;
#endif
if ((y = d1)) {
if ((k = lo0bits(&y))) {
x[0] = y | z << (32 - k);
z >>= k;
}
else
x[0] = y;
#ifndef Sudden_Underflow
i =
#endif
b->wds = (x[1] = z) ? 2 : 1;
}
else {
k = lo0bits(&z);
x[0] = z;
#ifndef Sudden_Underflow
i =
#endif
b->wds = 1;
k += 32;
}
#ifndef Sudden_Underflow
if (de) {
#endif
*e = de - Bias - (P-1) + k;
*bits = P - k;
#ifndef Sudden_Underflow
}
else {
*e = de - Bias - (P-1) + 1 + k;
*bits = 32*i - hi0bits(x[i-1]);
}
#endif
return b;
} /* d2b */
#undef d0
#undef d1
static double
ratio (Bigint *a, Bigint *b)
{
U da, db;
int k, ka, kb;
dval(&da) = b2d(a, &ka);
dval(&db) = b2d(b, &kb);
k = ka - kb + 32*(a->wds - b->wds);
if (k > 0)
word0(&da) += k*Exp_msk1;
else {
k = -k;
word0(&db) += k*Exp_msk1;
}
return dval(&da) / dval(&db);
} /* ratio */
static const double
tens[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1e20, 1e21, 1e22
};
static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
9007199254740992.*9007199254740992.e-256
/* = 2^106 * 1e-256 */
};
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
/* flag unnecessarily. It leads to a song and dance at the end of strtod. */
#define Scale_Bit 0x10
#define n_bigtens 5
#undef Need_Hexdig
#ifdef INFNAN_CHECK
#ifndef No_Hex_NaN
#define Need_Hexdig
#endif
#endif
#ifndef Need_Hexdig
#ifndef NO_HEX_FP
#define Need_Hexdig
#endif
#endif
#ifdef Need_Hexdig /*{*/
static unsigned char hexdig[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
#endif /* } Need_Hexdig */
#ifdef INFNAN_CHECK
#ifndef NAN_WORD0
#define NAN_WORD0 0x7ff80000
#endif
#ifndef NAN_WORD1
#define NAN_WORD1 0
#endif
static int
match (const char **sp, const char *t)
{
int c, d;
const char *s = *sp;