12 #if defined __MINGW32__ || defined __MINGW64__ 13 #define MINGW_HAS_SECURE_API 1 31 #define hexdigit ruby_hexdigits 36 register const char *s = start;
37 register unsigned long retval = 0;
39 while (len-- && *s >=
'0' && *s <=
'7') {
43 *retlen = (int)(s - start);
50 register const char *s = start;
51 register unsigned long retval = 0;
59 *retlen = (int)(s - start);
65 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
66 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
67 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
68 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
69 -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
70 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
71 -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
72 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
73 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
74 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
75 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
76 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
77 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
79 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
87 const char *start = str;
88 unsigned long ret = 0, x;
89 unsigned long mul_overflow = (~(
unsigned long)0) / base;
100 if (d == -1 || base <= d) {
104 if (mul_overflow < ret)
111 }
while (len < 0 || --len);
112 *retlen = str - start;
123 const char *subject_found = str;
125 if (base == 1 || 36 < base) {
130 while ((c = *str) &&
ISSPACE(c))
143 subject_found = str+1;
144 if (base == 0 || base == 16) {
145 if (str[1] ==
'x' || str[1] ==
'X') {
150 b = base == 0 ? 8 : 16;
160 b = base == 0 ? 10 : base;
166 subject_found = str+
len;
169 *endptr = (
char*)subject_found;
177 ret = (
unsigned long)(-(
long)ret);
185 #include <sys/types.h> 186 #include <sys/stat.h> 190 #if defined(HAVE_FCNTL_H) 195 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 198 #if !defined HAVE_BSD_QSORT_R && defined HAVE_QSORT_S 199 # define qsort_r(base, nel, size, arg, cmp) qsort_s(base, nel, size, cmp, arg) 200 # define cmp_bsd_qsort cmp_ms_qsort 201 # define HAVE_BSD_QSORT_R 1 203 #if defined HAVE_BSD_QSORT_R 204 typedef int (
cmpfunc_t)(
const void*,
const void*,
void*);
206 struct bsd_qsort_r_args {
212 cmp_bsd_qsort(
void *d,
const void *a,
const void *b)
214 const struct bsd_qsort_r_args *args = d;
215 return (*args->cmp)(a, b, args->arg);
221 struct bsd_qsort_r_args args;
224 qsort_r(base, nel,
size, &args, cmp_bsd_qsort);
226 #elif !defined HAVE_GNU_QSORT_R 230 #define mmcount (16 / SIZEOF_LONG) 231 #define A ((mmtype*)a) 232 #define B ((mmtype*)b) 233 #define C ((mmtype*)c) 234 #define D ((mmtype*)d) 236 #define mmstep (sizeof(mmtype) * mmcount) 237 #define mmprepare(base, size) do {\ 238 if (((VALUE)(base) % sizeof(mmtype)) == 0 && ((size) % sizeof(mmtype)) == 0) \ 239 if ((size) >= mmstep) mmkind = 1;\ 242 high = ((size) / mmstep) * mmstep;\ 243 low = ((size) % mmstep);\ 246 #define mmarg mmkind, size, high, low 247 #define mmargdecl int mmkind, size_t size, size_t high, size_t low 249 static void mmswap_(
register char *a,
register char *b,
mmargdecl)
256 register char *t = a + high;
258 s =
A[0];
A[0] =
B[0];
B[0] = s;
259 s =
A[1];
A[1] =
B[1];
B[1] = s;
261 s =
A[2];
A[2] =
B[2];
B[2] = s;
263 s =
A[3];
A[3] =
B[3];
B[3] = s;
270 if (low != 0) { s =
A[0];
A[0] =
B[0];
B[0] = s;
272 if (low >= 2 *
sizeof(
mmtype)) { s =
A[1];
A[1] =
B[1];
B[1] = s;
274 if (low >= 3 *
sizeof(
mmtype)) {s =
A[2];
A[2] =
B[2];
B[2] = s;}
281 register char *t = a +
size, s;
282 do {s = *a; *a++ = *b; *b++ = s;}
while (a < t);
285 #define mmswap(a,b) mmswap_((a),(b),mmarg) 288 static void mmrot3_(
register char *a,
register char *b,
register char *c,
mmargdecl)
294 register char *t = a + high;
296 s =
A[0];
A[0] =
B[0];
B[0] =
C[0];
C[0] = s;
297 s =
A[1];
A[1] =
B[1];
B[1] =
C[1];
C[1] = s;
299 s =
A[2];
A[2] =
B[2];
B[2] =
C[2];
C[2] = s;
301 s =
A[3];
A[3] =
B[3];
B[3] =
C[3];
C[3] = s;
308 if (low != 0) { s =
A[0];
A[0] =
B[0];
B[0] =
C[0];
C[0] = s;
310 if (low >= 2 *
sizeof(
mmtype)) { s =
A[1];
A[1] =
B[1];
B[1] =
C[1];
C[1] = s;
312 if (low == 3 *
sizeof(
mmtype)) {s =
A[2];
A[2] =
B[2];
B[2] =
C[2];
C[2] = s;}
319 register char *t = a +
size, s;
320 do {s = *a; *a++ = *b; *b++ = *c; *c++ = s;}
while (a < t);
323 #define mmrot3(a,b,c) mmrot3_((a),(b),(c),mmarg) 335 #define PUSH(ll,rr) do { top->LL = (ll); top->RR = (rr); ++top; } while (0) 336 #define POP(ll,rr) do { --top; (ll) = top->LL; (rr) = top->RR; } while (0) 338 #define med3(a,b,c) ((*cmp)((a),(b),d)<0 ? \ 339 ((*cmp)((b),(c),d)<0 ? (b) : ((*cmp)((a),(c),d)<0 ? (c) : (a))) : \ 340 ((*cmp)((b),(c),d)>0 ? (b) : ((*cmp)((a),(c),d)<0 ? (a) : (c)))) 342 typedef int (
cmpfunc_t)(
const void*,
const void*,
void*);
346 register char *l, *r, *m;
347 register int t, eq_l, eq_r;
349 char *
R = (
char*)base + size*(nel-1);
357 if (nel <= 1)
return;
362 if (stack == top)
return;
368 if ((*cmp)(L,R,d) > 0)
mmswap(L,R);
goto nxt;
372 n = (r - l +
size) / size;
373 m = l + size * (n >> 1);
381 register char *p1 = l + n;
382 register char *p2 = p1 + n;
383 register char *p3 = p2 + n;
384 m1 =
med3(p1, p2, p3);
388 m3 =
med3(p1, p2, p3);
399 if ((t = (*cmp)(l,m,d)) < 0) {
400 if ((t = (*cmp)(m,r,d)) < 0) {
401 if (chklim && nel >= chklim) {
404 for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) > 0)
goto fail;
410 if ((*cmp)(l,r,d) <= 0) {
mmswap(m,r);
goto loopA;}
411 mmrot3(r,m,l);
goto loopA;
417 if ((t = (*cmp)(m,r,d)) > 0) {
418 if (chklim && nel >= chklim) {
421 for (p=l; p<r; p+=
size)
if ((*cmp)(p,p+size,d) < 0)
goto fail2;
425 fail2:
mmswap(l,r);
goto loopA;
428 if ((*cmp)(l,r,d) <= 0) {
mmswap(l,m);
goto loopB;}
429 mmrot3(l,m,r);
goto loopA;
434 if ((t = (*cmp)(m,r,d)) < 0) {
goto loopA;}
435 if (t > 0) {
mmswap(l,r);
goto loopB;}
439 if ((l += size) == r)
goto nxt;
440 if (l == m)
continue;
441 if ((t = (*cmp)(l,m,d)) > 0) {
mmswap(l,r); l = L;
goto loopA;}
442 if (t < 0) {
mmswap(L,l); l = L;
goto loopB;}
445 loopA: eq_l = 1; eq_r = 1;
448 if ((l += size) == r)
450 if (l == m)
continue;
451 if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0;
break;}
455 if (l == (r -= size))
457 if (r == m) {m = l;
break;}
458 if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0;
break;}
464 loopB: eq_l = 1; eq_r = 1;
467 if (l == (r -= size))
469 if (r == m)
continue;
470 if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0;
break;}
474 if ((l += size) == r)
476 if (l == m) {m = r;
break;}
477 if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0;
break;}
486 if (l-L < R-r) {
PUSH(r,R); R = l;}
487 else {
PUSH(L,l); L = r;}
489 else if (eq_r == 0) L = r;
502 memcpy(tmp, str, len);
510 #if defined __native_client__ 513 #elif defined HAVE_GETCWD 514 # undef RUBY_UNTYPED_DATA_WARNING 515 # define RUBY_UNTYPED_DATA_WARNING 0 516 # if defined NO_GETCWD_MALLOC 521 while (!getcwd(buf, size)) {
534 char *
buf, *cwd = getcwd(
NULL, 0);
543 # define PATH_MAX 8192 721 #ifdef WORDS_BIGENDIAN 722 #define IEEE_BIG_ENDIAN 724 #define IEEE_LITTLE_ENDIAN 729 #undef IEEE_BIG_ENDIAN 730 #undef IEEE_LITTLE_ENDIAN 733 #if defined(__arm__) && !defined(__VFP_FP__) 734 #define IEEE_BIG_ENDIAN 735 #undef IEEE_LITTLE_ENDIAN 743 #define ULong unsigned int 744 #elif SIZEOF_LONG == 4 745 #define Long long int 746 #define ULong unsigned long int 750 #define Llong LONG_LONG 755 #define Bug(x) {fprintf(stderr, "%s\n", (x)); exit(EXIT_FAILURE);} 766 extern void *
MALLOC(
size_t);
768 #define MALLOC xmalloc 771 extern void FREE(
void*);
776 #ifndef Omit_Private_Memory 778 #define PRIVATE_MEM 2304 780 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) 781 static double private_mem[
PRIVATE_mem], *pmem_next = private_mem;
785 #undef Avoid_Underflow 786 #ifdef IEEE_BIG_ENDIAN 789 #ifdef IEEE_LITTLE_ENDIAN 797 #define DBL_MAX_10_EXP 308 798 #define DBL_MAX_EXP 1024 804 #define DBL_MAX_10_EXP 75 805 #define DBL_MAX_EXP 63 807 #define DBL_MAX 7.2370055773322621e+75 812 #define DBL_MAX_10_EXP 38 813 #define DBL_MAX_EXP 127 815 #define DBL_MAX 1.7014118346046923e+38 819 #define LONG_MAX 2147483647 837 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1 841 typedef union {
double d; ULong L[2]; }
U;
846 # ifdef IEEE_LITTLE_ENDIAN 847 # define word0(x) (((ULong *)&(x))[1]) 848 # define word1(x) (((ULong *)&(x))[0]) 850 # define word0(x) (((ULong *)&(x))[0]) 851 # define word1(x) (((ULong *)&(x))[1]) 855 # ifdef IEEE_LITTLE_ENDIAN 856 # define word0(x) ((x).L[1]) 857 # define word1(x) ((x).L[0]) 859 # define word0(x) ((x).L[0]) 860 # define word1(x) ((x).L[1]) 862 # define dval(x) ((x).d) 869 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__) 870 #define Storeinc(a,b,c) (((unsigned short *)(a))[1] = (unsigned short)(b), \ 871 ((unsigned short *)(a))[0] = (unsigned short)(c), (a)++) 873 #define Storeinc(a,b,c) (((unsigned short *)(a))[0] = (unsigned short)(b), \ 874 ((unsigned short *)(a))[1] = (unsigned short)(c), (a)++) 885 #define Exp_shift1 20 886 #define Exp_msk1 0x100000 887 #define Exp_msk11 0x100000 888 #define Exp_mask 0x7ff00000 892 #define Exp_1 0x3ff00000 893 #define Exp_11 0x3ff00000 895 #define Frac_mask 0xfffff 896 #define Frac_mask1 0xfffff 899 #define Bndry_mask 0xfffff 900 #define Bndry_mask1 0xfffff 902 #define Sign_bit 0x80000000 908 #ifndef NO_IEEE_Scale 909 #define Avoid_Underflow 911 #undef Sudden_Underflow 917 #define Flt_Rounds FLT_ROUNDS 923 #ifdef Honor_FLT_ROUNDS 924 #define Rounding rounding 925 #undef Check_FLT_ROUNDS 926 #define Check_FLT_ROUNDS 928 #define Rounding Flt_Rounds 932 #undef Check_FLT_ROUNDS 933 #undef Honor_FLT_ROUNDS 935 #undef Sudden_Underflow 936 #define Sudden_Underflow 941 #define Exp_shift1 24 942 #define Exp_msk1 0x1000000 943 #define Exp_msk11 0x1000000 944 #define Exp_mask 0x7f000000 947 #define Exp_1 0x41000000 948 #define Exp_11 0x41000000 950 #define Frac_mask 0xffffff 951 #define Frac_mask1 0xffffff 954 #define Bndry_mask 0xefffff 955 #define Bndry_mask1 0xffffff 957 #define Sign_bit 0x80000000 959 #define Tiny0 0x100000 968 #define Exp_msk1 0x80 969 #define Exp_msk11 0x800000 970 #define Exp_mask 0x7f80 973 #define Exp_1 0x40800000 974 #define Exp_11 0x4080 976 #define Frac_mask 0x7fffff 977 #define Frac_mask1 0xffff007f 980 #define Bndry_mask 0xffff007f 981 #define Bndry_mask1 0xffff007f 983 #define Sign_bit 0x8000 997 #define rounded_product(a,b) ((a) = rnd_prod((a), (b))) 998 #define rounded_quotient(a,b) ((a) = rnd_quot((a), (b))) 999 extern double rnd_prod(
double,
double), rnd_quot(
double,
double);
1001 #define rounded_product(a,b) ((a) *= (b)) 1002 #define rounded_quotient(a,b) ((a) /= (b)) 1005 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) 1006 #define Big1 0xffffffff 1012 #define FFFFFFFF 0xffffffffUL 1026 #define Llong long long 1029 #define ULLong unsigned Llong 1033 #define MULTIPLE_THREADS 1 1035 #ifndef MULTIPLE_THREADS 1036 #define ACQUIRE_DTOA_LOCK(n) 1037 #define FREE_DTOA_LOCK(n) 1039 #define ACQUIRE_DTOA_LOCK(n) 1040 #define FREE_DTOA_LOCK(n) 1060 #ifndef Omit_Private_Memory 1065 if (k <=
Kmax && (rv = freelist[k]) != 0) {
1066 freelist[
k] = rv->
next;
1070 #ifdef Omit_Private_Memory 1073 len = (
sizeof(
Bigint) + (x-1)*
sizeof(ULong) +
sizeof(
double) - 1)
1099 v->
next = freelist[v->
k];
1105 #define Bcopy(x,y) memcpy((char *)&(x)->sign, (char *)&(y)->sign, \ 1106 (y)->wds*sizeof(Long) + 2*sizeof(int)) 1109 multadd(
Bigint *b,
int m,
int a)
1129 y = *x * (
ULLong)m + carry;
1135 y = (xi & 0xffff) * m + carry;
1136 z = (xi >> 16) * m + (y >> 16);
1138 *x++ = (z << 16) + (y & 0xffff);
1145 }
while (++i < wds);
1148 b1 = Balloc(b->
k+1);
1153 b->
x[wds++] = (ULong)carry;
1160 s2b(
const char *s,
int nd0,
int nd, ULong y9)
1167 for (k = 0, y = 1; x > y; y <<= 1, k++) ;
1174 b->
x[0] = y9 & 0xffff;
1175 b->
wds = (b->
x[1] = y9 >> 16) ? 2 : 1;
1182 b = multadd(b, 10, *s++ -
'0');
1183 }
while (++i < nd0);
1189 b = multadd(b, 10, *s++ -
'0');
1194 hi0bits(
register ULong x)
1198 if (!(x & 0xffff0000)) {
1202 if (!(x & 0xff000000)) {
1206 if (!(x & 0xf0000000)) {
1210 if (!(x & 0xc0000000)) {
1214 if (!(x & 0x80000000)) {
1216 if (!(x & 0x40000000))
1226 register ULong x = *y;
1239 if (!(x & 0xffff)) {
1281 ULong *
x, *xa, *xae, *xb, *xbe, *xc, *xc0;
1304 for (x = c->
x, xa = x + wc; x < xa; x++)
1312 for (; xb < xbe; xc0++) {
1313 if ((y = *xb++) != 0) {
1318 z = *x++ * (
ULLong)y + *xc + carry;
1327 for (; xb < xbe; xb++, xc0++) {
1328 if (y = *xb & 0xffff) {
1333 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
1335 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
1341 if (y = *xb >> 16) {
1347 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
1350 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
1357 for (; xb < xbe; xc0++) {
1363 z = *x++ * y + *xc + carry;
1372 for (xc0 = c->
x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
1380 pow5mult(
Bigint *b,
int k)
1384 static int p05[3] = { 5, 25, 125 };
1386 if ((i = k & 3) != 0)
1387 b = multadd(b, p05[i-1], 0);
1393 #ifdef MULTIPLE_THREADS 1396 p5 = p5s = i2b(625);
1401 p5 = p5s = i2b(625);
1413 if (!(p51 = p5->
next)) {
1414 #ifdef MULTIPLE_THREADS 1416 if (!(p51 = p5->
next)) {
1417 p51 = p5->
next = mult(p5,p5);
1422 p51 = p5->
next = mult(p5,p5);
1436 ULong *
x, *x1, *xe, z;
1444 n1 = n + b->
wds + 1;
1445 for (i = b->
maxwds; n1 > i; i <<= 1)
1449 for (i = 0; i < n; i++)
1458 *x1++ = *x << k | z;
1469 *x1++ = *x << k & 0xffff | z;
1488 ULong *xa, *xa0, *xb, *xb0;
1494 if (i > 1 && !a->
x[i-1])
1495 Bug(
"cmp called with a->x[a->wds-1] == 0");
1496 if (j > 1 && !b->
x[j-1])
1497 Bug(
"cmp called with b->x[b->wds-1] == 0");
1507 return *xa < *xb ? -1 : 1;
1519 ULong *xa, *xae, *xb, *xbe, *xc;
1556 y = (
ULLong)*xa++ - *xb++ - borrow;
1557 borrow = y >> 32 & (ULong)1;
1562 borrow = y >> 32 & (ULong)1;
1568 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1569 borrow = (y & 0x10000) >> 16;
1570 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1571 borrow = (z & 0x10000) >> 16;
1575 y = (*xa & 0xffff) - borrow;
1576 borrow = (y & 0x10000) >> 16;
1577 z = (*xa++ >> 16) - borrow;
1578 borrow = (z & 0x10000) >> 16;
1583 y = *xa++ - *xb++ - borrow;
1584 borrow = (y & 0x10000) >> 16;
1589 borrow = (y & 0x10000) >> 16;
1608 #ifndef Avoid_Underflow 1609 #ifndef Sudden_Underflow 1618 #ifndef Avoid_Underflow 1619 #ifndef Sudden_Underflow 1623 if (L < Exp_shift) {
1624 word0(a) = 0x80000 >> L;
1630 word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1641 ULong *xa, *xa0, w, y, z;
1655 if (!y) Bug(
"zero y in b2d");
1662 w = xa > xa0 ? *--xa : 0;
1666 z = xa > xa0 ? *--xa : 0;
1668 d0 =
Exp_1 | y << k | z >> (32 -
k);
1669 y = xa > xa0 ? *--xa : 0;
1670 d1 = z << k | y >> (32 -
k);
1677 if (k <
Ebits + 16) {
1678 z = xa > xa0 ? *--xa : 0;
1680 w = xa > xa0 ? *--xa : 0;
1681 y = xa > xa0 ? *--xa : 0;
1685 z = xa > xa0 ? *--xa : 0;
1686 w = xa > xa0 ? *--xa : 0;
1688 d0 =
Exp_1 | y << k + 16 | z << k | w >> 16 -
k;
1689 y = xa > xa0 ? *--xa : 0;
1690 d1 = w << k + 16 | y <<
k;
1694 word0(d) = d0 >> 16 | d0 << 16;
1695 word1(d) = d1 >> 16 | d1 << 16;
1704 d2b(
double d_,
int *e,
int *bits)
1710 #ifndef Sudden_Underflow 1734 #ifdef Sudden_Underflow 1744 if ((y = d1) != 0) {
1745 if ((k = lo0bits(&y)) != 0) {
1746 x[0] = y | z << (32 -
k);
1751 #ifndef Sudden_Underflow 1754 b->
wds = (x[1] = z) ? 2 : 1;
1759 Bug(
"Zero passed to d2b");
1763 #ifndef Sudden_Underflow 1771 if (k = lo0bits(&y))
1773 x[0] = y | z << 32 - k & 0xffff;
1774 x[1] = z >> k - 16 & 0xffff;
1780 x[1] = y >> 16 | z << 16 - k & 0xffff;
1781 x[2] = z >> k & 0xffff;
1796 Bug(
"Zero passed to d2b");
1814 #ifndef Sudden_Underflow 1818 *e = (de -
Bias - (
P-1) << 2) +
k;
1819 *bits = 4*
P + 8 - k - hi0bits(
word0(d) & Frac_mask);
1821 *e = de -
Bias - (
P-1) + k;
1824 #ifndef Sudden_Underflow 1827 *e = de -
Bias - (
P-1) + 1 + k;
1829 *bits = 32*i - hi0bits(x[i-1]);
1831 *bits = (i+2)*16 - hi0bits(x[i]);
1846 dval(da) = b2d(a, &ka);
1847 dval(db) = b2d(b, &kb);
1849 k = ka - kb + 32*(a->
wds - b->
wds);
1851 k = ka - kb + 16*(a->
wds - b->
wds);
1878 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1879 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1888 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1889 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1890 #ifdef Avoid_Underflow 1891 9007199254740992.*9007199254740992.e-256
1899 #define Scale_Bit 0x10 1903 bigtens[] = { 1e16, 1e32, 1e64 };
1904 static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1907 bigtens[] = { 1e16, 1e32 };
1908 static const double tinytens[] = { 1e-16, 1e-32 };
1920 #define NAN_WORD0 0x7ff80000 1928 match(
const char **sp,
char *t)
1931 const char *s = *sp;
1934 if ((c = *++s) >=
'A' && c <=
'Z')
1945 hexnan(
double *rvp,
const char **sp)
1949 int havedig, udx0, xshift;
1952 havedig = xshift = 0;
1955 while (c = *(
const unsigned char*)++s) {
1956 if (c >=
'0' && c <=
'9')
1958 else if (c >=
'a' && c <=
'f')
1960 else if (c >=
'A' && c <=
'F')
1962 else if (c <=
' ') {
1963 if (udx0 && havedig) {
1969 else if ( c ==
')' && havedig) {
1982 x[0] = (x[0] << 4) | (x[1] >> 28);
1983 x[1] = (x[1] << 4) | c;
1985 if ((x[0] &= 0xfffff) || x[1]) {
1996 #ifdef Avoid_Underflow 1999 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
2000 e, e1, esign, i, j,
k, nd, nd0, nf, nz, nz0,
sign;
2001 const char *s, *s0, *s1;
2006 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
2008 int inexact, oldinexact;
2010 #ifdef Honor_FLT_ROUNDS 2018 sign = nz0 = nz = 0;
2043 if (s[1] ==
'x' || s[1] ==
'X') {
2051 while (*++s ==
'0');
2056 adj += aadj * ((s1 -
hexdigit) & 15);
2072 adj += aadj * ((s1 -
hexdigit) & 15);
2073 if ((aadj /= 16) == 0.0) {
2083 if (*s ==
'P' || *s ==
'p') {
2084 dsign = 0x2C - *++s;
2085 if (abs(dsign) == 1) s++;
2090 if (c <
'0' ||
'9' < c)
goto ret0;
2097 if (nd + dsign * nd0 > 2095) {
2098 while (
'0' <= c && c <=
'9') c = *++s;
2101 }
while (
'0' <= c && c <=
'9');
2105 if (dsign)
goto ret0;
2107 dval(rv) = ldexp(adj, nd0);
2111 while (*++s ==
'0') ;
2117 for (nd = nf = 0; (c = *s) >=
'0' && c <=
'9'; nd++, s++)
2124 s1 = localeconv()->decimal_point;
2147 for (; c ==
'0'; c = *++s)
2149 if (c >
'0' && c <=
'9') {
2157 for (; c >=
'0' && c <=
'9'; c = *++s) {
2165 for (i = 1; i < nz; i++)
2180 if (c ==
'e' || c ==
'E') {
2181 if (!nd && !nz && !nz0) {
2192 if (c >=
'0' && c <=
'9') {
2195 if (c >
'0' && c <=
'9') {
2198 while ((c = *++s) >=
'0' && c <=
'9')
2200 if (s - s1 > 8 || L > 19999)
2223 if (match(&s,
"nf")) {
2225 if (!match(&s,
"inity"))
2227 word0(rv) = 0x7ff00000;
2234 if (match(&s,
"an")) {
2235 word0(rv) = NAN_WORD0;
2236 word1(rv) = NAN_WORD1;
2265 oldinexact = get_inexact();
2267 dval(rv) = tens[k - 9] *
dval(rv) + z;
2269 bd0 = bb = bd = bs = delta = 0;
2271 #ifndef RND_PRODQUOT
2272 #ifndef Honor_FLT_ROUNDS
2282 goto vax_ovfl_check;
2284 #ifdef Honor_FLT_ROUNDS 2300 #ifdef Honor_FLT_ROUNDS 2308 dval(rv) *= tens[i];
2326 #ifndef Inaccurate_Divide 2328 #ifdef Honor_FLT_ROUNDS 2346 oldinexact = get_inexact();
2348 #ifdef Avoid_Underflow 2351 #ifdef Honor_FLT_ROUNDS 2354 rounding = rounding == 2 ? 0 : 2;
2365 if ((i = e1 & 15) != 0)
2366 dval(rv) *= tens[i];
2375 #ifdef Honor_FLT_ROUNDS 2404 for (j = 0; e1 > 1; j++, e1 >>= 1)
2406 dval(rv) *= bigtens[j];
2409 dval(rv) *= bigtens[j];
2425 if ((i = e1 & 15) != 0)
2426 dval(rv) /= tens[i];
2430 #ifdef Avoid_Underflow 2433 for (j = 0; e1 > 0; j++, e1 >>= 1)
2435 dval(rv) *= tinytens[j];
2444 word0(rv) &= 0xffffffff << (j-32);
2447 word1(rv) &= 0xffffffff << j;
2450 for (j = 0; e1 > 1; j++, e1 >>= 1)
2452 dval(rv) *= tinytens[j];
2455 dval(rv) *= tinytens[j];
2458 dval(rv) *= tinytens[j];
2470 #ifndef Avoid_Underflow 2485 bd0 = s2b(s0, nd0, nd, y);
2488 bd = Balloc(bd0->
k);
2490 bb = d2b(
dval(rv), &bbe, &bbbits);
2506 #ifdef Honor_FLT_ROUNDS 2510 #ifdef Avoid_Underflow 2518 #ifdef Sudden_Underflow 2520 j = 1 + 4*
P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2535 #ifdef Avoid_Underflow 2538 i = bb2 < bd2 ? bb2 : bd2;
2547 bs = pow5mult(bs, bb5);
2553 bb = lshift(bb, bb2);
2555 bd = pow5mult(bd, bd5);
2557 bd = lshift(bd, bd2);
2559 bs = lshift(bs, bs2);
2560 delta = diff(bb, bd);
2561 dsign = delta->
sign;
2564 #ifdef Honor_FLT_ROUNDS 2565 if (rounding != 1) {
2568 if (!delta->
x[0] && delta->
wds <= 1) {
2586 #ifdef Avoid_Underflow 2592 delta = lshift(delta,
Log2P);
2593 if (cmp(delta, bs) <= 0)
2598 #ifdef Avoid_Underflow 2601 word0(adj) += (2*
P+1)*Exp_msk1 - y;
2603 #ifdef Sudden_Underflow 2604 if ((
word0(rv) & Exp_mask) <=
2617 adj = ratio(delta, bs);
2620 if (adj <= 0x7ffffffe) {
2624 if (!((rounding>>1) ^ dsign))
2629 #ifdef Avoid_Underflow 2631 word0(adj) += (2*
P+1)*Exp_msk1 - y;
2633 #ifdef Sudden_Underflow 2636 adj *= ulp(
dval(rv));
2646 adj *= ulp(
dval(rv));
2669 if (!delta->
x[0] && delta->
wds <= 1)
2674 if (!delta->
x[0] && delta->
wds <= 1) {
2681 delta = lshift(delta,
Log2P);
2682 if (cmp(delta, bs) > 0)
2693 ? (0xffffffff & (0xffffffff << (2*
P+1-(y>>
Exp_shift)))) :
2704 #ifdef Avoid_Underflow 2713 #ifdef Sudden_Underflow 2718 #ifdef Avoid_Underflow 2727 #ifdef Avoid_Underflow 2730 if (L <= (2*
P+1)*Exp_msk1) {
2731 if (L > (
P+2)*Exp_msk1)
2743 word1(rv) = 0xffffffff;
2750 #ifndef ROUND_BIASED 2756 #ifndef ROUND_BIASED 2759 #ifndef Sudden_Underflow 2764 #ifdef Avoid_Underflow 2770 if ((aadj = ratio(delta, bs)) <= 2.) {
2772 aadj =
dval(aadj1) = 1.;
2774 #ifndef Sudden_Underflow 2789 dval(aadj1) = -aadj;
2794 dval(aadj1) = dsign ? aadj : -aadj;
2795 #ifdef Check_FLT_ROUNDS 2830 #ifdef Avoid_Underflow 2832 if (aadj <= 0x7fffffff) {
2833 if ((z = (
int)aadj) <= 0)
2836 dval(aadj1) = dsign ? aadj : -aadj;
2838 word0(aadj1) += (2*
P+1)*Exp_msk1 - y;
2843 #ifdef Sudden_Underflow 2850 if ((
word0(rv) & Exp_mask) <
P*Exp_msk1)
2852 if ((
word0(rv) & Exp_mask) <=
P*Exp_msk1)
2876 if (y <= (
P-1)*Exp_msk1 && aadj > 1.) {
2877 dval(aadj1) = (double)(
int)(aadj + 0.5);
2888 #ifdef Avoid_Underflow 2897 if (aadj < .4999999 || aadj > .5000001)
2918 else if (!oldinexact)
2921 #ifdef Avoid_Underflow 2949 return sign ? -
dval(rv) :
dval(rv);
2956 ULong *bx, *bxe, q, *sx, *sxe;
2958 ULLong borrow, carry, y, ys;
2960 ULong borrow, carry, y, ys;
2969 Bug(
"oversize b in quorem");
2977 q = *bxe / (*sxe + 1);
2980 Bug(
"oversized quotient in quorem");
2987 ys = *sx++ * (
ULLong)q + carry;
2989 y = *bx - (ys &
FFFFFFFF) - borrow;
2990 borrow = y >> 32 & (ULong)1;
2995 ys = (si & 0xffff) * q + carry;
2996 zs = (si >> 16) * q + (ys >> 16);
2998 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2999 borrow = (y & 0x10000) >> 16;
3000 z = (*bx >> 16) - (zs & 0xffff) - borrow;
3001 borrow = (z & 0x10000) >> 16;
3004 ys = *sx++ * q + carry;
3006 y = *bx - (ys & 0xffff) - borrow;
3007 borrow = (y & 0x10000) >> 16;
3011 }
while (sx <= sxe);
3014 while (--bxe > bx && !*bxe)
3019 if (cmp(b, S) >= 0) {
3029 y = *bx - (ys &
FFFFFFFF) - borrow;
3030 borrow = y >> 32 & (ULong)1;
3035 ys = (si & 0xffff) + carry;
3036 zs = (si >> 16) + (ys >> 16);
3038 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
3039 borrow = (y & 0x10000) >> 16;
3040 z = (*bx >> 16) - (zs & 0xffff) - borrow;
3041 borrow = (z & 0x10000) >> 16;
3046 y = *bx - (ys & 0xffff) - borrow;
3047 borrow = (y & 0x10000) >> 16;
3051 }
while (sx <= sxe);
3055 while (--bxe > bx && !*bxe)
3063 #ifndef MULTIPLE_THREADS 3064 static char *dtoa_result;
3067 #ifndef MULTIPLE_THREADS 3071 return dtoa_result =
xmalloc(i);
3074 #define rv_alloc(i) xmalloc(i) 3078 nrv_alloc(
const char *s,
char **rve,
size_t n)
3083 while ((*t = *s++) != 0) t++;
3089 #define rv_strdup(s, rve) nrv_alloc((s), (rve), strlen(s)+1) 3091 #ifndef MULTIPLE_THREADS 3105 static const char INFSTR[] =
"Infinity";
3106 static const char NANSTR[] =
"NaN";
3107 static const char ZEROSTR[] =
"0";
3180 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
3181 j, j1,
k, k0, k_check, leftright, m2, m5, s2, s5,
3182 spec_case, try_quick, half = 0;
3184 #ifndef Sudden_Underflow 3188 Bigint *b, *b1, *delta, *mlo = 0, *mhi = 0, *
S;
3192 #ifdef Honor_FLT_ROUNDS 3196 int inexact, oldinexact;
3201 #ifndef MULTIPLE_THREADS 3203 freedtoa(dtoa_result);
3211 word0(d) &= ~Sign_bit;
3216 #if defined(IEEE_Arith) + defined(VAX) 3220 if (
word0(d) == 0x8000)
3241 try_quick = oldinexact = get_inexact();
3244 #ifdef Honor_FLT_ROUNDS 3247 rounding = rounding == 2 ? 0 : 2;
3254 b = d2b(
dval(d), &be, &bbits);
3255 #ifdef Sudden_Underflow 3295 #ifndef Sudden_Underflow 3301 i = bbits + be + (
Bias + (
P-1) - 1);
3302 x = i > 32 ?
word0(d) << (64 - i) |
word1(d) >> (i - 32)
3303 :
word1(d) << (32 - i);
3306 i -= (
Bias + (
P-1) - 1) + 1;
3310 ds = (
dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
3312 if (ds < 0. && ds != k)
3316 if (
dval(d) < tens[
k])
3339 if (mode < 0 || mode > 9)
3343 #ifdef Check_FLT_ROUNDS 3368 ilim = ilim1 = i = ndigits;
3374 i = ndigits + k + 1;
3382 #ifdef Honor_FLT_ROUNDS 3383 if (mode > 1 && rounding != 1)
3387 if (ilim >= 0 && ilim <=
Quick_max && try_quick) {
3405 for (; j; j >>= 1, i++)
3412 else if ((j1 = -k) != 0) {
3413 dval(d) *= tens[j1 & 0xf];
3414 for (j = j1 >> 4; j; j >>= 1, i++)
3417 dval(d) *= bigtens[i];
3420 if (k_check &&
dval(d) < 1. && ilim > 0) {
3439 #ifndef No_leftright 3444 dval(eps) = 0.5/tens[ilim-1] -
dval(eps);
3448 *s++ =
'0' + (int)L;
3462 dval(eps) *= tens[ilim-1];
3463 for (i = 1;; i++,
dval(d) *= 10.) {
3464 L = (Long)(
dval(d));
3465 if (!(
dval(d) -= L))
3467 *s++ =
'0' + (int)L;
3471 else if (
dval(d) < 0.5 -
dval(eps)) {
3472 while (*--s ==
'0') ;
3477 if ((*(s-1) -
'0') & 1) {
3483 #ifndef No_leftright 3495 if (be >= 0 && k <=
Int_max) {
3498 if (ndigits < 0 && ilim <= 0) {
3500 if (ilim < 0 ||
dval(d) <= 5*ds)
3504 for (i = 1;; i++,
dval(d) *= 10.) {
3505 L = (Long)(
dval(d) / ds);
3507 #ifdef Check_FLT_ROUNDS 3514 *s++ =
'0' + (int)L;
3522 #ifdef Honor_FLT_ROUNDS 3526 case 2:
goto bump_up;
3530 if (
dval(d) > ds || (
dval(d) == ds && (L & 1))) {
3550 #ifndef Sudden_Underflow 3551 denorm ? be + (
Bias + (
P-1) - 1 + 1) :
3554 1 + 4*
P - 3 - bbits + ((bbits + be - 1) & 3);
3562 if (m2 > 0 && s2 > 0) {
3563 i = m2 < s2 ? m2 : s2;
3571 mhi = pow5mult(mhi, m5);
3576 if ((j = b5 - m5) != 0)
3580 b = pow5mult(b, b5);
3584 S = pow5mult(S, s5);
3589 if ((mode < 2 || leftright)
3590 #ifdef Honor_FLT_ROUNDS
3595 #ifndef Sudden_Underflow
3614 if ((i = ((s5 ? 32 - hi0bits(S->
x[S->
wds-1]) : 1) + s2) & 0x1f) != 0)
3617 if ((i = ((s5 ? 32 - hi0bits(S->
x[S->
wds-1]) : 1) + s2) & 0xf) != 0)
3639 b = multadd(b, 10, 0);
3641 mhi = multadd(mhi, 10, 0);
3645 if (ilim <= 0 && (mode == 3 || mode == 5)) {
3646 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3659 mhi = lshift(mhi, m2);
3667 mhi = Balloc(mhi->k);
3669 mhi = lshift(mhi,
Log2P);
3673 dig = quorem(b,S) +
'0';
3678 delta = diff(S, mhi);
3679 j1 = delta->
sign ? 1 : cmp(b, delta);
3681 #ifndef ROUND_BIASED 3682 if (j1 == 0 && mode != 1 && !(
word1(d) & 1)
3683 #ifdef Honor_FLT_ROUNDS
3692 else if (!b->
x[0] && b->
wds <= 1)
3699 if (j < 0 || (j == 0 && mode != 1
3700 #ifndef ROUND_BIASED
3704 if (!b->
x[0] && b->
wds <= 1) {
3710 #ifdef Honor_FLT_ROUNDS 3713 case 0:
goto accept_dig;
3714 case 2:
goto keep_dig;
3720 if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ ==
'9')
3728 #ifdef Honor_FLT_ROUNDS 3740 #ifdef Honor_FLT_ROUNDS 3746 b = multadd(b, 10, 0);
3748 mlo = mhi = multadd(mhi, 10, 0);
3750 mlo = multadd(mlo, 10, 0);
3751 mhi = multadd(mhi, 10, 0);
3757 *s++ = dig = quorem(b,S) +
'0';
3758 if (!b->
x[0] && b->
wds <= 1) {
3766 b = multadd(b, 10, 0);
3771 #ifdef Honor_FLT_ROUNDS 3773 case 0:
goto trimzeros;
3774 case 2:
goto roundoff;
3779 if (j > 0 || (j == 0 && (dig & 1))) {
3787 if (!half || (*s -
'0') & 1)
3791 while (*--s ==
'0') ;
3797 if (mlo && mlo != mhi)
3810 else if (!oldinexact)
3828 for (; *str; str = end) {
3829 while (
ISSPACE(*str) || *str ==
',') str++;
3832 while (*end && !
ISSPACE(*end) && *end !=
',') end++;
3833 len = (int)(end - str);
3834 (*func)(str,
len, arg);
3864 #define DBL_MANH_SIZE 20 3865 #define DBL_MANL_SIZE 32 3866 #define DBL_ADJ (DBL_MAX_EXP - 2) 3867 #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1) 3868 #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1) 3869 #define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift))) 3870 #define dmanh_get(u) ((uint32_t)(word0(u) & Frac_mask)) 3871 #define dmanl_get(u) ((uint32_t)word1(u)) 3911 word0(u) &= ~Sign_bit;
3920 else if (
isnan(d)) {
3924 else if (d == 0.0) {
3932 u.
d *= 5.363123171977039e+154 ;
3943 bufsize = (ndigits > 0) ? ndigits :
SIGFIGS;
3947 if (
SIGFIGS > ndigits && ndigits > 0) {
3959 for (s = s0 + 1; s < s0 + bufsize; s++) {
3967 for (ndigits =
SIGFIGS; s0[ndigits - 1] ==
'0'; ndigits--)
void rb_syserr_fail(int e, const char *mesg)
size_t strlen(const char *)
#define RUBY_DEFAULT_FREE
#define ACQUIRE_DTOA_LOCK(n)
int() cmpfunc_t(const void *, const void *, void *)
#define rv_strdup(s, rve)
unsigned long ruby_scan_hex(const char *start, size_t len, size_t *retlen)
#define Storeinc(a, b, c)
#define Data_Wrap_Struct(klass, mark, free, sval)
RUBY_EXTERN int isinf(double)
void rb_sys_fail(const char *mesg)
double ruby_strtod(const char *s00, char **se)
#define rounded_product(a, b)
void ruby_qsort(void *base, const size_t nel, const size_t size, cmpfunc_t *cmp, void *d)
unsigned char buf[MIME_BUF_SIZE]
const signed char ruby_digit36_to_number_table[]
#define rounded_quotient(a, b)
char * strchr(char *, char)
unsigned long ruby_scan_oct(const char *start, size_t len, size_t *retlen)
register unsigned int len
#define IEEE_LITTLE_ENDIAN
char * ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve)
const char ruby_hexdigits[]
unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow)
#define FREE_DTOA_LOCK(n)
#define mmprepare(base, size)
unsigned long ruby_strtoul(const char *str, char **endptr, int base)
void ruby_each_words(const char *str, void(*func)(const char *, int, void *), void *arg)
char * ruby_strdup(const char *str)
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)