Skip to content
Browse files

import lite-2

  • Loading branch information...
1 parent 839e77b commit 2794870ef6aab8c3ec9b5a2a654a70529957ad9c perry committed Jan 30, 1998
Showing with 320 additions and 345 deletions.
  1. +320 −345 lib/libc/stdlib/strtod.c
View
665 lib/libc/stdlib/strtod.c
@@ -1,3 +1,40 @@
+/*-
+ * Copyright (c) 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)strtod.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
/****************************************************************
*
* The author of this software is David M. Gay.
@@ -61,7 +98,6 @@
* significant byte has the lowest address.
* #define IEEE_MC68k for IEEE-arithmetic machines where the most
* significant byte has the lowest address.
- * #define Long int on machines with 32-bit ints and 64-bit longs.
* #define Sudden_Underflow for IEEE-format machines without gradual
* underflow (i.e., that flush to zero on underflow).
* #define IBM for IBM mainframe-style floating-point arithmetic.
@@ -76,21 +112,19 @@
* #define ROUND_BIASED for IEEE-format with biased rounding.
* #define Inaccurate_Divide for IEEE-format with correctly rounded
* products but inaccurate quotients, e.g., for Intel i860.
- * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
+ * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
* integer arithmetic. Whether this speeds things up or slows things
* down depends on the machine and the number being converted.
* #define KR_headers for old-style C function headers.
* #define Bad_float_h if your system lacks a float.h or if it does not
* define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
* FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
- * #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.
*/
-#ifndef Long
-#define Long long
+#if defined(i386) || defined(mips) && defined(MIPSEL)
+#define IEEE_8087
+#else
+#define IEEE_MC68k
#endif
#ifdef DEBUG
@@ -111,16 +145,6 @@
#endif
#endif
-#ifdef MALLOC
-#ifdef KR_headers
-extern char *MALLOC();
-#else
-extern void *MALLOC(size_t);
-#endif
-#else
-#define MALLOC malloc
-#endif
-
#include "errno.h"
#ifdef Bad_float_h
#undef __STDC__
@@ -130,7 +154,6 @@ extern void *MALLOC(size_t);
#ifdef IEEE_8087
#define IEEE_ARITHMETIC
#endif
-
#ifdef IEEE_ARITHMETIC
#define DBL_DIG 15
#define DBL_MAX_10_EXP 308
@@ -191,11 +214,11 @@ Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
#endif
#ifdef IEEE_8087
-#define word0(x) ((unsigned Long *)&x)[1]
-#define word1(x) ((unsigned Long *)&x)[0]
+#define word0(x) ((unsigned long *)&x)[1]
+#define word1(x) ((unsigned long *)&x)[0]
#else
-#define word0(x) ((unsigned Long *)&x)[0]
-#define word1(x) ((unsigned Long *)&x)[1]
+#define word0(x) ((unsigned long *)&x)[0]
+#define word1(x) ((unsigned long *)&x)[1]
#endif
/* The following definition of Storeinc is appropriate for MIPS processors.
@@ -318,10 +341,10 @@ extern double rnd_prod(double, double), rnd_quot(double, double);
#define Big1 0xffffffff
#ifndef Just_16
-/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
+/* When Pack_32 is not defined, we store 16 bits per 32-bit long.
* This makes some inner loops simpler and sometimes saves work
* during multiplications, but it often seems to make things slightly
- * slower. Hence the default is now to store 32 bits per Long.
+ * slower. Hence the default is now to store 32 bits per long.
*/
#ifndef Pack_32
#define Pack_32
@@ -340,8 +363,8 @@ extern "C" char *dtoa(double d, int mode, int ndigits,
Bigint {
struct Bigint *next;
int k, maxwds, sign, wds;
- unsigned Long x[1];
- };
+ unsigned long x[1];
+};
typedef struct Bigint Bigint;
@@ -360,16 +383,15 @@ Balloc
if (rv = freelist[k]) {
freelist[k] = rv->next;
- }
- else {
+ } else {
x = 1 << k;
- rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
+ rv = (Bigint *)malloc(sizeof(Bigint) + (x-1)*sizeof(long));
rv->k = k;
rv->maxwds = x;
- }
+ }
rv->sign = rv->wds = 0;
return rv;
- }
+}
static void
Bfree
@@ -382,11 +404,11 @@ Bfree
if (v) {
v->next = freelist[v->k];
freelist[v->k] = v;
- }
}
+}
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
-y->wds*sizeof(Long) + 2*sizeof(int))
+y->wds*sizeof(long) + 2*sizeof(int))
static Bigint *
multadd
@@ -397,9 +419,9 @@ multadd
#endif
{
int i, wds;
- unsigned Long *x, y;
+ unsigned long *x, y;
#ifdef Pack_32
- unsigned Long xi, z;
+ unsigned long xi, z;
#endif
Bigint *b1;
@@ -418,8 +440,7 @@ multadd
a = (int)(y >> 16);
*x++ = y & 0xffff;
#endif
- }
- while(++i < wds);
+ } while (++i < wds);
if (a) {
if (wds >= b->maxwds) {
b1 = Balloc(b->k+1);
@@ -429,24 +450,24 @@ multadd
}
b->x[wds++] = a;
b->wds = wds;
- }
- return b;
}
+ return b;
+}
static Bigint *
s2b
#ifdef KR_headers
- (s, nd0, nd, y9) CONST char *s; int nd0, nd; unsigned Long y9;
+ (s, nd0, nd, y9) CONST char *s; int nd0, nd; unsigned long y9;
#else
- (CONST char *s, int nd0, int nd, unsigned Long y9)
+ (CONST char *s, int nd0, int nd, unsigned long y9)
#endif
{
Bigint *b;
int i, k;
- Long x, y;
+ long x, y;
x = (nd + 8) / 9;
- for(k = 0, y = 1; x > y; y <<= 1, k++) ;
+ for (k = 0, y = 1; x > y; y <<= 1, k++) ;
#ifdef Pack_32
b = Balloc(k);
b->x[0] = y9;
@@ -460,98 +481,98 @@ s2b
i = 9;
if (9 < nd0) {
s += 9;
- do b = multadd(b, 10, *s++ - '0');
- while(++i < nd0);
+ do
+ b = multadd(b, 10, *s++ - '0');
+ while (++i < nd0);
s++;
- }
- else
+ } else
s += 10;
- for(; i < nd; i++)
+ for (; i < nd; i++)
b = multadd(b, 10, *s++ - '0');
return b;
- }
+}
static int
hi0bits
#ifdef KR_headers
- (x) register unsigned Long x;
+ (x) register unsigned long x;
#else
- (register unsigned Long x)
+ (register unsigned long x)
#endif
{
register 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;
}
+ return k;
+}
static int
lo0bits
#ifdef KR_headers
- (y) unsigned Long *y;
+ (y) unsigned long *y;
#else
- (unsigned Long *y)
+ (unsigned long *y)
#endif
{
register int k;
- register unsigned Long x = *y;
+ register unsigned long 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 & 1)
return 32;
- }
+ }
*y = x;
return k;
- }
+}
static Bigint *
i2b
@@ -579,33 +600,33 @@ mult
{
Bigint *c;
int k, wa, wb, wc;
- unsigned Long carry, y, z;
- unsigned Long *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
+ unsigned long carry, y, z;
+ unsigned long *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
#ifdef Pack_32
- unsigned Long z2;
+ unsigned long z2;
#endif
if (a->wds < b->wds) {
c = a;
a = b;
b = c;
- }
+ }
k = a->k;
wa = a->wds;
wb = b->wds;
wc = wa + wb;
if (wc > a->maxwds)
k++;
c = Balloc(k);
- for(x = c->x, xa = x + wc; x < xa; x++)
+ for (x = c->x, xa = x + wc; x < xa; x++)
*x = 0;
xa = a->x;
xae = xa + wa;
xb = b->x;
xbe = xb + wb;
xc0 = c->x;
#ifdef Pack_32
- for(; xb < xbe; xb++, xc0++) {
+ for (; xb < xbe; xb++, xc0++) {
if (y = *xb & 0xffff) {
x = xa;
xc = xc0;
@@ -616,10 +637,9 @@ mult
z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
carry = z2 >> 16;
Storeinc(xc, z2, z);
- }
- while(x < xae);
+ } while (x < xae);
*xc = carry;
- }
+ }
if (y = *xb >> 16) {
x = xa;
xc = xc0;
@@ -631,13 +651,12 @@ mult
Storeinc(xc, z, z2);
z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
carry = z2 >> 16;
- }
- while(x < xae);
+ } while (x < xae);
*xc = z2;
- }
}
+ }
#else
- for(; xb < xbe; xc0++) {
+ for (; xb < xbe; xc0++) {
if (y = *xb++) {
x = xa;
xc = xc0;
@@ -646,16 +665,15 @@ mult
z = *x++ * y + *xc + carry;
carry = z >> 16;
*xc++ = z & 0xffff;
- }
- while(x < xae);
+ } while (x < xae);
*xc = carry;
- }
}
+ }
#endif
- for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
+ for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
c->wds = wc;
return c;
- }
+}
static Bigint *p5s;
@@ -680,23 +698,23 @@ pow5mult
/* first time */
p5 = p5s = i2b(625);
p5->next = 0;
- }
- for(;;) {
+ }
+ for (;;) {
if (k & 1) {
b1 = mult(b, p5);
Bfree(b);
b = b1;
- }
+ }
if (!(k >>= 1))
break;
if (!(p51 = p5->next)) {
p51 = p5->next = mult(p5,p5);
p51->next = 0;
- }
- p5 = p51;
}
- return b;
+ p5 = p51;
}
+ return b;
+}
static Bigint *
lshift
@@ -708,7 +726,7 @@ lshift
{
int i, k1, n, n1;
Bigint *b1;
- unsigned Long *x, *x1, *xe, z;
+ unsigned long *x, *x1, *xe, z;
#ifdef Pack_32
n = k >> 5;
@@ -717,11 +735,11 @@ lshift
#endif
k1 = b->k;
n1 = n + b->wds + 1;
- for(i = b->maxwds; n1 > i; i <<= 1)
+ for (i = b->maxwds; n1 > i; i <<= 1)
k1++;
b1 = Balloc(k1);
x1 = b1->x;
- for(i = 0; i < n; i++)
+ for (i = 0; i < n; i++)
*x1++ = 0;
x = b->x;
xe = x + b->wds;
@@ -732,31 +750,30 @@ lshift
do {
*x1++ = *x << k | z;
z = *x++ >> k1;
- }
- while(x < xe);
+ } while (x < xe);
if (*x1 = z)
++n1;
- }
+ }
#else
if (k &= 0xf) {
k1 = 16 - k;
z = 0;
do {
*x1++ = *x << k & 0xffff | z;
z = *x++ >> k1;
- }
- while(x < xe);
+ } while (x < xe);
if (*x1 = z)
++n1;
- }
+ }
#endif
- else do
- *x1++ = *x++;
- while(x < xe);
+ else
+ do
+ *x1++ = *x++;
+ while (x < xe);
b1->wds = n1 - 1;
Bfree(b);
return b1;
- }
+}
static int
cmp
@@ -766,7 +783,7 @@ cmp
(Bigint *a, Bigint *b)
#endif
{
- unsigned Long *xa, *xa0, *xb, *xb0;
+ unsigned long *xa, *xa0, *xb, *xb0;
int i, j;
i = a->wds;
@@ -783,14 +800,14 @@ cmp
xa = xa0 + j;
xb0 = b->x;
xb = xb0 + j;
- for(;;) {
+ for (;;) {
if (*--xa != *--xb)
return *xa < *xb ? -1 : 1;
if (xa <= xa0)
break;
- }
- return 0;
}
+ return 0;
+}
static Bigint *
diff
@@ -802,10 +819,10 @@ diff
{
Bigint *c;
int i, wa, wb;
- Long borrow, y; /* We need signed shifts here. */
- unsigned Long *xa, *xae, *xb, *xbe, *xc;
+ long borrow, y; /* We need signed shifts here. */
+ unsigned long *xa, *xae, *xb, *xbe, *xc;
#ifdef Pack_32
- Long z;
+ long z;
#endif
i = cmp(a,b);
@@ -814,14 +831,13 @@ diff
c->wds = 1;
c->x[0] = 0;
return c;
- }
+ }
if (i < 0) {
c = a;
a = b;
b = c;
i = 1;
- }
- else
+ } else
i = 0;
c = Balloc(a->k);
c->sign = i;
@@ -842,37 +858,35 @@ diff
borrow = z >> 16;
Sign_Extend(borrow, z);
Storeinc(xc, z, y);
- }
- while(xb < xbe);
- while(xa < xae) {
+ } while (xb < xbe);
+ while (xa < xae) {
y = (*xa & 0xffff) + borrow;
borrow = y >> 16;
Sign_Extend(borrow, y);
z = (*xa++ >> 16) + borrow;
borrow = z >> 16;
Sign_Extend(borrow, z);
Storeinc(xc, z, y);
- }
+ }
#else
do {
y = *xa++ - *xb++ + borrow;
borrow = y >> 16;
Sign_Extend(borrow, y);
*xc++ = y & 0xffff;
- }
- while(xb < xbe);
- while(xa < xae) {
+ } while (xb < xbe);
+ while (xa < xae) {
y = *xa++ + borrow;
borrow = y >> 16;
Sign_Extend(borrow, y);
*xc++ = y & 0xffff;
- }
+ }
#endif
- while(!*--xc)
+ while (!*--xc)
wa--;
c->wds = wa;
return c;
- }
+}
static double
ulp
@@ -882,7 +896,7 @@ ulp
(double x)
#endif
{
- register Long L;
+ register long L;
double a;
L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
@@ -895,22 +909,20 @@ ulp
word0(a) = L;
word1(a) = 0;
#ifndef Sudden_Underflow
- }
- else {
+ } else {
L = -L >> Exp_shift;
if (L < Exp_shift) {
word0(a) = 0x80000 >> L;
word1(a) = 0;
- }
- else {
+ } else {
word0(a) = 0;
L -= Exp_shift;
word1(a) = L >= 31 ? 1 : 1 << 31 - L;
- }
}
+ }
#endif
return a;
- }
+}
static double
b2d
@@ -920,11 +932,11 @@ b2d
(Bigint *a, int *e)
#endif
{
- unsigned Long *xa, *xa0, w, y, z;
+ unsigned long *xa, *xa0, w, y, z;
int k;
double d;
#ifdef VAX
- unsigned Long d0, d1;
+ unsigned long d0, d1;
#else
#define d0 word0(d)
#define d1 word1(d)
@@ -950,11 +962,10 @@ b2d
d0 = Exp_1 | y << k | z >> 32 - k;
y = xa > xa0 ? *--xa : 0;
d1 = z << k | y >> 32 - k;
- }
- else {
+ } else {
d0 = Exp_1 | y;
d1 = z;
- }
+ }
#else
if (k < Ebits + 16) {
z = xa > xa0 ? *--xa : 0;
@@ -963,7 +974,7 @@ b2d
y = xa > xa0 ? *--xa : 0;
d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
goto ret_d;
- }
+ }
z = xa > xa0 ? *--xa : 0;
w = xa > xa0 ? *--xa : 0;
k -= Ebits + 16;
@@ -980,7 +991,7 @@ b2d
#undef d1
#endif
return d;
- }
+}
static Bigint *
d2b
@@ -992,9 +1003,9 @@ d2b
{
Bigint *b;
int de, i, k;
- unsigned Long *x, y, z;
+ unsigned long *x, y, z;
#ifdef VAX
- unsigned Long d0, d1;
+ unsigned long d0, d1;
d0 = word0(d) >> 16 | word0(d) << 16;
d1 = word1(d) >> 16 | word1(d) << 16;
#else
@@ -1029,8 +1040,7 @@ d2b
else
x[0] = y;
i = b->wds = (x[1] = z) ? 2 : 1;
- }
- else {
+ } else {
#ifdef DEBUG
if (!z)
Bug("Zero passed to d2b");
@@ -1039,7 +1049,7 @@ d2b
x[0] = z;
i = b->wds = 1;
k += 32;
- }
+ }
#else
if (y = d1) {
if (k = lo0bits(&y))
@@ -1048,23 +1058,21 @@ d2b
x[1] = z >> k - 16 & 0xffff;
x[2] = z >> k;
i = 2;
- }
- else {
+ } else {
x[0] = y & 0xffff;
x[1] = y >> 16 | z << 16 - k & 0xffff;
x[2] = z >> k & 0xffff;
x[3] = z >> k+16;
i = 3;
- }
+ }
else {
x[0] = y & 0xffff;
x[1] = y >> 16;
x[2] = z & 0xffff;
x[3] = z >> 16;
i = 3;
- }
}
- else {
+ } else {
#ifdef DEBUG
if (!z)
Bug("Zero passed to d2b");
@@ -1073,15 +1081,14 @@ d2b
if (k >= 16) {
x[0] = z;
i = 0;
- }
- else {
+ } else {
x[0] = z & 0xffff;
x[1] = z >> 16;
i = 1;
- }
- k += 32;
}
- while(!x[i])
+ k += 32;
+ }
+ while (!x[i])
--i;
b->wds = i + 1;
#endif
@@ -1096,18 +1103,17 @@ d2b
*bits = P - k;
#endif
#ifndef Sudden_Underflow
- }
- else {
+ } else {
*e = de - Bias - (P-1) + 1 + k;
#ifdef Pack_32
*bits = 32*i - hi0bits(x[i-1]);
#else
*bits = (i+2)*16 - hi0bits(x[i]);
#endif
- }
+ }
#endif
return b;
- }
+}
#undef d0
#undef d1
@@ -1134,25 +1140,24 @@ ratio
word0(da) += (k >> 2)*Exp_msk1;
if (k &= 3)
da *= 1 << k;
- }
- else {
+ } else {
k = -k;
word0(db) += (k >> 2)*Exp_msk1;
if (k &= 3)
db *= 1 << k;
- }
+ }
#else
if (k > 0)
word0(da) += k*Exp_msk1;
else {
k = -k;
word0(db) += k*Exp_msk1;
- }
+ }
#endif
return da / db;
- }
+}
- static CONST double
+ static double
tens[] = {
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
@@ -1162,19 +1167,19 @@ tens[] = {
#endif
};
- static CONST double
+ static double
#ifdef IEEE_Arith
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
-static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
+static double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
#define n_bigtens 5
#else
#ifdef IBM
bigtens[] = { 1e16, 1e32, 1e64 };
-static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
+static double tinytens[] = { 1e-16, 1e-32, 1e-64 };
#define n_bigtens 3
#else
bigtens[] = { 1e16, 1e32 };
-static CONST double tinytens[] = { 1e-16, 1e-32 };
+static double tinytens[] = { 1e-16, 1e-32 };
#define n_bigtens 2
#endif
#endif
@@ -1191,12 +1196,12 @@ strtod
e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
CONST char *s, *s0, *s1;
double aadj, aadj1, adj, rv, rv0;
- Long L;
- unsigned Long y, z;
+ long L;
+ unsigned long y, z;
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
sign = nz0 = nz = 0;
rv = 0.;
- for(s = s00;;s++) switch(*s) {
+ for (s = s00;;s++) switch(*s) {
case '-':
sign = 1;
/* no break */
@@ -1216,17 +1221,17 @@ strtod
continue;
default:
goto break2;
- }
+ }
break2:
if (*s == '0') {
nz0 = 1;
- while(*++s == '0') ;
+ while (*++s == '0') ;
if (!*s)
goto ret;
- }
+ }
s0 = s;
y = z = 0;
- for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
+ for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
if (nd < 9)
y = 10*y + c - '0';
else if (nd < 16)
@@ -1235,22 +1240,22 @@ strtod
if (c == '.') {
c = *++s;
if (!nd) {
- for(; c == '0'; c = *++s)
+ for (; c == '0'; c = *++s)
nz++;
if (c > '0' && c <= '9') {
s0 = s;
nf += nz;
nz = 0;
goto have_dig;
- }
- goto dig_done;
}
- for(; c >= '0' && c <= '9'; c = *++s) {
+ goto dig_done;
+ }
+ for (; c >= '0' && c <= '9'; c = *++s) {
have_dig:
nz++;
if (c -= '0') {
nf += nz;
- for(i = 1; i < nz; i++)
+ for (i = 1; i < nz; i++)
if (nd++ < 9)
y *= 10;
else if (nd <= DBL_DIG + 1)
@@ -1260,31 +1265,31 @@ strtod
else if (nd <= DBL_DIG + 1)
z = 10*z + c;
nz = 0;
- }
}
}
+ }
dig_done:
e = 0;
if (c == 'e' || c == 'E') {
if (!nd && !nz && !nz0) {
s = s00;
goto ret;
- }
+ }
s00 = s;
esign = 0;
switch(c = *++s) {
case '-':
esign = 1;
case '+':
c = *++s;
- }
+ }
if (c >= '0' && c <= '9') {
- while(c == '0')
+ while (c == '0')
c = *++s;
if (c > '0' && c <= '9') {
L = c - '0';
s1 = s;
- while((c = *++s) >= '0' && c <= '9')
+ while ((c = *++s) >= '0' && c <= '9')
L = 10*L + c - '0';
if (s - s1 > 8 || L > 19999)
/* Avoid confusion from exponents
@@ -1295,18 +1300,16 @@ strtod
e = (int)L;
if (esign)
e = -e;
- }
- else
+ } else
e = 0;
- }
- else
+ } else
s = s00;
- }
+ }
if (!nd) {
if (!nz && !nz0)
s = s00;
goto ret;
- }
+ }
e1 = e -= nf;
/* Now we have nd0 digits, starting at s0, followed by a
@@ -1320,7 +1323,6 @@ strtod
rv = y;
if (k > 9)
rv = tens[k - 9] * rv + z;
- bd0 = 0;
if (nd <= DBL_DIG
#ifndef RND_PRODQUOT
&& FLT_ROUNDS == 1
@@ -1359,15 +1361,15 @@ strtod
/* rv = */ rounded_product(rv, tens[e]);
#endif
goto ret;
- }
}
+ }
#ifndef Inaccurate_Divide
else if (e >= -Ten_pmax) {
/* rv = */ rounded_quotient(rv, tens[-e]);
goto ret;
- }
-#endif
}
+#endif
+ }
e1 += nd - k;
/* Get starting approximation = rv * 10**e1 */
@@ -1391,12 +1393,10 @@ strtod
word1(rv) = Big1;
#endif
#endif
- if (bd0)
- goto retfree;
goto ret;
- }
+ }
if (e1 >>= 4) {
- for(j = 0; e1 > 1; j++, e1 >>= 1)
+ for (j = 0; e1 > 1; j++, e1 >>= 1)
if (e1 & 1)
rv *= bigtens[j];
/* The last multiplication could overflow. */
@@ -1413,19 +1413,15 @@ strtod
}
else
word0(rv) += P*Exp_msk1;
- }
-
}
}
- else if (e1 < 0) {
+ } else if (e1 < 0) {
e1 = -e1;
if (i = e1 & 15)
rv /= tens[i];
if (e1 &= ~15) {
e1 >>= 4;
- if (e1 >= 1 << n_bigtens)
- goto undfl;
- for(j = 0; e1 > 1; j++, e1 >>= 1)
+ for (j = 0; e1 > 1; j++, e1 >>= 1)
if (e1 & 1)
rv *= tinytens[j];
/* The last multiplication could underflow. */
@@ -1438,26 +1434,24 @@ strtod
undfl:
rv = 0.;
errno = ERANGE;
- if (bd0)
- goto retfree;
goto ret;
}
word0(rv) = Tiny0;
word1(rv) = Tiny1;
/* The refinement below will clean
* this approximation up.
*/
- }
}
}
+ }
/* Now the hard part -- adjusting rv to the correct value.*/
/* Put digits into bd: true value = bd * 10^e */
bd0 = s2b(s0, nd0, nd, y);
- for(;;) {
+ for (;;) {
bd = Balloc(bd0->k);
Bcopy(bd, bd0);
bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
@@ -1466,11 +1460,10 @@ strtod
if (e >= 0) {
bb2 = bb5 = 0;
bd2 = bd5 = e;
- }
- else {
+ } else {
bb2 = bb5 = -e;
bd2 = bd5 = 0;
- }
+ }
if (bbe >= 0)
bb2 += bbe;
else
@@ -1527,7 +1520,7 @@ strtod
if (cmp(delta, bs) > 0)
goto drop_down;
break;
- }
+ }
if (i == 0) {
/* exactly half-way between */
if (dsign) {
@@ -1542,9 +1535,8 @@ strtod
;
word1(rv) = 0;
break;
- }
}
- else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
+ } else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
drop_down:
/* boundary case -- decrement exponent */
#ifdef Sudden_Underflow
@@ -1566,7 +1558,7 @@ strtod
#else
break;
#endif
- }
+ }
#ifndef ROUND_BIASED
if (!(word1(rv) & LSB))
break;
@@ -1580,10 +1572,10 @@ strtod
if (!rv)
goto undfl;
#endif
- }
+ }
#endif
break;
- }
+ }
if ((aadj = ratio(delta, bs)) <= 2.) {
if (dsign)
aadj = aadj1 = 1.;
@@ -1594,8 +1586,7 @@ strtod
#endif
aadj = 1.;
aadj1 = -1.;
- }
- else {
+ } else {
/* special case -- power of FLT_RADIX to be */
/* rounded down... */
@@ -1604,9 +1595,8 @@ strtod
else
aadj *= 0.5;
aadj1 = -aadj;
- }
}
- else {
+ } else {
aadj *= 0.5;
aadj1 = dsign ? aadj : -aadj;
#ifdef Check_FLT_ROUNDS
@@ -1617,12 +1607,12 @@ strtod
case 0: /* towards 0 */
case 3: /* towards -infinity */
aadj1 += 0.5;
- }
+ }
#else
if (FLT_ROUNDS == 0)
aadj1 += 0.5;
#endif
- }
+ }
y = word0(rv) & Exp_mask;
/* Check for overflow */
@@ -1639,11 +1629,9 @@ strtod
word0(rv) = Big0;
word1(rv) = Big1;
goto cont;
- }
- else
+ } else
word0(rv) += P*Exp_msk1;
- }
- else {
+ } else {
#ifdef Sudden_Underflow
if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
rv0 = rv;
@@ -1655,21 +1643,19 @@ strtod
#else
if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
#endif
- {
+ {
if (word0(rv0) == Tiny0
&& word1(rv0) == Tiny1)
goto undfl;
word0(rv) = Tiny0;
word1(rv) = Tiny1;
goto cont;
- }
- else
+ } else
word0(rv) -= P*Exp_msk1;
- }
- else {
+ } else {
adj = aadj1 * ulp(rv);
rv += adj;
- }
+ }
#else
/* Compute adj so that the IEEE rounding rules will
* correctly round rv + adj in some half-way cases.
@@ -1682,11 +1668,11 @@ strtod
aadj1 = (double)(int)(aadj + 0.5);
if (!dsign)
aadj1 = -aadj1;
- }
+ }
adj = aadj1 * ulp(rv);
rv += adj;
#endif
- }
+ }
z = word0(rv) & Exp_mask;
if (y == z) {
/* Can we stop now? */
@@ -1696,17 +1682,15 @@ strtod
if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
if (aadj < .4999999 || aadj > .5000001)
break;
- }
- else if (aadj < .4999999/FLT_RADIX)
+ } else if (aadj < .4999999/FLT_RADIX)
break;
- }
+ }
cont:
Bfree(bb);
Bfree(bd);
Bfree(bs);
Bfree(delta);
- }
- retfree:
+ }
Bfree(bb);
Bfree(bd);
Bfree(bs);
@@ -1716,7 +1700,7 @@ strtod
if (se)
*se = (char *)s;
return sign ? -rv : rv;
- }
+}
static int
quorem
@@ -1727,12 +1711,12 @@ quorem
#endif
{
int n;
- Long borrow, y;
- unsigned Long carry, q, ys;
- unsigned Long *bx, *bxe, *sx, *sxe;
+ long borrow, y;
+ unsigned long carry, q, ys;
+ unsigned long *bx, *bxe, *sx, *sxe;
#ifdef Pack_32
- Long z;
- unsigned Long si, zs;
+ long z;
+ unsigned long si, zs;
#endif
n = S->wds;
@@ -1775,15 +1759,14 @@ quorem
Sign_Extend(borrow, y);
*bx++ = y & 0xffff;
#endif
- }
- while(sx <= sxe);
+ } while (sx <= sxe);
if (!*bxe) {
bx = b->x;
- while(--bxe > bx && !*bxe)
+ while (--bxe > bx && !*bxe)
--n;
b->wds = n;
- }
}
+ }
if (cmp(b, S) >= 0) {
q++;
borrow = 0;
@@ -1811,18 +1794,17 @@ quorem
Sign_Extend(borrow, y);
*bx++ = y & 0xffff;
#endif
- }
- while(sx <= sxe);
+ } while (sx <= sxe);
bx = b->x;
bxe = bx + n;
if (!*bxe) {
- while(--bxe > bx && !*bxe)
+ while (--bxe > bx && !*bxe)
--n;
b->wds = n;
- }
}
- return q;
}
+ return q;
+}
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
*
@@ -1854,12 +1836,12 @@ quorem
* guarantee that the floating-point calculation has given
* the correctly rounded result. For k requested digits and
* "uniformly" distributed input, the probability is
- * something like 10^(k-15) that we must resort to the Long
+ * something like 10^(k-15) that we must resort to the long
* calculation.
*/
- char *
-dtoa
+char *
+__dtoa
#ifdef KR_headers
(d, mode, ndigits, decpt, sign, rve)
double d; int mode, ndigits, *decpt, *sign; char **rve;
@@ -1904,10 +1886,10 @@ dtoa
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
spec_case, try_quick;
- Long L;
+ long L;
#ifndef Sudden_Underflow
int denorm;
- unsigned Long x;
+ unsigned long x;
#endif
Bigint *b, *b1, *delta, *mlo, *mhi, *S;
double d2, ds, eps;
@@ -1920,13 +1902,13 @@ dtoa
result->maxwds = 1 << result_k;
Bfree(result);
result = 0;
- }
+ }
if (word0(d) & Sign_bit) {
/* set sign for everything, including 0's and NaNs */
*sign = 1;
word0(d) &= ~Sign_bit; /* clear sign bit */
- }
+ }
else
*sign = 0;
@@ -1936,7 +1918,7 @@ dtoa
#else
if (word0(d) == 0x8000)
#endif
- {
+ {
/* Infinity or NaN */
*decpt = 9999;
s =
@@ -1951,7 +1933,7 @@ dtoa
#endif
s + 3;
return s;
- }
+ }
#endif
#ifdef IBM
d += 0; /* normalize */
@@ -1962,7 +1944,7 @@ dtoa
if (rve)
*rve = s + 1;
return s;
- }
+ }
b = d2b(d, &be, &bbits);
#ifdef Sudden_Underflow
@@ -2007,8 +1989,7 @@ dtoa
#endif
#ifndef Sudden_Underflow
denorm = 0;
- }
- else {
+ } else {
/* d is denormalized */
i = bbits + be + (Bias + (P-1) - 1);
@@ -2018,7 +1999,7 @@ dtoa
word0(d2) -= 31*Exp_msk1; /* adjust exponent */
i -= (Bias + (P-1) - 1) + 1;
denorm = 1;
- }
+ }
#endif
ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
k = (int)ds;
@@ -2029,33 +2010,31 @@ dtoa
if (d < tens[k])
k--;
k_check = 0;
- }
+ }
j = bbits - i - 1;
if (j >= 0) {
b2 = 0;
s2 = j;
- }
- else {
+ } else {
b2 = -j;
s2 = 0;
- }
+ }
if (k >= 0) {
b5 = 0;
s5 = k;
s2 += k;
- }
- else {
+ } else {
b2 -= k;
b5 = -k;
s5 = 0;
- }
+ }
if (mode < 0 || mode > 9)
mode = 0;
try_quick = 1;
if (mode > 5) {
mode -= 4;
try_quick = 0;
- }
+ }
leftright = 1;
switch(mode) {
case 0:
@@ -2081,9 +2060,9 @@ dtoa
ilim1 = i - 1;
if (i <= 0)
i = 1;
- }
- j = sizeof(unsigned Long);
- for(result_k = 0; sizeof(Bigint) - sizeof(unsigned Long) + j <= i;
+ }
+ j = sizeof(unsigned long);
+ for (result_k = 0; sizeof(Bigint) - sizeof(unsigned long) + j < i;
j <<= 1) result_k++;
result = Balloc(result_k);
s = s0 = (char *)result;
@@ -2105,30 +2084,29 @@ dtoa
j &= Bletch - 1;
d /= bigtens[n_bigtens-1];
ieps++;
- }
- for(; j; j >>= 1, i++)
+ }
+ for (; j; j >>= 1, i++)
if (j & 1) {
ieps++;
ds *= bigtens[i];
- }
+ }
d /= ds;
- }
- else if (j1 = -k) {
+ } else if (j1 = -k) {
d *= tens[j1 & 0xf];
- for(j = j1 >> 4; j; j >>= 1, i++)
+ for (j = j1 >> 4; j; j >>= 1, i++)
if (j & 1) {
ieps++;
d *= bigtens[i];
- }
- }
+ }
+ }
if (k_check && d < 1. && ilim > 0) {
if (ilim1 <= 0)
goto fast_failed;
ilim = ilim1;
k--;
d *= 10.;
ieps++;
- }
+ }
eps = ieps*d + 7.;
word0(eps) -= (P-1)*Exp_msk1;
if (ilim == 0) {
@@ -2139,14 +2117,14 @@ dtoa
if (d < -eps)
goto no_digits;
goto fast_failed;
- }
+ }
#ifndef No_leftright
if (leftright) {
/* Use Steele & White method of only
* generating digits needed.
*/
eps = 0.5/tens[ilim-1] - eps;
- for(i = 0;;) {
+ for (i = 0;;) {
L = d;
d -= L;
*s++ = '0' + (int)L;
@@ -2158,36 +2136,35 @@ dtoa
break;
eps *= 10.;
d *= 10.;
- }
}
- else {
+ } else {
#endif
/* Generate ilim digits, then fix them up. */
eps *= tens[ilim-1];
- for(i = 1;; i++, d *= 10.) {
+ for (i = 1;; i++, d *= 10.) {
L = d;
d -= L;
*s++ = '0' + (int)L;
if (i == ilim) {
if (d > 0.5 + eps)
goto bump_up;
else if (d < 0.5 - eps) {
- while(*--s == '0');
+ while (*--s == '0');
s++;
goto ret1;
- }
- break;
}
+ break;
}
-#ifndef No_leftright
}
+#ifndef No_leftright
+ }
#endif
fast_failed:
s = s0;
d = d2;
k = k0;
ilim = ilim0;
- }
+ }
/* Do we have a "small" integer? */
@@ -2199,37 +2176,37 @@ dtoa
if (ilim < 0 || d <= 5*ds)
goto no_digits;
goto one_digit;
- }
- for(i = 1;; i++) {
+ }
+ for (i = 1;; i++) {
L = d / ds;
d -= L*ds;
#ifdef Check_FLT_ROUNDS
/* If FLT_ROUNDS == 2, L will usually be high by 1 */
if (d < 0) {
L--;
d += ds;
- }
+ }
#endif
*s++ = '0' + (int)L;
if (i == ilim) {
d += d;
if (d > ds || d == ds && L & 1) {
bump_up:
- while(*--s == '9')
+ while (*--s == '9')
if (s == s0) {
k++;
*s = '0';
break;
- }
+ }
++*s++;
- }
- break;
}
- if (!(d *= 10.))
break;
}
- goto ret1;
+ if (!(d *= 10.))
+ break;
}
+ goto ret1;
+ }
m2 = b2;
m5 = b5;
@@ -2245,31 +2222,30 @@ dtoa
#else
1 + P - bbits;
#endif
- }
- else {
+ } else {
j = ilim - 1;
if (m5 >= j)
m5 -= j;
else {
s5 += j -= m5;
b5 += j;
m5 = 0;
- }
+ }
if ((i = ilim) < 0) {
m2 -= i;
i = 0;
- }
}
+ }
b2 += i;
s2 += i;
mhi = i2b(1);
- }
+ }
if (m2 > 0 && s2 > 0) {
i = m2 < s2 ? m2 : s2;
b2 -= i;
m2 -= i;
s2 -= i;
- }
+ }
if (b5 > 0) {
if (leftright) {
if (m5 > 0) {
@@ -2280,10 +2256,9 @@ dtoa
}
if (j = b5 - m5)
b = pow5mult(b, j);
- }
- else
+ } else
b = pow5mult(b, b5);
- }
+ }
S = i2b(1);
if (s5 > 0)
S = pow5mult(S, s5);
@@ -2300,10 +2275,9 @@ dtoa
b2 += Log2P;
s2 += Log2P;
spec_case = 1;
- }
- else
+ } else
spec_case = 0;
- }
+ }
/* Arrange for convenient computation of quotients:
* shift left if necessary so divisor has 4 leading 0 bits.
@@ -2324,13 +2298,12 @@ dtoa
b2 += i;
m2 += i;
s2 += i;
- }
- else if (i < 4) {
+ } else if (i < 4) {
i += 28;
b2 += i;
m2 += i;
s2 += i;
- }
+ }
if (b2 > 0)
b = lshift(b, b2);
if (s2 > 0)
@@ -2342,20 +2315,20 @@ dtoa
if (leftright)
mhi = multadd(mhi, 10, 0);
ilim = ilim1;
- }
}
+ }
if (ilim <= 0 && mode > 2) {
if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
/* no digits, fcvt style */
no_digits:
k = -1 - ndigits;
goto ret;
- }
+ }
one_digit:
*s++ = '1';
k++;
goto ret;
- }
+ }
if (leftright) {
if (m2 > 0)
mhi = lshift(mhi, m2);
@@ -2369,9 +2342,9 @@ dtoa
mhi = Balloc(mhi->k);
Bcopy(mhi, mlo);
mhi = lshift(mhi, Log2P);
- }
+ }
- for(i = 1;;i++) {
+ for (i = 1;;i++) {
dig = quorem(b,S) + '0';
/* Do we yet have the shortest decimal string
* that will round to d?
@@ -2388,7 +2361,7 @@ dtoa
dig++;
*s++ = dig;
goto ret;
- }
+ }
#endif
if (j < 0 || j == 0 && !mode
#ifndef ROUND_BIASED
@@ -2401,19 +2374,19 @@ dtoa
if ((j1 > 0 || j1 == 0 && dig & 1)
&& dig++ == '9')
goto round_9_up;
- }
+ }
*s++ = dig;
goto ret;
- }
+ }
if (j1 > 0) {
if (dig == '9') { /* possible if i == 1 */
round_9_up:
*s++ = '9';
goto roundoff;
- }
+ }
*s++ = dig + 1;
goto ret;
- }
+ }
*s++ = dig;
if (i == ilim)
break;
@@ -2423,44 +2396,46 @@ dtoa
else {
mlo = multadd(mlo, 10, 0);
mhi = multadd(mhi, 10, 0);
- }
}
}
- else
- for(i = 1;; i++) {
+ } else
+ for (i = 1;; i++) {
*s++ = dig = quorem(b,S) + '0';
if (i >= ilim)
break;
b = multadd(b, 10, 0);
- }
+ }
/* Round off last digit */
b = lshift(b, 1);
j = cmp(b, S);
if (j > 0 || j == 0 && dig & 1) {
roundoff:
- while(*--s == '9')
+ while (*--s == '9')
if (s == s0) {
k++;
*s++ = '1';
goto ret;
- }
+ }
++*s++;
- }
- else {
- while(*--s == '0');
+ } else {
+ while (*--s == '0');
s++;
- }
+ }
ret:
Bfree(S);
if (mhi) {
if (mlo && mlo != mhi)
Bfree(mlo);
Bfree(mhi);
- }
+ }
ret1:
Bfree(b);
+ if (s == s0) { /* don't return empty string */
+ *s++ = '0';
+ k = 0;
+ }
*s = 0;
*decpt = k + 1;
if (rve)

0 comments on commit 2794870

Please sign in to comment.
Something went wrong with that request. Please try again.