summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtod.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtod.c')
-rw-r--r--src/lib/libc/stdlib/strtod.c409
1 files changed, 159 insertions, 250 deletions
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c
index b13fa128f5..d01158e10c 100644
--- a/src/lib/libc/stdlib/strtod.c
+++ b/src/lib/libc/stdlib/strtod.c
@@ -1,3 +1,4 @@
1/* $OpenBSD: strtod.c,v 1.22 2006/05/19 14:15:27 thib Exp $ */
1/**************************************************************** 2/****************************************************************
2 * 3 *
3 * The author of this software is David M. Gay. 4 * The author of this software is David M. Gay.
@@ -79,7 +80,6 @@
79 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision 80 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
80 * integer arithmetic. Whether this speeds things up or slows things 81 * integer arithmetic. Whether this speeds things up or slows things
81 * down depends on the machine and the number being converted. 82 * down depends on the machine and the number being converted.
82 * #define KR_headers for old-style C function headers.
83 * #define Bad_float_h if your system lacks a float.h or if it does not 83 * #define Bad_float_h if your system lacks a float.h or if it does not
84 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, 84 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
85 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. 85 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
@@ -89,13 +89,11 @@
89 * directly -- and assumed always to succeed. 89 * directly -- and assumed always to succeed.
90 */ 90 */
91 91
92#if defined(LIBC_SCCS) && !defined(lint)
93static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
94#endif /* LIBC_SCCS and not lint */
95
96#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 92#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
97 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) 93 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
98#include <machine/endian.h> 94 defined(__powerpc__) || defined(__m88k__) || defined(__hppa__) || \
95 defined(__x86_64__) || (defined(__arm__) && defined(__VFP_FP__))
96#include <sys/types.h>
99#if BYTE_ORDER == BIG_ENDIAN 97#if BYTE_ORDER == BIG_ENDIAN
100#define IEEE_BIG_ENDIAN 98#define IEEE_BIG_ENDIAN
101#else 99#else
@@ -103,7 +101,16 @@ static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"
103#endif 101#endif
104#endif 102#endif
105 103
106#ifdef vax 104#if defined(__arm__) && !defined(__VFP_FP__)
105/*
106 * Although the CPU is little endian the FP has different
107 * byte and word endianness. The byte order is still little endian
108 * but the word order is big endian.
109 */
110#define IEEE_BIG_ENDIAN
111#endif
112
113#ifdef __vax__
107#define VAX 114#define VAX
108#endif 115#endif
109 116
@@ -119,22 +126,13 @@ static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"
119#include "malloc.h" 126#include "malloc.h"
120#include "memory.h" 127#include "memory.h"
121#else 128#else
122#ifndef KR_headers
123#include "stdlib.h" 129#include "stdlib.h"
124#include "string.h" 130#include "string.h"
125#include "locale.h" 131#include "locale.h"
126#else
127#include "malloc.h"
128#include "memory.h"
129#endif
130#endif 132#endif
131 133
132#ifdef MALLOC 134#ifdef MALLOC
133#ifdef KR_headers
134extern char *MALLOC();
135#else
136extern void *MALLOC(size_t); 135extern void *MALLOC(size_t);
137#endif
138#else 136#else
139#define MALLOC malloc 137#define MALLOC malloc
140#endif 138#endif
@@ -143,7 +141,6 @@ extern void *MALLOC(size_t);
143#include "errno.h" 141#include "errno.h"
144 142
145#ifdef Bad_float_h 143#ifdef Bad_float_h
146#undef __STDC__
147#ifdef IEEE_BIG_ENDIAN 144#ifdef IEEE_BIG_ENDIAN
148#define IEEE_ARITHMETIC 145#define IEEE_ARITHMETIC
149#endif 146#endif
@@ -193,12 +190,8 @@ extern "C" {
193#endif 190#endif
194 191
195#ifndef CONST 192#ifndef CONST
196#ifdef KR_headers
197#define CONST /* blank */
198#else
199#define CONST const 193#define CONST const
200#endif 194#endif
201#endif
202 195
203#ifdef Unsigned_Shifts 196#ifdef Unsigned_Shifts
204#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; 197#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
@@ -212,19 +205,24 @@ Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
212IBM should be defined. 205IBM should be defined.
213#endif 206#endif
214 207
208typedef union {
209 double d;
210 ULong ul[2];
211} _double;
212#define value(x) ((x).d)
215#ifdef IEEE_LITTLE_ENDIAN 213#ifdef IEEE_LITTLE_ENDIAN
216#define word0(x) ((ULong *)&x)[1] 214#define word0(x) ((x).ul[1])
217#define word1(x) ((ULong *)&x)[0] 215#define word1(x) ((x).ul[0])
218#else 216#else
219#define word0(x) ((ULong *)&x)[0] 217#define word0(x) ((x).ul[0])
220#define word1(x) ((ULong *)&x)[1] 218#define word1(x) ((x).ul[1])
221#endif 219#endif
222 220
223/* The following definition of Storeinc is appropriate for MIPS processors. 221/* The following definition of Storeinc is appropriate for MIPS processors.
224 * An alternative that might be better on some machines is 222 * An alternative that might be better on some machines is
225 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) 223 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
226 */ 224 */
227#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) 225#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
228#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ 226#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
229((unsigned short *)a)[0] = (unsigned short)c, a++) 227((unsigned short *)a)[0] = (unsigned short)c, a++)
230#else 228#else
@@ -326,11 +324,7 @@ IBM should be defined.
326#ifdef RND_PRODQUOT 324#ifdef RND_PRODQUOT
327#define rounded_product(a,b) a = rnd_prod(a, b) 325#define rounded_product(a,b) a = rnd_prod(a, b)
328#define rounded_quotient(a,b) a = rnd_quot(a, b) 326#define rounded_quotient(a,b) a = rnd_quot(a, b)
329#ifdef KR_headers
330extern double rnd_prod(), rnd_quot();
331#else
332extern double rnd_prod(double, double), rnd_quot(double, double); 327extern double rnd_prod(double, double), rnd_quot(double, double);
333#endif
334#else 328#else
335#define rounded_product(a,b) a *= b 329#define rounded_product(a,b) a *= b
336#define rounded_quotient(a,b) a /= b 330#define rounded_quotient(a,b) a /= b
@@ -370,17 +364,12 @@ Bigint {
370 static Bigint *freelist[Kmax+1]; 364 static Bigint *freelist[Kmax+1];
371 365
372 static Bigint * 366 static Bigint *
373Balloc 367Balloc(int k)
374#ifdef KR_headers
375 (k) int k;
376#else
377 (int k)
378#endif
379{ 368{
380 int x; 369 int x;
381 Bigint *rv; 370 Bigint *rv;
382 371
383 if (rv = freelist[k]) { 372 if ((rv = freelist[k])) {
384 freelist[k] = rv->next; 373 freelist[k] = rv->next;
385 } 374 }
386 else { 375 else {
@@ -394,12 +383,7 @@ Balloc
394 } 383 }
395 384
396 static void 385 static void
397Bfree 386Bfree(Bigint *v)
398#ifdef KR_headers
399 (v) Bigint *v;
400#else
401 (Bigint *v)
402#endif
403{ 387{
404 if (v) { 388 if (v) {
405 v->next = freelist[v->k]; 389 v->next = freelist[v->k];
@@ -411,12 +395,7 @@ Bfree
411y->wds*sizeof(Long) + 2*sizeof(int)) 395y->wds*sizeof(Long) + 2*sizeof(int))
412 396
413 static Bigint * 397 static Bigint *
414multadd 398multadd(Bigint *b, int m, int a) /* multiply by m and add a */
415#ifdef KR_headers
416 (b, m, a) Bigint *b; int m, a;
417#else
418 (Bigint *b, int m, int a) /* multiply by m and add a */
419#endif
420{ 399{
421 int i, wds; 400 int i, wds;
422 ULong *x, y; 401 ULong *x, y;
@@ -456,12 +435,7 @@ multadd
456 } 435 }
457 436
458 static Bigint * 437 static Bigint *
459s2b 438s2b(CONST char *s, int nd0, int nd, ULong y9)
460#ifdef KR_headers
461 (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
462#else
463 (CONST char *s, int nd0, int nd, ULong y9)
464#endif
465{ 439{
466 Bigint *b; 440 Bigint *b;
467 int i, k; 441 int i, k;
@@ -494,14 +468,9 @@ s2b
494 } 468 }
495 469
496 static int 470 static int
497hi0bits 471hi0bits(ULong x)
498#ifdef KR_headers
499 (x) register ULong x;
500#else
501 (register ULong x)
502#endif
503{ 472{
504 register int k = 0; 473 int k = 0;
505 474
506 if (!(x & 0xffff0000)) { 475 if (!(x & 0xffff0000)) {
507 k = 16; 476 k = 16;
@@ -528,15 +497,10 @@ hi0bits
528 } 497 }
529 498
530 static int 499 static int
531lo0bits 500lo0bits(ULong *y)
532#ifdef KR_headers
533 (y) ULong *y;
534#else
535 (ULong *y)
536#endif
537{ 501{
538 register int k; 502 int k;
539 register ULong x = *y; 503 ULong x = *y;
540 504
541 if (x & 7) { 505 if (x & 7) {
542 if (x & 1) 506 if (x & 1)
@@ -568,7 +532,7 @@ lo0bits
568 if (!(x & 1)) { 532 if (!(x & 1)) {
569 k++; 533 k++;
570 x >>= 1; 534 x >>= 1;
571 if (!x & 1) 535 if (!(x & 1))
572 return 32; 536 return 32;
573 } 537 }
574 *y = x; 538 *y = x;
@@ -576,12 +540,7 @@ lo0bits
576 } 540 }
577 541
578 static Bigint * 542 static Bigint *
579i2b 543i2b(int i)
580#ifdef KR_headers
581 (i) int i;
582#else
583 (int i)
584#endif
585{ 544{
586 Bigint *b; 545 Bigint *b;
587 546
@@ -592,12 +551,7 @@ i2b
592 } 551 }
593 552
594 static Bigint * 553 static Bigint *
595mult 554mult(Bigint *a, Bigint *b)
596#ifdef KR_headers
597 (a, b) Bigint *a, *b;
598#else
599 (Bigint *a, Bigint *b)
600#endif
601{ 555{
602 Bigint *c; 556 Bigint *c;
603 int k, wa, wb, wc; 557 int k, wa, wb, wc;
@@ -628,7 +582,7 @@ mult
628 xc0 = c->x; 582 xc0 = c->x;
629#ifdef Pack_32 583#ifdef Pack_32
630 for(; xb < xbe; xb++, xc0++) { 584 for(; xb < xbe; xb++, xc0++) {
631 if (y = *xb & 0xffff) { 585 if ((y = *xb & 0xffff)) {
632 x = xa; 586 x = xa;
633 xc = xc0; 587 xc = xc0;
634 carry = 0; 588 carry = 0;
@@ -642,7 +596,7 @@ mult
642 while(x < xae); 596 while(x < xae);
643 *xc = carry; 597 *xc = carry;
644 } 598 }
645 if (y = *xb >> 16) { 599 if ((y = *xb >> 16)) {
646 x = xa; 600 x = xa;
647 xc = xc0; 601 xc = xc0;
648 carry = 0; 602 carry = 0;
@@ -682,18 +636,13 @@ mult
682 static Bigint *p5s; 636 static Bigint *p5s;
683 637
684 static Bigint * 638 static Bigint *
685pow5mult 639pow5mult(Bigint *b, int k)
686#ifdef KR_headers
687 (b, k) Bigint *b; int k;
688#else
689 (Bigint *b, int k)
690#endif
691{ 640{
692 Bigint *b1, *p5, *p51; 641 Bigint *b1, *p5, *p51;
693 int i; 642 int i;
694 static int p05[3] = { 5, 25, 125 }; 643 static int p05[3] = { 5, 25, 125 };
695 644
696 if (i = k & 3) 645 if ((i = k & 3))
697 b = multadd(b, p05[i-1], 0); 646 b = multadd(b, p05[i-1], 0);
698 647
699 if (!(k >>= 2)) 648 if (!(k >>= 2))
@@ -721,12 +670,7 @@ pow5mult
721 } 670 }
722 671
723 static Bigint * 672 static Bigint *
724lshift 673lshift(Bigint *b, int k)
725#ifdef KR_headers
726 (b, k) Bigint *b; int k;
727#else
728 (Bigint *b, int k)
729#endif
730{ 674{
731 int i, k1, n, n1; 675 int i, k1, n, n1;
732 Bigint *b1; 676 Bigint *b1;
@@ -756,7 +700,7 @@ lshift
756 z = *x++ >> k1; 700 z = *x++ >> k1;
757 } 701 }
758 while(x < xe); 702 while(x < xe);
759 if (*x1 = z) 703 if ((*x1 = z))
760 ++n1; 704 ++n1;
761 } 705 }
762#else 706#else
@@ -781,12 +725,7 @@ lshift
781 } 725 }
782 726
783 static int 727 static int
784cmp 728cmp(Bigint *a, Bigint *b)
785#ifdef KR_headers
786 (a, b) Bigint *a, *b;
787#else
788 (Bigint *a, Bigint *b)
789#endif
790{ 729{
791 ULong *xa, *xa0, *xb, *xb0; 730 ULong *xa, *xa0, *xb, *xb0;
792 int i, j; 731 int i, j;
@@ -815,12 +754,7 @@ cmp
815 } 754 }
816 755
817 static Bigint * 756 static Bigint *
818diff 757diff(Bigint *a, Bigint *b)
819#ifdef KR_headers
820 (a, b) Bigint *a, *b;
821#else
822 (Bigint *a, Bigint *b)
823#endif
824{ 758{
825 Bigint *c; 759 Bigint *c;
826 int i, wa, wb; 760 int i, wa, wb;
@@ -897,16 +831,13 @@ diff
897 } 831 }
898 832
899 static double 833 static double
900ulp 834ulp(double _x)
901#ifdef KR_headers
902 (x) double x;
903#else
904 (double x)
905#endif
906{ 835{
907 register Long L; 836 _double x;
908 double a; 837 Long L;
838 _double a;
909 839
840 value(x) = _x;
910 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; 841 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
911#ifndef Sudden_Underflow 842#ifndef Sudden_Underflow
912 if (L > 0) { 843 if (L > 0) {
@@ -931,20 +862,15 @@ ulp
931 } 862 }
932 } 863 }
933#endif 864#endif
934 return a; 865 return value(a);
935 } 866 }
936 867
937 static double 868 static double
938b2d 869b2d(Bigint *a, int *e)
939#ifdef KR_headers
940 (a, e) Bigint *a; int *e;
941#else
942 (Bigint *a, int *e)
943#endif
944{ 870{
945 ULong *xa, *xa0, w, y, z; 871 ULong *xa, *xa0, w, y, z;
946 int k; 872 int k;
947 double d; 873 _double d;
948#ifdef VAX 874#ifdef VAX
949 ULong d0, d1; 875 ULong d0, d1;
950#else 876#else
@@ -1001,22 +927,22 @@ b2d
1001#undef d0 927#undef d0
1002#undef d1 928#undef d1
1003#endif 929#endif
1004 return d; 930 return value(d);
1005 } 931 }
1006 932
1007 static Bigint * 933 static Bigint *
1008d2b 934d2b(double _d, int *e, int *bits)
1009#ifdef KR_headers
1010 (d, e, bits) double d; int *e, *bits;
1011#else
1012 (double d, int *e, int *bits)
1013#endif
1014{ 935{
1015 Bigint *b; 936 Bigint *b;
1016 int de, i, k; 937 int de, i, k;
1017 ULong *x, y, z; 938 ULong *x, y, z;
939 _double d;
1018#ifdef VAX 940#ifdef VAX
1019 ULong d0, d1; 941 ULong d0, d1;
942#endif
943
944 value(d) = _d;
945#ifdef VAX
1020 d0 = word0(d) >> 16 | word0(d) << 16; 946 d0 = word0(d) >> 16 | word0(d) << 16;
1021 d1 = word1(d) >> 16 | word1(d) << 16; 947 d1 = word1(d) >> 16 | word1(d) << 16;
1022#else 948#else
@@ -1134,18 +1060,13 @@ d2b
1134#undef d1 1060#undef d1
1135 1061
1136 static double 1062 static double
1137ratio 1063ratio(Bigint *a, Bigint *b)
1138#ifdef KR_headers
1139 (a, b) Bigint *a, *b;
1140#else
1141 (Bigint *a, Bigint *b)
1142#endif
1143{ 1064{
1144 double da, db; 1065 _double da, db;
1145 int k, ka, kb; 1066 int k, ka, kb;
1146 1067
1147 da = b2d(a, &ka); 1068 value(da) = b2d(a, &ka);
1148 db = b2d(b, &kb); 1069 value(db) = b2d(b, &kb);
1149#ifdef Pack_32 1070#ifdef Pack_32
1150 k = ka - kb + 32*(a->wds - b->wds); 1071 k = ka - kb + 32*(a->wds - b->wds);
1151#else 1072#else
@@ -1171,7 +1092,7 @@ ratio
1171 word0(db) += k*Exp_msk1; 1092 word0(db) += k*Exp_msk1;
1172 } 1093 }
1173#endif 1094#endif
1174 return da / db; 1095 return value(da) / value(db);
1175 } 1096 }
1176 1097
1177static CONST double 1098static CONST double
@@ -1201,32 +1122,24 @@ static CONST double tinytens[] = { 1e-16, 1e-32 };
1201#endif 1122#endif
1202 1123
1203 double 1124 double
1204strtod 1125strtod(CONST char *s00, char **se)
1205#ifdef KR_headers
1206 (s00, se) CONST char *s00; char **se;
1207#else
1208 (CONST char *s00, char **se)
1209#endif
1210{ 1126{
1211 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, 1127 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1212 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; 1128 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1213 CONST char *s, *s0, *s1; 1129 CONST char *s, *s0, *s1;
1214 double aadj, aadj1, adj, rv, rv0; 1130 double aadj, aadj1, adj;
1131 _double rv, rv0;
1215 Long L; 1132 Long L;
1216 ULong y, z; 1133 ULong y, z;
1217 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; 1134 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1218 1135
1219#ifndef KR_headers
1220 CONST char decimal_point = localeconv()->decimal_point[0]; 1136 CONST char decimal_point = localeconv()->decimal_point[0];
1221#else
1222 CONST char decimal_point = '.';
1223#endif
1224 1137
1225 sign = nz0 = nz = 0; 1138 sign = nz0 = nz = 0;
1226 rv = 0.; 1139 value(rv) = 0.;
1227 1140
1228 1141
1229 for(s = s00; isspace(*s); s++) 1142 for(s = s00; isspace((unsigned char) *s); s++)
1230 ; 1143 ;
1231 1144
1232 if (*s == '-') { 1145 if (*s == '-') {
@@ -1340,9 +1253,9 @@ strtod
1340 if (!nd0) 1253 if (!nd0)
1341 nd0 = nd; 1254 nd0 = nd;
1342 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; 1255 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1343 rv = y; 1256 value(rv) = y;
1344 if (k > 9) 1257 if (k > 9)
1345 rv = tens[k - 9] * rv + z; 1258 value(rv) = tens[k - 9] * value(rv) + z;
1346 bd0 = 0; 1259 bd0 = 0;
1347 if (nd <= DBL_DIG 1260 if (nd <= DBL_DIG
1348#ifndef RND_PRODQUOT 1261#ifndef RND_PRODQUOT
@@ -1356,7 +1269,8 @@ strtod
1356#ifdef VAX 1269#ifdef VAX
1357 goto vax_ovfl_check; 1270 goto vax_ovfl_check;
1358#else 1271#else
1359 /* rv = */ rounded_product(rv, tens[e]); 1272 /* value(rv) = */ rounded_product(value(rv),
1273 tens[e]);
1360 goto ret; 1274 goto ret;
1361#endif 1275#endif
1362 } 1276 }
@@ -1366,27 +1280,30 @@ strtod
1366 * this for larger i values. 1280 * this for larger i values.
1367 */ 1281 */
1368 e -= i; 1282 e -= i;
1369 rv *= tens[i]; 1283 value(rv) *= tens[i];
1370#ifdef VAX 1284#ifdef VAX
1371 /* VAX exponent range is so narrow we must 1285 /* VAX exponent range is so narrow we must
1372 * worry about overflow here... 1286 * worry about overflow here...
1373 */ 1287 */
1374 vax_ovfl_check: 1288 vax_ovfl_check:
1375 word0(rv) -= P*Exp_msk1; 1289 word0(rv) -= P*Exp_msk1;
1376 /* rv = */ rounded_product(rv, tens[e]); 1290 /* value(rv) = */ rounded_product(value(rv),
1291 tens[e]);
1377 if ((word0(rv) & Exp_mask) 1292 if ((word0(rv) & Exp_mask)
1378 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) 1293 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1379 goto ovfl; 1294 goto ovfl;
1380 word0(rv) += P*Exp_msk1; 1295 word0(rv) += P*Exp_msk1;
1381#else 1296#else
1382 /* rv = */ rounded_product(rv, tens[e]); 1297 /* value(rv) = */ rounded_product(value(rv),
1298 tens[e]);
1383#endif 1299#endif
1384 goto ret; 1300 goto ret;
1385 } 1301 }
1386 } 1302 }
1387#ifndef Inaccurate_Divide 1303#ifndef Inaccurate_Divide
1388 else if (e >= -Ten_pmax) { 1304 else if (e >= -Ten_pmax) {
1389 /* rv = */ rounded_quotient(rv, tens[-e]); 1305 /* value(rv) = */ rounded_quotient(value(rv),
1306 tens[-e]);
1390 goto ret; 1307 goto ret;
1391 } 1308 }
1392#endif 1309#endif
@@ -1397,13 +1314,13 @@ strtod
1397 1314
1398 if (e1 > 0) { 1315 if (e1 > 0) {
1399 if (i = e1 & 15) 1316 if (i = e1 & 15)
1400 rv *= tens[i]; 1317 value(rv) *= tens[i];
1401 if (e1 &= ~15) { 1318 if (e1 &= ~15) {
1402 if (e1 > DBL_MAX_10_EXP) { 1319 if (e1 > DBL_MAX_10_EXP) {
1403 ovfl: 1320 ovfl:
1404 errno = ERANGE; 1321 errno = ERANGE;
1405#ifdef __STDC__ 1322#ifndef Bad_float_h
1406 rv = HUGE_VAL; 1323 value(rv) = HUGE_VAL;
1407#else 1324#else
1408 /* Can't trust HUGE_VAL */ 1325 /* Can't trust HUGE_VAL */
1409#ifdef IEEE_Arith 1326#ifdef IEEE_Arith
@@ -1421,10 +1338,10 @@ strtod
1421 if (e1 >>= 4) { 1338 if (e1 >>= 4) {
1422 for(j = 0; e1 > 1; j++, e1 >>= 1) 1339 for(j = 0; e1 > 1; j++, e1 >>= 1)
1423 if (e1 & 1) 1340 if (e1 & 1)
1424 rv *= bigtens[j]; 1341 value(rv) *= bigtens[j];
1425 /* The last multiplication could overflow. */ 1342 /* The last multiplication could overflow. */
1426 word0(rv) -= P*Exp_msk1; 1343 word0(rv) -= P*Exp_msk1;
1427 rv *= bigtens[j]; 1344 value(rv) *= bigtens[j];
1428 if ((z = word0(rv) & Exp_mask) 1345 if ((z = word0(rv) & Exp_mask)
1429 > Exp_msk1*(DBL_MAX_EXP+Bias-P)) 1346 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1430 goto ovfl; 1347 goto ovfl;
@@ -1443,23 +1360,23 @@ strtod
1443 else if (e1 < 0) { 1360 else if (e1 < 0) {
1444 e1 = -e1; 1361 e1 = -e1;
1445 if (i = e1 & 15) 1362 if (i = e1 & 15)
1446 rv /= tens[i]; 1363 value(rv) /= tens[i];
1447 if (e1 &= ~15) { 1364 if (e1 &= ~15) {
1448 e1 >>= 4; 1365 e1 >>= 4;
1449 if (e1 >= 1 << n_bigtens) 1366 if (e1 >= 1 << n_bigtens)
1450 goto undfl; 1367 goto undfl;
1451 for(j = 0; e1 > 1; j++, e1 >>= 1) 1368 for(j = 0; e1 > 1; j++, e1 >>= 1)
1452 if (e1 & 1) 1369 if (e1 & 1)
1453 rv *= tinytens[j]; 1370 value(rv) *= tinytens[j];
1454 /* The last multiplication could underflow. */ 1371 /* The last multiplication could underflow. */
1455 rv0 = rv; 1372 value(rv0) = value(rv);
1456 rv *= tinytens[j]; 1373 value(rv) *= tinytens[j];
1457 if (!rv) { 1374 if (!value(rv)) {
1458 rv = 2.*rv0; 1375 value(rv) = 2.*value(rv0);
1459 rv *= tinytens[j]; 1376 value(rv) *= tinytens[j];
1460 if (!rv) { 1377 if (!value(rv)) {
1461 undfl: 1378 undfl:
1462 rv = 0.; 1379 value(rv) = 0.;
1463 errno = ERANGE; 1380 errno = ERANGE;
1464 if (bd0) 1381 if (bd0)
1465 goto retfree; 1382 goto retfree;
@@ -1483,7 +1400,7 @@ strtod
1483 for(;;) { 1400 for(;;) {
1484 bd = Balloc(bd0->k); 1401 bd = Balloc(bd0->k);
1485 Bcopy(bd, bd0); 1402 Bcopy(bd, bd0);
1486 bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ 1403 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1487 bs = i2b(1); 1404 bs = i2b(1);
1488 1405
1489 if (e >= 0) { 1406 if (e >= 0) {
@@ -1595,12 +1512,12 @@ strtod
1595 break; 1512 break;
1596#endif 1513#endif
1597 if (dsign) 1514 if (dsign)
1598 rv += ulp(rv); 1515 value(rv) += ulp(value(rv));
1599#ifndef ROUND_BIASED 1516#ifndef ROUND_BIASED
1600 else { 1517 else {
1601 rv -= ulp(rv); 1518 value(rv) -= ulp(value(rv));
1602#ifndef Sudden_Underflow 1519#ifndef Sudden_Underflow
1603 if (!rv) 1520 if (!value(rv))
1604 goto undfl; 1521 goto undfl;
1605#endif 1522#endif
1606 } 1523 }
@@ -1651,10 +1568,10 @@ strtod
1651 /* Check for overflow */ 1568 /* Check for overflow */
1652 1569
1653 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { 1570 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1654 rv0 = rv; 1571 value(rv0) = value(rv);
1655 word0(rv) -= P*Exp_msk1; 1572 word0(rv) -= P*Exp_msk1;
1656 adj = aadj1 * ulp(rv); 1573 adj = aadj1 * ulp(value(rv));
1657 rv += adj; 1574 value(rv) += adj;
1658 if ((word0(rv) & Exp_mask) >= 1575 if ((word0(rv) & Exp_mask) >=
1659 Exp_msk1*(DBL_MAX_EXP+Bias-P)) { 1576 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1660 if (word0(rv0) == Big0 && word1(rv0) == Big1) 1577 if (word0(rv0) == Big0 && word1(rv0) == Big1)
@@ -1669,10 +1586,10 @@ strtod
1669 else { 1586 else {
1670#ifdef Sudden_Underflow 1587#ifdef Sudden_Underflow
1671 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { 1588 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1672 rv0 = rv; 1589 value(rv0) = value(rv);
1673 word0(rv) += P*Exp_msk1; 1590 word0(rv) += P*Exp_msk1;
1674 adj = aadj1 * ulp(rv); 1591 adj = aadj1 * ulp(value(rv));
1675 rv += adj; 1592 value(rv) += adj;
1676#ifdef IBM 1593#ifdef IBM
1677 if ((word0(rv) & Exp_mask) < P*Exp_msk1) 1594 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1678#else 1595#else
@@ -1690,8 +1607,8 @@ strtod
1690 word0(rv) -= P*Exp_msk1; 1607 word0(rv) -= P*Exp_msk1;
1691 } 1608 }
1692 else { 1609 else {
1693 adj = aadj1 * ulp(rv); 1610 adj = aadj1 * ulp(value(rv));
1694 rv += adj; 1611 value(rv) += adj;
1695 } 1612 }
1696#else 1613#else
1697 /* Compute adj so that the IEEE rounding rules will 1614 /* Compute adj so that the IEEE rounding rules will
@@ -1706,8 +1623,8 @@ strtod
1706 if (!dsign) 1623 if (!dsign)
1707 aadj1 = -aadj1; 1624 aadj1 = -aadj1;
1708 } 1625 }
1709 adj = aadj1 * ulp(rv); 1626 adj = aadj1 * ulp(value(rv));
1710 rv += adj; 1627 value(rv) += adj;
1711#endif 1628#endif
1712 } 1629 }
1713 z = word0(rv) & Exp_mask; 1630 z = word0(rv) & Exp_mask;
@@ -1738,16 +1655,11 @@ strtod
1738 ret: 1655 ret:
1739 if (se) 1656 if (se)
1740 *se = (char *)s; 1657 *se = (char *)s;
1741 return sign ? -rv : rv; 1658 return sign ? -value(rv) : value(rv);
1742 } 1659 }
1743 1660
1744 static int 1661 static int
1745quorem 1662quorem(Bigint *b, Bigint *S)
1746#ifdef KR_headers
1747 (b, S) Bigint *b, *S;
1748#else
1749 (Bigint *b, Bigint *S)
1750#endif
1751{ 1663{
1752 int n; 1664 int n;
1753 Long borrow, y; 1665 Long borrow, y;
@@ -1882,13 +1794,7 @@ quorem
1882 */ 1794 */
1883 1795
1884 char * 1796 char *
1885__dtoa 1797__dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1886#ifdef KR_headers
1887 (d, mode, ndigits, decpt, sign, rve)
1888 double d; int mode, ndigits, *decpt, *sign; char **rve;
1889#else
1890 (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1891#endif
1892{ 1798{
1893 /* Arguments ndigits, decpt, sign are similar to those 1799 /* Arguments ndigits, decpt, sign are similar to those
1894 of ecvt and fcvt; trailing zeros are suppressed from 1800 of ecvt and fcvt; trailing zeros are suppressed from
@@ -1933,11 +1839,13 @@ __dtoa
1933 ULong x; 1839 ULong x;
1934#endif 1840#endif
1935 Bigint *b, *b1, *delta, *mlo, *mhi, *S; 1841 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1936 double d2, ds, eps; 1842 double ds;
1937 char *s, *s0; 1843 char *s, *s0;
1938 static Bigint *result; 1844 static Bigint *result;
1939 static int result_k; 1845 static int result_k;
1846 _double d, d2, eps;
1940 1847
1848 value(d) = _d;
1941 if (result) { 1849 if (result) {
1942 result->k = result_k; 1850 result->k = result_k;
1943 result->maxwds = 1 << result_k; 1851 result->maxwds = 1 << result_k;
@@ -1964,7 +1872,7 @@ __dtoa
1964 *decpt = 9999; 1872 *decpt = 9999;
1965 s = 1873 s =
1966#ifdef IEEE_Arith 1874#ifdef IEEE_Arith
1967 !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" : 1875 !word1(d) && !(word0(d) & 0xfffff) ? ndigits < 8 ? "Inf" : "Infinity" :
1968#endif 1876#endif
1969 "NaN"; 1877 "NaN";
1970 if (rve) 1878 if (rve)
@@ -1977,9 +1885,9 @@ __dtoa
1977 } 1885 }
1978#endif 1886#endif
1979#ifdef IBM 1887#ifdef IBM
1980 d += 0; /* normalize */ 1888 value(d) += 0; /* normalize */
1981#endif 1889#endif
1982 if (!d) { 1890 if (!value(d)) {
1983 *decpt = 1; 1891 *decpt = 1;
1984 s = "0"; 1892 s = "0";
1985 if (rve) 1893 if (rve)
@@ -1987,18 +1895,18 @@ __dtoa
1987 return s; 1895 return s;
1988 } 1896 }
1989 1897
1990 b = d2b(d, &be, &bbits); 1898 b = d2b(value(d), &be, &bbits);
1991#ifdef Sudden_Underflow 1899#ifdef Sudden_Underflow
1992 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); 1900 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1993#else 1901#else
1994 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { 1902 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
1995#endif 1903#endif
1996 d2 = d; 1904 value(d2) = value(d);
1997 word0(d2) &= Frac_mask1; 1905 word0(d2) &= Frac_mask1;
1998 word0(d2) |= Exp_11; 1906 word0(d2) |= Exp_11;
1999#ifdef IBM 1907#ifdef IBM
2000 if (j = 11 - hi0bits(word0(d2) & Frac_mask)) 1908 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2001 d2 /= 1 << j; 1909 value(d2) /= 1 << j;
2002#endif 1910#endif
2003 1911
2004 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 1912 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
@@ -2037,19 +1945,20 @@ __dtoa
2037 i = bbits + be + (Bias + (P-1) - 1); 1945 i = bbits + be + (Bias + (P-1) - 1);
2038 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 1946 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32
2039 : word1(d) << 32 - i; 1947 : word1(d) << 32 - i;
2040 d2 = x; 1948 value(d2) = x;
2041 word0(d2) -= 31*Exp_msk1; /* adjust exponent */ 1949 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2042 i -= (Bias + (P-1) - 1) + 1; 1950 i -= (Bias + (P-1) - 1) + 1;
2043 denorm = 1; 1951 denorm = 1;
2044 } 1952 }
2045#endif 1953#endif
2046 ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; 1954 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 +
1955 i*0.301029995663981;
2047 k = (int)ds; 1956 k = (int)ds;
2048 if (ds < 0. && ds != k) 1957 if (ds < 0. && ds != k)
2049 k--; /* want k = floor(ds) */ 1958 k--; /* want k = floor(ds) */
2050 k_check = 1; 1959 k_check = 1;
2051 if (k >= 0 && k <= Ten_pmax) { 1960 if (k >= 0 && k <= Ten_pmax) {
2052 if (d < tens[k]) 1961 if (value(d) < tens[k])
2053 k--; 1962 k--;
2054 k_check = 0; 1963 k_check = 0;
2055 } 1964 }
@@ -2116,7 +2025,7 @@ __dtoa
2116 /* Try to get by with floating-point arithmetic. */ 2025 /* Try to get by with floating-point arithmetic. */
2117 2026
2118 i = 0; 2027 i = 0;
2119 d2 = d; 2028 value(d2) = value(d);
2120 k0 = k; 2029 k0 = k;
2121 ilim0 = ilim; 2030 ilim0 = ilim;
2122 ieps = 2; /* conservative */ 2031 ieps = 2; /* conservative */
@@ -2126,7 +2035,7 @@ __dtoa
2126 if (j & Bletch) { 2035 if (j & Bletch) {
2127 /* prevent overflows */ 2036 /* prevent overflows */
2128 j &= Bletch - 1; 2037 j &= Bletch - 1;
2129 d /= bigtens[n_bigtens-1]; 2038 value(d) /= bigtens[n_bigtens-1];
2130 ieps++; 2039 ieps++;
2131 } 2040 }
2132 for(; j; j >>= 1, i++) 2041 for(; j; j >>= 1, i++)
@@ -2134,32 +2043,32 @@ __dtoa
2134 ieps++; 2043 ieps++;
2135 ds *= bigtens[i]; 2044 ds *= bigtens[i];
2136 } 2045 }
2137 d /= ds; 2046 value(d) /= ds;
2138 } 2047 }
2139 else if (j1 = -k) { 2048 else if (j1 = -k) {
2140 d *= tens[j1 & 0xf]; 2049 value(d) *= tens[j1 & 0xf];
2141 for(j = j1 >> 4; j; j >>= 1, i++) 2050 for(j = j1 >> 4; j; j >>= 1, i++)
2142 if (j & 1) { 2051 if (j & 1) {
2143 ieps++; 2052 ieps++;
2144 d *= bigtens[i]; 2053 value(d) *= bigtens[i];
2145 } 2054 }
2146 } 2055 }
2147 if (k_check && d < 1. && ilim > 0) { 2056 if (k_check && value(d) < 1. && ilim > 0) {
2148 if (ilim1 <= 0) 2057 if (ilim1 <= 0)
2149 goto fast_failed; 2058 goto fast_failed;
2150 ilim = ilim1; 2059 ilim = ilim1;
2151 k--; 2060 k--;
2152 d *= 10.; 2061 value(d) *= 10.;
2153 ieps++; 2062 ieps++;
2154 } 2063 }
2155 eps = ieps*d + 7.; 2064 value(eps) = ieps*value(d) + 7.;
2156 word0(eps) -= (P-1)*Exp_msk1; 2065 word0(eps) -= (P-1)*Exp_msk1;
2157 if (ilim == 0) { 2066 if (ilim == 0) {
2158 S = mhi = 0; 2067 S = mhi = 0;
2159 d -= 5.; 2068 value(d) -= 5.;
2160 if (d > eps) 2069 if (value(d) > value(eps))
2161 goto one_digit; 2070 goto one_digit;
2162 if (d < -eps) 2071 if (value(d) < -value(eps))
2163 goto no_digits; 2072 goto no_digits;
2164 goto fast_failed; 2073 goto fast_failed;
2165 } 2074 }
@@ -2168,33 +2077,33 @@ __dtoa
2168 /* Use Steele & White method of only 2077 /* Use Steele & White method of only
2169 * generating digits needed. 2078 * generating digits needed.
2170 */ 2079 */
2171 eps = 0.5/tens[ilim-1] - eps; 2080 value(eps) = 0.5/tens[ilim-1] - value(eps);
2172 for(i = 0;;) { 2081 for(i = 0;;) {
2173 L = d; 2082 L = value(d);
2174 d -= L; 2083 value(d) -= L;
2175 *s++ = '0' + (int)L; 2084 *s++ = '0' + (int)L;
2176 if (d < eps) 2085 if (value(d) < value(eps))
2177 goto ret1; 2086 goto ret1;
2178 if (1. - d < eps) 2087 if (1. - value(d) < value(eps))
2179 goto bump_up; 2088 goto bump_up;
2180 if (++i >= ilim) 2089 if (++i >= ilim)
2181 break; 2090 break;
2182 eps *= 10.; 2091 value(eps) *= 10.;
2183 d *= 10.; 2092 value(d) *= 10.;
2184 } 2093 }
2185 } 2094 }
2186 else { 2095 else {
2187#endif 2096#endif
2188 /* Generate ilim digits, then fix them up. */ 2097 /* Generate ilim digits, then fix them up. */
2189 eps *= tens[ilim-1]; 2098 value(eps) *= tens[ilim-1];
2190 for(i = 1;; i++, d *= 10.) { 2099 for(i = 1;; i++, value(d) *= 10.) {
2191 L = d; 2100 L = value(d);
2192 d -= L; 2101 value(d) -= L;
2193 *s++ = '0' + (int)L; 2102 *s++ = '0' + (int)L;
2194 if (i == ilim) { 2103 if (i == ilim) {
2195 if (d > 0.5 + eps) 2104 if (value(d) > 0.5 + value(eps))
2196 goto bump_up; 2105 goto bump_up;
2197 else if (d < 0.5 - eps) { 2106 else if (value(d) < 0.5 - value(eps)) {
2198 while(*--s == '0'); 2107 while(*--s == '0');
2199 s++; 2108 s++;
2200 goto ret1; 2109 goto ret1;
@@ -2207,7 +2116,7 @@ __dtoa
2207#endif 2116#endif
2208 fast_failed: 2117 fast_failed:
2209 s = s0; 2118 s = s0;
2210 d = d2; 2119 value(d) = value(d2);
2211 k = k0; 2120 k = k0;
2212 ilim = ilim0; 2121 ilim = ilim0;
2213 } 2122 }
@@ -2219,24 +2128,24 @@ __dtoa
2219 ds = tens[k]; 2128 ds = tens[k];
2220 if (ndigits < 0 && ilim <= 0) { 2129 if (ndigits < 0 && ilim <= 0) {
2221 S = mhi = 0; 2130 S = mhi = 0;
2222 if (ilim < 0 || d <= 5*ds) 2131 if (ilim < 0 || value(d) <= 5*ds)
2223 goto no_digits; 2132 goto no_digits;
2224 goto one_digit; 2133 goto one_digit;
2225 } 2134 }
2226 for(i = 1;; i++) { 2135 for(i = 1;; i++) {
2227 L = d / ds; 2136 L = value(d) / ds;
2228 d -= L*ds; 2137 value(d) -= L*ds;
2229#ifdef Check_FLT_ROUNDS 2138#ifdef Check_FLT_ROUNDS
2230 /* If FLT_ROUNDS == 2, L will usually be high by 1 */ 2139 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2231 if (d < 0) { 2140 if (value(d) < 0) {
2232 L--; 2141 L--;
2233 d += ds; 2142 value(d) += ds;
2234 } 2143 }
2235#endif 2144#endif
2236 *s++ = '0' + (int)L; 2145 *s++ = '0' + (int)L;
2237 if (i == ilim) { 2146 if (i == ilim) {
2238 d += d; 2147 value(d) += value(d);
2239 if (d > ds || d == ds && L & 1) { 2148 if (value(d) > ds || value(d) == ds && L & 1) {
2240 bump_up: 2149 bump_up:
2241 while(*--s == '9') 2150 while(*--s == '9')
2242 if (s == s0) { 2151 if (s == s0) {
@@ -2248,7 +2157,7 @@ __dtoa
2248 } 2157 }
2249 break; 2158 break;
2250 } 2159 }
2251 if (!(d *= 10.)) 2160 if (!(value(d) *= 10.))
2252 break; 2161 break;
2253 } 2162 }
2254 goto ret1; 2163 goto ret1;