32 #if defined __MINGW32__
33 #define USHORT _USHORT
36 #define BDIGITS(x) (RBIGNUM_DIGITS(x))
37 #define BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT)
38 #define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
39 #define BIGRAD_HALF ((BDIGIT)(BIGRAD >> 1))
40 #define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS)
42 # define DIGSPERLL (SIZEOF_LONG_LONG/SIZEOF_BDIGITS)
44 #define BIGUP(x) ((BDIGIT_DBL)(x) << BITSPERDIG)
45 #define BIGDN(x) RSHIFT((x),BITSPERDIG)
46 #define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
47 #define BDIGMAX ((BDIGIT)-1)
49 #define BIGZEROP(x) (RBIGNUM_LEN(x) == 0 || \
50 (BDIGITS(x)[0] == 0 && \
51 (RBIGNUM_LEN(x) == 1 || bigzero_p(x))))
53 #define BIGNUM_DEBUG 0
55 #define ON_DEBUG(x) do { x; } while (0)
105 if (l < 0)
return -1;
118 #define RBIGNUM_SET_LEN(b,l) \
119 ((RBASIC(b)->flags & RBIGNUM_EMBED_FLAG) ? \
120 (void)(RBASIC(b)->flags = \
121 (RBASIC(b)->flags & ~RBIGNUM_EMBED_LEN_MASK) | \
122 ((l) << RBIGNUM_EMBED_LEN_SHIFT)) : \
123 (void)(RBIGNUM(b)->as.heap.len = (l)))
134 RBIGNUM(big)->as.heap.digits = ds;
135 RBASIC(big)->flags &= ~RBIGNUM_EMBED_FLAG;
140 ds =
RBIGNUM(big)->as.heap.digits;
141 RBASIC(big)->flags |= RBIGNUM_EMBED_FLAG;
177 RBIGNUM(big)->as.heap.len = len;
183 #define bignew(len,sign) bignew_1(rb_cBignum,(len),(sign))
188 return bignew(len, sign != 0);
210 while (i--) ds[
i] = ~ds[
i];
214 ds[i++] =
BIGLO(num);
236 if (len == 0)
return x;
237 while (--len && !ds[len]);
250 if (len == 0)
return INT2FIX(0);
253 #if 2*SIZEOF_BDIGITS > SIZEOF_LONG
257 num = (long)(
BIGUP(num) + ds[len]);
298 digits[i++] =
BIGLO(num);
303 while (--i && !digits[i]) ;
316 u = 1 + (
VALUE)(-(n + 1));
343 #if SIZEOF_LONG % SIZEOF_BDIGITS != 0
344 # error unexpected SIZEOF_LONG : SIZEOF_BDIGITS ratio
377 buf[0] = (
unsigned long)tmp;
378 tmp = tmp < 0 ? ~0L : 0;
379 for (i = 1; i < num_longs; i++)
380 buf[i] = (
unsigned long)tmp;
387 for (i = 0; i < num_longs && ds < dend; i++) {
389 for (j = 0; j <
DIGSPERLONG && ds < dend; j++, ds++) {
390 l |= ((
unsigned long)*ds << (j *
BITSPERDIG));
394 for (; i < num_longs; i++)
397 for (i = 0; i < num_longs; i++) {
400 for (i = 0; i < num_longs; i++) {
413 while (2 <= num_longs) {
414 if (buf[num_longs-1] == 0 && (
long)buf[num_longs-2] >= 0)
416 else if (buf[num_longs-1] == ~0UL && (
long)buf[num_longs-2] < 0)
423 else if (num_longs == 1)
432 for (i = 0; i < num_longs; i++) {
433 unsigned long d = buf[
i];
434 #if SIZEOF_LONG == SIZEOF_BDIGITS
444 if ((
long)buf[num_longs-1] < 0) {
454 #if SIZEOF_LONG_LONG == QUAD_SIZE && SIZEOF_BDIGITS*2 == SIZEOF_LONG_LONG
480 memcpy(buf, (
char*)&q, SIZEOF_LONG_LONG);
486 unsigned LONG_LONG q;
492 memcpy(&q, buf, SIZEOF_LONG_LONG);
495 if ((LONG_LONG)q < 0) {
505 big =
bignew(DIGSPERLL, 1);
507 while (i < DIGSPERLL) {
508 digits[i++] =
BIGLO(q);
513 while (i-- && !digits[i]) ;
528 for (i = 0; i < len; i++)
530 for (i = 0; i < len; i++) {
552 memcpy(buf, (
char*)
BDIGITS(val), len);
558 #define BNEG(b) (RSHIFT(((BDIGIT*)(b))[QUAD_SIZE/SIZEOF_BDIGITS-1],BITSPERDIG-1) != 0)
566 if (sign &&
BNEG(buf)) {
567 char *tmp = (
char*)
BDIGITS(big);
583 char sign = 1, nondigit = 0;
592 #define ISDIGIT(c) ('0' <= (c) && (c) <= '9')
593 #define conv_digit(c) \
594 (!ISASCII(c) ? -1 : \
595 ISDIGIT(c) ? ((c) - '0') : \
596 ISLOWER(c) ? ((c) - 'a' + 10) : \
597 ISUPPER(c) ? ((c) - 'A' + 10) : \
601 if (badcheck)
goto bad;
609 else if (str[0] ==
'-') {
613 if (str[0] ==
'+' || str[0] ==
'-') {
614 if (badcheck)
goto bad;
636 else if (base < -1) {
646 if (str[0] ==
'0' && (str[1] ==
'b'||str[1] ==
'B')) {
654 if (str[0] ==
'0' && (str[1] ==
'o'||str[1] ==
'O')) {
657 case 4:
case 5:
case 6:
case 7:
661 if (str[0] ==
'0' && (str[1] ==
'd'||str[1] ==
'D')) {
664 case 9:
case 11:
case 12:
case 13:
case 14:
case 15:
669 if (str[0] ==
'0' && (str[1] ==
'x'||str[1] ==
'X')) {
674 if (base < 2 || 36 < base) {
687 while ((c = *++str) ==
'0' || c ==
'_') {
694 if (!(c = *str) ||
ISSPACE(c)) --str;
698 if (c < 0 || c >= base) {
699 if (badcheck)
goto bad;
702 len *=
strlen(str)*
sizeof(char);
704 if ((
size_t)len <= (
sizeof(long)*
CHAR_BIT)) {
707 if (str < end && *end ==
'_')
goto bigparse;
709 if (end == str)
goto bad;
710 while (*end &&
ISSPACE(*end)) end++;
729 if (badcheck && *str ==
'_')
goto bad;
733 for (i=len;i--;) zds[i]=0;
734 while ((c = *str++) != 0) {
737 if (badcheck)
goto bad;
746 if (c >= base)
break;
753 zds[i++] =
BIGLO(num);
765 if (s+1 < str && str[-1] ==
'_')
goto bad;
766 while (*str &&
ISSPACE(*str)) str++;
811 rb_ull2big(
unsigned LONG_LONG n)
818 big =
bignew(DIGSPERLL, 1);
820 while (i < DIGSPERLL) {
821 digits[i++] =
BIGLO(num);
826 while (i-- && !digits[i]) ;
832 rb_ll2big(LONG_LONG n)
849 rb_ull2inum(
unsigned LONG_LONG n)
852 return rb_ull2big(n);
856 rb_ll2inum(LONG_LONG n)
881 #define POW2_P(x) (((x)&((x)-1))==0)
887 # define MASK_55 0x5555555555555555UL
888 # define MASK_33 0x3333333333333333UL
889 # define MASK_0f 0x0f0f0f0f0f0f0f0fUL
891 # define MASK_55 0x55555555UL
892 # define MASK_33 0x33333333UL
893 # define MASK_0f 0x0f0f0f0fUL
903 return (
int)(x & 0x7f);
909 static inline unsigned long
934 return (
int)
ones(x) - 1;
943 #define LOG2_KARATSUBA_DIGITS 7
944 #define KARATSUBA_DIGITS (1L<<LOG2_KARATSUBA_DIGITS)
945 #define MAX_BIG2STR_TABLE_ENTRIES 64
953 for (i = 0; i < 35; ++
i) {
955 big2str_power_cache[
i][j] =
Qnil;
963 if (
NIL_P(big2str_power_cache[base - 2][i])) {
964 big2str_power_cache[base - 2][
i] =
969 return big2str_power_cache[base - 2][
i];
980 rb_bug(
"n1 > KARATSUBA_DIGITS");
983 if (m1) *m1 = 1 << m;
1013 static const double log_2[] = {
1014 1.0, 1.58496250072116, 2.0,
1015 2.32192809488736, 2.58496250072116, 2.8073549220576,
1016 3.0, 3.16992500144231, 3.32192809488736,
1017 3.4594316186373, 3.58496250072116, 3.70043971814109,
1018 3.8073549220576, 3.90689059560852, 4.0,
1019 4.08746284125034, 4.16992500144231, 4.24792751344359,
1020 4.32192809488736, 4.39231742277876, 4.4594316186373,
1021 4.52356195605701, 4.58496250072116, 4.64385618977472,
1022 4.70043971814109, 4.75488750216347, 4.8073549220576,
1023 4.85798099512757, 4.90689059560852, 4.95419631038688,
1024 5.0, 5.04439411935845, 5.08746284125034,
1025 5.12928301694497, 5.16992500144231
1029 if (base < 2 || 36 < base)
1030 rb_bug(
"invalid radix %d", base);
1033 bits = (SIZEOF_LONG*
CHAR_BIT - 1)/2 + 1;
1046 return (
long)ceil(((
double)bits)/log_2[base - 2]);
1055 while (i && j > 0) {
1060 num =
BIGUP(num) + ds[k];
1061 ds[k] = (
BDIGIT)(num / hbase);
1064 if (trim && ds[i-1] == 0) i--;
1067 ptr[--j] = ruby_digitmap[num % base];
1070 if (trim && i == 0 && num == 0)
break;
1074 while (j < len && ptr[j] ==
'0') j++;
1075 MEMMOVE(ptr, ptr + j,
char, len - j);
1083 long n1,
long len,
long hbase,
int trim)
1091 memset(ptr,
'0', len);
1103 len - m1, hbase, trim);
1106 m1, hbase, !lh && trim);
1117 long n1, n2, len, hbase;
1127 if (base < 2 || 36 < base)
1137 #if SIZEOF_BDIGITS > 2
1144 len = off +
big2str_orig(xx, base, ptr + off, n2, hbase, trim);
1183 if (argc == 0) base = 10;
1235 if (num == 1+(
unsigned long)(-(
LONG_MIN+1)))
1255 if (num == 1+(
unsigned long)(-(
LONG_MIN+1)))
1263 static unsigned LONG_LONG
1284 unsigned LONG_LONG num = big2ull(x,
"unsigned long long");
1290 if (num <= LLONG_MAX)
1291 return -(LONG_LONG)num;
1292 if (num == 1+(
unsigned LONG_LONG)(-(LLONG_MIN+1)))
1302 unsigned LONG_LONG num = big2ull(x,
"long long");
1305 if (LLONG_MAX < num)
1310 if (num <= LLONG_MAX)
1311 return -(LONG_LONG)num;
1312 if (num == 1+(
unsigned LONG_LONG)(-(LLONG_MIN+1)))
1327 double u = (d < 0)?-d:d;
1364 y = x >> 64;
if (y) {n -= 64; x = y;}
1367 y = x >> 32;
if (y) {n -= 32; x = y;}
1370 y = x >> 16;
if (y) {n -= 16; x = y;}
1372 y = x >> 8;
if (y) {n -= 8; x = y;}
1373 y = x >> 4;
if (y) {n -= 4; x = y;}
1374 y = x >> 2;
if (y) {n -= 2; x = y;}
1375 y = x >> 1;
if (y) {
return n - 2;}
1400 if (bits && (dl & (1UL << (bits %=
BITSPERDIG)))) {
1401 int carry = dl & ~(~(
BDIGIT)0 << bits);
1404 if ((carry = ds[i]) != 0)
break;
1408 dl &= (
BDIGIT)~0 << bits;
1468 if (yd > 0.0)
return INT2FIX(-1);
1473 #if SIZEOF_LONG * CHAR_BIT < DBL_MANT_DIG
1501 if (yf == 0.0 || rel !=
INT2FIX(0))
1520 #if SIZEOF_LONG * CHAR_BIT < DBL_MANT_DIG
1583 while (xlen-- && (xds[xlen]==yds[xlen]));
1584 if (-1 == xlen)
return INT2FIX(0);
1585 return (xds[xlen] > yds[xlen]) ?
1801 for (i = 0, num = 0; i < yn; i++) {
1806 while (num && i < xn) {
1808 zds[i++] =
BIGLO(num);
1830 z = x; x = y; y = z;
1837 if (xds[i] > yds[i]) {
1840 if (xds[i] < yds[i]) {
1841 z = x; x = y; y = z;
1873 #if SIZEOF_BDIGITS == SIZEOF_LONG
1875 if (xn == 1 && num < 0) {
1881 zds[0] =
BIGLO(num);
1886 for (i=0; i<(int)(
sizeof(y)/
sizeof(
BDIGIT)); i++) {
1893 while (num && i < xn) {
1895 zds[i++] =
BIGLO(num);
1930 #if SIZEOF_BDIGITS == SIZEOF_LONG
1932 zds[0] =
BIGLO(num);
1937 for (i=0; i<(int)(
sizeof(y)/
sizeof(
BDIGIT)); i++) {
1944 while (num && i < xn) {
1946 zds[i++] =
BIGLO(num);
1949 if (num) zds[i++] = (
BDIGIT)num;
1950 else while (i < xn) {
1970 tds = xds; xds = yds; yds = tds;
1971 i = xn; xn = yn; yn =
i;
1977 zds[i++] =
BIGLO(num);
1980 while (num && i < yn) {
1982 zds[i++] =
BIGLO(num);
1989 if (num) zds[i++] = (
BDIGIT)num;
2004 if (sign)
return bigsub(y, x);
2102 while (--i && !xds[i]);
2135 while (j--) zds[j] = 0;
2136 for (i = 0; i < xl; i++) {
2139 if (dd == 0)
continue;
2141 for (j = 0; j < yl; j++) {
2143 n = zds[i + j] + ee;
2144 if (ee) zds[i + j] =
BIGLO(n);
2162 long i, xn, yn, r, n;
2163 BDIGIT *yds, *zds, *t1ds;
2167 assert(2 * xn <= yn || 3 * xn <= 2*(yn+2));
2176 for (i = 0; i < xn + yn; i++) zds[i] = 0;
2180 r = xn > yn ? yn : xn;
2211 while (--hn && !vds[hn + ln]);
2217 while (--ln && !vds[ln]);
2230 long i, n, xn, yn, t1n, t2n;
2231 VALUE xh, xl, yh, yl, z, t1, t2, t3;
2266 for (i = 2 * n + t1n; i < xn + yn; i++) zds[i] = 0;
2275 for (i = t2n; i < 2 * n; i++) zds[i] = 0;
2282 for (i = 0; i < 2 * n; i++) zds[i] = 0;
2287 t3 = xl; xl = xh; xh = t3;
2297 t3 = yl; yl = yh; yh = t3;
2327 int const s3 = BITSPERDIG-s2;
2339 *zds-- = num | xds[--xn]>>s3;
2344 for (i = s1; i > 0; --
i)
2353 int s3 = BITSPERDIG - s2;
2366 xds[i++] = (
BDIGIT)(*zds<<s3) | num;
2369 while (i < xn - s1 - 1);
2377 VALUE v0, v12, v1, v2;
2395 VALUE x0, x1, x2, y0, y1, y2;
2396 VALUE u0, u1, u2, u3, u4, v1, v2, v3;
2397 VALUE z0, z1, z2, z3, z4, z, t;
2407 y0 = x0; y1 = x1; y2 = x2;
2464 v1 = u1; v2 = u2; v3 = u3;
2502 v1 = v2 = v3 =
Qnil;
2569 for (i = 2 * len + 1; i--; ) zds[i] = 0;
2570 for (i = 0; i < len; i++) {
2577 for (j = i + 1; j < len; j++) {
2580 zds[i + j] =
BIGLO(c);
2582 if (
BIGDN(v)) c += w;
2586 zds[i + len] =
BIGLO(c);
2589 if (c) zds[i + len + 1] += (
BDIGIT)c;
2594 #define KARATSUBA_MUL_DIGITS 70
2595 #define TOOM3_MUL_DIGITS 150
2623 t = x; x = y; y = t;
2624 tn = xn; xn = yn; yn = tn;
2647 else if (3*xn <= 2*(yn + 2))
2706 i = bds->
nyzero; num = 0; t2 = 0;
2710 ee = num -
BIGLO(t2);
2716 num +=
zds[j - ny +
i] - t2;
2718 i = 0; num = 0; q--;
2729 }
while (--j >= ny);
2754 if (nx <
ny || (nx ==
ny && xds[nx - 1] < yds[
ny - 1])) {
2756 if (modp) *modp = x;
2774 if (divp) *divp = z;
2780 if (nx==
ny) zds[nx+1] = 0;
2781 while (!yds[
ny-1])
ny--;
2796 tds[j++] =
BIGLO(t2);
2805 zds[j++] =
BIGLO(t2);
2813 while (j--) zds[
j] = xds[
j];
2821 bds.
j = nx==
ny?nx+1:
nx;
2823 if (nx > 10000 ||
ny > 10000) {
2840 j = (nx==
ny ? nx+2 : nx+1) -
ny;
2841 for (i = 0;i <
j;i++) zds[i] = zds[i+
ny];
2848 while (
ny > 1 && !zds[
ny-1]) --
ny;
2852 t2 = (t2 | zds[
i]) >> dd;
2858 if (!zds[
ny-1])
ny--;
2873 if (modp) *modp =
bigadd(mod, y, 1);
3035 BDIGIT bits = (~0 << nb);
3067 #define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
3099 #if SIZEOF_LONG > SIZEOF_INT
3103 if (l < INT_MIN)
return DBL2NUM(0.0);
3189 rb_warn(
"in a**b, b may be too big");
3203 const long BIGLEN_LIMIT =
BITSPERDIG*1024*1024;
3205 if ((xbits > BIGLEN_LIMIT) || (xbits * yy > BIGLEN_LIMIT)) {
3206 rb_warn(
"in a**b, b may be too big");
3210 for (mask =
FIXNUM_MAX + 1; mask; mask >>= 1) {
3236 if (y == 0)
return INT2FIX(0);
3240 #if SIZEOF_BDIGITS == SIZEOF_LONG
3250 #if SIZEOF_BDIGITS == SIZEOF_LONG
3252 zds[0] = xds[0] & y;
3257 for (i=0; i<(int)(
sizeof(y)/
sizeof(
BDIGIT)); i++) {
3264 zds[
i] = sign?0:xds[
i];
3281 volatile VALUE x, y, z;
3321 for (i=0; i<l1; i++) {
3322 zds[
i] = ds1[
i] & ds2[
i];
3325 zds[
i] = sign?0:ds2[
i];
3346 #if SIZEOF_BDIGITS == SIZEOF_LONG
3348 zds[0] = xds[0] | y;
3353 for (i=0; i<(int)(
sizeof(y)/
sizeof(
BDIGIT)); i++) {
3377 volatile VALUE x, y, z;
3417 for (i=0; i<l1; i++) {
3418 zds[
i] = ds1[
i] | ds2[
i];
3436 sign = (y >= 0) ? 1 : 0;
3442 #if SIZEOF_BDIGITS == SIZEOF_LONG
3444 zds[0] = xds[0] ^ y;
3449 for (i=0; i<(int)(
sizeof(y)/
sizeof(
BDIGIT)); i++) {
3456 zds[
i] = sign?xds[
i]:~xds[
i];
3472 volatile VALUE x, y;
3515 for (i=0; i<l1; i++) {
3516 zds[
i] = ds1[
i] ^ ds2[
i];
3519 zds[
i] = sign?ds2[
i]:~ds2[
i];
3561 if (!
NIL_P(t))
return t;
3587 for (i=0; i<s1; i++) {
3591 for (i=0; i<len; i++) {
3593 *zds++ =
BIGLO(num);
3625 if (!
NIL_P(t))
return t;
3649 volatile VALUE save_x;
3674 num = (num | xds[
i]) >> s2;
3676 num =
BIGUP(xds[i]);
3734 while (num += ~xds[i], ++i <= s1) {