Ruby  2.4.2p198(2017-09-14revision59899)
bignum.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  bignum.c -
4 
5  $Author: naruse $
6  created at: Fri Jun 10 00:48:55 JST 1994
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "internal.h"
13 #include "ruby/thread.h"
14 #include "ruby/util.h"
15 
16 #ifdef HAVE_STRINGS_H
17 #include <strings.h>
18 #endif
19 #include <math.h>
20 #include <float.h>
21 #include <ctype.h>
22 #ifdef HAVE_IEEEFP_H
23 #include <ieeefp.h>
24 #endif
25 #include "ruby_assert.h"
26 
27 #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
28 #define USE_GMP
29 #include <gmp.h>
30 #endif
31 
32 #define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM)
33 
34 #ifndef RUBY_INTEGER_UNIFICATION
36 #endif
37 const char ruby_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
38 
39 #ifndef SIZEOF_BDIGIT_DBL
40 # if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
41 # define SIZEOF_BDIGIT_DBL SIZEOF_LONG_LONG
42 # else
43 # define SIZEOF_BDIGIT_DBL SIZEOF_LONG
44 # endif
45 #endif
46 
47 STATIC_ASSERT(sizeof_bdigit_dbl, sizeof(BDIGIT_DBL) == SIZEOF_BDIGIT_DBL);
48 STATIC_ASSERT(sizeof_bdigit_dbl_signed, sizeof(BDIGIT_DBL_SIGNED) == SIZEOF_BDIGIT_DBL);
49 STATIC_ASSERT(sizeof_bdigit, SIZEOF_BDIGIT <= sizeof(BDIGIT));
50 STATIC_ASSERT(sizeof_bdigit_and_dbl, SIZEOF_BDIGIT*2 <= SIZEOF_BDIGIT_DBL);
51 STATIC_ASSERT(bdigit_signedness, 0 < (BDIGIT)-1);
52 STATIC_ASSERT(bdigit_dbl_signedness, 0 < (BDIGIT_DBL)-1);
53 STATIC_ASSERT(bdigit_dbl_signed_signedness, 0 > (BDIGIT_DBL_SIGNED)-1);
55 
56 #if SIZEOF_BDIGIT < SIZEOF_LONG
57 STATIC_ASSERT(sizeof_long_and_sizeof_bdigit, SIZEOF_LONG % SIZEOF_BDIGIT == 0);
58 #else
59 STATIC_ASSERT(sizeof_long_and_sizeof_bdigit, SIZEOF_BDIGIT % SIZEOF_LONG == 0);
60 #endif
61 
62 #ifdef WORDS_BIGENDIAN
63 # define HOST_BIGENDIAN_P 1
64 #else
65 # define HOST_BIGENDIAN_P 0
66 #endif
67 #define ALIGNOF(type) ((int)offsetof(struct { char f1; type f2; }, f2))
68 /* (!LSHIFTABLE(d, n) ? 0 : (n)) is same as n but suppress a warning, C4293, by Visual Studio. */
69 #define LSHIFTABLE(d, n) ((n) < sizeof(d) * CHAR_BIT)
70 #define LSHIFTX(d, n) (!LSHIFTABLE(d, n) ? 0 : ((d) << (!LSHIFTABLE(d, n) ? 0 : (n))))
71 #define CLEAR_LOWBITS(d, numbits) ((d) & LSHIFTX(~((d)*0), (numbits)))
72 #define FILL_LOWBITS(d, numbits) ((d) | (LSHIFTX(((d)*0+1), (numbits))-1))
73 #define POW2_P(x) (((x)&((x)-1))==0)
74 
75 #define BDIGITS(x) (BIGNUM_DIGITS(x))
76 #define BITSPERDIG (SIZEOF_BDIGIT*CHAR_BIT)
77 #define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
78 #define BIGRAD_HALF ((BDIGIT)(BIGRAD >> 1))
79 #define BDIGIT_MSB(d) (((d) & BIGRAD_HALF) != 0)
80 #define BIGUP(x) LSHIFTX(((x) + (BDIGIT_DBL)0), BITSPERDIG)
81 #define BIGDN(x) RSHIFT((x),BITSPERDIG)
82 #define BIGLO(x) ((BDIGIT)((x) & BDIGMAX))
83 #define BDIGMAX ((BDIGIT)(BIGRAD-1))
84 #define BDIGIT_DBL_MAX (~(BDIGIT_DBL)0)
85 
86 #if SIZEOF_BDIGIT == 2
87 # define swap_bdigit(x) swap16(x)
88 #elif SIZEOF_BDIGIT == 4
89 # define swap_bdigit(x) swap32(x)
90 #elif SIZEOF_BDIGIT == 8
91 # define swap_bdigit(x) swap64(x)
92 #endif
93 
94 #define BIGZEROP(x) (BIGNUM_LEN(x) == 0 || \
95  (BDIGITS(x)[0] == 0 && \
96  (BIGNUM_LEN(x) == 1 || bigzero_p(x))))
97 #define BIGSIZE(x) (BIGNUM_LEN(x) == 0 ? (size_t)0 : \
98  BDIGITS(x)[BIGNUM_LEN(x)-1] ? \
99  (size_t)(BIGNUM_LEN(x)*SIZEOF_BDIGIT - nlz(BDIGITS(x)[BIGNUM_LEN(x)-1])/CHAR_BIT) : \
100  rb_absint_size(x, NULL))
101 
102 #define BIGDIVREM_EXTRA_WORDS 1
103 #define bdigit_roomof(n) roomof(n, SIZEOF_BDIGIT)
104 #define BARY_ARGS(ary) ary, numberof(ary)
105 
106 #define BARY_ADD(z, x, y) bary_add(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
107 #define BARY_SUB(z, x, y) bary_sub(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
108 #define BARY_SHORT_MUL(z, x, y) bary_short_mul(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
109 #define BARY_DIVMOD(q, r, x, y) bary_divmod(BARY_ARGS(q), BARY_ARGS(r), BARY_ARGS(x), BARY_ARGS(y))
110 #define BARY_ZERO_P(x) bary_zero_p(BARY_ARGS(x))
111 
112 #define BIGNUM_SET_NEGATIVE_SIGN(b) BIGNUM_SET_SIGN(b, 0)
113 #define BIGNUM_SET_POSITIVE_SIGN(b) BIGNUM_SET_SIGN(b, 1)
114 
115 #define bignew(len,sign) bignew_1(rb_cInteger,(len),(sign))
116 
117 #define BDIGITS_ZERO(ptr, n) do { \
118  BDIGIT *bdigitz_zero_ptr = (ptr); \
119  size_t bdigitz_zero_n = (n); \
120  while (bdigitz_zero_n) { \
121  *bdigitz_zero_ptr++ = 0; \
122  bdigitz_zero_n--; \
123  } \
124 } while (0)
125 
126 #define BARY_TRUNC(ds, n) do { \
127  while (0 < (n) && (ds)[(n)-1] == 0) \
128  (n)--; \
129  } while (0)
130 
131 #define KARATSUBA_BALANCED(xn, yn) ((yn)/2 < (xn))
132 #define TOOM3_BALANCED(xn, yn) (((yn)+2)/3 * 2 < (xn))
133 
134 #define GMP_MUL_DIGITS 20
135 #define KARATSUBA_MUL_DIGITS 70
136 #define TOOM3_MUL_DIGITS 150
137 
138 #define GMP_DIV_DIGITS 20
139 #define GMP_BIG2STR_DIGITS 20
140 #define GMP_STR2BIG_DIGITS 20
141 
142 typedef void (mulfunc_t)(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
143 
146 static BDIGIT bigdivrem_single(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT y);
147 static void bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn);
148 
149 static VALUE bigmul0(VALUE x, VALUE y);
150 static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
151 static VALUE bignew_1(VALUE klass, size_t len, int sign);
152 static inline VALUE bigtrunc(VALUE x);
153 
154 static VALUE bigsq(VALUE x);
155 static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp);
156 static inline VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret);
157 
158 #if SIZEOF_BDIGIT <= SIZEOF_INT
159 static int nlz(BDIGIT x) { return nlz_int((unsigned int)x) - (SIZEOF_INT-SIZEOF_BDIGIT) * CHAR_BIT; }
160 #elif SIZEOF_BDIGIT <= SIZEOF_LONG
161 static int nlz(BDIGIT x) { return nlz_long((unsigned long)x) - (SIZEOF_LONG-SIZEOF_BDIGIT) * CHAR_BIT; }
162 #elif SIZEOF_BDIGIT <= SIZEOF_LONG_LONG
163 static int nlz(BDIGIT x) { return nlz_long_long((unsigned LONG_LONG)x) - (SIZEOF_LONG_LONG-SIZEOF_BDIGIT) * CHAR_BIT; }
164 #elif SIZEOF_BDIGIT <= SIZEOF_INT128_T
165 static int nlz(BDIGIT x) { return nlz_int128((uint128_t)x) - (SIZEOF_INT128_T-SIZEOF_BDIGIT) * CHAR_BIT; }
166 #endif
167 
168 #define U16(a) ((uint16_t)(a))
169 #define U32(a) ((uint32_t)(a))
170 #ifdef HAVE_UINT64_T
171 #define U64(a,b) (((uint64_t)(a) << 32) | (b))
172 #endif
173 #ifdef HAVE_UINT128_T
174 #define U128(a,b,c,d) (((uint128_t)U64(a,b) << 64) | U64(c,d))
175 #endif
176 
177 /* The following script, maxpow.rb, generates the tables follows.
178 
179 def big(n, bits)
180  ns = []
181  ((bits+31)/32).times {
182  ns << sprintf("0x%08x", n & 0xffff_ffff)
183  n >>= 32
184  }
185  "U#{bits}(" + ns.reverse.join(",") + ")"
186 end
187 def values(ary, width, indent)
188  lines = [""]
189  ary.each {|e|
190  lines << "" if !ary.last.empty? && width < (lines.last + e + ", ").length
191  lines.last << e + ", "
192  }
193  lines.map {|line| " " * indent + line.chomp(" ") + "\n" }.join
194 end
195 [16,32,64,128].each {|bits|
196  max = 2**bits-1
197  exps = []
198  nums = []
199  2.upto(36) {|base|
200  exp = 0
201  n = 1
202  while n * base <= max
203  exp += 1
204  n *= base
205  end
206  exps << exp.to_s
207  nums << big(n, bits)
208  }
209  puts "#ifdef HAVE_UINT#{bits}_T"
210  puts "static const int maxpow#{bits}_exp[35] = {"
211  print values(exps, 70, 4)
212  puts "};"
213  puts "static const uint#{bits}_t maxpow#{bits}_num[35] = {"
214  print values(nums, 70, 4)
215  puts "};"
216  puts "#endif"
217 }
218 
219  */
220 
221 #if SIZEOF_BDIGIT_DBL == 2
222 static const int maxpow16_exp[35] = {
223  15, 10, 7, 6, 6, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
224  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
225 };
226 static const uint16_t maxpow16_num[35] = {
227  U16(0x00008000), U16(0x0000e6a9), U16(0x00004000), U16(0x00003d09),
228  U16(0x0000b640), U16(0x000041a7), U16(0x00008000), U16(0x0000e6a9),
229  U16(0x00002710), U16(0x00003931), U16(0x00005100), U16(0x00006f91),
230  U16(0x00009610), U16(0x0000c5c1), U16(0x00001000), U16(0x00001331),
231  U16(0x000016c8), U16(0x00001acb), U16(0x00001f40), U16(0x0000242d),
232  U16(0x00002998), U16(0x00002f87), U16(0x00003600), U16(0x00003d09),
233  U16(0x000044a8), U16(0x00004ce3), U16(0x000055c0), U16(0x00005f45),
234  U16(0x00006978), U16(0x0000745f), U16(0x00008000), U16(0x00008c61),
235  U16(0x00009988), U16(0x0000a77b), U16(0x0000b640),
236 };
237 #elif SIZEOF_BDIGIT_DBL == 4
238 static const int maxpow32_exp[35] = {
239  31, 20, 15, 13, 12, 11, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7,
240  7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
241 };
242 static const uint32_t maxpow32_num[35] = {
243  U32(0x80000000), U32(0xcfd41b91), U32(0x40000000), U32(0x48c27395),
244  U32(0x81bf1000), U32(0x75db9c97), U32(0x40000000), U32(0xcfd41b91),
245  U32(0x3b9aca00), U32(0x8c8b6d2b), U32(0x19a10000), U32(0x309f1021),
246  U32(0x57f6c100), U32(0x98c29b81), U32(0x10000000), U32(0x18754571),
247  U32(0x247dbc80), U32(0x3547667b), U32(0x4c4b4000), U32(0x6b5a6e1d),
248  U32(0x94ace180), U32(0xcaf18367), U32(0x0b640000), U32(0x0e8d4a51),
249  U32(0x1269ae40), U32(0x17179149), U32(0x1cb91000), U32(0x23744899),
250  U32(0x2b73a840), U32(0x34e63b41), U32(0x40000000), U32(0x4cfa3cc1),
251  U32(0x5c13d840), U32(0x6d91b519), U32(0x81bf1000),
252 };
253 #elif SIZEOF_BDIGIT_DBL == 8 && defined HAVE_UINT64_T
254 static const int maxpow64_exp[35] = {
255  63, 40, 31, 27, 24, 22, 21, 20, 19, 18, 17, 17, 16, 16, 15, 15, 15,
256  15, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12,
257  12,
258 };
259 static const uint64_t maxpow64_num[35] = {
260  U64(0x80000000,0x00000000), U64(0xa8b8b452,0x291fe821),
261  U64(0x40000000,0x00000000), U64(0x6765c793,0xfa10079d),
262  U64(0x41c21cb8,0xe1000000), U64(0x36427987,0x50226111),
263  U64(0x80000000,0x00000000), U64(0xa8b8b452,0x291fe821),
264  U64(0x8ac72304,0x89e80000), U64(0x4d28cb56,0xc33fa539),
265  U64(0x1eca170c,0x00000000), U64(0x780c7372,0x621bd74d),
266  U64(0x1e39a505,0x7d810000), U64(0x5b27ac99,0x3df97701),
267  U64(0x10000000,0x00000000), U64(0x27b95e99,0x7e21d9f1),
268  U64(0x5da0e1e5,0x3c5c8000), U64(0xd2ae3299,0xc1c4aedb),
269  U64(0x16bcc41e,0x90000000), U64(0x2d04b7fd,0xd9c0ef49),
270  U64(0x5658597b,0xcaa24000), U64(0xa0e20737,0x37609371),
271  U64(0x0c29e980,0x00000000), U64(0x14adf4b7,0x320334b9),
272  U64(0x226ed364,0x78bfa000), U64(0x383d9170,0xb85ff80b),
273  U64(0x5a3c23e3,0x9c000000), U64(0x8e651373,0x88122bcd),
274  U64(0xdd41bb36,0xd259e000), U64(0x0aee5720,0xee830681),
275  U64(0x10000000,0x00000000), U64(0x172588ad,0x4f5f0981),
276  U64(0x211e44f7,0xd02c1000), U64(0x2ee56725,0xf06e5c71),
277  U64(0x41c21cb8,0xe1000000),
278 };
279 #elif SIZEOF_BDIGIT_DBL == 16 && defined HAVE_UINT128_T
280 static const int maxpow128_exp[35] = {
281  127, 80, 63, 55, 49, 45, 42, 40, 38, 37, 35, 34, 33, 32, 31, 31, 30,
282  30, 29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 26, 25, 25, 25, 25, 24,
283  24,
284 };
285 static const uint128_t maxpow128_num[35] = {
286  U128(0x80000000,0x00000000,0x00000000,0x00000000),
287  U128(0x6f32f1ef,0x8b18a2bc,0x3cea5978,0x9c79d441),
288  U128(0x40000000,0x00000000,0x00000000,0x00000000),
289  U128(0xd0cf4b50,0xcfe20765,0xfff4b4e3,0xf741cf6d),
290  U128(0x6558e2a0,0x921fe069,0x42860000,0x00000000),
291  U128(0x5080c7b7,0xd0e31ba7,0x5911a67d,0xdd3d35e7),
292  U128(0x40000000,0x00000000,0x00000000,0x00000000),
293  U128(0x6f32f1ef,0x8b18a2bc,0x3cea5978,0x9c79d441),
294  U128(0x4b3b4ca8,0x5a86c47a,0x098a2240,0x00000000),
295  U128(0xffd1390a,0x0adc2fb8,0xdabbb817,0x4d95c99b),
296  U128(0x2c6fdb36,0x4c25e6c0,0x00000000,0x00000000),
297  U128(0x384bacd6,0x42c343b4,0xe90c4272,0x13506d29),
298  U128(0x31f5db32,0xa34aced6,0x0bf13a0e,0x00000000),
299  U128(0x20753ada,0xfd1e839f,0x53686d01,0x3143ee01),
300  U128(0x10000000,0x00000000,0x00000000,0x00000000),
301  U128(0x68ca11d6,0xb4f6d1d1,0xfaa82667,0x8073c2f1),
302  U128(0x223e493b,0xb3bb69ff,0xa4b87d6c,0x40000000),
303  U128(0xad62418d,0x14ea8247,0x01c4b488,0x6cc66f59),
304  U128(0x2863c1f5,0xcdae42f9,0x54000000,0x00000000),
305  U128(0xa63fd833,0xb9386b07,0x36039e82,0xbe651b25),
306  U128(0x1d1f7a9c,0xd087a14d,0x28cdf3d5,0x10000000),
307  U128(0x651b5095,0xc2ea8fc1,0xb30e2c57,0x77aaf7e1),
308  U128(0x0ddef20e,0xff760000,0x00000000,0x00000000),
309  U128(0x29c30f10,0x29939b14,0x6664242d,0x97d9f649),
310  U128(0x786a435a,0xe9558b0e,0x6aaf6d63,0xa8000000),
311  U128(0x0c5afe6f,0xf302bcbf,0x94fd9829,0xd87f5079),
312  U128(0x1fce575c,0xe1692706,0x07100000,0x00000000),
313  U128(0x4f34497c,0x8597e144,0x36e91802,0x00528229),
314  U128(0xbf3a8e1d,0x41ef2170,0x7802130d,0x84000000),
315  U128(0x0e7819e1,0x7f1eb0fb,0x6ee4fb89,0x01d9531f),
316  U128(0x20000000,0x00000000,0x00000000,0x00000000),
317  U128(0x4510460d,0xd9e879c0,0x14a82375,0x2f22b321),
318  U128(0x91abce3c,0x4b4117ad,0xe76d35db,0x22000000),
319  U128(0x08973ea3,0x55d75bc2,0x2e42c391,0x727d69e1),
320  U128(0x10e425c5,0x6daffabc,0x35c10000,0x00000000),
321 };
322 #endif
323 
324 static BDIGIT_DBL
325 maxpow_in_bdigit_dbl(int base, int *exp_ret)
326 {
327  BDIGIT_DBL maxpow;
328  int exponent;
329 
330  assert(2 <= base && base <= 36);
331 
332  {
333 #if SIZEOF_BDIGIT_DBL == 2
334  maxpow = maxpow16_num[base-2];
335  exponent = maxpow16_exp[base-2];
336 #elif SIZEOF_BDIGIT_DBL == 4
337  maxpow = maxpow32_num[base-2];
338  exponent = maxpow32_exp[base-2];
339 #elif SIZEOF_BDIGIT_DBL == 8 && defined HAVE_UINT64_T
340  maxpow = maxpow64_num[base-2];
341  exponent = maxpow64_exp[base-2];
342 #elif SIZEOF_BDIGIT_DBL == 16 && defined HAVE_UINT128_T
343  maxpow = maxpow128_num[base-2];
344  exponent = maxpow128_exp[base-2];
345 #else
346  maxpow = base;
347  exponent = 1;
348  while (maxpow <= BDIGIT_DBL_MAX / base) {
349  maxpow *= base;
350  exponent++;
351  }
352 #endif
353  }
354 
355  *exp_ret = exponent;
356  return maxpow;
357 }
358 
359 static inline BDIGIT_DBL
360 bary2bdigitdbl(const BDIGIT *ds, size_t n)
361 {
362  assert(n <= 2);
363 
364  if (n == 2)
365  return ds[0] | BIGUP(ds[1]);
366  if (n == 1)
367  return ds[0];
368  return 0;
369 }
370 
371 static inline void
372 bdigitdbl2bary(BDIGIT *ds, size_t n, BDIGIT_DBL num)
373 {
374  assert(n == 2);
375 
376  ds[0] = BIGLO(num);
377  ds[1] = (BDIGIT)BIGDN(num);
378 }
379 
380 static int
381 bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
382 {
383  BARY_TRUNC(xds, xn);
384  BARY_TRUNC(yds, yn);
385 
386  if (xn < yn)
387  return -1;
388  if (xn > yn)
389  return 1;
390 
391  while (xn-- && xds[xn] == yds[xn])
392  ;
393  if (xn == (size_t)-1)
394  return 0;
395  return xds[xn] < yds[xn] ? -1 : 1;
396 }
397 
398 static BDIGIT
399 bary_small_lshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift)
400 {
401  size_t i;
402  BDIGIT_DBL num = 0;
403  assert(0 <= shift && shift < BITSPERDIG);
404 
405  for (i=0; i<n; i++) {
406  num = num | (BDIGIT_DBL)*xds++ << shift;
407  *zds++ = BIGLO(num);
408  num = BIGDN(num);
409  }
410  return BIGLO(num);
411 }
412 
413 static void
414 bary_small_rshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift, BDIGIT higher_bdigit)
415 {
416  BDIGIT_DBL num = 0;
417  BDIGIT x;
418 
419  assert(0 <= shift && shift < BITSPERDIG);
420 
421  num = BIGUP(higher_bdigit);
422  while (n--) {
423  num = (num | xds[n]) >> shift;
424  x = xds[n];
425  zds[n] = BIGLO(num);
426  num = BIGUP(x);
427  }
428 }
429 
430 static int
431 bary_zero_p(BDIGIT *xds, size_t xn)
432 {
433  if (xn == 0)
434  return 1;
435  do {
436  if (xds[--xn]) return 0;
437  } while (xn);
438  return 1;
439 }
440 
441 static void
442 bary_neg(BDIGIT *ds, size_t n)
443 {
444  while (n--)
445  ds[n] = BIGLO(~ds[n]);
446 }
447 
448 static int
449 bary_2comp(BDIGIT *ds, size_t n)
450 {
451  size_t i;
452  i = 0;
453  for (i = 0; i < n; i++) {
454  if (ds[i] != 0) {
455  goto non_zero;
456  }
457  }
458  return 1;
459 
460  non_zero:
461  ds[i] = BIGLO(~ds[i] + 1);
462  i++;
463  for (; i < n; i++) {
464  ds[i] = BIGLO(~ds[i]);
465  }
466  return 0;
467 }
468 
469 static void
470 bary_swap(BDIGIT *ds, size_t num_bdigits)
471 {
472  BDIGIT *p1 = ds;
473  BDIGIT *p2 = ds + num_bdigits - 1;
474  for (; p1 < p2; p1++, p2--) {
475  BDIGIT tmp = *p1;
476  *p1 = *p2;
477  *p2 = tmp;
478  }
479 }
480 
481 #define INTEGER_PACK_WORDORDER_MASK \
482  (INTEGER_PACK_MSWORD_FIRST | \
483  INTEGER_PACK_LSWORD_FIRST)
484 #define INTEGER_PACK_BYTEORDER_MASK \
485  (INTEGER_PACK_MSBYTE_FIRST | \
486  INTEGER_PACK_LSBYTE_FIRST | \
487  INTEGER_PACK_NATIVE_BYTE_ORDER)
488 
489 static void
490 validate_integer_pack_format(size_t numwords, size_t wordsize, size_t nails, int flags, int supported_flags)
491 {
492  int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK;
493  int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK;
494 
495  if (flags & ~supported_flags) {
496  rb_raise(rb_eArgError, "unsupported flags specified");
497  }
498  if (wordorder_bits == 0) {
499  if (1 < numwords)
500  rb_raise(rb_eArgError, "word order not specified");
501  }
502  else if (wordorder_bits != INTEGER_PACK_MSWORD_FIRST &&
503  wordorder_bits != INTEGER_PACK_LSWORD_FIRST)
504  rb_raise(rb_eArgError, "unexpected word order");
505  if (byteorder_bits == 0) {
506  rb_raise(rb_eArgError, "byte order not specified");
507  }
508  else if (byteorder_bits != INTEGER_PACK_MSBYTE_FIRST &&
509  byteorder_bits != INTEGER_PACK_LSBYTE_FIRST &&
510  byteorder_bits != INTEGER_PACK_NATIVE_BYTE_ORDER)
511  rb_raise(rb_eArgError, "unexpected byte order");
512  if (wordsize == 0)
513  rb_raise(rb_eArgError, "invalid wordsize: %"PRI_SIZE_PREFIX"u", wordsize);
514  if (SSIZE_MAX < wordsize)
515  rb_raise(rb_eArgError, "too big wordsize: %"PRI_SIZE_PREFIX"u", wordsize);
516  if (wordsize <= nails / CHAR_BIT)
517  rb_raise(rb_eArgError, "too big nails: %"PRI_SIZE_PREFIX"u", nails);
518  if (SIZE_MAX / wordsize < numwords)
519  rb_raise(rb_eArgError, "too big numwords * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize);
520 }
521 
522 static void
524  size_t numwords, size_t wordsize, size_t nails, int flags,
525  size_t *word_num_fullbytes_ret,
526  int *word_num_partialbits_ret,
527  size_t *word_start_ret,
528  ssize_t *word_step_ret,
529  size_t *word_last_ret,
530  size_t *byte_start_ret,
531  int *byte_step_ret)
532 {
533  int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK;
534  int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK;
535  size_t word_num_fullbytes;
536  int word_num_partialbits;
537  size_t word_start;
538  ssize_t word_step;
539  size_t word_last;
540  size_t byte_start;
541  int byte_step;
542 
543  word_num_partialbits = CHAR_BIT - (int)(nails % CHAR_BIT);
544  if (word_num_partialbits == CHAR_BIT)
545  word_num_partialbits = 0;
546  word_num_fullbytes = wordsize - (nails / CHAR_BIT);
547  if (word_num_partialbits != 0) {
548  word_num_fullbytes--;
549  }
550 
551  if (wordorder_bits == INTEGER_PACK_MSWORD_FIRST) {
552  word_start = wordsize*(numwords-1);
553  word_step = -(ssize_t)wordsize;
554  word_last = 0;
555  }
556  else {
557  word_start = 0;
558  word_step = wordsize;
559  word_last = wordsize*(numwords-1);
560  }
561 
562  if (byteorder_bits == INTEGER_PACK_NATIVE_BYTE_ORDER) {
563 #ifdef WORDS_BIGENDIAN
564  byteorder_bits = INTEGER_PACK_MSBYTE_FIRST;
565 #else
566  byteorder_bits = INTEGER_PACK_LSBYTE_FIRST;
567 #endif
568  }
569  if (byteorder_bits == INTEGER_PACK_MSBYTE_FIRST) {
570  byte_start = wordsize-1;
571  byte_step = -1;
572  }
573  else {
574  byte_start = 0;
575  byte_step = 1;
576  }
577 
578  *word_num_partialbits_ret = word_num_partialbits;
579  *word_num_fullbytes_ret = word_num_fullbytes;
580  *word_start_ret = word_start;
581  *word_step_ret = word_step;
582  *word_last_ret = word_last;
583  *byte_start_ret = byte_start;
584  *byte_step_ret = byte_step;
585 }
586 
587 static inline void
588 integer_pack_fill_dd(BDIGIT **dpp, BDIGIT **dep, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
589 {
590  if (*dpp < *dep && BITSPERDIG <= (int)sizeof(*ddp) * CHAR_BIT - *numbits_in_dd_p) {
591  *ddp |= (BDIGIT_DBL)(*(*dpp)++) << *numbits_in_dd_p;
592  *numbits_in_dd_p += BITSPERDIG;
593  }
594  else if (*dpp == *dep) {
595  /* higher bits are infinity zeros */
596  *numbits_in_dd_p = (int)sizeof(*ddp) * CHAR_BIT;
597  }
598 }
599 
600 static inline BDIGIT_DBL
601 integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
602 {
603  BDIGIT_DBL ret;
604  ret = (*ddp) & (((BDIGIT_DBL)1 << n) - 1);
605  *ddp >>= n;
606  *numbits_in_dd_p -= n;
607  return ret;
608 }
609 
610 #if !defined(WORDS_BIGENDIAN)
611 static int
612 bytes_2comp(unsigned char *buf, size_t len)
613 {
614  size_t i;
615  for (i = 0; i < len; i++)
616  buf[i] = ~buf[i];
617  for (i = 0; i < len; i++) {
618  buf[i]++;
619  if (buf[i] != 0)
620  return 0;
621  }
622  return 1;
623 }
624 #endif
625 
626 static int
627 bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
628 {
629  BDIGIT *dp, *de;
630  unsigned char *buf, *bufend;
631 
632  dp = ds;
633  de = ds + num_bdigits;
634 
635  validate_integer_pack_format(numwords, wordsize, nails, flags,
643 
644  while (dp < de && de[-1] == 0)
645  de--;
646  if (dp == de) {
647  sign = 0;
648  }
649 
651  if (sign == 0) {
652  MEMZERO(words, unsigned char, numwords * wordsize);
653  return 0;
654  }
655  if (nails == 0 && numwords == 1) {
656  int need_swap = wordsize != 1 &&
659  if (0 < sign || !(flags & INTEGER_PACK_2COMP)) {
660  BDIGIT d;
661  if (wordsize == 1) {
662  *((unsigned char *)words) = (unsigned char)(d = dp[0]);
663  return ((1 < de - dp || CLEAR_LOWBITS(d, 8) != 0) ? 2 : 1) * sign;
664  }
665 #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT
666  if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
667  uint16_t u = (uint16_t)(d = dp[0]);
668  if (need_swap) u = swap16(u);
669  *((uint16_t *)words) = u;
670  return ((1 < de - dp || CLEAR_LOWBITS(d, 16) != 0) ? 2 : 1) * sign;
671  }
672 #endif
673 #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT
674  if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
675  uint32_t u = (uint32_t)(d = dp[0]);
676  if (need_swap) u = swap32(u);
677  *((uint32_t *)words) = u;
678  return ((1 < de - dp || CLEAR_LOWBITS(d, 32) != 0) ? 2 : 1) * sign;
679  }
680 #endif
681 #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT
682  if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
683  uint64_t u = (uint64_t)(d = dp[0]);
684  if (need_swap) u = swap64(u);
685  *((uint64_t *)words) = u;
686  return ((1 < de - dp || CLEAR_LOWBITS(d, 64) != 0) ? 2 : 1) * sign;
687  }
688 #endif
689  }
690  else { /* sign < 0 && (flags & INTEGER_PACK_2COMP) */
692  if (wordsize == 1) {
693  *((unsigned char *)words) = (unsigned char)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
694  return (1 < de - dp || FILL_LOWBITS(d, 8) != -1) ? -2 : -1;
695  }
696 #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT
697  if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
698  uint16_t u = (uint16_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
699  if (need_swap) u = swap16(u);
700  *((uint16_t *)words) = u;
701  return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
702  (1 < de - dp || FILL_LOWBITS(d, 16) != -1) ? -2 : -1;
703  }
704 #endif
705 #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT
706  if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
707  uint32_t u = (uint32_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
708  if (need_swap) u = swap32(u);
709  *((uint32_t *)words) = u;
710  return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
711  (1 < de - dp || FILL_LOWBITS(d, 32) != -1) ? -2 : -1;
712  }
713 #endif
714 #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT
715  if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
716  uint64_t u = (uint64_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
717  if (need_swap) u = swap64(u);
718  *((uint64_t *)words) = u;
719  return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
720  (1 < de - dp || FILL_LOWBITS(d, 64) != -1) ? -2 : -1;
721  }
722 #endif
723  }
724  }
725 #if !defined(WORDS_BIGENDIAN)
726  if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
729  size_t src_size = (de - dp) * SIZEOF_BDIGIT;
730  size_t dst_size = numwords * wordsize;
731  int overflow = 0;
732  while (0 < src_size && ((unsigned char *)ds)[src_size-1] == 0)
733  src_size--;
734  if (src_size <= dst_size) {
735  MEMCPY(words, dp, char, src_size);
736  MEMZERO((char*)words + src_size, char, dst_size - src_size);
737  }
738  else {
739  MEMCPY(words, dp, char, dst_size);
740  overflow = 1;
741  }
742  if (sign < 0 && (flags & INTEGER_PACK_2COMP)) {
743  int zero_p = bytes_2comp(words, dst_size);
744  if (zero_p && overflow) {
745  unsigned char *p = (unsigned char *)dp;
746  if (dst_size == src_size-1 &&
747  p[dst_size] == 1) {
748  overflow = 0;
749  }
750  }
751  }
752  if (overflow)
753  sign *= 2;
754  return sign;
755  }
756 #endif
757  if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
758  wordsize % SIZEOF_BDIGIT == 0 && (uintptr_t)words % ALIGNOF(BDIGIT) == 0) {
759  size_t bdigits_per_word = wordsize / SIZEOF_BDIGIT;
760  size_t src_num_bdigits = de - dp;
761  size_t dst_num_bdigits = numwords * bdigits_per_word;
762  int overflow = 0;
763  int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0;
764  int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P :
765  (flags & INTEGER_PACK_MSBYTE_FIRST) != 0;
766  if (src_num_bdigits <= dst_num_bdigits) {
767  MEMCPY(words, dp, BDIGIT, src_num_bdigits);
768  BDIGITS_ZERO((BDIGIT*)words + src_num_bdigits, dst_num_bdigits - src_num_bdigits);
769  }
770  else {
771  MEMCPY(words, dp, BDIGIT, dst_num_bdigits);
772  overflow = 1;
773  }
774  if (sign < 0 && (flags & INTEGER_PACK_2COMP)) {
775  int zero_p = bary_2comp(words, dst_num_bdigits);
776  if (zero_p && overflow &&
777  dst_num_bdigits == src_num_bdigits-1 &&
778  dp[dst_num_bdigits] == 1)
779  overflow = 0;
780  }
781  if (msbytefirst_p != HOST_BIGENDIAN_P) {
782  size_t i;
783  for (i = 0; i < dst_num_bdigits; i++) {
784  BDIGIT d = ((BDIGIT*)words)[i];
785  ((BDIGIT*)words)[i] = swap_bdigit(d);
786  }
787  }
788  if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
789  size_t i;
790  BDIGIT *p = words;
791  for (i = 0; i < numwords; i++) {
792  bary_swap(p, bdigits_per_word);
793  p += bdigits_per_word;
794  }
795  }
796  if (mswordfirst_p) {
797  bary_swap(words, dst_num_bdigits);
798  }
799  if (overflow)
800  sign *= 2;
801  return sign;
802  }
803  }
804 
805  buf = words;
806  bufend = buf + numwords * wordsize;
807 
808  if (buf == bufend) {
809  /* overflow if non-zero*/
810  if (!(flags & INTEGER_PACK_2COMP) || 0 <= sign)
811  sign *= 2;
812  else {
813  if (de - dp == 1 && dp[0] == 1)
814  sign = -1; /* val == -1 == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
815  else
816  sign = -2; /* val < -1 == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
817  }
818  }
819  else if (dp == de) {
820  memset(buf, '\0', bufend - buf);
821  }
822  else if (dp < de && buf < bufend) {
823  int word_num_partialbits;
824  size_t word_num_fullbytes;
825 
826  ssize_t word_step;
827  size_t byte_start;
828  int byte_step;
829 
830  size_t word_start, word_last;
831  unsigned char *wordp, *last_wordp;
832  BDIGIT_DBL dd;
833  int numbits_in_dd;
834 
835  integer_pack_loop_setup(numwords, wordsize, nails, flags,
836  &word_num_fullbytes, &word_num_partialbits,
837  &word_start, &word_step, &word_last, &byte_start, &byte_step);
838 
839  wordp = buf + word_start;
840  last_wordp = buf + word_last;
841 
842  dd = 0;
843  numbits_in_dd = 0;
844 
845 #define FILL_DD \
846  integer_pack_fill_dd(&dp, &de, &dd, &numbits_in_dd)
847 #define TAKE_LOWBITS(n) \
848  integer_pack_take_lowbits(n, &dd, &numbits_in_dd)
849 
850  while (1) {
851  size_t index_in_word = 0;
852  unsigned char *bytep = wordp + byte_start;
853  while (index_in_word < word_num_fullbytes) {
854  FILL_DD;
855  *bytep = TAKE_LOWBITS(CHAR_BIT);
856  bytep += byte_step;
857  index_in_word++;
858  }
859  if (word_num_partialbits) {
860  FILL_DD;
861  *bytep = TAKE_LOWBITS(word_num_partialbits);
862  bytep += byte_step;
863  index_in_word++;
864  }
865  while (index_in_word < wordsize) {
866  *bytep = 0;
867  bytep += byte_step;
868  index_in_word++;
869  }
870 
871  if (wordp == last_wordp)
872  break;
873 
874  wordp += word_step;
875  }
876  FILL_DD;
877  /* overflow tests */
878  if (dp != de || 1 < dd) {
879  /* 2**(numwords*(wordsize*CHAR_BIT-nails)+1) <= abs(val) */
880  sign *= 2;
881  }
882  else if (dd == 1) {
883  /* 2**(numwords*(wordsize*CHAR_BIT-nails)) <= abs(val) < 2**(numwords*(wordsize*CHAR_BIT-nails)+1) */
884  if (!(flags & INTEGER_PACK_2COMP) || 0 <= sign)
885  sign *= 2;
886  else { /* overflow_2comp && sign == -1 */
887  /* test lower bits are all zero. */
888  dp = ds;
889  while (dp < de && *dp == 0)
890  dp++;
891  if (de - dp == 1 && /* only one non-zero word. */
892  POW2_P(*dp)) /* *dp contains only one bit set. */
893  sign = -1; /* val == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
894  else
895  sign = -2; /* val < -2**(numwords*(wordsize*CHAR_BIT-nails)) */
896  }
897  }
898  }
899 
900  if ((flags & INTEGER_PACK_2COMP) && (sign < 0 && numwords != 0)) {
901  unsigned char *buf;
902 
903  int word_num_partialbits;
904  size_t word_num_fullbytes;
905 
906  ssize_t word_step;
907  size_t byte_start;
908  int byte_step;
909 
910  size_t word_start, word_last;
911  unsigned char *wordp, *last_wordp;
912 
913  unsigned int partialbits_mask;
914  int carry;
915 
916  integer_pack_loop_setup(numwords, wordsize, nails, flags,
917  &word_num_fullbytes, &word_num_partialbits,
918  &word_start, &word_step, &word_last, &byte_start, &byte_step);
919 
920  partialbits_mask = (1 << word_num_partialbits) - 1;
921 
922  buf = words;
923  wordp = buf + word_start;
924  last_wordp = buf + word_last;
925 
926  carry = 1;
927  while (1) {
928  size_t index_in_word = 0;
929  unsigned char *bytep = wordp + byte_start;
930  while (index_in_word < word_num_fullbytes) {
931  carry += (unsigned char)~*bytep;
932  *bytep = (unsigned char)carry;
933  carry >>= CHAR_BIT;
934  bytep += byte_step;
935  index_in_word++;
936  }
937  if (word_num_partialbits) {
938  carry += (*bytep & partialbits_mask) ^ partialbits_mask;
939  *bytep = carry & partialbits_mask;
940  carry >>= word_num_partialbits;
941  bytep += byte_step;
942  index_in_word++;
943  }
944 
945  if (wordp == last_wordp)
946  break;
947 
948  wordp += word_step;
949  }
950  }
951 
952  return sign;
953 #undef FILL_DD
954 #undef TAKE_LOWBITS
955 }
956 
957 static size_t
958 integer_unpack_num_bdigits_small(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
959 {
960  /* nlp_bits stands for number of leading padding bits */
961  size_t num_bits = (wordsize * CHAR_BIT - nails) * numwords;
962  size_t num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG;
963  *nlp_bits_ret = (int)(num_bdigits * BITSPERDIG - num_bits);
964  return num_bdigits;
965 }
966 
967 static size_t
968 integer_unpack_num_bdigits_generic(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
969 {
970  /* BITSPERDIG = SIZEOF_BDIGIT * CHAR_BIT */
971  /* num_bits = (wordsize * CHAR_BIT - nails) * numwords */
972  /* num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG */
973 
974  /* num_bits = CHAR_BIT * (wordsize * numwords) - nails * numwords = CHAR_BIT * num_bytes1 - nails * numwords */
975  size_t num_bytes1 = wordsize * numwords;
976 
977  /* q1 * CHAR_BIT + r1 = numwords */
978  size_t q1 = numwords / CHAR_BIT;
979  size_t r1 = numwords % CHAR_BIT;
980 
981  /* num_bits = CHAR_BIT * num_bytes1 - nails * (q1 * CHAR_BIT + r1) = CHAR_BIT * num_bytes2 - nails * r1 */
982  size_t num_bytes2 = num_bytes1 - nails * q1;
983 
984  /* q2 * CHAR_BIT + r2 = nails */
985  size_t q2 = nails / CHAR_BIT;
986  size_t r2 = nails % CHAR_BIT;
987 
988  /* num_bits = CHAR_BIT * num_bytes2 - (q2 * CHAR_BIT + r2) * r1 = CHAR_BIT * num_bytes3 - r1 * r2 */
989  size_t num_bytes3 = num_bytes2 - q2 * r1;
990 
991  /* q3 * BITSPERDIG + r3 = num_bytes3 */
992  size_t q3 = num_bytes3 / BITSPERDIG;
993  size_t r3 = num_bytes3 % BITSPERDIG;
994 
995  /* num_bits = CHAR_BIT * (q3 * BITSPERDIG + r3) - r1 * r2 = BITSPERDIG * num_digits1 + CHAR_BIT * r3 - r1 * r2 */
996  size_t num_digits1 = CHAR_BIT * q3;
997 
998  /*
999  * if CHAR_BIT * r3 >= r1 * r2
1000  * CHAR_BIT * r3 - r1 * r2 = CHAR_BIT * BITSPERDIG - (CHAR_BIT * BITSPERDIG - (CHAR_BIT * r3 - r1 * r2))
1001  * q4 * BITSPERDIG + r4 = CHAR_BIT * BITSPERDIG - (CHAR_BIT * r3 - r1 * r2)
1002  * num_bits = BITSPERDIG * num_digits1 + CHAR_BIT * BITSPERDIG - (q4 * BITSPERDIG + r4) = BITSPERDIG * num_digits2 - r4
1003  * else
1004  * q4 * BITSPERDIG + r4 = -(CHAR_BIT * r3 - r1 * r2)
1005  * num_bits = BITSPERDIG * num_digits1 - (q4 * BITSPERDIG + r4) = BITSPERDIG * num_digits2 - r4
1006  * end
1007  */
1008 
1009  if (CHAR_BIT * r3 >= r1 * r2) {
1010  size_t tmp1 = CHAR_BIT * BITSPERDIG - (CHAR_BIT * r3 - r1 * r2);
1011  size_t q4 = tmp1 / BITSPERDIG;
1012  int r4 = (int)(tmp1 % BITSPERDIG);
1013  size_t num_digits2 = num_digits1 + CHAR_BIT - q4;
1014  *nlp_bits_ret = r4;
1015  return num_digits2;
1016  }
1017  else {
1018  size_t tmp1 = r1 * r2 - CHAR_BIT * r3;
1019  size_t q4 = tmp1 / BITSPERDIG;
1020  int r4 = (int)(tmp1 % BITSPERDIG);
1021  size_t num_digits2 = num_digits1 - q4;
1022  *nlp_bits_ret = r4;
1023  return num_digits2;
1024  }
1025 }
1026 
1027 static size_t
1028 integer_unpack_num_bdigits(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
1029 {
1030  size_t num_bdigits;
1031 
1032  if (numwords <= (SIZE_MAX - (BITSPERDIG-1)) / CHAR_BIT / wordsize) {
1033  num_bdigits = integer_unpack_num_bdigits_small(numwords, wordsize, nails, nlp_bits_ret);
1034 #ifdef DEBUG_INTEGER_PACK
1035  {
1036  int nlp_bits1;
1037  size_t num_bdigits1 = integer_unpack_num_bdigits_generic(numwords, wordsize, nails, &nlp_bits1);
1038  assert(num_bdigits == num_bdigits1);
1039  assert(*nlp_bits_ret == nlp_bits1);
1040  }
1041 #endif
1042  }
1043  else {
1044  num_bdigits = integer_unpack_num_bdigits_generic(numwords, wordsize, nails, nlp_bits_ret);
1045  }
1046  return num_bdigits;
1047 }
1048 
1049 static inline void
1050 integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in_dd_p, BDIGIT **dpp)
1051 {
1052  (*ddp) |= ((BDIGIT_DBL)data) << (*numbits_in_dd_p);
1053  *numbits_in_dd_p += numbits;
1054  while (BITSPERDIG <= *numbits_in_dd_p) {
1055  *(*dpp)++ = BIGLO(*ddp);
1056  *ddp = BIGDN(*ddp);
1057  *numbits_in_dd_p -= BITSPERDIG;
1058  }
1059 }
1060 
1061 static int
1063 {
1064  int sign;
1065  if (flags & INTEGER_PACK_2COMP) {
1066  sign = (flags & INTEGER_PACK_NEGATIVE) ?
1067  ((size == SIZEOF_BDIGIT && u == 0) ? -2 : -1) :
1068  ((u >> (size * CHAR_BIT - 1)) ? -1 : 1);
1069  if (sign < 0) {
1070  u |= LSHIFTX(BDIGMAX, size * CHAR_BIT);
1071  u = BIGLO(1 + ~u);
1072  }
1073  }
1074  else
1075  sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
1076  *dp = u;
1077  return sign;
1078 }
1079 
1080 static int
1081 bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags, int nlp_bits)
1082 {
1083  int sign;
1084  const unsigned char *buf = words;
1085  BDIGIT *dp;
1086  BDIGIT *de;
1087 
1088  dp = bdigits;
1089  de = dp + num_bdigits;
1090 
1092  if (nails == 0 && numwords == 1) {
1093  int need_swap = wordsize != 1 &&
1096  if (wordsize == 1) {
1097  return integer_unpack_single_bdigit(*(uint8_t *)buf, sizeof(uint8_t), flags, dp);
1098  }
1099 #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT
1100  if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
1101  uint16_t u = *(uint16_t *)buf;
1102  return integer_unpack_single_bdigit(need_swap ? swap16(u) : u, sizeof(uint16_t), flags, dp);
1103  }
1104 #endif
1105 #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT
1106  if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
1107  uint32_t u = *(uint32_t *)buf;
1108  return integer_unpack_single_bdigit(need_swap ? swap32(u) : u, sizeof(uint32_t), flags, dp);
1109  }
1110 #endif
1111 #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT
1112  if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
1113  uint64_t u = *(uint64_t *)buf;
1114  return integer_unpack_single_bdigit(need_swap ? swap64(u) : u, sizeof(uint64_t), flags, dp);
1115  }
1116 #endif
1117  }
1118 #if !defined(WORDS_BIGENDIAN)
1119  if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
1122  size_t src_size = numwords * wordsize;
1123  size_t dst_size = num_bdigits * SIZEOF_BDIGIT;
1124  MEMCPY(dp, words, char, src_size);
1125  if (flags & INTEGER_PACK_2COMP) {
1126  if (flags & INTEGER_PACK_NEGATIVE) {
1127  int zero_p;
1128  memset((char*)dp + src_size, 0xff, dst_size - src_size);
1129  zero_p = bary_2comp(dp, num_bdigits);
1130  sign = zero_p ? -2 : -1;
1131  }
1132  else if (buf[src_size-1] >> (CHAR_BIT-1)) {
1133  memset((char*)dp + src_size, 0xff, dst_size - src_size);
1134  bary_2comp(dp, num_bdigits);
1135  sign = -1;
1136  }
1137  else {
1138  MEMZERO((char*)dp + src_size, char, dst_size - src_size);
1139  sign = 1;
1140  }
1141  }
1142  else {
1143  MEMZERO((char*)dp + src_size, char, dst_size - src_size);
1144  sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
1145  }
1146  return sign;
1147  }
1148 #endif
1149  if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
1150  wordsize % SIZEOF_BDIGIT == 0) {
1151  size_t bdigits_per_word = wordsize / SIZEOF_BDIGIT;
1152  int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0;
1153  int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P :
1154  (flags & INTEGER_PACK_MSBYTE_FIRST) != 0;
1155  MEMCPY(dp, words, BDIGIT, numwords*bdigits_per_word);
1156  if (mswordfirst_p) {
1157  bary_swap(dp, num_bdigits);
1158  }
1159  if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
1160  size_t i;
1161  BDIGIT *p = dp;
1162  for (i = 0; i < numwords; i++) {
1163  bary_swap(p, bdigits_per_word);
1164  p += bdigits_per_word;
1165  }
1166  }
1167  if (msbytefirst_p != HOST_BIGENDIAN_P) {
1168  BDIGIT *p;
1169  for (p = dp; p < de; p++) {
1170  BDIGIT d = *p;
1171  *p = swap_bdigit(d);
1172  }
1173  }
1174  if (flags & INTEGER_PACK_2COMP) {
1175  if (flags & INTEGER_PACK_NEGATIVE) {
1176  int zero_p = bary_2comp(dp, num_bdigits);
1177  sign = zero_p ? -2 : -1;
1178  }
1179  else if (BDIGIT_MSB(de[-1])) {
1180  bary_2comp(dp, num_bdigits);
1181  sign = -1;
1182  }
1183  else {
1184  sign = 1;
1185  }
1186  }
1187  else {
1188  sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
1189  }
1190  return sign;
1191  }
1192  }
1193 
1194  if (num_bdigits != 0) {
1195  int word_num_partialbits;
1196  size_t word_num_fullbytes;
1197 
1198  ssize_t word_step;
1199  size_t byte_start;
1200  int byte_step;
1201 
1202  size_t word_start, word_last;
1203  const unsigned char *wordp, *last_wordp;
1204  BDIGIT_DBL dd;
1205  int numbits_in_dd;
1206 
1207  integer_pack_loop_setup(numwords, wordsize, nails, flags,
1208  &word_num_fullbytes, &word_num_partialbits,
1209  &word_start, &word_step, &word_last, &byte_start, &byte_step);
1210 
1211  wordp = buf + word_start;
1212  last_wordp = buf + word_last;
1213 
1214  dd = 0;
1215  numbits_in_dd = 0;
1216 
1217 #define PUSH_BITS(data, numbits) \
1218  integer_unpack_push_bits(data, numbits, &dd, &numbits_in_dd, &dp)
1219 
1220  while (1) {
1221  size_t index_in_word = 0;
1222  const unsigned char *bytep = wordp + byte_start;
1223  while (index_in_word < word_num_fullbytes) {
1224  PUSH_BITS(*bytep, CHAR_BIT);
1225  bytep += byte_step;
1226  index_in_word++;
1227  }
1228  if (word_num_partialbits) {
1229  PUSH_BITS(*bytep & ((1 << word_num_partialbits) - 1), word_num_partialbits);
1230  bytep += byte_step;
1231  index_in_word++;
1232  }
1233 
1234  if (wordp == last_wordp)
1235  break;
1236 
1237  wordp += word_step;
1238  }
1239  if (dd)
1240  *dp++ = (BDIGIT)dd;
1241  assert(dp <= de);
1242  while (dp < de)
1243  *dp++ = 0;
1244 #undef PUSH_BITS
1245  }
1246 
1247  if (!(flags & INTEGER_PACK_2COMP)) {
1248  sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
1249  }
1250  else {
1251  if (nlp_bits) {
1252  if ((flags & INTEGER_PACK_NEGATIVE) ||
1253  (bdigits[num_bdigits-1] >> (BITSPERDIG - nlp_bits - 1))) {
1254  bdigits[num_bdigits-1] |= BIGLO(BDIGMAX << (BITSPERDIG - nlp_bits));
1255  sign = -1;
1256  }
1257  else {
1258  sign = 1;
1259  }
1260  }
1261  else {
1262  if (flags & INTEGER_PACK_NEGATIVE) {
1263  sign = bary_zero_p(bdigits, num_bdigits) ? -2 : -1;
1264  }
1265  else {
1266  if (num_bdigits != 0 && BDIGIT_MSB(bdigits[num_bdigits-1]))
1267  sign = -1;
1268  else
1269  sign = 1;
1270  }
1271  }
1272  if (sign == -1 && num_bdigits != 0) {
1273  bary_2comp(bdigits, num_bdigits);
1274  }
1275  }
1276 
1277  return sign;
1278 }
1279 
1280 static void
1281 bary_unpack(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
1282 {
1283  size_t num_bdigits0;
1284  int nlp_bits;
1285  int sign;
1286 
1287  validate_integer_pack_format(numwords, wordsize, nails, flags,
1297 
1298  num_bdigits0 = integer_unpack_num_bdigits(numwords, wordsize, nails, &nlp_bits);
1299 
1300  assert(num_bdigits0 <= num_bdigits);
1301 
1302  sign = bary_unpack_internal(bdigits, num_bdigits0, words, numwords, wordsize, nails, flags, nlp_bits);
1303 
1304  if (num_bdigits0 < num_bdigits) {
1305  BDIGITS_ZERO(bdigits + num_bdigits0, num_bdigits - num_bdigits0);
1306  if (sign == -2) {
1307  bdigits[num_bdigits0] = 1;
1308  }
1309  }
1310 }
1311 
1312 static int
1313 bary_subb(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, int borrow)
1314 {
1315  BDIGIT_DBL_SIGNED num;
1316  size_t i;
1317  size_t sn;
1318 
1319  assert(xn <= zn);
1320  assert(yn <= zn);
1321 
1322  sn = xn < yn ? xn : yn;
1323 
1324  num = borrow ? -1 : 0;
1325  for (i = 0; i < sn; i++) {
1326  num += (BDIGIT_DBL_SIGNED)xds[i] - yds[i];
1327  zds[i] = BIGLO(num);
1328  num = BIGDN(num);
1329  }
1330  if (yn <= xn) {
1331  for (; i < xn; i++) {
1332  if (num == 0) goto num_is_zero;
1333  num += xds[i];
1334  zds[i] = BIGLO(num);
1335  num = BIGDN(num);
1336  }
1337  }
1338  else {
1339  for (; i < yn; i++) {
1340  num -= yds[i];
1341  zds[i] = BIGLO(num);
1342  num = BIGDN(num);
1343  }
1344  }
1345  if (num == 0) goto num_is_zero;
1346  for (; i < zn; i++) {
1347  zds[i] = BDIGMAX;
1348  }
1349  return 1;
1350 
1351  num_is_zero:
1352  if (xds == zds && xn == zn)
1353  return 0;
1354  for (; i < xn; i++) {
1355  zds[i] = xds[i];
1356  }
1357  for (; i < zn; i++) {
1358  zds[i] = 0;
1359  }
1360  return 0;
1361 }
1362 
1363 static int
1364 bary_sub(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
1365 {
1366  return bary_subb(zds, zn, xds, xn, yds, yn, 0);
1367 }
1368 
1369 static int
1370 bary_sub_one(BDIGIT *zds, size_t zn)
1371 {
1372  return bary_subb(zds, zn, zds, zn, NULL, 0, 1);
1373 }
1374 
1375 static int
1376 bary_addc(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, int carry)
1377 {
1378  BDIGIT_DBL num;
1379  size_t i;
1380 
1381  assert(xn <= zn);
1382  assert(yn <= zn);
1383 
1384  if (xn > yn) {
1385  const BDIGIT *tds;
1386  tds = xds; xds = yds; yds = tds;
1387  i = xn; xn = yn; yn = i;
1388  }
1389 
1390  num = carry ? 1 : 0;
1391  for (i = 0; i < xn; i++) {
1392  num += (BDIGIT_DBL)xds[i] + yds[i];
1393  zds[i] = BIGLO(num);
1394  num = BIGDN(num);
1395  }
1396  for (; i < yn; i++) {
1397  if (num == 0) goto num_is_zero;
1398  num += yds[i];
1399  zds[i] = BIGLO(num);
1400  num = BIGDN(num);
1401  }
1402  for (; i < zn; i++) {
1403  if (num == 0) goto num_is_zero;
1404  zds[i] = BIGLO(num);
1405  num = BIGDN(num);
1406  }
1407  return num != 0;
1408 
1409  num_is_zero:
1410  if (yds == zds && yn == zn)
1411  return 0;
1412  for (; i < yn; i++) {
1413  zds[i] = yds[i];
1414  }
1415  for (; i < zn; i++) {
1416  zds[i] = 0;
1417  }
1418  return 0;
1419 }
1420 
1421 static int
1422 bary_add(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
1423 {
1424  return bary_addc(zds, zn, xds, xn, yds, yn, 0);
1425 }
1426 
1427 static int
1428 bary_add_one(BDIGIT *ds, size_t n)
1429 {
1430  size_t i;
1431  for (i = 0; i < n; i++) {
1432  ds[i] = BIGLO(ds[i]+1);
1433  if (ds[i] != 0)
1434  return 0;
1435  }
1436  return 1;
1437 }
1438 
1439 static void
1440 bary_mul_single(BDIGIT *zds, size_t zn, BDIGIT x, BDIGIT y)
1441 {
1442  BDIGIT_DBL n;
1443 
1444  assert(2 <= zn);
1445 
1446  n = (BDIGIT_DBL)x * y;
1447  bdigitdbl2bary(zds, 2, n);
1448  BDIGITS_ZERO(zds + 2, zn - 2);
1449 }
1450 
1451 static int
1452 bary_muladd_1xN(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
1453 {
1454  BDIGIT_DBL n;
1455  BDIGIT_DBL dd;
1456  size_t j;
1457 
1458  assert(zn > yn);
1459 
1460  if (x == 0)
1461  return 0;
1462  dd = x;
1463  n = 0;
1464  for (j = 0; j < yn; j++) {
1465  BDIGIT_DBL ee = n + dd * yds[j];
1466  if (ee) {
1467  n = zds[j] + ee;
1468  zds[j] = BIGLO(n);
1469  n = BIGDN(n);
1470  }
1471  else {
1472  n = 0;
1473  }
1474 
1475  }
1476  for (; j < zn; j++) {
1477  if (n == 0)
1478  break;
1479  n += zds[j];
1480  zds[j] = BIGLO(n);
1481  n = BIGDN(n);
1482  }
1483  return n != 0;
1484 }
1485 
1486 static BDIGIT_DBL_SIGNED
1487 bigdivrem_mulsub(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
1488 {
1489  size_t i;
1490  BDIGIT_DBL t2;
1491  BDIGIT_DBL_SIGNED num;
1492 
1493  assert(zn == yn + 1);
1494 
1495  num = 0;
1496  t2 = 0;
1497  i = 0;
1498 
1499  do {
1500  BDIGIT_DBL ee;
1501  t2 += (BDIGIT_DBL)yds[i] * x;
1502  ee = num - BIGLO(t2);
1503  num = (BDIGIT_DBL)zds[i] + ee;
1504  if (ee) zds[i] = BIGLO(num);
1505  num = BIGDN(num);
1506  t2 = BIGDN(t2);
1507  } while (++i < yn);
1508  num += zds[i] - t2; /* borrow from high digit; don't update */
1509  return num;
1510 }
1511 
1512 static int
1513 bary_mulsub_1xN(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
1514 {
1515  BDIGIT_DBL_SIGNED num;
1516 
1517  assert(zn == yn + 1);
1518 
1519  num = bigdivrem_mulsub(zds, zn, x, yds, yn);
1520  zds[yn] = BIGLO(num);
1521  if (BIGDN(num))
1522  return 1;
1523  return 0;
1524 }
1525 
1526 static void
1527 bary_mul_normal(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
1528 {
1529  size_t i;
1530 
1531  assert(xn + yn <= zn);
1532 
1533  BDIGITS_ZERO(zds, zn);
1534  for (i = 0; i < xn; i++) {
1535  bary_muladd_1xN(zds+i, zn-i, xds[i], yds, yn);
1536  }
1537 }
1538 
1539 VALUE
1541 {
1542  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), zn = xn + yn;
1543  VALUE z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
1544  bary_mul_normal(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn);
1545  RB_GC_GUARD(x);
1546  RB_GC_GUARD(y);
1547  return z;
1548 }
1549 
1550 /* efficient squaring (2 times faster than normal multiplication)
1551  * ref: Handbook of Applied Cryptography, Algorithm 14.16
1552  * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1553  */
1554 static void
1555 bary_sq_fast(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn)
1556 {
1557  size_t i, j;
1558  BDIGIT_DBL c, v, w;
1559  BDIGIT vl;
1560  int vh;
1561 
1562  assert(xn * 2 <= zn);
1563 
1564  BDIGITS_ZERO(zds, zn);
1565 
1566  if (xn == 0)
1567  return;
1568 
1569  for (i = 0; i < xn-1; i++) {
1570  v = (BDIGIT_DBL)xds[i];
1571  if (!v)
1572  continue;
1573  c = (BDIGIT_DBL)zds[i + i] + v * v;
1574  zds[i + i] = BIGLO(c);
1575  c = BIGDN(c);
1576  v *= 2;
1577  vl = BIGLO(v);
1578  vh = (int)BIGDN(v);
1579  for (j = i + 1; j < xn; j++) {
1580  w = (BDIGIT_DBL)xds[j];
1581  c += (BDIGIT_DBL)zds[i + j] + vl * w;
1582  zds[i + j] = BIGLO(c);
1583  c = BIGDN(c);
1584  if (vh)
1585  c += w;
1586  }
1587  if (c) {
1588  c += (BDIGIT_DBL)zds[i + xn];
1589  zds[i + xn] = BIGLO(c);
1590  c = BIGDN(c);
1591  if (c)
1592  zds[i + xn + 1] += (BDIGIT)c;
1593  }
1594  }
1595 
1596  /* i == xn-1 */
1597  v = (BDIGIT_DBL)xds[i];
1598  if (!v)
1599  return;
1600  c = (BDIGIT_DBL)zds[i + i] + v * v;
1601  zds[i + i] = BIGLO(c);
1602  c = BIGDN(c);
1603  if (c) {
1604  zds[i + xn] += BIGLO(c);
1605  }
1606 }
1607 
1608 VALUE
1610 {
1611  size_t xn = BIGNUM_LEN(x), zn = 2 * xn;
1612  VALUE z = bignew(zn, 1);
1613  bary_sq_fast(BDIGITS(z), zn, BDIGITS(x), xn);
1614  RB_GC_GUARD(x);
1615  return z;
1616 }
1617 
1618 /* balancing multiplication by slicing larger argument */
1619 static void
1620 bary_mul_balance_with_mulfunc(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn, mulfunc_t *mulfunc)
1621 {
1622  VALUE work = 0;
1623  size_t yn0 = yn;
1624  size_t r, n;
1625 
1626  assert(xn + yn <= zn);
1627  assert(xn <= yn);
1628  assert(!KARATSUBA_BALANCED(xn, yn) || !TOOM3_BALANCED(xn, yn));
1629 
1630  BDIGITS_ZERO(zds, xn);
1631 
1632  n = 0;
1633  while (yn > 0) {
1634  BDIGIT *tds;
1635  size_t tn;
1636  r = xn > yn ? yn : xn;
1637  tn = xn + r;
1638  if (2 * (xn + r) <= zn - n) {
1639  tds = zds + n + xn + r;
1640  mulfunc(tds, tn, xds, xn, yds + n, r, wds, wn);
1641  BDIGITS_ZERO(zds + n + xn, r);
1642  bary_add(zds + n, tn,
1643  zds + n, tn,
1644  tds, tn);
1645  }
1646  else {
1647  if (wn < xn) {
1648  wn = xn;
1649  wds = ALLOCV_N(BDIGIT, work, wn);
1650  }
1651  tds = zds + n;
1652  MEMCPY(wds, zds + n, BDIGIT, xn);
1653  mulfunc(tds, tn, xds, xn, yds + n, r, wds+xn, wn-xn);
1654  bary_add(zds + n, tn,
1655  zds + n, tn,
1656  wds, xn);
1657  }
1658  yn -= r;
1659  n += r;
1660  }
1661  BDIGITS_ZERO(zds+xn+yn0, zn - (xn+yn0));
1662 
1663  if (work)
1664  ALLOCV_END(work);
1665 }
1666 
1667 VALUE
1669 {
1670  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), zn = xn + yn;
1671  VALUE z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
1672  bary_mul_balance_with_mulfunc(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn, NULL, 0, bary_mul_toom3_start);
1673  RB_GC_GUARD(x);
1674  RB_GC_GUARD(y);
1675  return z;
1676 }
1677 
1678 /* multiplication by karatsuba method */
1679 static void
1680 bary_mul_karatsuba(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
1681 {
1682  VALUE work = 0;
1683 
1684  size_t n;
1685  int sub_p, borrow, carry1, carry2, carry3;
1686 
1687  int odd_y = 0;
1688  int odd_xy = 0;
1689  int sq;
1690 
1691  const BDIGIT *xds0, *xds1, *yds0, *yds1;
1692  BDIGIT *zds0, *zds1, *zds2, *zds3;
1693 
1694  assert(xn + yn <= zn);
1695  assert(xn <= yn);
1696  assert(yn < 2 * xn);
1697 
1698  sq = xds == yds && xn == yn;
1699 
1700  if (yn & 1) {
1701  odd_y = 1;
1702  yn--;
1703  if (yn < xn) {
1704  odd_xy = 1;
1705  xn--;
1706  }
1707  }
1708 
1709  n = yn / 2;
1710 
1711  assert(n < xn);
1712 
1713  if (wn < n) {
1714  /* This function itself needs only n BDIGITs for work area.
1715  * However this function calls bary_mul_karatsuba and
1716  * bary_mul_balance recursively.
1717  * 2n BDIGITs are enough to avoid allocations in
1718  * the recursively called functions.
1719  */
1720  wn = 2*n;
1721  wds = ALLOCV_N(BDIGIT, work, wn);
1722  }
1723 
1724  /* Karatsuba algorithm:
1725  *
1726  * x = x0 + r*x1
1727  * y = y0 + r*y1
1728  * z = x*y
1729  * = (x0 + r*x1) * (y0 + r*y1)
1730  * = x0*y0 + r*(x1*y0 + x0*y1) + r*r*x1*y1
1731  * = x0*y0 + r*(x0*y0 + x1*y1 - (x1-x0)*(y1-y0)) + r*r*x1*y1
1732  * = x0*y0 + r*(x0*y0 + x1*y1 - (x0-x1)*(y0-y1)) + r*r*x1*y1
1733  */
1734 
1735  xds0 = xds;
1736  xds1 = xds + n;
1737  yds0 = yds;
1738  yds1 = yds + n;
1739  zds0 = zds;
1740  zds1 = zds + n;
1741  zds2 = zds + 2*n;
1742  zds3 = zds + 3*n;
1743 
1744  sub_p = 1;
1745 
1746  /* zds0:? zds1:? zds2:? zds3:? wds:? */
1747 
1748  if (bary_sub(zds0, n, xds, n, xds+n, xn-n)) {
1749  bary_2comp(zds0, n);
1750  sub_p = !sub_p;
1751  }
1752 
1753  /* zds0:|x1-x0| zds1:? zds2:? zds3:? wds:? */
1754 
1755  if (sq) {
1756  sub_p = 1;
1757  bary_mul_karatsuba_start(zds1, 2*n, zds0, n, zds0, n, wds, wn);
1758  }
1759  else {
1760  if (bary_sub(wds, n, yds, n, yds+n, n)) {
1761  bary_2comp(wds, n);
1762  sub_p = !sub_p;
1763  }
1764 
1765  /* zds0:|x1-x0| zds1:? zds2:? zds3:? wds:|y1-y0| */
1766 
1767  bary_mul_karatsuba_start(zds1, 2*n, zds0, n, wds, n, wds+n, wn-n);
1768  }
1769 
1770  /* zds0:|x1-x0| zds1,zds2:|x1-x0|*|y1-y0| zds3:? wds:|y1-y0| */
1771 
1772  borrow = 0;
1773  if (sub_p) {
1774  borrow = !bary_2comp(zds1, 2*n);
1775  }
1776  /* zds0:|x1-x0| zds1,zds2:-?|x1-x0|*|y1-y0| zds3:? wds:|y1-y0| */
1777 
1778  MEMCPY(wds, zds1, BDIGIT, n);
1779 
1780  /* zds0:|x1-x0| zds1,zds2:-?|x1-x0|*|y1-y0| zds3:? wds:lo(-?|x1-x0|*|y1-y0|) */
1781 
1782  bary_mul_karatsuba_start(zds0, 2*n, xds0, n, yds0, n, wds+n, wn-n);
1783 
1784  /* zds0,zds1:x0*y0 zds2:hi(-?|x1-x0|*|y1-y0|) zds3:? wds:lo(-?|x1-x0|*|y1-y0|) */
1785 
1786  carry1 = bary_add(wds, n, wds, n, zds0, n);
1787  carry1 = bary_addc(zds2, n, zds2, n, zds1, n, carry1);
1788 
1789  /* zds0,zds1:x0*y0 zds2:hi(x0*y0-?|x1-x0|*|y1-y0|) zds3:? wds:lo(x0*y0-?|x1-x0|*|y1-y0|) */
1790 
1791  carry2 = bary_add(zds1, n, zds1, n, wds, n);
1792 
1793  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|) zds2:hi(x0*y0-?|x1-x0|*|y1-y0|) zds3:? wds:lo(x0*y0-?|x1-x0|*|y1-y0|) */
1794 
1795  MEMCPY(wds, zds2, BDIGIT, n);
1796 
1797  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|) zds2:_ zds3:? wds:hi(x0*y0-?|x1-x0|*|y1-y0|) */
1798 
1799  bary_mul_karatsuba_start(zds2, zn-2*n, xds1, xn-n, yds1, n, wds+n, wn-n);
1800 
1801  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|) zds2,zds3:x1*y1 wds:hi(x0*y0-?|x1-x0|*|y1-y0|) */
1802 
1803  carry3 = bary_add(zds1, n, zds1, n, zds2, n);
1804 
1805  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|)+lo(x1*y1) zds2,zds3:x1*y1 wds:hi(x0*y0-?|x1-x0|*|y1-y0|) */
1806 
1807  carry3 = bary_addc(zds2, n, zds2, n, zds3, (4*n < zn ? n : zn-3*n), carry3);
1808 
1809  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|)+lo(x1*y1) zds2,zds3:x1*y1+hi(x1*y1) wds:hi(x0*y0-?|x1-x0|*|y1-y0|) */
1810 
1811  bary_add(zds2, zn-2*n, zds2, zn-2*n, wds, n);
1812 
1813  /* zds0:lo(x0*y0) zds1:hi(x0*y0)+lo(x0*y0-?|x1-x0|*|y1-y0|)+lo(x1*y1) zds2,zds3:x1*y1+hi(x1*y1)+hi(x0*y0-?|x1-x0|*|y1-y0|) wds:_ */
1814 
1815  if (carry2)
1816  bary_add_one(zds2, zn-2*n);
1817 
1818  if (carry1 + carry3 - borrow < 0)
1819  bary_sub_one(zds3, zn-3*n);
1820  else if (carry1 + carry3 - borrow > 0) {
1821  BDIGIT c = carry1 + carry3 - borrow;
1822  bary_add(zds3, zn-3*n, zds3, zn-3*n, &c, 1);
1823  }
1824 
1825  /*
1826  if (SIZEOF_BDIGIT * zn <= 16) {
1827  uint128_t z, x, y;
1828  ssize_t i;
1829  for (x = 0, i = xn-1; 0 <= i; i--) { x <<= SIZEOF_BDIGIT*CHAR_BIT; x |= xds[i]; }
1830  for (y = 0, i = yn-1; 0 <= i; i--) { y <<= SIZEOF_BDIGIT*CHAR_BIT; y |= yds[i]; }
1831  for (z = 0, i = zn-1; 0 <= i; i--) { z <<= SIZEOF_BDIGIT*CHAR_BIT; z |= zds[i]; }
1832  assert(z == x * y);
1833  }
1834  */
1835 
1836  if (odd_xy) {
1837  bary_muladd_1xN(zds+yn, zn-yn, yds[yn], xds, xn);
1838  bary_muladd_1xN(zds+xn, zn-xn, xds[xn], yds, yn+1);
1839  }
1840  else if (odd_y) {
1841  bary_muladd_1xN(zds+yn, zn-yn, yds[yn], xds, xn);
1842  }
1843 
1844  if (work)
1845  ALLOCV_END(work);
1846 }
1847 
1848 VALUE
1850 {
1851  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), zn = xn + yn;
1852  VALUE z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
1853  if (!((xn <= yn && yn < 2) || KARATSUBA_BALANCED(xn, yn)))
1854  rb_raise(rb_eArgError, "unexpected bignum length for karatsuba");
1855  bary_mul_karatsuba(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn, NULL, 0);
1856  RB_GC_GUARD(x);
1857  RB_GC_GUARD(y);
1858  return z;
1859 }
1860 
1861 static void
1862 bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
1863 {
1864  size_t n;
1865  size_t wnc;
1866  VALUE work = 0;
1867 
1868  /* "p" stands for "positive". Actually it means "non-negative", though. */
1869  size_t x0n; const BDIGIT *x0ds;
1870  size_t x1n; const BDIGIT *x1ds;
1871  size_t x2n; const BDIGIT *x2ds;
1872  size_t y0n; const BDIGIT *y0ds;
1873  size_t y1n; const BDIGIT *y1ds;
1874  size_t y2n; const BDIGIT *y2ds;
1875 
1876  size_t u1n; BDIGIT *u1ds; int u1p;
1877  size_t u2n; BDIGIT *u2ds; int u2p;
1878  size_t u3n; BDIGIT *u3ds; int u3p;
1879 
1880  size_t v1n; BDIGIT *v1ds; int v1p;
1881  size_t v2n; BDIGIT *v2ds; int v2p;
1882  size_t v3n; BDIGIT *v3ds; int v3p;
1883 
1884  size_t t0n; BDIGIT *t0ds; int t0p;
1885  size_t t1n; BDIGIT *t1ds; int t1p;
1886  size_t t2n; BDIGIT *t2ds; int t2p;
1887  size_t t3n; BDIGIT *t3ds; int t3p;
1888  size_t t4n; BDIGIT *t4ds; int t4p;
1889 
1890  size_t z0n; BDIGIT *z0ds;
1891  size_t z1n; BDIGIT *z1ds; int z1p;
1892  size_t z2n; BDIGIT *z2ds; int z2p;
1893  size_t z3n; BDIGIT *z3ds; int z3p;
1894  size_t z4n; BDIGIT *z4ds;
1895 
1896  size_t zzn; BDIGIT *zzds;
1897 
1898  int sq = xds == yds && xn == yn;
1899 
1900  assert(xn <= yn); /* assume y >= x */
1901  assert(xn + yn <= zn);
1902 
1903  n = (yn + 2) / 3;
1904  assert(2*n < xn);
1905 
1906  wnc = 0;
1907 
1908  wnc += (u1n = n+1); /* BITSPERDIG*n+2 bits */
1909  wnc += (u2n = n+1); /* BITSPERDIG*n+1 bits */
1910  wnc += (u3n = n+1); /* BITSPERDIG*n+3 bits */
1911  wnc += (v1n = n+1); /* BITSPERDIG*n+2 bits */
1912  wnc += (v2n = n+1); /* BITSPERDIG*n+1 bits */
1913  wnc += (v3n = n+1); /* BITSPERDIG*n+3 bits */
1914 
1915  wnc += (t0n = 2*n); /* BITSPERDIG*2*n bits */
1916  wnc += (t1n = 2*n+2); /* BITSPERDIG*2*n+4 bits but bary_mul needs u1n+v1n */
1917  wnc += (t2n = 2*n+2); /* BITSPERDIG*2*n+2 bits but bary_mul needs u2n+v2n */
1918  wnc += (t3n = 2*n+2); /* BITSPERDIG*2*n+6 bits but bary_mul needs u3n+v3n */
1919  wnc += (t4n = 2*n); /* BITSPERDIG*2*n bits */
1920 
1921  wnc += (z1n = 2*n+1); /* BITSPERDIG*2*n+5 bits */
1922  wnc += (z2n = 2*n+1); /* BITSPERDIG*2*n+6 bits */
1923  wnc += (z3n = 2*n+1); /* BITSPERDIG*2*n+8 bits */
1924 
1925  if (wn < wnc) {
1926  wn = wnc * 3 / 2; /* Allocate working memory for whole recursion at once. */
1927  wds = ALLOCV_N(BDIGIT, work, wn);
1928  }
1929 
1930  u1ds = wds; wds += u1n;
1931  u2ds = wds; wds += u2n;
1932  u3ds = wds; wds += u3n;
1933 
1934  v1ds = wds; wds += v1n;
1935  v2ds = wds; wds += v2n;
1936  v3ds = wds; wds += v3n;
1937 
1938  t0ds = wds; wds += t0n;
1939  t1ds = wds; wds += t1n;
1940  t2ds = wds; wds += t2n;
1941  t3ds = wds; wds += t3n;
1942  t4ds = wds; wds += t4n;
1943 
1944  z1ds = wds; wds += z1n;
1945  z2ds = wds; wds += z2n;
1946  z3ds = wds; wds += z3n;
1947 
1948  wn -= wnc;
1949 
1950  zzds = u1ds;
1951  zzn = 6*n+1;
1952 
1953  x0n = n;
1954  x1n = n;
1955  x2n = xn - 2*n;
1956  x0ds = xds;
1957  x1ds = xds + n;
1958  x2ds = xds + 2*n;
1959 
1960  if (sq) {
1961  y0n = x0n;
1962  y1n = x1n;
1963  y2n = x2n;
1964  y0ds = x0ds;
1965  y1ds = x1ds;
1966  y2ds = x2ds;
1967  }
1968  else {
1969  y0n = n;
1970  y1n = n;
1971  y2n = yn - 2*n;
1972  y0ds = yds;
1973  y1ds = yds + n;
1974  y2ds = yds + 2*n;
1975  }
1976 
1977  /*
1978  * ref. http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
1979  *
1980  * x(b) = x0 * b^0 + x1 * b^1 + x2 * b^2
1981  * y(b) = y0 * b^0 + y1 * b^1 + y2 * b^2
1982  *
1983  * z(b) = x(b) * y(b)
1984  * z(b) = z0 * b^0 + z1 * b^1 + z2 * b^2 + z3 * b^3 + z4 * b^4
1985  * where:
1986  * z0 = x0 * y0
1987  * z1 = x0 * y1 + x1 * y0
1988  * z2 = x0 * y2 + x1 * y1 + x2 * y0
1989  * z3 = x1 * y2 + x2 * y1
1990  * z4 = x2 * y2
1991  *
1992  * Toom3 method (a.k.a. Toom-Cook method):
1993  * (Step1) calculating 5 points z(b0), z(b1), z(b2), z(b3), z(b4),
1994  * where:
1995  * b0 = 0, b1 = 1, b2 = -1, b3 = -2, b4 = inf,
1996  * z(0) = x(0) * y(0) = x0 * y0
1997  * z(1) = x(1) * y(1) = (x0 + x1 + x2) * (y0 + y1 + y2)
1998  * z(-1) = x(-1) * y(-1) = (x0 - x1 + x2) * (y0 - y1 + y2)
1999  * z(-2) = x(-2) * y(-2) = (x0 - 2 * (x1 - 2 * x2)) * (y0 - 2 * (y1 - 2 * y2))
2000  * z(inf) = x(inf) * y(inf) = x2 * y2
2001  *
2002  * (Step2) interpolating z0, z1, z2, z3 and z4.
2003  *
2004  * (Step3) Substituting base value into b of the polynomial z(b),
2005  */
2006 
2007  /*
2008  * [Step1] calculating 5 points z(b0), z(b1), z(b2), z(b3), z(b4)
2009  */
2010 
2011  /* u1 <- x0 + x2 */
2012  bary_add(u1ds, u1n, x0ds, x0n, x2ds, x2n);
2013  u1p = 1;
2014 
2015  /* x(-1) : u2 <- u1 - x1 = x0 - x1 + x2 */
2016  if (bary_sub(u2ds, u2n, u1ds, u1n, x1ds, x1n)) {
2017  bary_2comp(u2ds, u2n);
2018  u2p = 0;
2019  }
2020  else {
2021  u2p = 1;
2022  }
2023 
2024  /* x(1) : u1 <- u1 + x1 = x0 + x1 + x2 */
2025  bary_add(u1ds, u1n, u1ds, u1n, x1ds, x1n);
2026 
2027  /* x(-2) : u3 <- 2 * (u2 + x2) - x0 = x0 - 2 * (x1 - 2 * x2) */
2028  u3p = 1;
2029  if (u2p) {
2030  bary_add(u3ds, u3n, u2ds, u2n, x2ds, x2n);
2031  }
2032  else if (bary_sub(u3ds, u3n, x2ds, x2n, u2ds, u2n)) {
2033  bary_2comp(u3ds, u3n);
2034  u3p = 0;
2035  }
2036  bary_small_lshift(u3ds, u3ds, u3n, 1);
2037  if (!u3p) {
2038  bary_add(u3ds, u3n, u3ds, u3n, x0ds, x0n);
2039  }
2040  else if (bary_sub(u3ds, u3n, u3ds, u3n, x0ds, x0n)) {
2041  bary_2comp(u3ds, u3n);
2042  u3p = 0;
2043  }
2044 
2045  if (sq) {
2046  v1n = u1n; v1ds = u1ds; v1p = u1p;
2047  v2n = u2n; v2ds = u2ds; v2p = u2p;
2048  v3n = u3n; v3ds = u3ds; v3p = u3p;
2049  }
2050  else {
2051  /* v1 <- y0 + y2 */
2052  bary_add(v1ds, v1n, y0ds, y0n, y2ds, y2n);
2053  v1p = 1;
2054 
2055  /* y(-1) : v2 <- v1 - y1 = y0 - y1 + y2 */
2056  v2p = 1;
2057  if (bary_sub(v2ds, v2n, v1ds, v1n, y1ds, y1n)) {
2058  bary_2comp(v2ds, v2n);
2059  v2p = 0;
2060  }
2061 
2062  /* y(1) : v1 <- v1 + y1 = y0 + y1 + y2 */
2063  bary_add(v1ds, v1n, v1ds, v1n, y1ds, y1n);
2064 
2065  /* y(-2) : v3 <- 2 * (v2 + y2) - y0 = y0 - 2 * (y1 - 2 * y2) */
2066  v3p = 1;
2067  if (v2p) {
2068  bary_add(v3ds, v3n, v2ds, v2n, y2ds, y2n);
2069  }
2070  else if (bary_sub(v3ds, v3n, y2ds, y2n, v2ds, v2n)) {
2071  bary_2comp(v3ds, v3n);
2072  v3p = 0;
2073  }
2074  bary_small_lshift(v3ds, v3ds, v3n, 1);
2075  if (!v3p) {
2076  bary_add(v3ds, v3n, v3ds, v3n, y0ds, y0n);
2077  }
2078  else if (bary_sub(v3ds, v3n, v3ds, v3n, y0ds, y0n)) {
2079  bary_2comp(v3ds, v3n);
2080  v3p = 0;
2081  }
2082  }
2083 
2084  /* z(0) : t0 <- x0 * y0 */
2085  bary_mul_toom3_start(t0ds, t0n, x0ds, x0n, y0ds, y0n, wds, wn);
2086  t0p = 1;
2087 
2088  /* z(1) : t1 <- u1 * v1 */
2089  bary_mul_toom3_start(t1ds, t1n, u1ds, u1n, v1ds, v1n, wds, wn);
2090  t1p = u1p == v1p;
2091  assert(t1ds[t1n-1] == 0);
2092  t1n--;
2093 
2094  /* z(-1) : t2 <- u2 * v2 */
2095  bary_mul_toom3_start(t2ds, t2n, u2ds, u2n, v2ds, v2n, wds, wn);
2096  t2p = u2p == v2p;
2097  assert(t2ds[t2n-1] == 0);
2098  t2n--;
2099 
2100  /* z(-2) : t3 <- u3 * v3 */
2101  bary_mul_toom3_start(t3ds, t3n, u3ds, u3n, v3ds, v3n, wds, wn);
2102  t3p = u3p == v3p;
2103  assert(t3ds[t3n-1] == 0);
2104  t3n--;
2105 
2106  /* z(inf) : t4 <- x2 * y2 */
2107  bary_mul_toom3_start(t4ds, t4n, x2ds, x2n, y2ds, y2n, wds, wn);
2108  t4p = 1;
2109 
2110  /*
2111  * [Step2] interpolating z0, z1, z2, z3 and z4.
2112  */
2113 
2114  /* z0 <- z(0) == t0 */
2115  z0n = t0n; z0ds = t0ds;
2116 
2117  /* z4 <- z(inf) == t4 */
2118  z4n = t4n; z4ds = t4ds;
2119 
2120  /* z3 <- (z(-2) - z(1)) / 3 == (t3 - t1) / 3 */
2121  if (t3p == t1p) {
2122  z3p = t3p;
2123  if (bary_sub(z3ds, z3n, t3ds, t3n, t1ds, t1n)) {
2124  bary_2comp(z3ds, z3n);
2125  z3p = !z3p;
2126  }
2127  }
2128  else {
2129  z3p = t3p;
2130  bary_add(z3ds, z3n, t3ds, t3n, t1ds, t1n);
2131  }
2132  bigdivrem_single(z3ds, z3ds, z3n, 3);
2133 
2134  /* z1 <- (z(1) - z(-1)) / 2 == (t1 - t2) / 2 */
2135  if (t1p == t2p) {
2136  z1p = t1p;
2137  if (bary_sub(z1ds, z1n, t1ds, t1n, t2ds, t2n)) {
2138  bary_2comp(z1ds, z1n);
2139  z1p = !z1p;
2140  }
2141  }
2142  else {
2143  z1p = t1p;
2144  bary_add(z1ds, z1n, t1ds, t1n, t2ds, t2n);
2145  }
2146  bary_small_rshift(z1ds, z1ds, z1n, 1, 0);
2147 
2148  /* z2 <- z(-1) - z(0) == t2 - t0 */
2149  if (t2p == t0p) {
2150  z2p = t2p;
2151  if (bary_sub(z2ds, z2n, t2ds, t2n, t0ds, t0n)) {
2152  bary_2comp(z2ds, z2n);
2153  z2p = !z2p;
2154  }
2155  }
2156  else {
2157  z2p = t2p;
2158  bary_add(z2ds, z2n, t2ds, t2n, t0ds, t0n);
2159  }
2160 
2161  /* z3 <- (z2 - z3) / 2 + 2 * z(inf) == (z2 - z3) / 2 + 2 * t4 */
2162  if (z2p == z3p) {
2163  z3p = z2p;
2164  if (bary_sub(z3ds, z3n, z2ds, z2n, z3ds, z3n)) {
2165  bary_2comp(z3ds, z3n);
2166  z3p = !z3p;
2167  }
2168  }
2169  else {
2170  z3p = z2p;
2171  bary_add(z3ds, z3n, z2ds, z2n, z3ds, z3n);
2172  }
2173  bary_small_rshift(z3ds, z3ds, z3n, 1, 0);
2174  if (z3p == t4p) {
2175  bary_muladd_1xN(z3ds, z3n, 2, t4ds, t4n);
2176  }
2177  else {
2178  if (bary_mulsub_1xN(z3ds, z3n, 2, t4ds, t4n)) {
2179  bary_2comp(z3ds, z3n);
2180  z3p = !z3p;
2181  }
2182  }
2183 
2184  /* z2 <- z2 + z1 - z(inf) == z2 + z1 - t4 */
2185  if (z2p == z1p) {
2186  bary_add(z2ds, z2n, z2ds, z2n, z1ds, z1n);
2187  }
2188  else {
2189  if (bary_sub(z2ds, z2n, z2ds, z2n, z1ds, z1n)) {
2190  bary_2comp(z2ds, z2n);
2191  z2p = !z2p;
2192  }
2193  }
2194 
2195  if (z2p == t4p) {
2196  if (bary_sub(z2ds, z2n, z2ds, z2n, t4ds, t4n)) {
2197  bary_2comp(z2ds, z2n);
2198  z2p = !z2p;
2199  }
2200  }
2201  else {
2202  bary_add(z2ds, z2n, z2ds, z2n, t4ds, t4n);
2203  }
2204 
2205  /* z1 <- z1 - z3 */
2206  if (z1p == z3p) {
2207  if (bary_sub(z1ds, z1n, z1ds, z1n, z3ds, z3n)) {
2208  bary_2comp(z1ds, z1n);
2209  z1p = !z1p;
2210  }
2211  }
2212  else {
2213  bary_add(z1ds, z1n, z1ds, z1n, z3ds, z3n);
2214  }
2215 
2216  /*
2217  * [Step3] Substituting base value into b of the polynomial z(b),
2218  */
2219 
2220  MEMCPY(zzds, z0ds, BDIGIT, z0n);
2221  BDIGITS_ZERO(zzds + z0n, 4*n - z0n);
2222  MEMCPY(zzds + 4*n, z4ds, BDIGIT, z4n);
2223  BDIGITS_ZERO(zzds + 4*n + z4n, zzn - (4*n + z4n));
2224  if (z1p)
2225  bary_add(zzds + n, zzn - n, zzds + n, zzn - n, z1ds, z1n);
2226  else
2227  bary_sub(zzds + n, zzn - n, zzds + n, zzn - n, z1ds, z1n);
2228  if (z2p)
2229  bary_add(zzds + 2*n, zzn - 2*n, zzds + 2*n, zzn - 2*n, z2ds, z2n);
2230  else
2231  bary_sub(zzds + 2*n, zzn - 2*n, zzds + 2*n, zzn - 2*n, z2ds, z2n);
2232  if (z3p)
2233  bary_add(zzds + 3*n, zzn - 3*n, zzds + 3*n, zzn - 3*n, z3ds, z3n);
2234  else
2235  bary_sub(zzds + 3*n, zzn - 3*n, zzds + 3*n, zzn - 3*n, z3ds, z3n);
2236 
2237  BARY_TRUNC(zzds, zzn);
2238  MEMCPY(zds, zzds, BDIGIT, zzn);
2239  BDIGITS_ZERO(zds + zzn, zn - zzn);
2240 
2241  if (work)
2242  ALLOCV_END(work);
2243 }
2244 
2245 VALUE
2247 {
2248  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), zn = xn + yn;
2249  VALUE z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
2250  if (xn > yn || yn < 3 || !TOOM3_BALANCED(xn,yn))
2251  rb_raise(rb_eArgError, "unexpected bignum length for toom3");
2252  bary_mul_toom3(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn, NULL, 0);
2253  RB_GC_GUARD(x);
2254  RB_GC_GUARD(y);
2255  return z;
2256 }
2257 
2258 #ifdef USE_GMP
2259 static void
2260 bary_mul_gmp(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2261 {
2262  const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
2263  mpz_t x, y, z;
2264  size_t count;
2265 
2266  assert(xn + yn <= zn);
2267 
2268  mpz_init(x);
2269  mpz_init(y);
2270  mpz_init(z);
2271  mpz_import(x, xn, -1, sizeof(BDIGIT), 0, nails, xds);
2272  if (xds == yds && xn == yn) {
2273  mpz_mul(z, x, x);
2274  }
2275  else {
2276  mpz_import(y, yn, -1, sizeof(BDIGIT), 0, nails, yds);
2277  mpz_mul(z, x, y);
2278  }
2279  mpz_export(zds, &count, -1, sizeof(BDIGIT), 0, nails, z);
2280  BDIGITS_ZERO(zds+count, zn-count);
2281  mpz_clear(x);
2282  mpz_clear(y);
2283  mpz_clear(z);
2284 }
2285 
2286 VALUE
2287 rb_big_mul_gmp(VALUE x, VALUE y)
2288 {
2289  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), zn = xn + yn;
2290  VALUE z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
2291  bary_mul_gmp(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn);
2292  RB_GC_GUARD(x);
2293  RB_GC_GUARD(y);
2294  return z;
2295 }
2296 #endif
2297 
2298 static void
2299 bary_short_mul(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2300 {
2301  assert(xn + yn <= zn);
2302 
2303  if (xn == 1 && yn == 1) {
2304  bary_mul_single(zds, zn, xds[0], yds[0]);
2305  }
2306  else {
2307  bary_mul_normal(zds, zn, xds, xn, yds, yn);
2309  }
2310 }
2311 
2312 /* determine whether a bignum is sparse or not by random sampling */
2313 static inline int
2314 bary_sparse_p(const BDIGIT *ds, size_t n)
2315 {
2316  long c = 0;
2317 
2318  if ( ds[rb_genrand_ulong_limited(n / 2) + n / 4]) c++;
2319  if (c <= 1 && ds[rb_genrand_ulong_limited(n / 2) + n / 4]) c++;
2320  if (c <= 1 && ds[rb_genrand_ulong_limited(n / 2) + n / 4]) c++;
2321 
2322  return (c <= 1) ? 1 : 0;
2323 }
2324 
2325 static int
2326 bary_mul_precheck(BDIGIT **zdsp, size_t *znp, const BDIGIT **xdsp, size_t *xnp, const BDIGIT **ydsp, size_t *ynp)
2327 {
2328  size_t nlsz; /* number of least significant zero BDIGITs */
2329 
2330  BDIGIT *zds = *zdsp;
2331  size_t zn = *znp;
2332  const BDIGIT *xds = *xdsp;
2333  size_t xn = *xnp;
2334  const BDIGIT *yds = *ydsp;
2335  size_t yn = *ynp;
2336 
2337  assert(xn + yn <= zn);
2338 
2339  nlsz = 0;
2340 
2341  while (0 < xn) {
2342  if (xds[xn-1] == 0) {
2343  xn--;
2344  }
2345  else {
2346  do {
2347  if (xds[0] != 0)
2348  break;
2349  xds++;
2350  xn--;
2351  nlsz++;
2352  } while (0 < xn);
2353  break;
2354  }
2355  }
2356 
2357  while (0 < yn) {
2358  if (yds[yn-1] == 0) {
2359  yn--;
2360  }
2361  else {
2362  do {
2363  if (yds[0] != 0)
2364  break;
2365  yds++;
2366  yn--;
2367  nlsz++;
2368  } while (0 < yn);
2369  break;
2370  }
2371  }
2372 
2373  if (nlsz) {
2374  BDIGITS_ZERO(zds, nlsz);
2375  zds += nlsz;
2376  zn -= nlsz;
2377  }
2378 
2379  /* make sure that y is longer than x */
2380  if (xn > yn) {
2381  const BDIGIT *tds;
2382  size_t tn;
2383  tds = xds; xds = yds; yds = tds;
2384  tn = xn; xn = yn; yn = tn;
2385  }
2386  assert(xn <= yn);
2387 
2388  if (xn <= 1) {
2389  if (xn == 0) {
2390  BDIGITS_ZERO(zds, zn);
2391  return 1;
2392  }
2393 
2394  if (xds[0] == 1) {
2395  MEMCPY(zds, yds, BDIGIT, yn);
2396  BDIGITS_ZERO(zds+yn, zn-yn);
2397  return 1;
2398  }
2399  if (POW2_P(xds[0])) {
2400  zds[yn] = bary_small_lshift(zds, yds, yn, bit_length(xds[0])-1);
2401  BDIGITS_ZERO(zds+yn+1, zn-yn-1);
2402  return 1;
2403  }
2404  if (yn == 1 && yds[0] == 1) {
2405  zds[0] = xds[0];
2406  BDIGITS_ZERO(zds+1, zn-1);
2407  return 1;
2408  }
2409  bary_mul_normal(zds, zn, xds, xn, yds, yn);
2410  return 1;
2411  }
2412 
2413  *zdsp = zds;
2414  *znp = zn;
2415  *xdsp = xds;
2416  *xnp = xn;
2417  *ydsp = yds;
2418  *ynp = yn;
2419 
2420  return 0;
2421 }
2422 
2423 static void
2424 bary_mul_karatsuba_branch(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
2425 {
2426  /* normal multiplication when x is small */
2427  if (xn < KARATSUBA_MUL_DIGITS) {
2428  normal:
2429  if (xds == yds && xn == yn)
2430  bary_sq_fast(zds, zn, xds, xn);
2431  else
2432  bary_short_mul(zds, zn, xds, xn, yds, yn);
2433  return;
2434  }
2435 
2436  /* normal multiplication when x or y is a sparse bignum */
2437  if (bary_sparse_p(xds, xn)) goto normal;
2438  if (bary_sparse_p(yds, yn)) {
2439  bary_short_mul(zds, zn, yds, yn, xds, xn);
2440  return;
2441  }
2442 
2443  /* balance multiplication by slicing y when x is much smaller than y */
2444  if (!KARATSUBA_BALANCED(xn, yn)) {
2445  bary_mul_balance_with_mulfunc(zds, zn, xds, xn, yds, yn, wds, wn, bary_mul_karatsuba_start);
2446  return;
2447  }
2448 
2449  /* multiplication by karatsuba method */
2450  bary_mul_karatsuba(zds, zn, xds, xn, yds, yn, wds, wn);
2451 }
2452 
2453 static void
2454 bary_mul_karatsuba_start(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
2455 {
2456  if (bary_mul_precheck(&zds, &zn, &xds, &xn, &yds, &yn))
2457  return;
2458 
2459  bary_mul_karatsuba_branch(zds, zn, xds, xn, yds, yn, wds, wn);
2460 }
2461 
2462 static void
2463 bary_mul_toom3_branch(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
2464 {
2465  if (xn < TOOM3_MUL_DIGITS) {
2466  bary_mul_karatsuba_branch(zds, zn, xds, xn, yds, yn, wds, wn);
2467  return;
2468  }
2469 
2470  if (!TOOM3_BALANCED(xn, yn)) {
2471  bary_mul_balance_with_mulfunc(zds, zn, xds, xn, yds, yn, wds, wn, bary_mul_toom3_start);
2472  return;
2473  }
2474 
2475  bary_mul_toom3(zds, zn, xds, xn, yds, yn, wds, wn);
2476 }
2477 
2478 static void
2479 bary_mul_toom3_start(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
2480 {
2481  if (bary_mul_precheck(&zds, &zn, &xds, &xn, &yds, &yn))
2482  return;
2483 
2484  bary_mul_toom3_branch(zds, zn, xds, xn, yds, yn, wds, wn);
2485 }
2486 
2487 static void
2488 bary_mul(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2489 {
2490 #ifdef USE_GMP
2491  const size_t naive_threshold = GMP_MUL_DIGITS;
2492 #else
2493  const size_t naive_threshold = KARATSUBA_MUL_DIGITS;
2494 #endif
2495  if (xn <= yn) {
2496  if (xn < naive_threshold) {
2497  if (xds == yds && xn == yn)
2498  bary_sq_fast(zds, zn, xds, xn);
2499  else
2500  bary_short_mul(zds, zn, xds, xn, yds, yn);
2501  return;
2502  }
2503  }
2504  else {
2505  if (yn < naive_threshold) {
2506  bary_short_mul(zds, zn, yds, yn, xds, xn);
2507  return;
2508  }
2509  }
2510 
2511 #ifdef USE_GMP
2512  bary_mul_gmp(zds, zn, xds, xn, yds, yn);
2513 #else
2514  bary_mul_toom3_start(zds, zn, xds, xn, yds, yn, NULL, 0);
2515 #endif
2516 }
2517 
2519  size_t yn, zn;
2521  volatile VALUE stop;
2522 };
2523 
2524 static void *
2525 bigdivrem1(void *ptr)
2526 {
2527  struct big_div_struct *bds = (struct big_div_struct*)ptr;
2528  size_t yn = bds->yn;
2529  size_t zn = bds->zn;
2530  BDIGIT *yds = bds->yds, *zds = bds->zds;
2531  BDIGIT_DBL_SIGNED num;
2532  BDIGIT q;
2533 
2534  do {
2535  if (bds->stop) {
2536  bds->zn = zn;
2537  return 0;
2538  }
2539  if (zds[zn-1] == yds[yn-1]) q = BDIGMAX;
2540  else q = (BDIGIT)((BIGUP(zds[zn-1]) + zds[zn-2])/yds[yn-1]);
2541  if (q) {
2542  num = bigdivrem_mulsub(zds+zn-(yn+1), yn+1,
2543  q,
2544  yds, yn);
2545  while (num) { /* "add back" required */
2546  q--;
2547  num = bary_add(zds+zn-(yn+1), yn,
2548  zds+zn-(yn+1), yn,
2549  yds, yn);
2550  num--;
2551  }
2552  }
2553  zn--;
2554  zds[zn] = q;
2555  } while (zn > yn);
2556  return 0;
2557 }
2558 
2559 static void
2560 rb_big_stop(void *ptr)
2561 {
2562  struct big_div_struct *bds = ptr;
2563  bds->stop = Qtrue;
2564 }
2565 
2566 static BDIGIT
2567 bigdivrem_single1(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT x_higher_bdigit, BDIGIT y)
2568 {
2569  assert(0 < xn);
2570  assert(x_higher_bdigit < y);
2571  if (POW2_P(y)) {
2572  BDIGIT r;
2573  r = xds[0] & (y-1);
2574  bary_small_rshift(qds, xds, xn, bit_length(y)-1, x_higher_bdigit);
2575  return r;
2576  }
2577  else {
2578  size_t i;
2579  BDIGIT_DBL t2;
2580  t2 = x_higher_bdigit;
2581  i = xn;
2582  while (i--) {
2583  t2 = BIGUP(t2) + xds[i];
2584  qds[i] = (BDIGIT)(t2 / y);
2585  t2 %= y;
2586  }
2587  return (BDIGIT)t2;
2588  }
2589 }
2590 
2591 static BDIGIT
2592 bigdivrem_single(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT y)
2593 {
2594  return bigdivrem_single1(qds, xds, xn, 0, y);
2595 }
2596 
2597 static void
2598 bigdivrem_restoring(BDIGIT *zds, size_t zn, BDIGIT *yds, size_t yn)
2599 {
2600  struct big_div_struct bds;
2601  size_t ynzero;
2602 
2603  assert(yn < zn);
2604  assert(BDIGIT_MSB(yds[yn-1]));
2605  assert(zds[zn-1] < yds[yn-1]);
2606 
2607  for (ynzero = 0; !yds[ynzero]; ynzero++);
2608 
2609  if (ynzero+1 == yn) {
2610  BDIGIT r;
2611  r = bigdivrem_single1(zds+yn, zds+ynzero, zn-yn, zds[zn-1], yds[ynzero]);
2612  zds[ynzero] = r;
2613  return;
2614  }
2615 
2616  bds.yn = yn - ynzero;
2617  bds.zds = zds + ynzero;
2618  bds.yds = yds + ynzero;
2619  bds.stop = Qfalse;
2620  bds.zn = zn - ynzero;
2621  if (bds.zn > 10000 || bds.yn > 10000) {
2622  retry:
2623  bds.stop = Qfalse;
2625 
2626  if (bds.stop == Qtrue) {
2627  /* execute trap handler, but exception was not raised. */
2628  goto retry;
2629  }
2630  }
2631  else {
2632  bigdivrem1(&bds);
2633  }
2634 }
2635 
2636 static void
2637 bary_divmod_normal(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2638 {
2639  int shift;
2640  BDIGIT *zds, *yyds;
2641  size_t zn;
2642  VALUE tmpyz = 0;
2643 
2644  assert(yn < xn || (xn == yn && yds[yn - 1] <= xds[xn - 1]));
2645  assert(qds ? (xn - yn + 1) <= qn : 1);
2646  assert(rds ? yn <= rn : 1);
2647 
2648  zn = xn + BIGDIVREM_EXTRA_WORDS;
2649 
2650  shift = nlz(yds[yn-1]);
2651  if (shift) {
2652  int alloc_y = !rds;
2653  int alloc_z = !qds || qn < zn;
2654  if (alloc_y && alloc_z) {
2655  yyds = ALLOCV_N(BDIGIT, tmpyz, yn+zn);
2656  zds = yyds + yn;
2657  }
2658  else {
2659  yyds = alloc_y ? ALLOCV_N(BDIGIT, tmpyz, yn) : rds;
2660  zds = alloc_z ? ALLOCV_N(BDIGIT, tmpyz, zn) : qds;
2661  }
2662  zds[xn] = bary_small_lshift(zds, xds, xn, shift);
2663  bary_small_lshift(yyds, yds, yn, shift);
2664  }
2665  else {
2666  if (qds && zn <= qn)
2667  zds = qds;
2668  else
2669  zds = ALLOCV_N(BDIGIT, tmpyz, zn);
2670  MEMCPY(zds, xds, BDIGIT, xn);
2671  zds[xn] = 0;
2672  /* bigdivrem_restoring will not modify y.
2673  * So use yds directly. */
2674  yyds = (BDIGIT *)yds;
2675  }
2676 
2677  bigdivrem_restoring(zds, zn, yyds, yn);
2678 
2679  if (rds) {
2680  if (shift)
2681  bary_small_rshift(rds, zds, yn, shift, 0);
2682  else
2683  MEMCPY(rds, zds, BDIGIT, yn);
2684  BDIGITS_ZERO(rds+yn, rn-yn);
2685  }
2686 
2687  if (qds) {
2688  size_t j = zn - yn;
2689  MEMMOVE(qds, zds+yn, BDIGIT, j);
2690  BDIGITS_ZERO(qds+j, qn-j);
2691  }
2692 
2693  if (tmpyz)
2694  ALLOCV_END(tmpyz);
2695 }
2696 
2697 VALUE
2699 {
2700  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), qn, rn;
2701  BDIGIT *xds = BDIGITS(x), *yds = BDIGITS(y), *qds, *rds;
2702  VALUE q, r;
2703 
2704  BARY_TRUNC(yds, yn);
2705  if (yn == 0)
2706  rb_num_zerodiv();
2707  BARY_TRUNC(xds, xn);
2708 
2709  if (xn < yn || (xn == yn && xds[xn - 1] < yds[yn - 1]))
2710  return rb_assoc_new(LONG2FIX(0), x);
2711 
2712  qn = xn + BIGDIVREM_EXTRA_WORDS;
2713  q = bignew(qn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
2714  qds = BDIGITS(q);
2715 
2716  rn = yn;
2717  r = bignew(rn, BIGNUM_SIGN(x));
2718  rds = BDIGITS(r);
2719 
2720  bary_divmod_normal(qds, qn, rds, rn, xds, xn, yds, yn);
2721 
2722  bigtrunc(q);
2723  bigtrunc(r);
2724 
2725  RB_GC_GUARD(x);
2726  RB_GC_GUARD(y);
2727 
2728  return rb_assoc_new(q, r);
2729 }
2730 
2731 #ifdef USE_GMP
2732 static void
2733 bary_divmod_gmp(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2734 {
2735  const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
2736  mpz_t x, y, q, r;
2737  size_t count;
2738 
2739  assert(yn < xn || (xn == yn && yds[yn - 1] <= xds[xn - 1]));
2740  assert(qds ? (xn - yn + 1) <= qn : 1);
2741  assert(rds ? yn <= rn : 1);
2742  assert(qds || rds);
2743 
2744  mpz_init(x);
2745  mpz_init(y);
2746  if (qds) mpz_init(q);
2747  if (rds) mpz_init(r);
2748 
2749  mpz_import(x, xn, -1, sizeof(BDIGIT), 0, nails, xds);
2750  mpz_import(y, yn, -1, sizeof(BDIGIT), 0, nails, yds);
2751 
2752  if (!rds) {
2753  mpz_fdiv_q(q, x, y);
2754  }
2755  else if (!qds) {
2756  mpz_fdiv_r(r, x, y);
2757  }
2758  else {
2759  mpz_fdiv_qr(q, r, x, y);
2760  }
2761 
2762  mpz_clear(x);
2763  mpz_clear(y);
2764 
2765  if (qds) {
2766  mpz_export(qds, &count, -1, sizeof(BDIGIT), 0, nails, q);
2767  BDIGITS_ZERO(qds+count, qn-count);
2768  mpz_clear(q);
2769  }
2770 
2771  if (rds) {
2772  mpz_export(rds, &count, -1, sizeof(BDIGIT), 0, nails, r);
2773  BDIGITS_ZERO(rds+count, rn-count);
2774  mpz_clear(r);
2775  }
2776 }
2777 
2778 VALUE
2779 rb_big_divrem_gmp(VALUE x, VALUE y)
2780 {
2781  size_t xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y), qn, rn;
2782  BDIGIT *xds = BDIGITS(x), *yds = BDIGITS(y), *qds, *rds;
2783  VALUE q, r;
2784 
2785  BARY_TRUNC(yds, yn);
2786  if (yn == 0)
2787  rb_num_zerodiv();
2788  BARY_TRUNC(xds, xn);
2789 
2790  if (xn < yn || (xn == yn && xds[xn - 1] < yds[yn - 1]))
2791  return rb_assoc_new(LONG2FIX(0), x);
2792 
2793  qn = xn - yn + 1;
2794  q = bignew(qn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
2795  qds = BDIGITS(q);
2796 
2797  rn = yn;
2798  r = bignew(rn, BIGNUM_SIGN(x));
2799  rds = BDIGITS(r);
2800 
2801  bary_divmod_gmp(qds, qn, rds, rn, xds, xn, yds, yn);
2802 
2803  bigtrunc(q);
2804  bigtrunc(r);
2805 
2806  RB_GC_GUARD(x);
2807  RB_GC_GUARD(y);
2808 
2809  return rb_assoc_new(q, r);
2810 }
2811 #endif
2812 
2813 static void
2814 bary_divmod_branch(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2815 {
2816 #ifdef USE_GMP
2817  if (GMP_DIV_DIGITS < xn) {
2818  bary_divmod_gmp(qds, qn, rds, rn, xds, xn, yds, yn);
2819  return;
2820  }
2821 #endif
2822  bary_divmod_normal(qds, qn, rds, rn, xds, xn, yds, yn);
2823 }
2824 
2825 static void
2826 bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
2827 {
2828  assert(xn <= qn);
2829  assert(yn <= rn);
2830 
2831  BARY_TRUNC(yds, yn);
2832  if (yn == 0)
2833  rb_num_zerodiv();
2834 
2835  BARY_TRUNC(xds, xn);
2836  if (xn == 0) {
2837  BDIGITS_ZERO(qds, qn);
2838  BDIGITS_ZERO(rds, rn);
2839  return;
2840  }
2841 
2842  if (xn < yn || (xn == yn && xds[xn - 1] < yds[yn - 1])) {
2843  MEMCPY(rds, xds, BDIGIT, xn);
2844  BDIGITS_ZERO(rds+xn, rn-xn);
2845  BDIGITS_ZERO(qds, qn);
2846  }
2847  else if (yn == 1) {
2848  MEMCPY(qds, xds, BDIGIT, xn);
2849  BDIGITS_ZERO(qds+xn, qn-xn);
2850  rds[0] = bigdivrem_single(qds, xds, xn, yds[0]);
2851  BDIGITS_ZERO(rds+1, rn-1);
2852  }
2853  else if (xn == 2 && yn == 2) {
2854  BDIGIT_DBL x = bary2bdigitdbl(xds, 2);
2855  BDIGIT_DBL y = bary2bdigitdbl(yds, 2);
2856  BDIGIT_DBL q = x / y;
2857  BDIGIT_DBL r = x % y;
2858  qds[0] = BIGLO(q);
2859  qds[1] = BIGLO(BIGDN(q));
2860  BDIGITS_ZERO(qds+2, qn-2);
2861  rds[0] = BIGLO(r);
2862  rds[1] = BIGLO(BIGDN(r));
2863  BDIGITS_ZERO(rds+2, rn-2);
2864  }
2865  else {
2866  bary_divmod_branch(qds, qn, rds, rn, xds, xn, yds, yn);
2867  }
2868 }
2869 
2870 
2871 #define BIGNUM_DEBUG 0
2872 #if BIGNUM_DEBUG
2873 #define ON_DEBUG(x) do { x; } while (0)
2874 static void
2875 dump_bignum(VALUE x)
2876 {
2877  long i;
2878  printf("%c0x0", BIGNUM_SIGN(x) ? '+' : '-');
2879  for (i = BIGNUM_LEN(x); i--; ) {
2880  printf("_%0*"PRIxBDIGIT, SIZEOF_BDIGIT*2, BDIGITS(x)[i]);
2881  }
2882  printf(", len=%"PRIuSIZE, BIGNUM_LEN(x));
2883  puts("");
2884 }
2885 
2886 static VALUE
2887 rb_big_dump(VALUE x)
2888 {
2889  dump_bignum(x);
2890  return x;
2891 }
2892 #else
2893 #define ON_DEBUG(x)
2894 #endif
2895 
2896 static int
2898 {
2899  return bary_zero_p(BDIGITS(x), BIGNUM_LEN(x));
2900 }
2901 
2902 int
2904 {
2905  return BIGZEROP(x);
2906 }
2907 
2908 int
2910 {
2911  if (NIL_P(val)) {
2912  rb_cmperr(a, b);
2913  }
2914  if (FIXNUM_P(val)) {
2915  long l = FIX2LONG(val);
2916  if (l > 0) return 1;
2917  if (l < 0) return -1;
2918  return 0;
2919  }
2920  if (RB_BIGNUM_TYPE_P(val)) {
2921  if (BIGZEROP(val)) return 0;
2922  if (BIGNUM_SIGN(val)) return 1;
2923  return -1;
2924  }
2925  if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
2926  if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
2927  return 0;
2928 }
2929 
2930 #define BIGNUM_SET_LEN(b,l) \
2931  ((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
2932  (void)(RBASIC(b)->flags = \
2933  (RBASIC(b)->flags & ~BIGNUM_EMBED_LEN_MASK) | \
2934  ((l) << BIGNUM_EMBED_LEN_SHIFT)) : \
2935  (void)(RBIGNUM(b)->as.heap.len = (l)))
2936 
2937 static void
2939 {
2940  BDIGIT *ds;
2941  if (RBASIC(big)->flags & BIGNUM_EMBED_FLAG) {
2942  if (BIGNUM_EMBED_LEN_MAX < len) {
2943  ds = ALLOC_N(BDIGIT, len);
2944  MEMCPY(ds, RBIGNUM(big)->as.ary, BDIGIT, BIGNUM_EMBED_LEN_MAX);
2945  RBIGNUM(big)->as.heap.len = BIGNUM_LEN(big);
2946  RBIGNUM(big)->as.heap.digits = ds;
2947  RBASIC(big)->flags &= ~BIGNUM_EMBED_FLAG;
2948  }
2949  }
2950  else {
2951  if (len <= BIGNUM_EMBED_LEN_MAX) {
2952  ds = RBIGNUM(big)->as.heap.digits;
2953  RBASIC(big)->flags |= BIGNUM_EMBED_FLAG;
2954  BIGNUM_SET_LEN(big, len);
2955  (void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
2956  if (ds) {
2957  MEMCPY(RBIGNUM(big)->as.ary, ds, BDIGIT, len);
2958  xfree(ds);
2959  }
2960  }
2961  else {
2962  if (BIGNUM_LEN(big) == 0) {
2963  RBIGNUM(big)->as.heap.digits = ALLOC_N(BDIGIT, len);
2964  }
2965  else {
2966  REALLOC_N(RBIGNUM(big)->as.heap.digits, BDIGIT, len);
2967  }
2968  }
2969  }
2970 }
2971 
2972 void
2974 {
2975  rb_big_realloc(big, len);
2976  BIGNUM_SET_LEN(big, len);
2977 }
2978 
2979 static VALUE
2980 bignew_1(VALUE klass, size_t len, int sign)
2981 {
2982  NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
2983  BIGNUM_SET_SIGN(big, sign);
2984  if (len <= BIGNUM_EMBED_LEN_MAX) {
2985  RBASIC(big)->flags |= BIGNUM_EMBED_FLAG;
2986  BIGNUM_SET_LEN(big, len);
2987  (void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
2988  }
2989  else {
2990  RBIGNUM(big)->as.heap.digits = ALLOC_N(BDIGIT, len);
2991  RBIGNUM(big)->as.heap.len = len;
2992  }
2993  OBJ_FREEZE(big);
2994  return (VALUE)big;
2995 }
2996 
2997 VALUE
2998 rb_big_new(size_t len, int sign)
2999 {
3000  return bignew(len, sign != 0);
3001 }
3002 
3003 VALUE
3005 {
3006  size_t len = BIGNUM_LEN(x);
3007  VALUE z = bignew_1(CLASS_OF(x), len, BIGNUM_SIGN(x));
3008 
3009  MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, len);
3010  return z;
3011 }
3012 
3013 static void
3015 {
3016  rb_big_resize(x, BIGNUM_LEN(x)+1);
3017  BDIGITS(x)[BIGNUM_LEN(x)-1] = 1;
3018 }
3019 
3020 /* modify a bignum by 2's complement */
3021 static void
3023 {
3024  long i = BIGNUM_LEN(x);
3025  BDIGIT *ds = BDIGITS(x);
3026 
3027  if (bary_2comp(ds, i)) {
3028  big_extend_carry(x);
3029  }
3030 }
3031 
3032 void
3033 rb_big_2comp(VALUE x) /* get 2's complement */
3034 {
3035  get2comp(x);
3036 }
3037 
3038 static BDIGIT
3039 abs2twocomp(VALUE *xp, long *n_ret)
3040 {
3041  VALUE x = *xp;
3042  long n = BIGNUM_LEN(x);
3043  BDIGIT *ds = BDIGITS(x);
3044  BDIGIT hibits = 0;
3045 
3046  BARY_TRUNC(ds, n);
3047 
3048  if (n != 0 && BIGNUM_NEGATIVE_P(x)) {
3049  VALUE z = bignew_1(CLASS_OF(x), n, 0);
3050  MEMCPY(BDIGITS(z), ds, BDIGIT, n);
3051  bary_2comp(BDIGITS(z), n);
3052  hibits = BDIGMAX;
3053  *xp = z;
3054  }
3055  *n_ret = n;
3056  return hibits;
3057 }
3058 
3059 static void
3060 twocomp2abs_bang(VALUE x, int hibits)
3061 {
3062  BIGNUM_SET_SIGN(x, !hibits);
3063  if (hibits) {
3064  get2comp(x);
3065  }
3066 }
3067 
3068 static inline VALUE
3070 {
3071  size_t len = BIGNUM_LEN(x);
3072  BDIGIT *ds = BDIGITS(x);
3073 
3074  if (len == 0) return x;
3075  while (--len && !ds[len]);
3076  if (BIGNUM_LEN(x) > len+1) {
3077  rb_big_resize(x, len+1);
3078  }
3079  return x;
3080 }
3081 
3082 static inline VALUE
3084 {
3085  size_t n = BIGNUM_LEN(x);
3086  BDIGIT *ds = BDIGITS(x);
3087 #if SIZEOF_BDIGIT < SIZEOF_LONG
3088  unsigned long u;
3089 #else
3090  BDIGIT u;
3091 #endif
3092 
3093  BARY_TRUNC(ds, n);
3094 
3095  if (n == 0) return INT2FIX(0);
3096 
3097 #if SIZEOF_BDIGIT < SIZEOF_LONG
3098  if (sizeof(long)/SIZEOF_BDIGIT < n)
3099  goto return_big;
3100  else {
3101  int i = (int)n;
3102  u = 0;
3103  while (i--) {
3104  u = (unsigned long)(BIGUP(u) + ds[i]);
3105  }
3106  }
3107 #else /* SIZEOF_BDIGIT >= SIZEOF_LONG */
3108  if (1 < n)
3109  goto return_big;
3110  else
3111  u = ds[0];
3112 #endif
3113 
3114  if (BIGNUM_POSITIVE_P(x)) {
3115  if (POSFIXABLE(u)) return LONG2FIX((long)u);
3116  }
3117  else {
3118  if (u <= -FIXNUM_MIN) return LONG2FIX(-(long)u);
3119  }
3120 
3121  return_big:
3122  rb_big_resize(x, n);
3123  return x;
3124 }
3125 
3126 static VALUE
3128 {
3129  if (RB_BIGNUM_TYPE_P(x)) {
3130  x = bigfixize(x);
3131  }
3132  return x;
3133 }
3134 
3135 VALUE
3137 {
3138  return bignorm(x);
3139 }
3140 
3141 VALUE
3143 {
3144  long i;
3146  BDIGIT *digits = BDIGITS(big);
3147 
3148 #if SIZEOF_BDIGIT >= SIZEOF_VALUE
3149  digits[0] = n;
3150 #else
3151  for (i = 0; i < bdigit_roomof(SIZEOF_VALUE); i++) {
3152  digits[i] = BIGLO(n);
3153  n = BIGDN(n);
3154  }
3155 #endif
3156 
3158  while (--i && !digits[i]) ;
3159  BIGNUM_SET_LEN(big, i+1);
3160  return big;
3161 }
3162 
3163 VALUE
3165 {
3166  long neg = 0;
3167  VALUE u;
3168  VALUE big;
3169 
3170  if (n < 0) {
3171  u = 1 + (VALUE)(-(n + 1)); /* u = -n avoiding overflow */
3172  neg = 1;
3173  }
3174  else {
3175  u = n;
3176  }
3177  big = rb_uint2big(u);
3178  if (neg) {
3180  }
3181  return big;
3182 }
3183 
3184 VALUE
3186 {
3187  if (POSFIXABLE(n)) return LONG2FIX(n);
3188  return rb_uint2big(n);
3189 }
3190 
3191 VALUE
3193 {
3194  if (FIXABLE(n)) return LONG2FIX(n);
3195  return rb_int2big(n);
3196 }
3197 
3198 void
3199 rb_big_pack(VALUE val, unsigned long *buf, long num_longs)
3200 {
3201  rb_integer_pack(val, buf, num_longs, sizeof(long), 0,
3204 }
3205 
3206 VALUE
3207 rb_big_unpack(unsigned long *buf, long num_longs)
3208 {
3209  return rb_integer_unpack(buf, num_longs, sizeof(long), 0,
3212 }
3213 
3214 /*
3215  * Calculate the number of bytes to be required to represent
3216  * the absolute value of the integer given as _val_.
3217  *
3218  * [val] an integer.
3219  * [nlz_bits_ret] number of leading zero bits in the most significant byte is returned if not NULL.
3220  *
3221  * This function returns ((val_numbits * CHAR_BIT + CHAR_BIT - 1) / CHAR_BIT)
3222  * where val_numbits is the number of bits of abs(val).
3223  * This function should not overflow.
3224  *
3225  * If nlz_bits_ret is not NULL,
3226  * (return_value * CHAR_BIT - val_numbits) is stored in *nlz_bits_ret.
3227  * In this case, 0 <= *nlz_bits_ret < CHAR_BIT.
3228  *
3229  */
3230 size_t
3231 rb_absint_size(VALUE val, int *nlz_bits_ret)
3232 {
3233  BDIGIT *dp;
3234  BDIGIT *de;
3235  BDIGIT fixbuf[bdigit_roomof(sizeof(long))];
3236 
3237  int num_leading_zeros;
3238 
3239  val = rb_to_int(val);
3240 
3241  if (FIXNUM_P(val)) {
3242  long v = FIX2LONG(val);
3243  if (v < 0) {
3244  v = -v;
3245  }
3246 #if SIZEOF_BDIGIT >= SIZEOF_LONG
3247  fixbuf[0] = v;
3248 #else
3249  {
3250  int i;
3251  for (i = 0; i < numberof(fixbuf); i++) {
3252  fixbuf[i] = BIGLO(v);
3253  v = BIGDN(v);
3254  }
3255  }
3256 #endif
3257  dp = fixbuf;
3258  de = fixbuf + numberof(fixbuf);
3259  }
3260  else {
3261  dp = BDIGITS(val);
3262  de = dp + BIGNUM_LEN(val);
3263  }
3264  while (dp < de && de[-1] == 0)
3265  de--;
3266  if (dp == de) {
3267  if (nlz_bits_ret)
3268  *nlz_bits_ret = 0;
3269  return 0;
3270  }
3271  num_leading_zeros = nlz(de[-1]);
3272  if (nlz_bits_ret)
3273  *nlz_bits_ret = num_leading_zeros % CHAR_BIT;
3274  return (de - dp) * SIZEOF_BDIGIT - num_leading_zeros / CHAR_BIT;
3275 }
3276 
3277 static size_t
3278 absint_numwords_small(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
3279 {
3280  size_t val_numbits = numbytes * CHAR_BIT - nlz_bits_in_msbyte;
3281  size_t div = val_numbits / word_numbits;
3282  size_t mod = val_numbits % word_numbits;
3283  size_t numwords;
3284  size_t nlz_bits;
3285  numwords = mod == 0 ? div : div + 1;
3286  nlz_bits = mod == 0 ? 0 : word_numbits - mod;
3287  *nlz_bits_ret = nlz_bits;
3288  return numwords;
3289 }
3290 
3291 static size_t
3292 absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
3293 {
3294  static const BDIGIT char_bit[1] = { CHAR_BIT };
3295  BDIGIT numbytes_bary[bdigit_roomof(sizeof(numbytes))];
3296  BDIGIT val_numbits_bary[bdigit_roomof(sizeof(numbytes) + 1)];
3297  BDIGIT nlz_bits_in_msbyte_bary[1];
3298  BDIGIT word_numbits_bary[bdigit_roomof(sizeof(word_numbits))];
3299  BDIGIT div_bary[numberof(val_numbits_bary) + BIGDIVREM_EXTRA_WORDS];
3300  BDIGIT mod_bary[numberof(word_numbits_bary)];
3301  BDIGIT one[1] = { 1 };
3302  size_t nlz_bits;
3303  size_t mod;
3304  int sign;
3305  size_t numwords;
3306 
3307  nlz_bits_in_msbyte_bary[0] = nlz_bits_in_msbyte;
3308 
3309  /*
3310  * val_numbits = numbytes * CHAR_BIT - nlz_bits_in_msbyte
3311  * div, mod = val_numbits.divmod(word_numbits)
3312  * numwords = mod == 0 ? div : div + 1
3313  * nlz_bits = mod == 0 ? 0 : word_numbits - mod
3314  */
3315 
3316  bary_unpack(BARY_ARGS(numbytes_bary), &numbytes, 1, sizeof(numbytes), 0,
3318  BARY_SHORT_MUL(val_numbits_bary, numbytes_bary, char_bit);
3319  if (nlz_bits_in_msbyte)
3320  BARY_SUB(val_numbits_bary, val_numbits_bary, nlz_bits_in_msbyte_bary);
3321  bary_unpack(BARY_ARGS(word_numbits_bary), &word_numbits, 1, sizeof(word_numbits), 0,
3323  BARY_DIVMOD(div_bary, mod_bary, val_numbits_bary, word_numbits_bary);
3324  if (BARY_ZERO_P(mod_bary)) {
3325  nlz_bits = 0;
3326  }
3327  else {
3328  BARY_ADD(div_bary, div_bary, one);
3329  bary_pack(+1, BARY_ARGS(mod_bary), &mod, 1, sizeof(mod), 0,
3331  nlz_bits = word_numbits - mod;
3332  }
3333  sign = bary_pack(+1, BARY_ARGS(div_bary), &numwords, 1, sizeof(numwords), 0,
3335 
3336  if (sign == 2) {
3337 #if defined __GNUC__ && (__GNUC__ == 4 && __GNUC_MINOR__ == 4)
3338  *nlz_bits_ret = 0;
3339 #endif
3340  return (size_t)-1;
3341  }
3342  *nlz_bits_ret = nlz_bits;
3343  return numwords;
3344 }
3345 
3346 /*
3347  * Calculate the number of words to be required to represent
3348  * the absolute value of the integer given as _val_.
3349  *
3350  * [val] an integer.
3351  * [word_numbits] number of bits in a word.
3352  * [nlz_bits_ret] number of leading zero bits in the most significant word is returned if not NULL.
3353  *
3354  * This function returns ((val_numbits * CHAR_BIT + word_numbits - 1) / word_numbits)
3355  * where val_numbits is the number of bits of abs(val).
3356  *
3357  * This function can overflow.
3358  * When overflow occur, (size_t)-1 is returned.
3359  *
3360  * If nlz_bits_ret is not NULL and overflow is not occur,
3361  * (return_value * word_numbits - val_numbits) is stored in *nlz_bits_ret.
3362  * In this case, 0 <= *nlz_bits_ret < word_numbits.
3363  *
3364  */
3365 size_t
3366 rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
3367 {
3368  size_t numbytes;
3369  int nlz_bits_in_msbyte;
3370  size_t numwords;
3371  size_t nlz_bits;
3372 
3373  if (word_numbits == 0)
3374  return (size_t)-1;
3375 
3376  numbytes = rb_absint_size(val, &nlz_bits_in_msbyte);
3377 
3378  if (numbytes <= SIZE_MAX / CHAR_BIT) {
3379  numwords = absint_numwords_small(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
3380 #ifdef DEBUG_INTEGER_PACK
3381  {
3382  size_t numwords0, nlz_bits0;
3383  numwords0 = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits0);
3384  assert(numwords0 == numwords);
3385  assert(nlz_bits0 == nlz_bits);
3386  }
3387 #endif
3388  }
3389  else {
3390  numwords = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
3391  }
3392  if (numwords == (size_t)-1)
3393  return numwords;
3394 
3395  if (nlz_bits_ret)
3396  *nlz_bits_ret = nlz_bits;
3397 
3398  return numwords;
3399 }
3400 
3401 /* Test abs(val) consists only a bit or not.
3402  *
3403  * Returns 1 if abs(val) == 1 << n for some n >= 0.
3404  * Returns 0 otherwise.
3405  *
3406  * rb_absint_singlebit_p can be used to determine required buffer size
3407  * for rb_integer_pack used with INTEGER_PACK_2COMP (two's complement).
3408  *
3409  * Following example calculates number of bits required to
3410  * represent val in two's complement number, without sign bit.
3411  *
3412  * size_t size;
3413  * int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val);
3414  * size = rb_absint_numwords(val, 1, NULL)
3415  * if (size == (size_t)-1) ...overflow...
3416  * if (neg && rb_absint_singlebit_p(val))
3417  * size--;
3418  *
3419  * Following example calculates number of bytes required to
3420  * represent val in two's complement number, with sign bit.
3421  *
3422  * size_t size;
3423  * int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val);
3424  * int nlz_bits;
3425  * size = rb_absint_size(val, &nlz_bits);
3426  * if (nlz_bits == 0 && !(neg && rb_absint_singlebit_p(val)))
3427  * size++;
3428  */
3429 int
3431 {
3432  BDIGIT *dp;
3433  BDIGIT *de;
3434  BDIGIT fixbuf[bdigit_roomof(sizeof(long))];
3435  BDIGIT d;
3436 
3437  val = rb_to_int(val);
3438 
3439  if (FIXNUM_P(val)) {
3440  long v = FIX2LONG(val);
3441  if (v < 0) {
3442  v = -v;
3443  }
3444 #if SIZEOF_BDIGIT >= SIZEOF_LONG
3445  fixbuf[0] = v;
3446 #else
3447  {
3448  int i;
3449  for (i = 0; i < numberof(fixbuf); i++) {
3450  fixbuf[i] = BIGLO(v);
3451  v = BIGDN(v);
3452  }
3453  }
3454 #endif
3455  dp = fixbuf;
3456  de = fixbuf + numberof(fixbuf);
3457  }
3458  else {
3459  dp = BDIGITS(val);
3460  de = dp + BIGNUM_LEN(val);
3461  }
3462  while (dp < de && de[-1] == 0)
3463  de--;
3464  while (dp < de && dp[0] == 0)
3465  dp++;
3466  if (dp == de) /* no bit set. */
3467  return 0;
3468  if (dp != de-1) /* two non-zero words. two bits set, at least. */
3469  return 0;
3470  d = *dp;
3471  return POW2_P(d);
3472 }
3473 
3474 
3475 /*
3476  * Export an integer into a buffer.
3477  *
3478  * This function fills the buffer specified by _words_ and _numwords_ as
3479  * val in the format specified by _wordsize_, _nails_ and _flags_.
3480  *
3481  * [val] Fixnum, Bignum or another integer like object which has to_int method.
3482  * [words] buffer to export abs(val).
3483  * [numwords] the size of given buffer as number of words.
3484  * [wordsize] the size of word as number of bytes.
3485  * [nails] number of padding bits in a word.
3486  * Most significant nails bits of each word are filled by zero.
3487  * [flags] bitwise or of constants which name starts "INTEGER_PACK_".
3488  *
3489  * flags:
3490  * [INTEGER_PACK_MSWORD_FIRST] Store the most significant word as the first word.
3491  * [INTEGER_PACK_LSWORD_FIRST] Store the least significant word as the first word.
3492  * [INTEGER_PACK_MSBYTE_FIRST] Store the most significant byte in a word as the first byte in the word.
3493  * [INTEGER_PACK_LSBYTE_FIRST] Store the least significant byte in a word as the first byte in the word.
3494  * [INTEGER_PACK_NATIVE_BYTE_ORDER] INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST corresponding to the host's endian.
3495  * [INTEGER_PACK_2COMP] Use 2's complement representation.
3496  * [INTEGER_PACK_LITTLE_ENDIAN] Same as INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_LSBYTE_FIRST
3497  * [INTEGER_PACK_BIG_ENDIAN] Same as INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_MSBYTE_FIRST
3498  * [INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION] Use generic implementation (for test and debug).
3499  *
3500  * This function fills the buffer specified by _words_
3501  * as abs(val) if INTEGER_PACK_2COMP is not specified in _flags_.
3502  * If INTEGER_PACK_2COMP is specified, 2's complement representation of val is
3503  * filled in the buffer.
3504  *
3505  * This function returns the signedness and overflow condition.
3506  * The overflow condition depends on INTEGER_PACK_2COMP.
3507  *
3508  * INTEGER_PACK_2COMP is not specified:
3509  * -2 : negative overflow. val <= -2**(numwords*(wordsize*CHAR_BIT-nails))
3510  * -1 : negative without overflow. -2**(numwords*(wordsize*CHAR_BIT-nails)) < val < 0
3511  * 0 : zero. val == 0
3512  * 1 : positive without overflow. 0 < val < 2**(numwords*(wordsize*CHAR_BIT-nails))
3513  * 2 : positive overflow. 2**(numwords*(wordsize*CHAR_BIT-nails)) <= val
3514  *
3515  * INTEGER_PACK_2COMP is specified:
3516  * -2 : negative overflow. val < -2**(numwords*(wordsize*CHAR_BIT-nails))
3517  * -1 : negative without overflow. -2**(numwords*(wordsize*CHAR_BIT-nails)) <= val < 0
3518  * 0 : zero. val == 0
3519  * 1 : positive without overflow. 0 < val < 2**(numwords*(wordsize*CHAR_BIT-nails))
3520  * 2 : positive overflow. 2**(numwords*(wordsize*CHAR_BIT-nails)) <= val
3521  *
3522  * The value, -2**(numwords*(wordsize*CHAR_BIT-nails)), is representable
3523  * in 2's complement representation but not representable in absolute value.
3524  * So -1 is returned for the value if INTEGER_PACK_2COMP is specified
3525  * but returns -2 if INTEGER_PACK_2COMP is not specified.
3526  *
3527  * The least significant words are filled in the buffer when overflow occur.
3528  */
3529 
3530 int
3531 rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
3532 {
3533  int sign;
3534  BDIGIT *ds;
3535  size_t num_bdigits;
3536  BDIGIT fixbuf[bdigit_roomof(sizeof(long))];
3537 
3538  RB_GC_GUARD(val) = rb_to_int(val);
3539 
3540  if (FIXNUM_P(val)) {
3541  long v = FIX2LONG(val);
3542  if (v < 0) {
3543  sign = -1;
3544  v = -v;
3545  }
3546  else {
3547  sign = 1;
3548  }
3549 #if SIZEOF_BDIGIT >= SIZEOF_LONG
3550  fixbuf[0] = v;
3551 #else
3552  {
3553  int i;
3554  for (i = 0; i < numberof(fixbuf); i++) {
3555  fixbuf[i] = BIGLO(v);
3556  v = BIGDN(v);
3557  }
3558  }
3559 #endif
3560  ds = fixbuf;
3561  num_bdigits = numberof(fixbuf);
3562  }
3563  else {
3564  sign = BIGNUM_POSITIVE_P(val) ? 1 : -1;
3565  ds = BDIGITS(val);
3566  num_bdigits = BIGNUM_LEN(val);
3567  }
3568 
3569  return bary_pack(sign, ds, num_bdigits, words, numwords, wordsize, nails, flags);
3570 }
3571 
3572 /*
3573  * Import an integer into a buffer.
3574  *
3575  * [words] buffer to import.
3576  * [numwords] the size of given buffer as number of words.
3577  * [wordsize] the size of word as number of bytes.
3578  * [nails] number of padding bits in a word.
3579  * Most significant nails bits of each word are ignored.
3580  * [flags] bitwise or of constants which name starts "INTEGER_PACK_".
3581  *
3582  * flags:
3583  * [INTEGER_PACK_MSWORD_FIRST] Interpret the first word as the most significant word.
3584  * [INTEGER_PACK_LSWORD_FIRST] Interpret the first word as the least significant word.
3585  * [INTEGER_PACK_MSBYTE_FIRST] Interpret the first byte in a word as the most significant byte in the word.
3586  * [INTEGER_PACK_LSBYTE_FIRST] Interpret the first byte in a word as the least significant byte in the word.
3587  * [INTEGER_PACK_NATIVE_BYTE_ORDER] INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST corresponding to the host's endian.
3588  * [INTEGER_PACK_2COMP] Use 2's complement representation.
3589  * [INTEGER_PACK_LITTLE_ENDIAN] Same as INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_LSBYTE_FIRST
3590  * [INTEGER_PACK_BIG_ENDIAN] Same as INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_MSBYTE_FIRST
3591  * [INTEGER_PACK_FORCE_BIGNUM] the result will be a Bignum
3592  * even if it is representable as a Fixnum.
3593  * [INTEGER_PACK_NEGATIVE] Returns non-positive value.
3594  * (Returns non-negative value if not specified.)
3595  * [INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION] Use generic implementation (for test and debug).
3596  *
3597  * This function returns the imported integer as Fixnum or Bignum.
3598  *
3599  * The range of the result value depends on INTEGER_PACK_2COMP and INTEGER_PACK_NEGATIVE.
3600  *
3601  * INTEGER_PACK_2COMP is not set:
3602  * 0 <= val < 2**(numwords*(wordsize*CHAR_BIT-nails)) if !INTEGER_PACK_NEGATIVE
3603  * -2**(numwords*(wordsize*CHAR_BIT-nails)) < val <= 0 if INTEGER_PACK_NEGATIVE
3604  *
3605  * INTEGER_PACK_2COMP is set:
3606  * -2**(numwords*(wordsize*CHAR_BIT-nails)-1) <= val <= 2**(numwords*(wordsize*CHAR_BIT-nails)-1)-1 if !INTEGER_PACK_NEGATIVE
3607  * -2**(numwords*(wordsize*CHAR_BIT-nails)) <= val <= -1 if INTEGER_PACK_NEGATIVE
3608  *
3609  * INTEGER_PACK_2COMP without INTEGER_PACK_NEGATIVE means sign extension.
3610  * INTEGER_PACK_2COMP with INTEGER_PACK_NEGATIVE mean assuming the higher bits are 1.
3611  *
3612  * Note that this function returns 0 when numwords is zero and
3613  * INTEGER_PACK_2COMP is set but INTEGER_PACK_NEGATIVE is not set.
3614  */
3615 
3616 VALUE
3617 rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
3618 {
3619  VALUE val;
3620  size_t num_bdigits;
3621  int sign;
3622  int nlp_bits;
3623  BDIGIT *ds;
3624  BDIGIT fixbuf[2] = { 0, 0 };
3625 
3626  validate_integer_pack_format(numwords, wordsize, nails, flags,
3636 
3637  num_bdigits = integer_unpack_num_bdigits(numwords, wordsize, nails, &nlp_bits);
3638 
3639  if (LONG_MAX-1 < num_bdigits)
3640  rb_raise(rb_eArgError, "too big to unpack as an integer");
3641  if (num_bdigits <= numberof(fixbuf) && !(flags & INTEGER_PACK_FORCE_BIGNUM)) {
3642  val = Qfalse;
3643  ds = fixbuf;
3644  }
3645  else {
3646  val = bignew((long)num_bdigits, 0);
3647  ds = BDIGITS(val);
3648  }
3649  sign = bary_unpack_internal(ds, num_bdigits, words, numwords, wordsize, nails, flags, nlp_bits);
3650 
3651  if (sign == -2) {
3652  if (val) {
3653  big_extend_carry(val);
3654  }
3655  else if (num_bdigits == numberof(fixbuf)) {
3656  val = bignew((long)num_bdigits+1, 0);
3657  MEMCPY(BDIGITS(val), fixbuf, BDIGIT, num_bdigits);
3658  BDIGITS(val)[num_bdigits++] = 1;
3659  }
3660  else {
3661  ds[num_bdigits++] = 1;
3662  }
3663  }
3664 
3665  if (!val) {
3666  BDIGIT_DBL u = fixbuf[0] + BIGUP(fixbuf[1]);
3667  if (u == 0)
3668  return LONG2FIX(0);
3669  if (0 < sign && POSFIXABLE(u))
3670  return LONG2FIX(u);
3671  if (sign < 0 && BDIGIT_MSB(fixbuf[1]) == 0 &&
3673  return LONG2FIX(-(BDIGIT_DBL_SIGNED)u);
3674  val = bignew((long)num_bdigits, 0 <= sign);
3675  MEMCPY(BDIGITS(val), fixbuf, BDIGIT, num_bdigits);
3676  }
3677 
3678  if ((flags & INTEGER_PACK_FORCE_BIGNUM) && sign != 0 &&
3679  bary_zero_p(BDIGITS(val), BIGNUM_LEN(val)))
3680  sign = 0;
3681  BIGNUM_SET_SIGN(val, 0 <= sign);
3682 
3683  if (flags & INTEGER_PACK_FORCE_BIGNUM)
3684  return bigtrunc(val);
3685  return bignorm(val);
3686 }
3687 
3688 #define conv_digit(c) (ruby_digit36_to_number_table[(unsigned char)(c)])
3689 
3690 NORETURN(static inline void invalid_radix(int base));
3691 NORETURN(static inline void invalid_integer(VALUE s));
3692 
3693 static inline int
3694 valid_radix_p(int base)
3695 {
3696  return (1 < base && base <= 36);
3697 }
3698 
3699 static inline void
3700 invalid_radix(int base)
3701 {
3702  rb_raise(rb_eArgError, "invalid radix %d", base);
3703 }
3704 
3705 static inline void
3707 {
3708  rb_raise(rb_eArgError, "invalid value for Integer(): %+"PRIsVALUE, s);
3709 }
3710 
3711 static int
3712 str2big_scan_digits(const char *s, const char *str, int base, int badcheck, size_t *num_digits_p, ssize_t *len_p)
3713 {
3714  char nondigit = 0;
3715  size_t num_digits = 0;
3716  const char *digits_start = str;
3717  const char *digits_end = str;
3718  ssize_t len = *len_p;
3719 
3720  int c;
3721 
3722  if (!len) {
3723  *num_digits_p = 0;
3724  *len_p = 0;
3725  return TRUE;
3726  }
3727 
3728  if (badcheck && *str == '_') goto bad;
3729 
3730  while ((c = *str++) != 0) {
3731  if (c == '_') {
3732  if (nondigit) {
3733  if (badcheck) goto bad;
3734  break;
3735  }
3736  nondigit = (char) c;
3737  }
3738  else if ((c = conv_digit(c)) < 0 || c >= base) {
3739  break;
3740  }
3741  else {
3742  nondigit = 0;
3743  num_digits++;
3744  digits_end = str;
3745  }
3746  if (len > 0 && !--len) break;
3747  }
3748  if (badcheck && nondigit) goto bad;
3749  if (badcheck && len) {
3750  str--;
3751  while (*str && ISSPACE(*str)) {
3752  str++;
3753  if (len > 0 && !--len) break;
3754  }
3755  if (len && *str) {
3756  bad:
3757  return FALSE;
3758  }
3759  }
3760  *num_digits_p = num_digits;
3761  *len_p = digits_end - digits_start;
3762  return TRUE;
3763 }
3764 
3765 static VALUE
3767  int sign,
3768  const char *digits_start,
3769  const char *digits_end,
3770  size_t num_digits,
3771  int bits_per_digit)
3772 {
3773  BDIGIT *dp;
3774  BDIGIT_DBL dd;
3775  int numbits;
3776 
3777  size_t num_bdigits;
3778  const char *p;
3779  int c;
3780  VALUE z;
3781 
3782  num_bdigits = (num_digits / BITSPERDIG) * bits_per_digit + roomof((num_digits % BITSPERDIG) * bits_per_digit, BITSPERDIG);
3783  z = bignew(num_bdigits, sign);
3784  dp = BDIGITS(z);
3785  dd = 0;
3786  numbits = 0;
3787  for (p = digits_end; digits_start < p; p--) {
3788  if ((c = conv_digit(p[-1])) < 0)
3789  continue;
3790  dd |= (BDIGIT_DBL)c << numbits;
3791  numbits += bits_per_digit;
3792  if (BITSPERDIG <= numbits) {
3793  *dp++ = BIGLO(dd);
3794  dd = BIGDN(dd);
3795  numbits -= BITSPERDIG;
3796  }
3797  }
3798  if (numbits) {
3799  *dp++ = BIGLO(dd);
3800  }
3801  assert((size_t)(dp - BDIGITS(z)) == num_bdigits);
3802 
3803  return z;
3804 }
3805 
3806 static VALUE
3808  int sign,
3809  const char *digits_start,
3810  const char *digits_end,
3811  size_t num_bdigits,
3812  int base)
3813 {
3814  size_t blen = 1;
3815  BDIGIT *zds;
3816  BDIGIT_DBL num;
3817 
3818  size_t i;
3819  const char *p;
3820  int c;
3821  VALUE z;
3822 
3823  z = bignew(num_bdigits, sign);
3824  zds = BDIGITS(z);
3825  BDIGITS_ZERO(zds, num_bdigits);
3826 
3827  for (p = digits_start; p < digits_end; p++) {
3828  if ((c = conv_digit(*p)) < 0)
3829  continue;
3830  num = c;
3831  i = 0;
3832  for (;;) {
3833  while (i<blen) {
3834  num += (BDIGIT_DBL)zds[i]*base;
3835  zds[i++] = BIGLO(num);
3836  num = BIGDN(num);
3837  }
3838  if (num) {
3839  blen++;
3840  continue;
3841  }
3842  break;
3843  }
3844  assert(blen <= num_bdigits);
3845  }
3846 
3847  return z;
3848 }
3849 
3850 static VALUE
3852  int sign,
3853  const char *digits_start,
3854  const char *digits_end,
3855  size_t num_digits,
3856  size_t num_bdigits,
3857  int digits_per_bdigits_dbl,
3858  int base)
3859 {
3860  VALUE powerv;
3861  size_t unit;
3862  VALUE tmpuv = 0;
3863  BDIGIT *uds, *vds, *tds;
3864  BDIGIT_DBL dd;
3865  BDIGIT_DBL current_base;
3866  int m;
3867  int power_level = 0;
3868 
3869  size_t i;
3870  const char *p;
3871  int c;
3872  VALUE z;
3873 
3874  uds = ALLOCV_N(BDIGIT, tmpuv, 2*num_bdigits);
3875  vds = uds + num_bdigits;
3876 
3877  powerv = power_cache_get_power(base, power_level, NULL);
3878 
3879  i = 0;
3880  dd = 0;
3881  current_base = 1;
3882  m = digits_per_bdigits_dbl;
3883  if (num_digits < (size_t)m)
3884  m = (int)num_digits;
3885  for (p = digits_end; digits_start < p; p--) {
3886  if ((c = conv_digit(p[-1])) < 0)
3887  continue;
3888  dd = dd + c * current_base;
3889  current_base *= base;
3890  num_digits--;
3891  m--;
3892  if (m == 0) {
3893  uds[i++] = BIGLO(dd);
3894  uds[i++] = (BDIGIT)BIGDN(dd);
3895  dd = 0;
3896  m = digits_per_bdigits_dbl;
3897  if (num_digits < (size_t)m)
3898  m = (int)num_digits;
3899  current_base = 1;
3900  }
3901  }
3902  assert(i == num_bdigits);
3903  for (unit = 2; unit < num_bdigits; unit *= 2) {
3904  for (i = 0; i < num_bdigits; i += unit*2) {
3905  if (2*unit <= num_bdigits - i) {
3906  bary_mul(vds+i, unit*2, BDIGITS(powerv), BIGNUM_LEN(powerv), uds+i+unit, unit);
3907  bary_add(vds+i, unit*2, vds+i, unit*2, uds+i, unit);
3908  }
3909  else if (unit <= num_bdigits - i) {
3910  bary_mul(vds+i, num_bdigits-i, BDIGITS(powerv), BIGNUM_LEN(powerv), uds+i+unit, num_bdigits-(i+unit));
3911  bary_add(vds+i, num_bdigits-i, vds+i, num_bdigits-i, uds+i, unit);
3912  }
3913  else {
3914  MEMCPY(vds+i, uds+i, BDIGIT, num_bdigits-i);
3915  }
3916  }
3917  power_level++;
3918  powerv = power_cache_get_power(base, power_level, NULL);
3919  tds = vds;
3920  vds = uds;
3921  uds = tds;
3922  }
3923  BARY_TRUNC(uds, num_bdigits);
3924  z = bignew(num_bdigits, sign);
3925  MEMCPY(BDIGITS(z), uds, BDIGIT, num_bdigits);
3926 
3927  if (tmpuv)
3928  ALLOCV_END(tmpuv);
3929 
3930  return z;
3931 }
3932 
3933 #ifdef USE_GMP
3934 static VALUE
3935 str2big_gmp(
3936  int sign,
3937  const char *digits_start,
3938  const char *digits_end,
3939  size_t num_digits,
3940  size_t num_bdigits,
3941  int base)
3942 {
3943  const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
3944  char *buf, *p;
3945  const char *q;
3946  VALUE tmps;
3947  mpz_t mz;
3948  VALUE z;
3949  BDIGIT *zds;
3950  size_t zn, count;
3951 
3952  buf = ALLOCV_N(char, tmps, num_digits+1);
3953  p = buf;
3954  for (q = digits_start; q < digits_end; q++) {
3955  if (conv_digit(*q) < 0)
3956  continue;
3957  *p++ = *q;
3958  }
3959  *p = '\0';
3960 
3961  mpz_init(mz);
3962  mpz_set_str(mz, buf, base);
3963  zn = num_bdigits;
3964  z = bignew(zn, sign);
3965  zds = BDIGITS(z);
3966  mpz_export(BDIGITS(z), &count, -1, sizeof(BDIGIT), 0, nails, mz);
3967  BDIGITS_ZERO(zds+count, zn-count);
3968  mpz_clear(mz);
3969 
3970  if (tmps)
3971  ALLOCV_END(tmps);
3972 
3973  return z;
3974 }
3975 #endif
3976 
3977 /*
3978  * Parse +str+ as Ruby Integer, i.e., underscores, 0d and 0b prefixes.
3979  *
3980  * str: pointer to the string to be parsed.
3981  * should be NUL-terminated.
3982  * base: base of conversion, must be 2..36, or -36..0.
3983  * if +base+ > 0, the conversion is done according to the +base+
3984  * and unmatched prefix is parsed as a part of the result if
3985  * present.
3986  * if +base+ <= 0, the conversion is done according to the
3987  * prefix if present, in base <code>-base</code> if +base+ < -1,
3988  * or in base 10.
3989  * badcheck: if non-zero, +ArgumentError+ is raised when +str+ is not
3990  * valid as an Integer. if zero, Fixnum 0 is returned in
3991  * that case.
3992  */
3993 VALUE
3994 rb_cstr_to_inum(const char *str, int base, int badcheck)
3995 {
3996  char *end;
3997  VALUE ret = rb_cstr_parse_inum(str, -1, (badcheck ? NULL : &end), base);
3998  if (NIL_P(ret)) {
3999  if (badcheck) rb_invalid_str(str, "Integer()");
4000  ret = INT2FIX(0);
4001  }
4002  return ret;
4003 }
4004 
4005 /*
4006  * Parse +str+ as Ruby Integer, i.e., underscores, 0d and 0b prefixes.
4007  *
4008  * str: pointer to the string to be parsed.
4009  * should be NUL-terminated if +len+ is negative.
4010  * len: length of +str+ if >= 0. if +len+ is negative, +str+ should
4011  * be NUL-terminated.
4012  * endp: if non-NULL, the address after parsed part is stored. if
4013  * NULL, Qnil is returned when +str+ is not valid as an Integer.
4014  * base: see +rb_cstr_to_inum+
4015  */
4016 
4017 VALUE
4018 rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base)
4019 {
4020  const char *const s = str;
4021  char sign = 1;
4022  int c;
4023  VALUE z;
4024 
4025  unsigned long val;
4026  int ov;
4027 
4028  const char *digits_start, *digits_end;
4029  size_t num_digits;
4030  size_t num_bdigits;
4031  const ssize_t len0 = len;
4032  const int badcheck = !endp;
4033 
4034 #define ADV(n) do {\
4035  if (len > 0 && len <= (n)) goto bad; \
4036  str += (n); \
4037  len -= (n); \
4038  } while (0)
4039 #define ASSERT_LEN() do {\
4040  assert(len != 0); \
4041  if (len0 >= 0) assert(s + len0 == str + len); \
4042  } while (0)
4043 
4044  if (!str) {
4045  bad:
4046  if (endp) *endp = (char *)str;
4047  return Qnil;
4048  }
4049  if (len) {
4050  while (ISSPACE(*str)) ADV(1);
4051 
4052  if (str[0] == '+') {
4053  ADV(1);
4054  }
4055  else if (str[0] == '-') {
4056  ADV(1);
4057  sign = 0;
4058  }
4059  ASSERT_LEN();
4060  }
4061  if (base <= 0) {
4062  if (str[0] == '0' && len > 1) {
4063  switch (str[1]) {
4064  case 'x': case 'X':
4065  base = 16;
4066  ADV(2);
4067  break;
4068  case 'b': case 'B':
4069  base = 2;
4070  ADV(2);
4071  break;
4072  case 'o': case 'O':
4073  base = 8;
4074  ADV(2);
4075  break;
4076  case 'd': case 'D':
4077  base = 10;
4078  ADV(2);
4079  break;
4080  default:
4081  base = 8;
4082  }
4083  }
4084  else if (base < -1) {
4085  base = -base;
4086  }
4087  else {
4088  base = 10;
4089  }
4090  }
4091  else if (len == 1) {
4092  /* no prefix */
4093  }
4094  else if (base == 2) {
4095  if (str[0] == '0' && (str[1] == 'b'||str[1] == 'B')) {
4096  ADV(2);
4097  }
4098  }
4099  else if (base == 8) {
4100  if (str[0] == '0' && (str[1] == 'o'||str[1] == 'O')) {
4101  ADV(2);
4102  }
4103  }
4104  else if (base == 10) {
4105  if (str[0] == '0' && (str[1] == 'd'||str[1] == 'D')) {
4106  ADV(2);
4107  }
4108  }
4109  else if (base == 16) {
4110  if (str[0] == '0' && (str[1] == 'x'||str[1] == 'X')) {
4111  ADV(2);
4112  }
4113  }
4114  if (!valid_radix_p(base)) {
4115  invalid_radix(base);
4116  }
4117  if (!len) goto bad;
4118  if (*str == '0' && len != 1) { /* squeeze preceding 0s */
4119  int us = 0;
4120  const char *end = len < 0 ? NULL : str + len;
4121  while ((c = *++str) == '0' || c == '_') {
4122  if (c == '_') {
4123  if (++us >= 2)
4124  break;
4125  }
4126  else {
4127  us = 0;
4128  }
4129  if (str == end) break;
4130  }
4131  if (!c || ISSPACE(c)) --str;
4132  if (end) len = end - str;
4133  ASSERT_LEN();
4134  }
4135  c = *str;
4136  c = conv_digit(c);
4137  if (c < 0 || c >= base) {
4138  goto bad;
4139  }
4140 
4141  val = ruby_scan_digits(str, len, base, &num_digits, &ov);
4142  if (!ov) {
4143  const char *end = &str[num_digits];
4144  if (num_digits > 0 && *end == '_') goto bigparse;
4145  if (endp) *endp = (char *)end;
4146  if (badcheck) {
4147  if (num_digits == 0) return Qnil; /* no number */
4148  while (len < 0 ? *end : end < str + len) {
4149  if (!ISSPACE(*end)) return Qnil; /* trailing garbage */
4150  end++;
4151  }
4152  }
4153 
4154  if (POSFIXABLE(val)) {
4155  if (sign) return LONG2FIX(val);
4156  else {
4157  long result = -(long)val;
4158  return LONG2FIX(result);
4159  }
4160  }
4161  else {
4162  VALUE big = rb_uint2big(val);
4163  BIGNUM_SET_SIGN(big, sign);
4164  return bignorm(big);
4165  }
4166  }
4167 
4168  bigparse:
4169  digits_start = str;
4170  if (!str2big_scan_digits(s, str, base, badcheck, &num_digits, &len))
4171  goto bad;
4172  if (endp) *endp = (char *)(str + len);
4173  digits_end = digits_start + len;
4174 
4175  if (POW2_P(base)) {
4176  z = str2big_poweroftwo(sign, digits_start, digits_end, num_digits,
4177  bit_length(base-1));
4178  }
4179  else {
4180  int digits_per_bdigits_dbl;
4181  maxpow_in_bdigit_dbl(base, &digits_per_bdigits_dbl);
4182  num_bdigits = roomof(num_digits, digits_per_bdigits_dbl)*2;
4183 
4184 #ifdef USE_GMP
4185  if (GMP_STR2BIG_DIGITS < num_bdigits) {
4186  z = str2big_gmp(sign, digits_start, digits_end, num_digits,
4187  num_bdigits, base);
4188  }
4189  else
4190 #endif
4191  if (num_bdigits < KARATSUBA_MUL_DIGITS) {
4192  z = str2big_normal(sign, digits_start, digits_end,
4193  num_bdigits, base);
4194  }
4195  else {
4196  z = str2big_karatsuba(sign, digits_start, digits_end, num_digits,
4197  num_bdigits, digits_per_bdigits_dbl, base);
4198  }
4199  }
4200 
4201  return bignorm(z);
4202 }
4203 
4204 VALUE
4205 rb_str_to_inum(VALUE str, int base, int badcheck)
4206 {
4207  VALUE ret;
4208  const char *s;
4209  long len;
4210  char *end;
4211 
4212  StringValue(str);
4213  rb_must_asciicompat(str);
4214  RSTRING_GETMEM(str, s, len);
4215  ret = rb_cstr_parse_inum(s, len, (badcheck ? NULL : &end), base);
4216  if (NIL_P(ret)) {
4217  if (badcheck) invalid_integer(str);
4218  ret = INT2FIX(0);
4219  }
4220  return ret;
4221 }
4222 
4223 VALUE
4224 rb_str2big_poweroftwo(VALUE arg, int base, int badcheck)
4225 {
4226  int positive_p = 1;
4227  const char *s, *str;
4228  const char *digits_start, *digits_end;
4229  size_t num_digits;
4230  ssize_t len;
4231  VALUE z;
4232 
4233  if (!valid_radix_p(base) || !POW2_P(base)) {
4234  invalid_radix(base);
4235  }
4236 
4237  rb_must_asciicompat(arg);
4238  s = str = StringValueCStr(arg);
4239  len = RSTRING_LEN(arg);
4240  if (*str == '-') {
4241  len--;
4242  str++;
4243  positive_p = 0;
4244  }
4245 
4246  digits_start = str;
4247  if (!str2big_scan_digits(s, str, base, badcheck, &num_digits, &len))
4248  invalid_integer(arg);
4249  digits_end = digits_start + len;
4250 
4251  z = str2big_poweroftwo(positive_p, digits_start, digits_end, num_digits,
4252  bit_length(base-1));
4253 
4254  RB_GC_GUARD(arg);
4255 
4256  return bignorm(z);
4257 }
4258 
4259 VALUE
4260 rb_str2big_normal(VALUE arg, int base, int badcheck)
4261 {
4262  int positive_p = 1;
4263  const char *s, *str;
4264  const char *digits_start, *digits_end;
4265  size_t num_digits;
4266  ssize_t len;
4267  VALUE z;
4268 
4269  int digits_per_bdigits_dbl;
4270  size_t num_bdigits;
4271 
4272  if (!valid_radix_p(base)) {
4273  invalid_radix(base);
4274  }
4275 
4276  rb_must_asciicompat(arg);
4277  s = str = StringValuePtr(arg);
4278  len = RSTRING_LEN(arg);
4279  if (len > 0 && *str == '-') {
4280  len--;
4281  str++;
4282  positive_p = 0;
4283  }
4284 
4285  digits_start = str;
4286  if (!str2big_scan_digits(s, str, base, badcheck, &num_digits, &len))
4287  invalid_integer(arg);
4288  digits_end = digits_start + len;
4289 
4290  maxpow_in_bdigit_dbl(base, &digits_per_bdigits_dbl);
4291  num_bdigits = roomof(num_digits, digits_per_bdigits_dbl)*2;
4292 
4293  z = str2big_normal(positive_p, digits_start, digits_end,
4294  num_bdigits, base);
4295 
4296  RB_GC_GUARD(arg);
4297 
4298  return bignorm(z);
4299 }
4300 
4301 VALUE
4302 rb_str2big_karatsuba(VALUE arg, int base, int badcheck)
4303 {
4304  int positive_p = 1;
4305  const char *s, *str;
4306  const char *digits_start, *digits_end;
4307  size_t num_digits;
4308  ssize_t len;
4309  VALUE z;
4310 
4311  int digits_per_bdigits_dbl;
4312  size_t num_bdigits;
4313 
4314  if (!valid_radix_p(base)) {
4315  invalid_radix(base);
4316  }
4317 
4318  rb_must_asciicompat(arg);
4319  s = str = StringValuePtr(arg);
4320  len = RSTRING_LEN(arg);
4321  if (len > 0 && *str == '-') {
4322  len--;
4323  str++;
4324  positive_p = 0;
4325  }
4326 
4327  digits_start = str;
4328  if (!str2big_scan_digits(s, str, base, badcheck, &num_digits, &len))
4329  invalid_integer(arg);
4330  digits_end = digits_start + len;
4331 
4332  maxpow_in_bdigit_dbl(base, &digits_per_bdigits_dbl);
4333  num_bdigits = roomof(num_digits, digits_per_bdigits_dbl)*2;
4334 
4335  z = str2big_karatsuba(positive_p, digits_start, digits_end, num_digits,
4336  num_bdigits, digits_per_bdigits_dbl, base);
4337 
4338  RB_GC_GUARD(arg);
4339 
4340  return bignorm(z);
4341 }
4342 
4343 #ifdef USE_GMP
4344 VALUE
4345 rb_str2big_gmp(VALUE arg, int base, int badcheck)
4346 {
4347  int positive_p = 1;
4348  const char *s, *str;
4349  const char *digits_start, *digits_end;
4350  size_t num_digits;
4351  ssize_t len;
4352  VALUE z;
4353 
4354  int digits_per_bdigits_dbl;
4355  size_t num_bdigits;
4356 
4357  if (!valid_radix_p(base)) {
4358  invalid_radix(base);
4359  }
4360 
4361  rb_must_asciicompat(arg);
4362  s = str = StringValuePtr(arg);
4363  len = RSTRING_LEN(arg);
4364  if (len > 0 && *str == '-') {
4365  len--;
4366  str++;
4367  positive_p = 0;
4368  }
4369 
4370  digits_start = str;
4371  if (!str2big_scan_digits(s, str, base, badcheck, &num_digits, &len))
4372  invalid_integer(arg);
4373  digits_end = digits_start + len;
4374 
4375  maxpow_in_bdigit_dbl(base, &digits_per_bdigits_dbl);
4376  num_bdigits = roomof(num_digits, digits_per_bdigits_dbl)*2;
4377 
4378  z = str2big_gmp(positive_p, digits_start, digits_end, num_digits, num_bdigits, base);
4379 
4380  RB_GC_GUARD(arg);
4381 
4382  return bignorm(z);
4383 }
4384 #endif
4385 
4386 #if HAVE_LONG_LONG
4387 
4388 static VALUE
4389 rb_ull2big(unsigned LONG_LONG n)
4390 {
4391  long i;
4392  VALUE big = bignew(bdigit_roomof(SIZEOF_LONG_LONG), 1);
4393  BDIGIT *digits = BDIGITS(big);
4394 
4395 #if SIZEOF_BDIGIT >= SIZEOF_LONG_LONG
4396  digits[0] = n;
4397 #else
4398  for (i = 0; i < bdigit_roomof(SIZEOF_LONG_LONG); i++) {
4399  digits[i] = BIGLO(n);
4400  n = BIGDN(n);
4401  }
4402 #endif
4403 
4404  i = bdigit_roomof(SIZEOF_LONG_LONG);
4405  while (i-- && !digits[i]) ;
4406  BIGNUM_SET_LEN(big, i+1);
4407  return big;
4408 }
4409 
4410 static VALUE
4411 rb_ll2big(LONG_LONG n)
4412 {
4413  long neg = 0;
4414  unsigned LONG_LONG u;
4415  VALUE big;
4416 
4417  if (n < 0) {
4418  u = 1 + (unsigned LONG_LONG)(-(n + 1)); /* u = -n avoiding overflow */
4419  neg = 1;
4420  }
4421  else {
4422  u = n;
4423  }
4424  big = rb_ull2big(u);
4425  if (neg) {
4427  }
4428  return big;
4429 }
4430 
4431 VALUE
4432 rb_ull2inum(unsigned LONG_LONG n)
4433 {
4434  if (POSFIXABLE(n)) return LONG2FIX(n);
4435  return rb_ull2big(n);
4436 }
4437 
4438 VALUE
4439 rb_ll2inum(LONG_LONG n)
4440 {
4441  if (FIXABLE(n)) return LONG2FIX(n);
4442  return rb_ll2big(n);
4443 }
4444 
4445 #endif /* HAVE_LONG_LONG */
4446 
4447 #ifdef HAVE_INT128_T
4448 static VALUE
4449 rb_uint128t2big(uint128_t n)
4450 {
4451  long i;
4452  VALUE big = bignew(bdigit_roomof(SIZEOF_INT128_T), 1);
4453  BDIGIT *digits = BDIGITS(big);
4454 
4455  for (i = 0; i < bdigit_roomof(SIZEOF_INT128_T); i++) {
4456  digits[i] = BIGLO(RSHIFT(n ,BITSPERDIG*i));
4457  }
4458 
4459  i = bdigit_roomof(SIZEOF_INT128_T);
4460  while (i-- && !digits[i]) ;
4461  BIGNUM_SET_LEN(big, i+1);
4462  return big;
4463 }
4464 
4465 VALUE
4466 rb_int128t2big(int128_t n)
4467 {
4468  int neg = 0;
4469  uint128_t u;
4470  VALUE big;
4471 
4472  if (n < 0) {
4473  u = 1 + (uint128_t)(-(n + 1)); /* u = -n avoiding overflow */
4474  neg = 1;
4475  }
4476  else {
4477  u = n;
4478  }
4479  big = rb_uint128t2big(u);
4480  if (neg) {
4482  }
4483  return big;
4484 }
4485 #endif
4486 
4487 VALUE
4488 rb_cstr2inum(const char *str, int base)
4489 {
4490  return rb_cstr_to_inum(str, base, base==0);
4491 }
4492 
4493 VALUE
4494 rb_str2inum(VALUE str, int base)
4495 {
4496  return rb_str_to_inum(str, base, base==0);
4497 }
4498 
4499 static VALUE
4500 big_shift3(VALUE x, int lshift_p, size_t shift_numdigits, int shift_numbits)
4501 {
4502  BDIGIT *xds, *zds;
4503  long s1;
4504  int s2;
4505  VALUE z;
4506  long xn;
4507 
4508  if (lshift_p) {
4509  if (LONG_MAX < shift_numdigits) {
4510  rb_raise(rb_eArgError, "too big number");
4511  }
4512  s1 = shift_numdigits;
4513  s2 = shift_numbits;
4514  xn = BIGNUM_LEN(x);
4515  z = bignew(xn+s1+1, BIGNUM_SIGN(x));
4516  zds = BDIGITS(z);
4517  BDIGITS_ZERO(zds, s1);
4518  xds = BDIGITS(x);
4519  zds[xn+s1] = bary_small_lshift(zds+s1, xds, xn, s2);
4520  }
4521  else {
4522  long zn;
4523  BDIGIT hibitsx;
4524  if (LONG_MAX < shift_numdigits || (size_t)BIGNUM_LEN(x) <= shift_numdigits) {
4525  if (BIGNUM_POSITIVE_P(x) ||
4526  bary_zero_p(BDIGITS(x), BIGNUM_LEN(x)))
4527  return INT2FIX(0);
4528  else
4529  return INT2FIX(-1);
4530  }
4531  s1 = shift_numdigits;
4532  s2 = shift_numbits;
4533  hibitsx = abs2twocomp(&x, &xn);
4534  xds = BDIGITS(x);
4535  if (xn <= s1) {
4536  return hibitsx ? INT2FIX(-1) : INT2FIX(0);
4537  }
4538  zn = xn - s1;
4539  z = bignew(zn, 0);
4540  zds = BDIGITS(z);
4541  bary_small_rshift(zds, xds+s1, zn, s2, hibitsx != 0 ? BDIGMAX : 0);
4542  twocomp2abs_bang(z, hibitsx != 0);
4543  }
4544  RB_GC_GUARD(x);
4545  return z;
4546 }
4547 
4548 static VALUE
4549 big_shift2(VALUE x, int lshift_p, VALUE y)
4550 {
4551  int sign;
4552  size_t lens[2];
4553  size_t shift_numdigits;
4554  int shift_numbits;
4555 
4558 
4559  if (BIGZEROP(x))
4560  return INT2FIX(0);
4561  sign = rb_integer_pack(y, lens, numberof(lens), sizeof(size_t), 0,
4563  if (sign < 0) {
4564  lshift_p = !lshift_p;
4565  sign = -sign;
4566  }
4567  if (lshift_p) {
4568  if (1 < sign || CHAR_BIT <= lens[1])
4569  rb_raise(rb_eRangeError, "shift width too big");
4570  }
4571  else {
4572  if (1 < sign || CHAR_BIT <= lens[1])
4573  return BIGNUM_POSITIVE_P(x) ? INT2FIX(0) : INT2FIX(-1);
4574  }
4575  shift_numbits = (int)(lens[0] & (BITSPERDIG-1));
4576  shift_numdigits = (lens[0] >> bit_length(BITSPERDIG-1)) |
4577  (lens[1] << (CHAR_BIT*SIZEOF_SIZE_T - bit_length(BITSPERDIG-1)));
4578  return big_shift3(x, lshift_p, shift_numdigits, shift_numbits);
4579 }
4580 
4581 static VALUE
4582 big_lshift(VALUE x, unsigned long shift)
4583 {
4584  long s1 = shift/BITSPERDIG;
4585  int s2 = (int)(shift%BITSPERDIG);
4586  return big_shift3(x, 1, s1, s2);
4587 }
4588 
4589 static VALUE
4590 big_rshift(VALUE x, unsigned long shift)
4591 {
4592  long s1 = shift/BITSPERDIG;
4593  int s2 = (int)(shift%BITSPERDIG);
4594  return big_shift3(x, 0, s1, s2);
4595 }
4596 
4597 #define MAX_BASE36_POWER_TABLE_ENTRIES (SIZEOF_SIZE_T * CHAR_BIT + 1)
4598 
4601 
4602 static void
4604 {
4605  int i, j;
4606  for (i = 0; i < 35; ++i) {
4607  for (j = 0; j < MAX_BASE36_POWER_TABLE_ENTRIES; ++j) {
4608  base36_power_cache[i][j] = Qnil;
4609  }
4610  }
4611 }
4612 
4613 static inline VALUE
4614 power_cache_get_power(int base, int power_level, size_t *numdigits_ret)
4615 {
4616  /*
4617  * MAX_BASE36_POWER_TABLE_ENTRIES is big enough to that
4618  * base36_power_cache[base][MAX_BASE36_POWER_TABLE_ENTRIES-1] fills whole memory.
4619  * So MAX_BASE36_POWER_TABLE_ENTRIES <= power_level is not possible to calculate.
4620  *
4621  * number-of-bytes =
4622  * log256(base36_power_cache[base][MAX_BASE36_POWER_TABLE_ENTRIES-1]) =
4623  * log256(maxpow_in_bdigit_dbl(base)**(2**(MAX_BASE36_POWER_TABLE_ENTRIES-1))) =
4624  * log256(maxpow_in_bdigit_dbl(base)**(2**(SIZEOF_SIZE_T*CHAR_BIT))) =
4625  * (2**(SIZEOF_SIZE_T*CHAR_BIT))*log256(maxpow_in_bdigit_dbl(base)) =
4626  * (256**SIZEOF_SIZE_T)*log256(maxpow_in_bdigit_dbl(base)) >
4627  * (256**SIZEOF_SIZE_T)*(sizeof(BDIGIT_DBL)-1) >
4628  * 256**SIZEOF_SIZE_T
4629  */
4630  if (MAX_BASE36_POWER_TABLE_ENTRIES <= power_level)
4631  rb_bug("too big power number requested: maxpow_in_bdigit_dbl(%d)**(2**%d)", base, power_level);
4632 
4633  if (NIL_P(base36_power_cache[base - 2][power_level])) {
4634  VALUE power;
4635  size_t numdigits;
4636  if (power_level == 0) {
4637  int numdigits0;
4638  BDIGIT_DBL dd = maxpow_in_bdigit_dbl(base, &numdigits0);
4639  power = bignew(2, 1);
4640  bdigitdbl2bary(BDIGITS(power), 2, dd);
4641  numdigits = numdigits0;
4642  }
4643  else {
4644  power = bigtrunc(bigsq(power_cache_get_power(base, power_level - 1, &numdigits)));
4645  numdigits *= 2;
4646  }
4647  rb_obj_hide(power);
4648  base36_power_cache[base - 2][power_level] = power;
4649  base36_numdigits_cache[base - 2][power_level] = numdigits;
4651  }
4652  if (numdigits_ret)
4653  *numdigits_ret = base36_numdigits_cache[base - 2][power_level];
4654  return base36_power_cache[base - 2][power_level];
4655 }
4656 
4659  int base;
4663  char *ptr;
4664 };
4665 
4666 static void
4667 big2str_alloc(struct big2str_struct *b2s, size_t len)
4668 {
4669  if (LONG_MAX-1 < len)
4670  rb_raise(rb_eArgError, "too big number");
4671  b2s->result = rb_usascii_str_new(0, (long)(len + 1)); /* plus one for sign */
4672  b2s->ptr = RSTRING_PTR(b2s->result);
4673  if (b2s->negative)
4674  *b2s->ptr++ = '-';
4675 }
4676 
4677 static void
4678 big2str_2bdigits(struct big2str_struct *b2s, BDIGIT *xds, size_t xn, size_t taillen)
4679 {
4680  size_t j;
4681  BDIGIT_DBL num;
4682  char buf[SIZEOF_BDIGIT_DBL*CHAR_BIT], *p;
4683  int beginning = !b2s->ptr;
4684  size_t len = 0;
4685 
4686  assert(xn <= 2);
4687  num = bary2bdigitdbl(xds, xn);
4688 
4689  if (beginning) {
4690  if (num == 0)
4691  return;
4692  p = buf;
4693  j = sizeof(buf);
4694  do {
4695  BDIGIT_DBL idx = num % b2s->base;
4696  num /= b2s->base;
4697  p[--j] = ruby_digitmap[idx];
4698  } while (num);
4699  len = sizeof(buf) - j;
4700  big2str_alloc(b2s, len + taillen);
4701  MEMCPY(b2s->ptr, buf + j, char, len);
4702  }
4703  else {
4704  p = b2s->ptr;
4705  j = b2s->hbase2_numdigits;
4706  do {
4707  BDIGIT_DBL idx = num % b2s->base;
4708  num /= b2s->base;
4709  p[--j] = ruby_digitmap[idx];
4710  } while (j);
4711  len = b2s->hbase2_numdigits;
4712  }
4713  b2s->ptr += len;
4714 }
4715 
4716 static void
4717 big2str_karatsuba(struct big2str_struct *b2s, BDIGIT *xds, size_t xn, size_t wn,
4718  int power_level, size_t taillen)
4719 {
4720  VALUE b;
4721  size_t half_numdigits, lower_numdigits;
4722  int lower_power_level;
4723  size_t bn;
4724  const BDIGIT *bds;
4725  size_t len;
4726 
4727  /*
4728  * Precondition:
4729  * abs(x) < maxpow**(2**power_level)
4730  * where
4731  * maxpow = maxpow_in_bdigit_dbl(base, &numdigits)
4732  *
4733  * This function generates sequence of zeros, and then stringized abs(x) into b2s->ptr.
4734  *
4735  * b2s->ptr can be NULL.
4736  * It is allocated when the first character is generated via big2str_alloc.
4737  *
4738  * The prefix zeros should be generated if and only if b2s->ptr is not NULL.
4739  * When the zeros are generated, the zeros and abs(x) consists
4740  * numdigits*(2**power_level) characters at total.
4741  *
4742  * Note:
4743  * power_cache_get_power(base, power_level, &len) may not be cached yet. It should not be called.
4744  * power_cache_get_power(base, power_level-1, &len) should be cached already if 0 <= power_level-1.
4745  */
4746 
4747  if (xn == 0 || bary_zero_p(xds, xn)) {
4748  if (b2s->ptr) {
4749  /* When x is zero, power_cache_get_power(base, power_level) should be cached already. */
4750  power_cache_get_power(b2s->base, power_level, &len);
4751  memset(b2s->ptr, '0', len);
4752  b2s->ptr += len;
4753  }
4754  return;
4755  }
4756 
4757  if (power_level == 0) {
4758  big2str_2bdigits(b2s, xds, xn, taillen);
4759  return;
4760  }
4761 
4762  lower_power_level = power_level-1;
4763  b = power_cache_get_power(b2s->base, lower_power_level, &lower_numdigits);
4764  bn = BIGNUM_LEN(b);
4765  bds = BDIGITS(b);
4766 
4767  half_numdigits = lower_numdigits;
4768 
4769  while (0 < lower_power_level &&
4770  (xn < bn ||
4771  (xn == bn && bary_cmp(xds, xn, bds, bn) < 0))) {
4772  lower_power_level--;
4773  b = power_cache_get_power(b2s->base, lower_power_level, &lower_numdigits);
4774  bn = BIGNUM_LEN(b);
4775  bds = BDIGITS(b);
4776  }
4777 
4778  if (lower_power_level == 0 &&
4779  (xn < bn ||
4780  (xn == bn && bary_cmp(xds, xn, bds, bn) < 0))) {
4781  if (b2s->ptr) {
4782  len = half_numdigits * 2 - lower_numdigits;
4783  memset(b2s->ptr, '0', len);
4784  b2s->ptr += len;
4785  }
4786  big2str_2bdigits(b2s, xds, xn, taillen);
4787  }
4788  else {
4789  BDIGIT *qds, *rds;
4790  size_t qn, rn;
4791  BDIGIT *tds;
4792  int shift;
4793 
4794  if (lower_power_level != power_level-1 && b2s->ptr) {
4795  len = (half_numdigits - lower_numdigits) * 2;
4796  memset(b2s->ptr, '0', len);
4797  b2s->ptr += len;
4798  }
4799 
4800  shift = nlz(bds[bn-1]);
4801 
4802  qn = xn + BIGDIVREM_EXTRA_WORDS;
4803 
4804  if (shift == 0) {
4805  /* bigdivrem_restoring will not modify y.
4806  * So use bds directly. */
4807  tds = (BDIGIT *)bds;
4808  xds[xn] = 0;
4809  }
4810  else {
4811  /* bigdivrem_restoring will modify y.
4812  * So use temporary buffer. */
4813  tds = xds + qn;
4814  assert(qn + bn <= xn + wn);
4815  bary_small_lshift(tds, bds, bn, shift);
4816  xds[xn] = bary_small_lshift(xds, xds, xn, shift);
4817  }
4818 
4819  bigdivrem_restoring(xds, qn, tds, bn);
4820 
4821  rds = xds;
4822  rn = bn;
4823 
4824  qds = xds + bn;
4825  qn = qn - bn;
4826 
4827  if (shift) {
4828  bary_small_rshift(rds, rds, rn, shift, 0);
4829  }
4830 
4831  BARY_TRUNC(qds, qn);
4832  assert(qn <= bn);
4833  big2str_karatsuba(b2s, qds, qn, xn+wn - (rn+qn), lower_power_level, lower_numdigits+taillen);
4834  BARY_TRUNC(rds, rn);
4835  big2str_karatsuba(b2s, rds, rn, xn+wn - rn, lower_power_level, taillen);
4836  }
4837 }
4838 
4839 static VALUE
4841 {
4842  int word_numbits = ffs(base) - 1;
4843  size_t numwords;
4844  VALUE result;
4845  char *ptr;
4846  numwords = rb_absint_numwords(x, word_numbits, NULL);
4847  if (BIGNUM_NEGATIVE_P(x)) {
4848  if (LONG_MAX-1 < numwords)
4849  rb_raise(rb_eArgError, "too big number");
4850  result = rb_usascii_str_new(0, 1+numwords);
4851  ptr = RSTRING_PTR(result);
4852  *ptr++ = BIGNUM_POSITIVE_P(x) ? '+' : '-';
4853  }
4854  else {
4855  if (LONG_MAX < numwords)
4856  rb_raise(rb_eArgError, "too big number");
4857  result = rb_usascii_str_new(0, numwords);
4858  ptr = RSTRING_PTR(result);
4859  }
4860  rb_integer_pack(x, ptr, numwords, 1, CHAR_BIT-word_numbits,
4862  while (0 < numwords) {
4863  *ptr = ruby_digitmap[*(unsigned char *)ptr];
4864  ptr++;
4865  numwords--;
4866  }
4867  return result;
4868 }
4869 
4870 VALUE
4872 {
4873  return big2str_base_poweroftwo(x, base);
4874 }
4875 
4876 static VALUE
4878 {
4879  BDIGIT *xds;
4880  size_t xn;
4881  struct big2str_struct b2s_data;
4882  int power_level;
4883  VALUE power;
4884 
4885  xds = BDIGITS(x);
4886  xn = BIGNUM_LEN(x);
4887  BARY_TRUNC(xds, xn);
4888 
4889  if (xn == 0) {
4890  return rb_usascii_str_new2("0");
4891  }
4892 
4893  if (!valid_radix_p(base))
4894  invalid_radix(base);
4895 
4896  if (xn >= LONG_MAX/BITSPERDIG) {
4897  rb_raise(rb_eRangeError, "bignum too big to convert into `string'");
4898  }
4899 
4900  power_level = 0;
4901  power = power_cache_get_power(base, power_level, NULL);
4902  while (power_level < MAX_BASE36_POWER_TABLE_ENTRIES &&
4903  (size_t)BIGNUM_LEN(power) <= (xn+1)/2) {
4904  power_level++;
4905  power = power_cache_get_power(base, power_level, NULL);
4906  }
4907  assert(power_level != MAX_BASE36_POWER_TABLE_ENTRIES);
4908 
4909  if ((size_t)BIGNUM_LEN(power) <= xn) {
4910  /*
4911  * This increment guarantees x < power_cache_get_power(base, power_level)
4912  * without invoking it actually.
4913  * (power_cache_get_power(base, power_level) can be slow and not used
4914  * in big2str_karatsuba.)
4915  *
4916  * Although it is possible that x < power_cache_get_power(base, power_level-1),
4917  * it is no problem because big2str_karatsuba checks it and
4918  * doesn't affect the result when b2s_data.ptr is NULL.
4919  */
4920  power_level++;
4921  }
4922 
4923  b2s_data.negative = BIGNUM_NEGATIVE_P(x);
4924  b2s_data.base = base;
4925  b2s_data.hbase2 = maxpow_in_bdigit_dbl(base, &b2s_data.hbase2_numdigits);
4926 
4927  b2s_data.result = Qnil;
4928  b2s_data.ptr = NULL;
4929 
4930  if (power_level == 0) {
4931  big2str_2bdigits(&b2s_data, xds, xn, 0);
4932  }
4933  else {
4934  VALUE tmpw = 0;
4935  BDIGIT *wds;
4936  size_t wn;
4937  wn = power_level * BIGDIVREM_EXTRA_WORDS + BIGNUM_LEN(power);
4938  wds = ALLOCV_N(BDIGIT, tmpw, xn + wn);
4939  MEMCPY(wds, xds, BDIGIT, xn);
4940  big2str_karatsuba(&b2s_data, wds, xn, wn, power_level, 0);
4941  if (tmpw)
4942  ALLOCV_END(tmpw);
4943  }
4944  RB_GC_GUARD(x);
4945 
4946  *b2s_data.ptr = '\0';
4947  rb_str_resize(b2s_data.result, (long)(b2s_data.ptr - RSTRING_PTR(b2s_data.result)));
4948 
4949  RB_GC_GUARD(x);
4950  return b2s_data.result;
4951 }
4952 
4953 VALUE
4955 {
4956  return big2str_generic(x, base);
4957 }
4958 
4959 #ifdef USE_GMP
4960 static VALUE
4961 big2str_gmp(VALUE x, int base)
4962 {
4963  const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
4964  mpz_t mx;
4965  size_t size;
4966  VALUE str;
4967  BDIGIT *xds = BDIGITS(x);
4968  size_t xn = BIGNUM_LEN(x);
4969 
4970  mpz_init(mx);
4971  mpz_import(mx, xn, -1, sizeof(BDIGIT), 0, nails, xds);
4972 
4973  size = mpz_sizeinbase(mx, base);
4974 
4975  if (BIGNUM_NEGATIVE_P(x)) {
4976  mpz_neg(mx, mx);
4977  str = rb_usascii_str_new(0, size+1);
4978  }
4979  else {
4980  str = rb_usascii_str_new(0, size);
4981  }
4982  mpz_get_str(RSTRING_PTR(str), base, mx);
4983  mpz_clear(mx);
4984 
4985  if (RSTRING_PTR(str)[RSTRING_LEN(str)-1] == '\0') {
4986  rb_str_set_len(str, RSTRING_LEN(str)-1);
4987  }
4988 
4989  RB_GC_GUARD(x);
4990  return str;
4991 }
4992 
4993 VALUE
4994 rb_big2str_gmp(VALUE x, int base)
4995 {
4996  return big2str_gmp(x, base);
4997 }
4998 #endif
4999 
5000 static VALUE
5001 rb_big2str1(VALUE x, int base)
5002 {
5003  BDIGIT *xds;
5004  size_t xn;
5005 
5006  if (FIXNUM_P(x)) {
5007  return rb_fix2str(x, base);
5008  }
5009 
5010  bigtrunc(x);
5011  xds = BDIGITS(x);
5012  xn = BIGNUM_LEN(x);
5013  BARY_TRUNC(xds, xn);
5014 
5015  if (xn == 0) {
5016  return rb_usascii_str_new2("0");
5017  }
5018 
5019  if (!valid_radix_p(base))
5020  invalid_radix(base);
5021 
5022  if (xn >= LONG_MAX/BITSPERDIG) {
5023  rb_raise(rb_eRangeError, "bignum too big to convert into `string'");
5024  }
5025 
5026  if (POW2_P(base)) {
5027  /* base == 2 || base == 4 || base == 8 || base == 16 || base == 32 */
5028  return big2str_base_poweroftwo(x, base);
5029  }
5030 
5031 #ifdef USE_GMP
5032  if (GMP_BIG2STR_DIGITS < xn) {
5033  return big2str_gmp(x, base);
5034  }
5035 #endif
5036 
5037  return big2str_generic(x, base);
5038 }
5039 
5040 VALUE
5041 rb_big2str(VALUE x, int base)
5042 {
5043  return rb_big2str1(x, base);
5044 }
5045 
5046 static unsigned long
5047 big2ulong(VALUE x, const char *type)
5048 {
5049  size_t len = BIGNUM_LEN(x);
5050  unsigned long num;
5051  BDIGIT *ds;
5052 
5053  if (len == 0)
5054  return 0;
5055  if (BIGSIZE(x) > sizeof(long)) {
5056  rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
5057  }
5058  ds = BDIGITS(x);
5059 #if SIZEOF_LONG <= SIZEOF_BDIGIT
5060  num = (unsigned long)ds[0];
5061 #else
5062  num = 0;
5063  while (len--) {
5064  num <<= BITSPERDIG;
5065  num += (unsigned long)ds[len]; /* overflow is already checked */
5066  }
5067 #endif
5068  return num;
5069 }
5070 
5071 unsigned long
5073 {
5074  unsigned long num = big2ulong(x, "unsigned long");
5075 
5076  if (BIGNUM_POSITIVE_P(x)) {
5077  return num;
5078  }
5079  else {
5080  if (num <= 1+(unsigned long)(-(LONG_MIN+1)))
5081  return -(long)(num-1)-1;
5082  }
5083  rb_raise(rb_eRangeError, "bignum out of range of unsigned long");
5084 }
5085 
5086 long
5088 {
5089  unsigned long num = big2ulong(x, "long");
5090 
5091  if (BIGNUM_POSITIVE_P(x)) {
5092  if (num <= LONG_MAX)
5093  return num;
5094  }
5095  else {
5096  if (num <= 1+(unsigned long)(-(LONG_MIN+1)))
5097  return -(long)(num-1)-1;
5098  }
5099  rb_raise(rb_eRangeError, "bignum too big to convert into `long'");
5100 }
5101 
5102 #if HAVE_LONG_LONG
5103 
5104 static unsigned LONG_LONG
5105 big2ull(VALUE x, const char *type)
5106 {
5107  size_t len = BIGNUM_LEN(x);
5108  unsigned LONG_LONG num;
5109  BDIGIT *ds = BDIGITS(x);
5110 
5111  if (len == 0)
5112  return 0;
5113  if (BIGSIZE(x) > SIZEOF_LONG_LONG)
5114  rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
5115 #if SIZEOF_LONG_LONG <= SIZEOF_BDIGIT
5116  num = (unsigned LONG_LONG)ds[0];
5117 #else
5118  num = 0;
5119  while (len--) {
5120  num = BIGUP(num);
5121  num += ds[len];
5122  }
5123 #endif
5124  return num;
5125 }
5126 
5127 unsigned LONG_LONG
5128 rb_big2ull(VALUE x)
5129 {
5130  unsigned LONG_LONG num = big2ull(x, "unsigned long long");
5131 
5132  if (BIGNUM_POSITIVE_P(x)) {
5133  return num;
5134  }
5135  else {
5136  if (num <= 1+(unsigned LONG_LONG)(-(LLONG_MIN+1)))
5137  return -(LONG_LONG)(num-1)-1;
5138  }
5139  rb_raise(rb_eRangeError, "bignum out of range of unsigned long long");
5140 }
5141 
5142 LONG_LONG
5143 rb_big2ll(VALUE x)
5144 {
5145  unsigned LONG_LONG num = big2ull(x, "long long");
5146 
5147  if (BIGNUM_POSITIVE_P(x)) {
5148  if (num <= LLONG_MAX)
5149  return num;
5150  }
5151  else {
5152  if (num <= 1+(unsigned LONG_LONG)(-(LLONG_MIN+1)))
5153  return -(LONG_LONG)(num-1)-1;
5154  }
5155  rb_raise(rb_eRangeError, "bignum too big to convert into `long long'");
5156 }
5157 
5158 #endif /* HAVE_LONG_LONG */
5159 
5160 static VALUE
5161 dbl2big(double d)
5162 {
5163  long i = 0;
5164  BDIGIT c;
5165  BDIGIT *digits;
5166  VALUE z;
5167  double u = (d < 0)?-d:d;
5168 
5169  if (isinf(d)) {
5170  rb_raise(rb_eFloatDomainError, d < 0 ? "-Infinity" : "Infinity");
5171  }
5172  if (isnan(d)) {
5174  }
5175 
5176  while (1.0 <= u) {
5177  u /= (double)(BIGRAD);
5178  i++;
5179  }
5180  z = bignew(i, d>=0);
5181  digits = BDIGITS(z);
5182  while (i--) {
5183  u *= BIGRAD;
5184  c = (BDIGIT)u;
5185  u -= c;
5186  digits[i] = c;
5187  }
5188 
5189  return z;
5190 }
5191 
5192 VALUE
5193 rb_dbl2big(double d)
5194 {
5195  return bignorm(dbl2big(d));
5196 }
5197 
5198 static double
5200 {
5201  double d = 0.0;
5202  long i = (bigtrunc(x), BIGNUM_LEN(x)), lo = 0, bits;
5203  BDIGIT *ds = BDIGITS(x), dl;
5204 
5205  if (i) {
5206  bits = i * BITSPERDIG - nlz(ds[i-1]);
5207  if (bits > DBL_MANT_DIG+DBL_MAX_EXP) {
5208  d = HUGE_VAL;
5209  }
5210  else {
5211  if (bits > DBL_MANT_DIG+1)
5212  lo = (bits -= DBL_MANT_DIG+1) / BITSPERDIG;
5213  else
5214  bits = 0;
5215  while (--i > lo) {
5216  d = ds[i] + BIGRAD*d;
5217  }
5218  dl = ds[i];
5219  if (bits && (dl & ((BDIGIT)1 << (bits %= BITSPERDIG)))) {
5220  int carry = (dl & ~(BDIGMAX << bits)) != 0;
5221  if (!carry) {
5222  while (i-- > 0) {
5223  carry = ds[i] != 0;
5224  if (carry) break;
5225  }
5226  }
5227  if (carry) {
5228  dl &= BDIGMAX << bits;
5229  dl = BIGLO(dl + ((BDIGIT)1 << bits));
5230  if (!dl) d += 1;
5231  }
5232  }
5233  d = dl + BIGRAD*d;
5234  if (lo) {
5235  if (lo > INT_MAX / BITSPERDIG)
5236  d = HUGE_VAL;
5237  else if (lo < INT_MIN / BITSPERDIG)
5238  d = 0.0;
5239  else
5240  d = ldexp(d, (int)(lo * BITSPERDIG));
5241  }
5242  }
5243  }
5244  if (BIGNUM_NEGATIVE_P(x)) d = -d;
5245  return d;
5246 }
5247 
5248 double
5250 {
5251  double d = big2dbl(x);
5252 
5253  if (isinf(d)) {
5254  rb_warning("Bignum out of Float range");
5255  if (d < 0.0)
5256  d = -HUGE_VAL;
5257  else
5258  d = HUGE_VAL;
5259  }
5260  return d;
5261 }
5262 
5263 VALUE
5265 {
5266  double yd = RFLOAT_VALUE(y);
5267  double yi, yf;
5268  VALUE rel;
5269 
5270  if (isnan(yd))
5271  return Qnil;
5272  if (isinf(yd)) {
5273  if (yd > 0.0) return INT2FIX(-1);
5274  else return INT2FIX(1);
5275  }
5276  yf = modf(yd, &yi);
5277  if (FIXNUM_P(x)) {
5278 #if SIZEOF_LONG * CHAR_BIT < DBL_MANT_DIG /* assume FLT_RADIX == 2 */
5279  double xd = (double)FIX2LONG(x);
5280  if (xd < yd)
5281  return INT2FIX(-1);
5282  if (xd > yd)
5283  return INT2FIX(1);
5284  return INT2FIX(0);
5285 #else
5286  long xn, yn;
5287  if (yi < FIXNUM_MIN)
5288  return INT2FIX(1);
5289  if (FIXNUM_MAX+1 <= yi)
5290  return INT2FIX(-1);
5291  xn = FIX2LONG(x);
5292  yn = (long)yi;
5293  if (xn < yn)
5294  return INT2FIX(-1);
5295  if (xn > yn)
5296  return INT2FIX(1);
5297  if (yf < 0.0)
5298  return INT2FIX(1);
5299  if (0.0 < yf)
5300  return INT2FIX(-1);
5301  return INT2FIX(0);
5302 #endif
5303  }
5304  y = rb_dbl2big(yi);
5305  rel = rb_big_cmp(x, y);
5306  if (yf == 0.0 || rel != INT2FIX(0))
5307  return rel;
5308  if (yf < 0.0)
5309  return INT2FIX(1);
5310  return INT2FIX(-1);
5311 }
5312 
5313 VALUE
5315 {
5316  double yd = RFLOAT_VALUE(y);
5317  double yi, yf;
5318 
5319  if (isnan(yd) || isinf(yd))
5320  return Qfalse;
5321  yf = modf(yd, &yi);
5322  if (yf != 0)
5323  return Qfalse;
5324  if (FIXNUM_P(x)) {
5325 #if SIZEOF_LONG * CHAR_BIT < DBL_MANT_DIG /* assume FLT_RADIX == 2 */
5326  double xd = (double)FIX2LONG(x);
5327  if (xd != yd)
5328  return Qfalse;
5329  return Qtrue;
5330 #else
5331  long xn, yn;
5332  if (yi < LONG_MIN || LONG_MAX < yi)
5333  return Qfalse;
5334  xn = FIX2LONG(x);
5335  yn = (long)yi;
5336  if (xn != yn)
5337  return Qfalse;
5338  return Qtrue;
5339 #endif
5340  }
5341  y = rb_dbl2big(yi);
5342  return rb_big_eq(x, y);
5343 }
5344 
5345 VALUE
5347 {
5348  if (FIXNUM_P(y)) {
5349  x = bigfixize(x);
5350  if (FIXNUM_P(x)) {
5351  /* SIGNED_VALUE and Fixnum have same sign-bits, same
5352  * order */
5353  SIGNED_VALUE sx = (SIGNED_VALUE)x, sy = (SIGNED_VALUE)y;
5354  if (sx < sy) return INT2FIX(-1);
5355  return INT2FIX(sx > sy);
5356  }
5357  }
5358  else if (RB_BIGNUM_TYPE_P(y)) {
5359  if (BIGNUM_SIGN(x) == BIGNUM_SIGN(y)) {
5360  int cmp = bary_cmp(BDIGITS(x), BIGNUM_LEN(x), BDIGITS(y), BIGNUM_LEN(y));
5361  return INT2FIX(BIGNUM_SIGN(x) ? cmp : -cmp);
5362  }
5363  }
5364  else if (RB_FLOAT_TYPE_P(y)) {
5365  return rb_integer_float_cmp(x, y);
5366  }
5367  else {
5368  return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
5369  }
5370  return INT2FIX(BIGNUM_SIGN(x) ? 1 : -1);
5371 }
5372 
5373 enum big_op_t {
5378 };
5379 
5380 static VALUE
5381 big_op(VALUE x, VALUE y, enum big_op_t op)
5382 {
5383  VALUE rel;
5384  int n;
5385 
5386  if (RB_INTEGER_TYPE_P(y)) {
5387  rel = rb_big_cmp(x, y);
5388  }
5389  else if (RB_FLOAT_TYPE_P(y)) {
5390  rel = rb_integer_float_cmp(x, y);
5391  }
5392  else {
5393  ID id = 0;
5394  switch (op) {
5395  case big_op_gt: id = '>'; break;
5396  case big_op_ge: id = rb_intern(">="); break;
5397  case big_op_lt: id = '<'; break;
5398  case big_op_le: id = rb_intern("<="); break;
5399  }
5400  return rb_num_coerce_relop(x, y, id);
5401  }
5402 
5403  if (NIL_P(rel)) return Qfalse;
5404  n = FIX2INT(rel);
5405 
5406  switch (op) {
5407  case big_op_gt: return n > 0 ? Qtrue : Qfalse;
5408  case big_op_ge: return n >= 0 ? Qtrue : Qfalse;
5409  case big_op_lt: return n < 0 ? Qtrue : Qfalse;
5410  case big_op_le: return n <= 0 ? Qtrue : Qfalse;
5411  }
5412  return Qundef;
5413 }
5414 
5415 VALUE
5417 {
5418  return big_op(x, y, big_op_gt);
5419 }
5420 
5421 VALUE
5423 {
5424  return big_op(x, y, big_op_ge);
5425 }
5426 
5427 VALUE
5429 {
5430  return big_op(x, y, big_op_lt);
5431 }
5432 
5433 VALUE
5435 {
5436  return big_op(x, y, big_op_le);
5437 }
5438 
5439 /*
5440  * call-seq:
5441  * big == obj -> true or false
5442  *
5443  * Returns <code>true</code> only if <i>obj</i> has the same value
5444  * as <i>big</i>. Contrast this with <code>Integer#eql?</code>, which
5445  * requires <i>obj</i> to be a <code>Integer</code>.
5446  *
5447  * 68719476736 == 68719476736.0 #=> true
5448  */
5449 
5450 VALUE
5452 {
5453  if (FIXNUM_P(y)) {
5454  return bignorm(x) == y ? Qtrue : Qfalse;
5455  }
5456  else if (RB_BIGNUM_TYPE_P(y)) {
5457  }
5458  else if (RB_FLOAT_TYPE_P(y)) {
5459  return rb_integer_float_eq(x, y);
5460  }
5461  else {
5462  return rb_equal(y, x);
5463  }
5464  if (BIGNUM_SIGN(x) != BIGNUM_SIGN(y)) return Qfalse;
5465  if (BIGNUM_LEN(x) != BIGNUM_LEN(y)) return Qfalse;
5466  if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,BIGNUM_LEN(y)) != 0) return Qfalse;
5467  return Qtrue;
5468 }
5469 
5470 VALUE
5472 {
5473  if (!RB_BIGNUM_TYPE_P(y)) return Qfalse;
5474  if (BIGNUM_SIGN(x) != BIGNUM_SIGN(y)) return Qfalse;
5475  if (BIGNUM_LEN(x) != BIGNUM_LEN(y)) return Qfalse;
5476  if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,BIGNUM_LEN(y)) != 0) return Qfalse;
5477  return Qtrue;
5478 }
5479 
5480 VALUE
5482 {
5483  VALUE z = rb_big_clone(x);
5484 
5485  BIGNUM_NEGATE(z);
5486 
5487  return bignorm(z);
5488 }
5489 
5490 VALUE
5492 {
5493  VALUE z = rb_big_clone(x);
5494  BDIGIT *ds = BDIGITS(z);
5495  long n = BIGNUM_LEN(z);
5496 
5497  if (!n) return INT2FIX(-1);
5498 
5499  if (BIGNUM_POSITIVE_P(z)) {
5500  if (bary_add_one(ds, n)) {
5501  big_extend_carry(z);
5502  }
5504  }
5505  else {
5506  bary_neg(ds, n);
5507  if (bary_add_one(ds, n))
5508  return INT2FIX(-1);
5509  bary_neg(ds, n);
5511  }
5512 
5513  return bignorm(z);
5514 }
5515 
5516 static VALUE
5518 {
5519  VALUE z;
5520  BDIGIT *xds, *yds, *zds;
5521  long xn, yn, zn;
5522 
5523  xn = BIGNUM_LEN(x);
5524  yn = BIGNUM_LEN(y);
5525  zn = xn < yn ? yn : xn;
5526 
5527  z = bignew(zn, 1);
5528 
5529  xds = BDIGITS(x);
5530  yds = BDIGITS(y);
5531  zds = BDIGITS(z);
5532 
5533  if (bary_sub(zds, zn, xds, xn, yds, yn)) {
5534  bary_2comp(zds, zn);
5536  }
5537 
5538  return z;
5539 }
5540 
5541 static VALUE bigadd_int(VALUE x, long y);
5542 
5543 static VALUE
5544 bigsub_int(VALUE x, long y0)
5545 {
5546  VALUE z;
5547  BDIGIT *xds, *zds;
5548  long xn, zn;
5549  BDIGIT_DBL_SIGNED num;
5550  long i, y;
5551 
5552  y = y0;
5553  xds = BDIGITS(x);
5554  xn = BIGNUM_LEN(x);
5555 
5556  if (xn == 0)
5557  return LONG2NUM(-y0);
5558 
5559  zn = xn;
5560 #if SIZEOF_BDIGIT < SIZEOF_LONG
5561  if (zn < bdigit_roomof(SIZEOF_LONG))
5562  zn = bdigit_roomof(SIZEOF_LONG);
5563 #endif
5564  z = bignew(zn, BIGNUM_SIGN(x));
5565  zds = BDIGITS(z);
5566 
5567 #if SIZEOF_BDIGIT >= SIZEOF_LONG
5568  assert(xn == zn);
5569  num = (BDIGIT_DBL_SIGNED)xds[0] - y;
5570  if (xn == 1 && num < 0) {
5571  BIGNUM_NEGATE(z);
5572  zds[0] = (BDIGIT)-num;
5573  RB_GC_GUARD(x);
5574  return bignorm(z);
5575  }
5576  zds[0] = BIGLO(num);
5577  num = BIGDN(num);
5578  i = 1;
5579  if (i < xn)
5580  goto y_is_zero_x;
5581  goto finish;
5582 #else
5583  num = 0;
5584  for (i=0; i < xn; i++) {
5585  if (y == 0) goto y_is_zero_x;
5586  num += (BDIGIT_DBL_SIGNED)xds[i] - BIGLO(y);
5587  zds[i] = BIGLO(num);
5588  num = BIGDN(num);
5589  y = BIGDN(y);
5590  }
5591  for (; i < zn; i++) {
5592  if (y == 0) goto y_is_zero_z;
5593  num -= BIGLO(y);
5594  zds[i] = BIGLO(num);
5595  num = BIGDN(num);
5596  y = BIGDN(y);
5597  }
5598  goto finish;
5599 #endif
5600 
5601  for (; i < xn; i++) {
5602  y_is_zero_x:
5603  if (num == 0) goto num_is_zero_x;
5604  num += xds[i];
5605  zds[i] = BIGLO(num);
5606  num = BIGDN(num);
5607  }
5608 #if SIZEOF_BDIGIT < SIZEOF_LONG
5609  for (; i < zn; i++) {
5610  y_is_zero_z:
5611  if (num == 0) goto num_is_zero_z;
5612  zds[i] = BIGLO(num);
5613  num = BIGDN(num);
5614  }
5615 #endif
5616  goto finish;
5617 
5618  for (; i < xn; i++) {
5619  num_is_zero_x:
5620  zds[i] = xds[i];
5621  }
5622 #if SIZEOF_BDIGIT < SIZEOF_LONG
5623  for (; i < zn; i++) {
5624  num_is_zero_z:
5625  zds[i] = 0;
5626  }
5627 #endif
5628  goto finish;
5629 
5630  finish:
5631  assert(num == 0 || num == -1);
5632  if (num < 0) {
5633  get2comp(z);
5634  BIGNUM_NEGATE(z);
5635  }
5636  RB_GC_GUARD(x);
5637  return bignorm(z);
5638 }
5639 
5640 static VALUE
5641 bigadd_int(VALUE x, long y)
5642 {
5643  VALUE z;
5644  BDIGIT *xds, *zds;
5645  long xn, zn;
5646  BDIGIT_DBL num;
5647  long i;
5648 
5649  xds = BDIGITS(x);
5650  xn = BIGNUM_LEN(x);
5651 
5652  if (xn == 0)
5653  return LONG2NUM(y);
5654 
5655  zn = xn;
5656 #if SIZEOF_BDIGIT < SIZEOF_LONG
5657  if (zn < bdigit_roomof(SIZEOF_LONG))
5658  zn = bdigit_roomof(SIZEOF_LONG);
5659 #endif
5660  zn++;
5661 
5662  z = bignew(zn, BIGNUM_SIGN(x));
5663  zds = BDIGITS(z);
5664 
5665 #if SIZEOF_BDIGIT >= SIZEOF_LONG
5666  num = (BDIGIT_DBL)xds[0] + y;
5667  zds[0] = BIGLO(num);
5668  num = BIGDN(num);
5669  i = 1;
5670  if (i < xn)
5671  goto y_is_zero_x;
5672  goto y_is_zero_z;
5673 #else
5674  num = 0;
5675  for (i=0; i < xn; i++) {
5676  if (y == 0) goto y_is_zero_x;
5677  num += (BDIGIT_DBL)xds[i] + BIGLO(y);
5678  zds[i] = BIGLO(num);
5679  num = BIGDN(num);
5680  y = BIGDN(y);
5681  }
5682  for (; i < zn; i++) {
5683  if (y == 0) goto y_is_zero_z;
5684  num += BIGLO(y);
5685  zds[i] = BIGLO(num);
5686  num = BIGDN(num);
5687  y = BIGDN(y);
5688  }
5689  goto finish;
5690 
5691 #endif
5692 
5693  for (;i < xn; i++) {
5694  y_is_zero_x:
5695  if (num == 0) goto num_is_zero_x;
5696  num += (BDIGIT_DBL)xds[i];
5697  zds[i] = BIGLO(num);
5698  num = BIGDN(num);
5699  }
5700  for (; i < zn; i++) {
5701  y_is_zero_z:
5702  if (num == 0) goto num_is_zero_z;
5703  zds[i] = BIGLO(num);
5704  num = BIGDN(num);
5705  }
5706  goto finish;
5707 
5708  for (;i < xn; i++) {
5709  num_is_zero_x:
5710  zds[i] = xds[i];
5711  }
5712  for (; i < zn; i++) {
5713  num_is_zero_z:
5714  zds[i] = 0;
5715  }
5716  goto finish;
5717 
5718  finish:
5719  RB_GC_GUARD(x);
5720  return bignorm(z);
5721 }
5722 
5723 static VALUE
5724 bigadd(VALUE x, VALUE y, int sign)
5725 {
5726  VALUE z;
5727  size_t len;
5728 
5729  sign = (sign == BIGNUM_SIGN(y));
5730  if (BIGNUM_SIGN(x) != sign) {
5731  if (sign) return bigsub(y, x);
5732  return bigsub(x, y);
5733  }
5734 
5735  if (BIGNUM_LEN(x) > BIGNUM_LEN(y)) {
5736  len = BIGNUM_LEN(x) + 1;
5737  }
5738  else {
5739  len = BIGNUM_LEN(y) + 1;
5740  }
5741  z = bignew(len, sign);
5742 
5743  bary_add(BDIGITS(z), BIGNUM_LEN(z),
5744  BDIGITS(x), BIGNUM_LEN(x),
5745  BDIGITS(y), BIGNUM_LEN(y));
5746 
5747  return z;
5748 }
5749 
5750 VALUE
5752 {
5753  long n;
5754 
5755  if (FIXNUM_P(y)) {
5756  n = FIX2LONG(y);
5757  if ((n > 0) != BIGNUM_SIGN(x)) {
5758  if (n < 0) {
5759  n = -n;
5760  }
5761  return bigsub_int(x, n);
5762  }
5763  if (n < 0) {
5764  n = -n;
5765  }
5766  return bigadd_int(x, n);
5767  }
5768  else if (RB_BIGNUM_TYPE_P(y)) {
5769  return bignorm(bigadd(x, y, 1));
5770  }
5771  else if (RB_FLOAT_TYPE_P(y)) {
5772  return DBL2NUM(rb_big2dbl(x) + RFLOAT_VALUE(y));
5773  }
5774  else {
5775  return rb_num_coerce_bin(x, y, '+');
5776  }
5777 }
5778 
5779 VALUE
5781 {
5782  long n;
5783 
5784  if (FIXNUM_P(y)) {
5785  n = FIX2LONG(y);
5786  if ((n > 0) != BIGNUM_SIGN(x)) {
5787  if (n < 0) {
5788  n = -n;
5789  }
5790  return bigadd_int(x, n);
5791  }
5792  if (n < 0) {
5793  n = -n;
5794  }
5795  return bigsub_int(x, n);
5796  }
5797  else if (RB_BIGNUM_TYPE_P(y)) {
5798  return bignorm(bigadd(x, y, 0));
5799  }
5800  else if (RB_FLOAT_TYPE_P(y)) {
5801  return DBL2NUM(rb_big2dbl(x) - RFLOAT_VALUE(y));
5802  }
5803  else {
5804  return rb_num_coerce_bin(x, y, '-');
5805  }
5806 }
5807 
5808 static VALUE
5810 {
5811  long xn, zn;
5812  VALUE z;
5813  BDIGIT *xds, *zds;
5814 
5815  xn = BIGNUM_LEN(x);
5816  zn = 2 * xn;
5817 
5818  z = bignew(zn, 1);
5819 
5820  xds = BDIGITS(x);
5821  zds = BDIGITS(z);
5822 
5823 #ifdef USE_GMP
5824  if (xn < GMP_MUL_DIGITS)
5825  bary_sq_fast(zds, zn, xds, xn);
5826  else
5827  bary_mul(zds, zn, xds, xn, xds, xn);
5828 #else
5829  if (xn < KARATSUBA_MUL_DIGITS)
5830  bary_sq_fast(zds, zn, xds, xn);
5831  else
5832  bary_mul(zds, zn, xds, xn, xds, xn);
5833 #endif
5834 
5835  RB_GC_GUARD(x);
5836  return z;
5837 }
5838 
5839 static VALUE
5841 {
5842  long xn, yn, zn;
5843  VALUE z;
5844  BDIGIT *xds, *yds, *zds;
5845 
5846  if (x == y)
5847  return bigsq(x);
5848 
5849  xn = BIGNUM_LEN(x);
5850  yn = BIGNUM_LEN(y);
5851  zn = xn + yn;
5852 
5853  z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
5854 
5855  xds = BDIGITS(x);
5856  yds = BDIGITS(y);
5857  zds = BDIGITS(z);
5858 
5859  bary_mul(zds, zn, xds, xn, yds, yn);
5860 
5861  RB_GC_GUARD(x);
5862  RB_GC_GUARD(y);
5863  return z;
5864 }
5865 
5866 VALUE
5868 {
5869  if (FIXNUM_P(y)) {
5870  y = rb_int2big(FIX2LONG(y));
5871  }
5872  else if (RB_BIGNUM_TYPE_P(y)) {
5873  }
5874  else if (RB_FLOAT_TYPE_P(y)) {
5875  return DBL2NUM(rb_big2dbl(x) * RFLOAT_VALUE(y));
5876  }
5877  else {
5878  return rb_num_coerce_bin(x, y, '*');
5879  }
5880 
5881  return bignorm(bigmul0(x, y));
5882 }
5883 
5884 static VALUE
5885 bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
5886 {
5887  long xn = BIGNUM_LEN(x), yn = BIGNUM_LEN(y);
5888  VALUE z;
5889  BDIGIT *xds, *yds, *zds;
5890  BDIGIT dd;
5891 
5892  VALUE q = Qnil, r = Qnil;
5893  BDIGIT *qds, *rds;
5894  long qn, rn;
5895 
5896  yds = BDIGITS(y);
5897  BARY_TRUNC(yds, yn);
5898  if (yn == 0)
5899  rb_num_zerodiv();
5900 
5901  xds = BDIGITS(x);
5902  BARY_TRUNC(xds, xn);
5903 
5904  if (xn < yn || (xn == yn && xds[xn - 1] < yds[yn - 1])) {
5905  if (divp) *divp = rb_int2big(0);
5906  if (modp) *modp = x;
5907  return Qnil;
5908  }
5909  if (yn == 1) {
5910  dd = yds[0];
5911  z = bignew(xn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
5912  zds = BDIGITS(z);
5913  dd = bigdivrem_single(zds, xds, xn, dd);
5914  if (modp) {
5915  *modp = rb_uint2big((VALUE)dd);
5916  BIGNUM_SET_SIGN(*modp, BIGNUM_SIGN(x));
5917  }
5918  if (divp) *divp = z;
5919  return Qnil;
5920  }
5921  if (xn == 2 && yn == 2) {
5922  BDIGIT_DBL x0 = bary2bdigitdbl(xds, 2);
5923  BDIGIT_DBL y0 = bary2bdigitdbl(yds, 2);
5924  BDIGIT_DBL q0 = x0 / y0;
5925  BDIGIT_DBL r0 = x0 % y0;
5926  if (divp) {
5927  z = bignew(bdigit_roomof(sizeof(BDIGIT_DBL)), BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
5928  zds = BDIGITS(z);
5929  zds[0] = BIGLO(q0);
5930  zds[1] = BIGLO(BIGDN(q0));
5931  *divp = z;
5932  }
5933  if (modp) {
5934  z = bignew(bdigit_roomof(sizeof(BDIGIT_DBL)), BIGNUM_SIGN(x));
5935  zds = BDIGITS(z);
5936  zds[0] = BIGLO(r0);
5937  zds[1] = BIGLO(BIGDN(r0));
5938  *modp = z;
5939  }
5940  return Qnil;
5941  }
5942 
5943  if (divp) {
5944  qn = xn + BIGDIVREM_EXTRA_WORDS;
5945  q = bignew(qn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));
5946  qds = BDIGITS(q);
5947  }
5948  else {
5949  qn = 0;
5950  qds = NULL;
5951  }
5952 
5953  if (modp) {
5954  rn = yn;
5955  r = bignew(rn, BIGNUM_SIGN(x));
5956  rds = BDIGITS(r);
5957  }
5958  else {
5959  rn = 0;
5960  rds = NULL;
5961  }
5962 
5963  bary_divmod_branch(qds, qn, rds, rn, xds, xn, yds, yn);
5964 
5965  if (divp) {
5966  bigtrunc(q);
5967  *divp = q;
5968  }
5969  if (modp) {
5970  bigtrunc(r);
5971  *modp = r;
5972  }
5973 
5974  return Qnil;
5975 }
5976 
5977 static void
5978 bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
5979 {
5980  VALUE mod;
5981 
5982  bigdivrem(x, y, divp, &mod);
5983  if (BIGNUM_SIGN(x) != BIGNUM_SIGN(y) && !BIGZEROP(mod)) {
5984  if (divp) *divp = bigadd(*divp, rb_int2big(1), 0);
5985  if (modp) *modp = bigadd(mod, y, 1);
5986  }
5987  else if (modp) {
5988  *modp = mod;
5989  }
5990 }
5991 
5992 
5993 static VALUE
5995 {
5996  VALUE z;
5997 
5998  if (FIXNUM_P(y)) {
5999  y = rb_int2big(FIX2LONG(y));
6000  }
6001  else if (RB_BIGNUM_TYPE_P(y)) {
6002  }
6003  else if (RB_FLOAT_TYPE_P(y)) {
6004  if (op == '/') {
6005  return DBL2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
6006  }
6007  else {
6008  double dy = RFLOAT_VALUE(y);
6009  if (dy == 0.0) rb_num_zerodiv();
6010  return rb_dbl2big(rb_big2dbl(x) / dy);
6011  }
6012  }
6013  else {
6014  return rb_num_coerce_bin(x, y, op);
6015  }
6016  bigdivmod(x, y, &z, 0);
6017 
6018  return bignorm(z);
6019 }
6020 
6021 VALUE
6023 {
6024  return rb_big_divide(x, y, '/');
6025 }
6026 
6027 VALUE
6029 {
6030  return rb_big_divide(x, y, rb_intern("div"));
6031 }
6032 
6033 VALUE
6035 {
6036  VALUE z;
6037 
6038  if (FIXNUM_P(y)) {
6039  y = rb_int2big(FIX2LONG(y));
6040  }
6041  else if (!RB_BIGNUM_TYPE_P(y)) {
6042  return rb_num_coerce_bin(x, y, '%');
6043  }
6044  bigdivmod(x, y, 0, &z);
6045 
6046  return bignorm(z);
6047 }
6048 
6049 VALUE
6051 {
6052  VALUE z;
6053 
6054  if (FIXNUM_P(y)) {
6055  y = rb_int2big(FIX2LONG(y));
6056  }
6057  else if (!RB_BIGNUM_TYPE_P(y)) {
6058  return rb_num_coerce_bin(x, y, rb_intern("remainder"));
6059  }
6060  bigdivrem(x, y, 0, &z);
6061 
6062  return bignorm(z);
6063 }
6064 
6065 VALUE
6067 {
6068  VALUE div, mod;
6069 
6070  if (FIXNUM_P(y)) {
6071  y = rb_int2big(FIX2LONG(y));
6072  }
6073  else if (!RB_BIGNUM_TYPE_P(y)) {
6074  return rb_num_coerce_bin(x, y, rb_intern("divmod"));
6075  }
6076  bigdivmod(x, y, &div, &mod);
6077 
6078  return rb_assoc_new(bignorm(div), bignorm(mod));
6079 }
6080 
6081 static VALUE
6082 big_shift(VALUE x, long n)
6083 {
6084  if (n < 0)
6085  return big_lshift(x, 1+(unsigned long)(-(n+1)));
6086  else if (n > 0)
6087  return big_rshift(x, (unsigned long)n);
6088  return x;
6089 }
6090 
6091 static double
6092 big_fdiv(VALUE x, VALUE y, long ey)
6093 {
6094 #define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG)
6095  VALUE z;
6096  long l, ex;
6097 
6098  bigtrunc(x);
6099  l = BIGNUM_LEN(x);
6100  ex = l * BITSPERDIG - nlz(BDIGITS(x)[l-1]);
6101  ex -= 2 * DBL_BIGDIG * BITSPERDIG;
6102  if (ex > BITSPERDIG) ex -= BITSPERDIG;
6103  else if (ex > 0) ex = 0;
6104  if (ex) x = big_shift(x, ex);
6105 
6106  bigdivrem(x, y, &z, 0);
6107  l = ex - ey;
6108 #if SIZEOF_LONG > SIZEOF_INT
6109  {
6110  /* Visual C++ can't be here */
6111  if (l > INT_MAX) return INFINITY;
6112  if (l < INT_MIN) return 0.0;
6113  }
6114 #endif
6115  return ldexp(big2dbl(z), (int)l);
6116 }
6117 
6118 static double
6120 {
6121  long l, ey;
6122  bigtrunc(y);
6123  l = BIGNUM_LEN(y);
6124  ey = l * BITSPERDIG - nlz(BDIGITS(y)[l-1]);
6125  ey -= DBL_BIGDIG * BITSPERDIG;
6126  if (ey) y = big_shift(y, ey);
6127  return big_fdiv(x, y, ey);
6128 }
6129 
6130 static double
6132 {
6133  int i;
6134  y = dbl2big(ldexp(frexp(RFLOAT_VALUE(y), &i), DBL_MANT_DIG));
6135  return big_fdiv(x, y, i - DBL_MANT_DIG);
6136 }
6137 
6138 double
6140 {
6141  double dx, dy;
6142 
6143  dx = big2dbl(x);
6144  if (FIXNUM_P(y)) {
6145  dy = (double)FIX2LONG(y);
6146  if (isinf(dx))
6147  return big_fdiv_int(x, rb_int2big(FIX2LONG(y)));
6148  }
6149  else if (RB_BIGNUM_TYPE_P(y)) {
6150  dy = rb_big2dbl(y);
6151  if (isinf(dx) || isinf(dy))
6152  return big_fdiv_int(x, y);
6153  }
6154  else if (RB_FLOAT_TYPE_P(y)) {
6155  dy = RFLOAT_VALUE(y);
6156  if (isnan(dy))
6157  return dy;
6158  if (isinf(dx))
6159  return big_fdiv_float(x, y);
6160  }
6161  else {
6162  return RFLOAT_VALUE(rb_num_coerce_bin(x, y, rb_intern("fdiv")));
6163  }
6164  return dx / dy;
6165 }
6166 
6167 VALUE
6169 {
6170  return DBL2NUM(rb_big_fdiv_double(x, y));
6171 }
6172 
6173 VALUE
6175 {
6176  double d;
6177  SIGNED_VALUE yy;
6178 
6179  again:
6180  if (y == INT2FIX(0)) return INT2FIX(1);
6181  if (RB_FLOAT_TYPE_P(y)) {
6182  d = RFLOAT_VALUE(y);
6183  if ((BIGNUM_NEGATIVE_P(x) && !BIGZEROP(x)) && d != round(d))
6184  return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
6185  }
6186  else if (RB_BIGNUM_TYPE_P(y)) {
6187  y = bignorm(y);
6188  if (FIXNUM_P(y))
6189  goto again;
6190  rb_warn("in a**b, b may be too big");
6191  d = rb_big2dbl(y);
6192  }
6193  else if (FIXNUM_P(y)) {
6194  yy = FIX2LONG(y);
6195 
6196  if (yy < 0)
6197  return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y);
6198  else {
6199  VALUE z = 0;
6200  SIGNED_VALUE mask;
6201  const size_t xbits = rb_absint_numwords(x, 1, NULL);
6202  const size_t BIGLEN_LIMIT = 32*1024*1024;
6203 
6204  if (xbits == (size_t)-1 ||
6205  (xbits > BIGLEN_LIMIT) ||
6206  (xbits * yy > BIGLEN_LIMIT)) {
6207  rb_warn("in a**b, b may be too big");
6208  d = (double)yy;
6209  }
6210  else {
6211  for (mask = FIXNUM_MAX + 1; mask; mask >>= 1) {
6212  if (z) z = bigsq(z);
6213  if (yy & mask) {
6214  z = z ? bigtrunc(bigmul0(z, x)) : x;
6215  }
6216  }
6217  return bignorm(z);
6218  }
6219  }
6220  }
6221  else {
6222  return rb_num_coerce_bin(x, y, rb_intern("**"));
6223  }
6224  return DBL2NUM(pow(rb_big2dbl(x), d));
6225 }
6226 
6227 static VALUE
6228 bigand_int(VALUE x, long xn, BDIGIT hibitsx, long y)
6229 {
6230  VALUE z;
6231  BDIGIT *xds, *zds;
6232  long zn;
6233  long i;
6234  BDIGIT hibitsy;
6235 
6236  if (y == 0) return INT2FIX(0);
6237  if (xn == 0) return hibitsx ? LONG2NUM(y) : 0;
6238  hibitsy = 0 <= y ? 0 : BDIGMAX;
6239  xds = BDIGITS(x);
6240 #if SIZEOF_BDIGIT >= SIZEOF_LONG
6241  if (!hibitsy) {
6242  y &= xds[0];
6243  return LONG2NUM(y);
6244  }
6245 #endif
6246 
6247  zn = xn;
6248 #if SIZEOF_BDIGIT < SIZEOF_LONG
6249  if (hibitsx && zn < bdigit_roomof(SIZEOF_LONG))
6250  zn = bdigit_roomof(SIZEOF_LONG);
6251 #endif
6252 
6253  z = bignew(zn, 0);
6254  zds = BDIGITS(z);
6255 
6256 #if SIZEOF_BDIGIT >= SIZEOF_LONG
6257  i = 1;
6258  zds[0] = xds[0] & BIGLO(y);
6259 #else
6260  for (i=0; i < xn; i++) {
6261  if (y == 0 || y == -1) break;
6262  zds[i] = xds[i] & BIGLO(y);
6263  y = BIGDN(y);
6264  }
6265  for (; i < zn; i++) {
6266  if (y == 0 || y == -1) break;
6267  zds[i] = hibitsx & BIGLO(y);
6268  y = BIGDN(y);
6269  }
6270 #endif
6271  for (;i < xn; i++) {
6272  zds[i] = xds[i] & hibitsy;
6273  }
6274  for (;i < zn; i++) {
6275  zds[i] = hibitsx & hibitsy;
6276  }
6277  twocomp2abs_bang(z, hibitsx && hibitsy);
6278  RB_GC_GUARD(x);
6279  return bignorm(z);
6280 }
6281 
6282 VALUE
6284 {
6285  VALUE z;
6286  BDIGIT *ds1, *ds2, *zds;
6287  long i, xn, yn, n1, n2;
6288  BDIGIT hibitsx, hibitsy;
6289  BDIGIT hibits1, hibits2;
6290  VALUE tmpv;
6291  BDIGIT tmph;
6292  long tmpn;
6293 
6294  if (!RB_INTEGER_TYPE_P(y)) {
6295  return rb_num_coerce_bit(x, y, '&');
6296  }
6297 
6298  hibitsx = abs2twocomp(&x, &xn);
6299  if (FIXNUM_P(y)) {
6300  return bigand_int(x, xn, hibitsx, FIX2LONG(y));
6301  }
6302  hibitsy = abs2twocomp(&y, &yn);
6303  if (xn > yn) {
6304  tmpv = x; x = y; y = tmpv;
6305  tmpn = xn; xn = yn; yn = tmpn;
6306  tmph = hibitsx; hibitsx = hibitsy; hibitsy = tmph;
6307  }
6308  n1 = xn;
6309  n2 = yn;
6310  ds1 = BDIGITS(x);
6311  ds2 = BDIGITS(y);
6312  hibits1 = hibitsx;
6313  hibits2 = hibitsy;
6314 
6315  if (!hibits1)
6316  n2 = n1;
6317 
6318  z = bignew(n2, 0);
6319  zds = BDIGITS(z);
6320 
6321  for (i=0; i<n1; i++) {
6322  zds[i] = ds1[i] & ds2[i];
6323  }
6324  for (; i<n2; i++) {
6325  zds[i] = hibits1 & ds2[i];
6326  }
6327  twocomp2abs_bang(z, hibits1 && hibits2);
6328  RB_GC_GUARD(x);
6329  RB_GC_GUARD(y);
6330  return bignorm(z);
6331 }
6332 
6333 static VALUE
6334 bigor_int(VALUE x, long xn, BDIGIT hibitsx, long y)
6335 {
6336  VALUE z;
6337  BDIGIT *xds, *zds;
6338  long zn;
6339  long i;
6340  BDIGIT hibitsy;
6341 
6342  if (y == -1) return INT2FIX(-1);
6343  if (xn == 0) return hibitsx ? INT2FIX(-1) : LONG2FIX(y);
6344  hibitsy = 0 <= y ? 0 : BDIGMAX;
6345  xds = BDIGITS(x);
6346 
6347  zn = BIGNUM_LEN(x);
6348 #if SIZEOF_BDIGIT < SIZEOF_LONG
6349  if (zn < bdigit_roomof(SIZEOF_LONG))
6350  zn = bdigit_roomof(SIZEOF_LONG);
6351 #endif
6352  z = bignew(zn, 0);
6353  zds = BDIGITS(z);
6354 
6355 #if SIZEOF_BDIGIT >= SIZEOF_LONG
6356  i = 1;
6357  zds[0] = xds[0] | BIGLO(y);
6358  if (i < zn)
6359  goto y_is_fixed_point;
6360  goto finish;
6361 #else
6362  for (i=0; i < xn; i++) {
6363  if (y == 0 || y == -1) goto y_is_fixed_point;
6364  zds[i] = xds[i] | BIGLO(y);
6365  y = BIGDN(y);
6366  }
6367  if (hibitsx)
6368  goto fill_hibits;
6369  for (; i < zn; i++) {
6370  if (y == 0 || y == -1) goto y_is_fixed_point;
6371  zds[i] = BIGLO(y);
6372  y = BIGDN(y);
6373  }
6374  goto finish;
6375 #endif
6376 
6377  y_is_fixed_point:
6378  if (hibitsy)
6379  goto fill_hibits;
6380  for (; i < xn; i++) {
6381  zds[i] = xds[i];
6382  }
6383  if (hibitsx)
6384  goto fill_hibits;
6385  for (; i < zn; i++) {
6386  zds[i] = 0;
6387  }
6388  goto finish;
6389 
6390  fill_hibits:
6391  for (; i < zn; i++) {
6392  zds[i] = BDIGMAX;
6393  }
6394 
6395  finish:
6396  twocomp2abs_bang(z, hibitsx || hibitsy);
6397  RB_GC_GUARD(x);
6398  return bignorm(z);
6399 }
6400 
6401 VALUE
6403 {
6404  VALUE z;
6405  BDIGIT *ds1, *ds2, *zds;
6406  long i, xn, yn, n1, n2;
6407  BDIGIT hibitsx, hibitsy;
6408  BDIGIT hibits1, hibits2;
6409  VALUE tmpv;
6410  BDIGIT tmph;
6411  long tmpn;
6412 
6413  if (!RB_INTEGER_TYPE_P(y)) {
6414  return rb_num_coerce_bit(x, y, '|');
6415  }
6416 
6417  hibitsx = abs2twocomp(&x, &xn);
6418  if (FIXNUM_P(y)) {
6419  return bigor_int(x, xn, hibitsx, FIX2LONG(y));
6420  }
6421  hibitsy = abs2twocomp(&y, &yn);
6422  if (xn > yn) {
6423  tmpv = x; x = y; y = tmpv;
6424  tmpn = xn; xn = yn; yn = tmpn;
6425  tmph = hibitsx; hibitsx = hibitsy; hibitsy = tmph;
6426  }
6427  n1 = xn;
6428  n2 = yn;
6429  ds1 = BDIGITS(x);
6430  ds2 = BDIGITS(y);
6431  hibits1 = hibitsx;
6432  hibits2 = hibitsy;
6433 
6434  if (hibits1)
6435  n2 = n1;
6436 
6437  z = bignew(n2, 0);
6438  zds = BDIGITS(z);
6439 
6440  for (i=0; i<n1; i++) {
6441  zds[i] = ds1[i] | ds2[i];
6442  }
6443  for (; i<n2; i++) {
6444  zds[i] = hibits1 | ds2[i];
6445  }
6446  twocomp2abs_bang(z, hibits1 || hibits2);
6447  RB_GC_GUARD(x);
6448  RB_GC_GUARD(y);
6449  return bignorm(z);
6450 }
6451 
6452 static VALUE
6453 bigxor_int(VALUE x, long xn, BDIGIT hibitsx, long y)
6454 {
6455  VALUE z;
6456  BDIGIT *xds, *zds;
6457  long zn;
6458  long i;
6459  BDIGIT hibitsy;
6460 
6461  hibitsy = 0 <= y ? 0 : BDIGMAX;
6462  xds = BDIGITS(x);
6463  zn = BIGNUM_LEN(x);
6464 #if SIZEOF_BDIGIT < SIZEOF_LONG
6465  if (zn < bdigit_roomof(SIZEOF_LONG))
6466  zn = bdigit_roomof(SIZEOF_LONG);
6467 #endif
6468  z = bignew(zn, 0);
6469  zds = BDIGITS(z);
6470 
6471 #if SIZEOF_BDIGIT >= SIZEOF_LONG
6472  i = 1;
6473  zds[0] = xds[0] ^ BIGLO(y);
6474 #else
6475  for (i = 0; i < xn; i++) {
6476  zds[i] = xds[i] ^ BIGLO(y);
6477  y = BIGDN(y);
6478  }
6479  for (; i < zn; i++) {
6480  zds[i] = hibitsx ^ BIGLO(y);
6481  y = BIGDN(y);
6482  }
6483 #endif
6484  for (; i < xn; i++) {
6485  zds[i] = xds[i] ^ hibitsy;
6486  }
6487  for (; i < zn; i++) {
6488  zds[i] = hibitsx ^ hibitsy;
6489  }
6490  twocomp2abs_bang(z, (hibitsx ^ hibitsy) != 0);
6491  RB_GC_GUARD(x);
6492  return bignorm(z);
6493 }
6494 
6495 VALUE
6497 {
6498  VALUE z;
6499  BDIGIT *ds1, *ds2, *zds;
6500  long i, xn, yn, n1, n2;
6501  BDIGIT hibitsx, hibitsy;
6502  BDIGIT hibits1, hibits2;
6503  VALUE tmpv;
6504  BDIGIT tmph;
6505  long tmpn;
6506 
6507  if (!RB_INTEGER_TYPE_P(y)) {
6508  return rb_num_coerce_bit(x, y, '^');
6509  }
6510 
6511  hibitsx = abs2twocomp(&x, &xn);
6512  if (FIXNUM_P(y)) {
6513  return bigxor_int(x, xn, hibitsx, FIX2LONG(y));
6514  }
6515  hibitsy = abs2twocomp(&y, &yn);
6516  if (xn > yn) {
6517  tmpv = x; x = y; y = tmpv;
6518  tmpn = xn; xn = yn; yn = tmpn;
6519  tmph = hibitsx; hibitsx = hibitsy; hibitsy = tmph;
6520  }
6521  n1 = xn;
6522  n2 = yn;
6523  ds1 = BDIGITS(x);
6524  ds2 = BDIGITS(y);
6525  hibits1 = hibitsx;
6526  hibits2 = hibitsy;
6527 
6528  z = bignew(n2, 0);
6529  zds = BDIGITS(z);
6530 
6531  for (i=0; i<n1; i++) {
6532  zds[i] = ds1[i] ^ ds2[i];
6533  }
6534  for (; i<n2; i++) {
6535  zds[i] = hibitsx ^ ds2[i];
6536  }
6537  twocomp2abs_bang(z, (hibits1 ^ hibits2) != 0);
6538  RB_GC_GUARD(x);
6539  RB_GC_GUARD(y);
6540  return bignorm(z);
6541 }
6542 
6543 VALUE
6545 {
6546  int lshift_p;
6547  size_t shift_numdigits;
6548  int shift_numbits;
6549 
6550  for (;;) {
6551  if (FIXNUM_P(y)) {
6552  long l = FIX2LONG(y);
6553  unsigned long shift;
6554  if (0 <= l) {
6555  lshift_p = 1;
6556  shift = l;
6557  }
6558  else {
6559  lshift_p = 0;
6560  shift = 1+(unsigned long)(-(l+1));
6561  }
6562  shift_numbits = (int)(shift & (BITSPERDIG-1));
6563  shift_numdigits = shift >> bit_length(BITSPERDIG-1);
6564  return bignorm(big_shift3(x, lshift_p, shift_numdigits, shift_numbits));
6565  }
6566  else if (RB_BIGNUM_TYPE_P(y)) {
6567  return bignorm(big_shift2(x, 1, y));
6568  }
6569  y = rb_to_int(y);
6570  }
6571 }
6572 
6573 VALUE
6575 {
6576  int lshift_p;
6577  size_t shift_numdigits;
6578  int shift_numbits;
6579 
6580  for (;;) {
6581  if (FIXNUM_P(y)) {
6582  long l = FIX2LONG(y);
6583  unsigned long shift;
6584  if (0 <= l) {
6585  lshift_p = 0;
6586  shift = l;
6587  }
6588  else {
6589  lshift_p = 1;
6590  shift = 1+(unsigned long)(-(l+1));
6591  }
6592  shift_numbits = (int)(shift & (BITSPERDIG-1));
6593  shift_numdigits = shift >> bit_length(BITSPERDIG-1);
6594  return bignorm(big_shift3(x, lshift_p, shift_numdigits, shift_numbits));
6595  }
6596  else if (RB_BIGNUM_TYPE_P(y)) {
6597  return bignorm(big_shift2(x, 0, y));
6598  }
6599  y = rb_to_int(y);
6600  }
6601 }
6602 
6603 VALUE
6605 {
6606  BDIGIT *xds;
6607  size_t shift;
6608  size_t i, s1, s2;
6609  long l;
6610  BDIGIT bit;
6611 
6612  if (RB_BIGNUM_TYPE_P(y)) {
6613  if (BIGNUM_NEGATIVE_P(y))
6614  return INT2FIX(0);
6615  bigtrunc(y);
6616  if (BIGSIZE(y) > sizeof(size_t)) {
6617  out_of_range:
6618  return BIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1);
6619  }
6620 #if SIZEOF_SIZE_T <= SIZEOF_LONG
6621  shift = big2ulong(y, "long");
6622 #else
6623  shift = big2ull(y, "long long");
6624 #endif
6625  }
6626  else {
6627  l = NUM2LONG(y);
6628  if (l < 0) return INT2FIX(0);
6629  shift = (size_t)l;
6630  }
6631  s1 = shift/BITSPERDIG;
6632  s2 = shift%BITSPERDIG;
6633  bit = (BDIGIT)1 << s2;
6634 
6635  if (s1 >= BIGNUM_LEN(x)) goto out_of_range;
6636 
6637  xds = BDIGITS(x);
6638  if (BIGNUM_POSITIVE_P(x))
6639  return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
6640  if (xds[s1] & (bit-1))
6641  return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
6642  for (i = 0; i < s1; i++)
6643  if (xds[i])
6644  return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
6645  return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
6646 }
6647 
6648 VALUE
6650 {
6651  st_index_t hash;
6652 
6653  hash = rb_memhash(BDIGITS(x), sizeof(BDIGIT)*BIGNUM_LEN(x)) ^ BIGNUM_SIGN(x);
6654  return ST2FIX(hash);
6655 }
6656 
6657 /*
6658  * call-seq:
6659  * big.coerce(numeric) -> array
6660  *
6661  * Returns an array with both a +numeric+ and a +big+ represented as Bignum
6662  * objects.
6663  *
6664  * This is achieved by converting +numeric+ to a Bignum.
6665  *
6666  * A TypeError is raised if the +numeric+ is not a Fixnum or Bignum type.
6667  *
6668  * (0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
6669  */
6670 
6671 static VALUE
6673 {
6674  if (RB_INTEGER_TYPE_P(y)) {
6675  return rb_assoc_new(y, x);
6676  }
6677  else {
6678  x = rb_Float(x);
6679  y = rb_Float(y);
6680  return rb_assoc_new(y, x);
6681  }
6682 }
6683 
6684 VALUE
6686 {
6687  if (BIGNUM_NEGATIVE_P(x)) {
6688  x = rb_big_clone(x);
6690  }
6691  return x;
6692 }
6693 
6694 int
6696 {
6697  return BIGNUM_SIGN(x);
6698 }
6699 
6700 size_t
6702 {
6703  return BIGSIZE(big);
6704 }
6705 
6706 VALUE
6708 {
6709  return SIZET2NUM(rb_big_size(big));
6710 }
6711 
6712 VALUE
6714 {
6715  int nlz_bits;
6716  size_t numbytes;
6717 
6718  static const BDIGIT char_bit[1] = { CHAR_BIT };
6719  BDIGIT numbytes_bary[bdigit_roomof(sizeof(size_t))];
6720  BDIGIT nlz_bary[1];
6721  BDIGIT result_bary[bdigit_roomof(sizeof(size_t)+1)];
6722 
6723  numbytes = rb_absint_size(big, &nlz_bits);
6724 
6725  if (numbytes == 0)
6726  return LONG2FIX(0);
6727 
6728  if (BIGNUM_NEGATIVE_P(big) && rb_absint_singlebit_p(big)) {
6729  if (nlz_bits != CHAR_BIT-1) {
6730  nlz_bits++;
6731  }
6732  else {
6733  nlz_bits = 0;
6734  numbytes--;
6735  }
6736  }
6737 
6738  if (numbytes <= SIZE_MAX / CHAR_BIT) {
6739  return SIZET2NUM(numbytes * CHAR_BIT - nlz_bits);
6740  }
6741 
6742  nlz_bary[0] = nlz_bits;
6743 
6744  bary_unpack(BARY_ARGS(numbytes_bary), &numbytes, 1, sizeof(numbytes), 0,
6746  BARY_SHORT_MUL(result_bary, numbytes_bary, char_bit);
6747  BARY_SUB(result_bary, result_bary, nlz_bary);
6748 
6749  return rb_integer_unpack(result_bary, numberof(result_bary), sizeof(BDIGIT), 0,
6751 }
6752 
6753 VALUE
6755 {
6756  if (BIGNUM_LEN(num) != 0 && BDIGITS(num)[0] & 1) {
6757  return Qtrue;
6758  }
6759  return Qfalse;
6760 }
6761 
6762 VALUE
6764 {
6765  if (BIGNUM_LEN(num) != 0 && BDIGITS(num)[0] & 1) {
6766  return Qfalse;
6767  }
6768  return Qtrue;
6769 }
6770 
6771 /*
6772  * Bignum objects hold integers outside the range of
6773  * Fixnum. Bignum objects are created
6774  * automatically when integer calculations would otherwise overflow a
6775  * Fixnum. When a calculation involving
6776  * Bignum objects returns a result that will fit in a
6777  * Fixnum, the result is automatically converted.
6778  *
6779  * For the purposes of the bitwise operations and <code>[]</code>, a
6780  * Bignum is treated as if it were an infinite-length
6781  * bitstring with 2's complement representation.
6782  *
6783  * While Fixnum values are immediate, Bignum
6784  * objects are not---assignment and parameter passing work with
6785  * references to objects, not the objects themselves.
6786  *
6787  */
6788 
6789 void
6791 {
6792 #ifndef RUBY_INTEGER_UNIFICATION
6794 #endif
6796  rb_deprecate_constant(rb_cObject, "Bignum");
6797 
6799 
6800 #ifdef USE_GMP
6801  /* The version of loaded GMP. */
6802  rb_define_const(rb_cInteger, "GMP_VERSION", rb_sprintf("GMP %s", gmp_version));
6803 #endif
6804 
6805  power_cache_init();
6806 }
static unsigned long big2ulong(VALUE x, const char *type)
Definition: bignum.c:5047
VALUE rb_big_modulo(VALUE x, VALUE y)
Definition: bignum.c:6034
int rb_bigzero_p(VALUE x)
Definition: bignum.c:2903
#define BIGNUM_EMBED_LEN_MASK
Definition: internal.h:507
static unsigned int nlz_long(unsigned long x)
Definition: internal.h:167
static VALUE bignorm(VALUE x)
Definition: bignum.c:3127
#define MEMCMP(p1, p2, type, n)
Definition: ruby.h:1663
size_t zn
Definition: bignum.c:2519
static int cmp(VALUE x, VALUE y)
Definition: time.c:57
static VALUE rb_big2str1(VALUE x, int base)
Definition: bignum.c:5001
VALUE rb_big_clone(VALUE x)
Definition: bignum.c:3004
STATIC_ASSERT(sizeof_bdigit_dbl, sizeof(BDIGIT_DBL)==SIZEOF_BDIGIT_DBL)
NORETURN(static inline void invalid_radix(int base))
#define ALIGNOF(type)
Definition: bignum.c:67
#define BARY_TRUNC(ds, n)
Definition: bignum.c:126
VALUE rb_str2big_poweroftwo(VALUE arg, int base, int badcheck)
Definition: bignum.c:4224
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3531
static VALUE bigtrunc(VALUE x)
Definition: bignum.c:3069
big_op_t
Definition: bignum.c:5373
#define BIGNUM_EMBED_LEN_SHIFT
Definition: internal.h:508
void rb_bug(const char *fmt,...)
Definition: error.c:482
VALUE rb_num_coerce_bin(VALUE, VALUE, ID)
Definition: numeric.c:509
VALUE rb_uint2big(VALUE n)
Definition: bignum.c:3142
#define FALSE
Definition: nkf.h:174
static size_t integer_unpack_num_bdigits_generic(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
Definition: bignum.c:968
static void twocomp2abs_bang(VALUE x, int hibits)
Definition: bignum.c:3060
#define SSIZE_MAX
Definition: ruby.h:292
static VALUE big_shift(VALUE x, long n)
Definition: bignum.c:6082
#define roomof(x, y)
Definition: internal.h:838
BDIGIT * yds
Definition: bignum.c:2520
static mulfunc_t bary_mul_toom3_start
Definition: bignum.c:144
#define POW2_P(x)
Definition: bignum.c:73
BDIGIT * zds
Definition: bignum.c:2520
#define INTEGER_PACK_LSWORD_FIRST
Definition: intern.h:139
static void integer_pack_loop_setup(size_t numwords, size_t wordsize, size_t nails, int flags, size_t *word_num_fullbytes_ret, int *word_num_partialbits_ret, size_t *word_start_ret, ssize_t *word_step_ret, size_t *word_last_ret, size_t *byte_start_ret, int *byte_step_ret)
Definition: bignum.c:523
int count
Definition: encoding.c:56
#define BIGRAD
Definition: bignum.c:77
static unsigned int hash(str, len) register const char *str
static VALUE big2str_base_poweroftwo(VALUE x, int base)
Definition: bignum.c:4840
VALUE result
Definition: bignum.c:4662
static size_t absint_numwords_small(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
Definition: bignum.c:3278
static BDIGIT bigdivrem_single1(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT x_higher_bdigit, BDIGIT y)
Definition: bignum.c:2567
static int bary_addc(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, int carry)
Definition: bignum.c:1376
#define KARATSUBA_BALANCED(xn, yn)
Definition: bignum.c:131
#define rb_usascii_str_new2
Definition: intern.h:863
#define CLASS_OF(v)
Definition: ruby.h:453
#define r1
static void * bigdivrem1(void *ptr)
Definition: bignum.c:2525
#define BDIGIT_MSB(d)
Definition: bignum.c:79
#define FIXNUM_MAX
Definition: ruby.h:228
#define Qtrue
Definition: ruby.h:437
void rb_big_pack(VALUE val, unsigned long *buf, long num_longs)
Definition: bignum.c:3199
static void rb_big_stop(void *ptr)
Definition: bignum.c:2560
#define BIGNUM_LEN(b)
Definition: internal.h:509
static void bary_swap(BDIGIT *ds, size_t num_bdigits)
Definition: bignum.c:470
static VALUE dbl2big(double d)
Definition: bignum.c:5161
#define BDIGMAX
Definition: bignum.c:83
const char ruby_digitmap[]
Definition: bignum.c:37
unsigned long rb_big2ulong(VALUE x)
Definition: bignum.c:5072
static VALUE bignew_1(VALUE klass, size_t len, int sign)
Definition: bignum.c:2980
VALUE rb_big_odd_p(VALUE num)
Definition: bignum.c:6754
static BDIGIT bigdivrem_single(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT y)
Definition: bignum.c:2592
static void bary_mul_karatsuba_branch(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
Definition: bignum.c:2424
static VALUE str2big_normal(int sign, const char *digits_start, const char *digits_end, size_t num_bdigits, int base)
Definition: bignum.c:3807
VALUE rb_big_eql(VALUE x, VALUE y)
Definition: bignum.c:5471
VALUE rb_big_plus(VALUE x, VALUE y)
Definition: bignum.c:5751
#define BIGNUM_SET_NEGATIVE_SIGN(b)
Definition: bignum.c:112
static int str2big_scan_digits(const char *s, const char *str, int base, int badcheck, size_t *num_digits_p, ssize_t *len_p)
Definition: bignum.c:3712
static void bary_divmod_branch(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:2814
VALUE rb_big_mul_normal(VALUE x, VALUE y)
Definition: bignum.c:1540
static VALUE bigand_int(VALUE x, long xn, BDIGIT hibitsx, long y)
Definition: bignum.c:6228
#define FIXNUM_MIN
Definition: ruby.h:229
size_t rb_big_size(VALUE big)
Definition: bignum.c:6701
void rb_must_asciicompat(VALUE)
Definition: string.c:2032
static double big_fdiv_float(VALUE x, VALUE y)
Definition: bignum.c:6131
#define SIZEOF_SIZE_T
Definition: fficonfig.h:17
#define bignew(len, sign)
Definition: bignum.c:115
#define FILL_DD
static int bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:381
static VALUE big_shift2(VALUE x, int lshift_p, VALUE y)
Definition: bignum.c:4549
st_index_t rb_memhash(const void *ptr, long len)
Definition: random.c:1502
VALUE rb_big_bit_length(VALUE big)
Definition: bignum.c:6713
#define MAX_BASE36_POWER_TABLE_ENTRIES
Definition: bignum.c:4597
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
VALUE rb_str2big_normal(VALUE arg, int base, int badcheck)
Definition: bignum.c:4260
void rb_str_set_len(VALUE, long)
Definition: string.c:2545
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Definition: intern.h:142
#define LONG_MIN
Definition: ruby.h:193
#define BARY_DIVMOD(q, r, x, y)
Definition: bignum.c:109
static VALUE bigfixize(VALUE x)
Definition: bignum.c:3083
VALUE rb_to_int(VALUE)
Definition: object.c:2687
VALUE rb_big_fdiv(VALUE x, VALUE y)
Definition: bignum.c:6168
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
static void invalid_integer(VALUE s)
Definition: bignum.c:3706
VALUE rb_integer_float_cmp(VALUE x, VALUE y)
Definition: bignum.c:5264
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: ruby.h:991
static VALUE bigxor_int(VALUE x, long xn, BDIGIT hibitsx, long y)
Definition: bignum.c:6453
#define INTEGER_PACK_LSBYTE_FIRST
Definition: intern.h:141
#define r3
#define RB_GC_GUARD(v)
Definition: ruby.h:552
void() mulfunc_t(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
Definition: bignum.c:142
static VALUE bigadd_int(VALUE x, long y)
Definition: bignum.c:5641
void Init_Bignum(void)
Definition: bignum.c:6790
#define HOST_BIGENDIAN_P
Definition: bignum.c:65
static int bary_zero_p(BDIGIT *xds, size_t xn)
Definition: bignum.c:431
st_data_t st_index_t
Definition: st.h:50
double rb_big2dbl(VALUE x)
Definition: bignum.c:5249
static void power_cache_init(void)
Definition: bignum.c:4603
#define rb_complex_raw1(x)
Definition: intern.h:177
#define VALGRIND_MAKE_MEM_UNDEFINED(p, n)
Definition: zlib.c:25
static void bary_mul_balance_with_mulfunc(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn, mulfunc_t *mulfunc)
Definition: bignum.c:1620
#define assert(x)
Definition: dlmalloc.c:1176
VALUE rb_big_unpack(unsigned long *buf, long num_longs)
Definition: bignum.c:3207
#define U16(a)
Definition: bignum.c:168
static VALUE str2big_poweroftwo(int sign, const char *digits_start, const char *digits_end, size_t num_digits, int bits_per_digit)
Definition: bignum.c:3766
#define FIXNUM_P(f)
Definition: ruby.h:365
VALUE rb_Float(VALUE)
Definition: object.c:2961
int rb_cmpint(VALUE val, VALUE a, VALUE b)
Definition: bignum.c:2909
#define PRI_SIZE_PREFIX
Definition: ruby.h:168
static int bary_add_one(BDIGIT *ds, size_t n)
Definition: bignum.c:1428
VALUE rb_fix2str(VALUE, int)
Definition: numeric.c:3404
void rb_big_resize(VALUE big, size_t len)
Definition: bignum.c:2973
static VALUE big_rshift(VALUE x, unsigned long shift)
Definition: bignum.c:4590
#define INTEGER_PACK_2COMP
Definition: intern.h:143
static void bary_mul_toom3_branch(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
Definition: bignum.c:2463
VALUE rb_eRangeError
Definition: error.c:766
VALUE rb_big_hash(VALUE x)
Definition: bignum.c:6649
static double big_fdiv_int(VALUE x, VALUE y)
Definition: bignum.c:6119
unsigned char uint8_t
Definition: sha2.h:100
static int bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags, int nlp_bits)
Definition: bignum.c:1081
#define FILL_LOWBITS(d, numbits)
Definition: bignum.c:72
RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow)
Definition: util.c:84
#define BIGNUM_EMBED_LEN_MAX
Definition: internal.h:480
#define SIZEOF_BDIGIT
Definition: internal.h:426
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:754
int rb_absint_singlebit_p(VALUE val)
Definition: bignum.c:3430
static void bary_divmod_normal(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:2637
int rb_big_sign(VALUE x)
Definition: bignum.c:6695
static VALUE rb_big_divide(VALUE x, VALUE y, ID op)
Definition: bignum.c:5994
static BDIGIT_DBL maxpow_in_bdigit_dbl(int base, int *exp_ret)
Definition: bignum.c:325
#define POSFIXABLE(f)
Definition: ruby.h:366
VALUE rb_big_divmod(VALUE x, VALUE y)
Definition: bignum.c:6066
#define neg(x)
Definition: time.c:119
#define MEMZERO(p, type, n)
Definition: ruby.h:1660
static VALUE rb_int_coerce(VALUE x, VALUE y)
Definition: bignum.c:6672
static int bary_subb(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, int borrow)
Definition: bignum.c:1313
VALUE rb_big_ge(VALUE x, VALUE y)
Definition: bignum.c:5422
unsigned long long uint64_t
Definition: sha2.h:102
VALUE rb_big_mul_balance(VALUE x, VALUE y)
Definition: bignum.c:1668
static size_t base36_numdigits_cache[35][MAX_BASE36_POWER_TABLE_ENTRIES]
Definition: bignum.c:4600
#define div(x, y)
Definition: date_strftime.c:27
VALUE rb_big_sq_fast(VALUE x)
Definition: bignum.c:1609
static unsigned int nlz_int(unsigned int x)
Definition: internal.h:137
#define rb_rational_raw1(x)
Definition: intern.h:163
VALUE rb_dbl2big(double d)
Definition: bignum.c:5193
VALUE rb_big_eq(VALUE x, VALUE y)
Definition: bignum.c:5451
#define ALLOC_N(type, n)
Definition: ruby.h:1587
#define rb_cBignum
Definition: internal.h:852
#define RB_BIGNUM_TYPE_P(x)
Definition: bignum.c:32
static void bary_mul(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:2488
static void big2str_alloc(struct big2str_struct *b2s, size_t len)
Definition: bignum.c:4667
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
static int valid_radix_p(int base)
Definition: bignum.c:3694
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:4205
#define BIGNUM_SET_LEN(b, l)
Definition: bignum.c:2930
VALUE rb_big2str_generic(VALUE x, int base)
Definition: bignum.c:4954
static int bary_add(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1422
#define BIGSIZE(x)
Definition: bignum.c:97
static VALUE big_op(VALUE x, VALUE y, enum big_op_t op)
Definition: bignum.c:5381
VALUE rb_big_cmp(VALUE x, VALUE y)
Definition: bignum.c:5346
#define dp(v)
Definition: vm_debug.h:21
#define NIL_P(v)
Definition: ruby.h:451
#define SIZEOF_BDIGIT_DBL
Definition: bignum.c:41
#define BDIGIT_DBL_SIGNED
Definition: bigdecimal.h:48
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2734
static void bary_unpack(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:1281
static void bary_mul_normal(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1527
static VALUE bigmul0(VALUE x, VALUE y)
Definition: bignum.c:5840
VALUE rb_big_new(size_t len, int sign)
Definition: bignum.c:2998
static double one(void)
Definition: isinf.c:52
static BDIGIT_DBL integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
Definition: bignum.c:601
#define Qfalse
Definition: ruby.h:436
#define ALLOCV_N(type, v, n)
Definition: ruby.h:1657
#define ADV(n)
#define BIGZEROP(x)
Definition: bignum.c:94
#define BIGNUM_NEGATIVE_P(b)
Definition: internal.h:503
#define T_BIGNUM
Definition: ruby.h:501
#define LONG_MAX
Definition: ruby.h:189
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:6154
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
RUBY_EXTERN int isinf(double)
Definition: isinf.c:56
#define OBJ_FREEZE(x)
Definition: ruby.h:1308
void rb_num_zerodiv(void)
Definition: numeric.c:192
#define ALLOCV_END(v)
Definition: ruby.h:1658
VALUE rb_big2str(VALUE x, int base)
Definition: bignum.c:5041
static int bary_sparse_p(const BDIGIT *ds, size_t n)
Definition: bignum.c:2314
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
static VALUE bigor_int(VALUE x, long xn, BDIGIT hibitsx, long y)
Definition: bignum.c:6334
#define numberof(array)
Definition: etc.c:616
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3617
static VALUE bigsub_int(VALUE x, long y0)
Definition: bignum.c:5544
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2562
VALUE rb_num_coerce_bit(VALUE, VALUE, ID)
Definition: numeric.c:4317
static VALUE big2str_generic(VALUE x, int base)
Definition: bignum.c:4877
VALUE rb_big_minus(VALUE x, VALUE y)
Definition: bignum.c:5780
#define BIGNUM_SET_SIGN(b, sign)
Definition: internal.h:499
#define BIGNUM_SET_POSITIVE_SIGN(b)
Definition: bignum.c:113
#define BDIGITS_ZERO(ptr, n)
Definition: bignum.c:117
#define GMP_BIG2STR_DIGITS
Definition: bignum.c:139
#define RSTRING_LEN(str)
Definition: ruby.h:978
static double big_fdiv(VALUE x, VALUE y, long ey)
Definition: bignum.c:6092
BDIGIT_DBL hbase2
Definition: bignum.c:4660
static void integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in_dd_p, BDIGIT **dpp)
Definition: bignum.c:1050
#define bit_length(x)
Definition: internal.h:417
#define REALLOC_N(var, type, n)
Definition: ruby.h:1591
unsigned long rb_genrand_ulong_limited(unsigned long i)
Definition: random.c:893
static int bary_sub(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1364
#define lo
Definition: siphash.c:21
void rb_deprecate_constant(VALUE mod, const char *name)
Definition: variable.c:2792
#define TRUE
Definition: nkf.h:175
#define DBL_BIGDIG
static VALUE big_shift3(VALUE x, int lshift_p, size_t shift_numdigits, int shift_numbits)
Definition: bignum.c:4500
#define swap32(x)
Definition: internal.h:110
#define BARY_ZERO_P(x)
Definition: bignum.c:110
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
VALUE rb_big_div(VALUE x, VALUE y)
Definition: bignum.c:6022
VALUE rb_big_idiv(VALUE x, VALUE y)
Definition: bignum.c:6028
static size_t integer_unpack_num_bdigits(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
Definition: bignum.c:1028
#define MEMMOVE(p1, p2, type, n)
Definition: ruby.h:1662
#define BARY_SUB(z, x, y)
Definition: bignum.c:107
VALUE rb_big_size_m(VALUE big)
Definition: bignum.c:6707
static void bary_neg(BDIGIT *ds, size_t n)
Definition: bignum.c:442
VALUE rb_big2str_poweroftwo(VALUE x, int base)
Definition: bignum.c:4871
static void integer_pack_fill_dd(BDIGIT **dpp, BDIGIT **dep, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
Definition: bignum.c:588
#define TOOM3_BALANCED(xn, yn)
Definition: bignum.c:132
static void bdigitdbl2bary(BDIGIT *ds, size_t n, BDIGIT_DBL num)
Definition: bignum.c:372
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:623
#define PRIsVALUE
Definition: ruby.h:135
long rb_big2long(VALUE x)
Definition: bignum.c:5087
#define INTEGER_PACK_WORDORDER_MASK
Definition: bignum.c:481
unsigned long ID
Definition: ruby.h:86
#define ASSERT_LEN()
#define Qnil
Definition: ruby.h:438
unsigned int uintptr_t
Definition: win32.h:106
static BDIGIT bary_small_lshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift)
Definition: bignum.c:399
#define GMP_DIV_DIGITS
Definition: bignum.c:138
#define RBIGNUM(obj)
Definition: internal.h:521
unsigned long VALUE
Definition: ruby.h:85
VALUE rb_big_mul(VALUE x, VALUE y)
Definition: bignum.c:5867
#define BITSPERDIG
Definition: bignum.c:76
static VALUE result
Definition: nkf.c:40
#define BIGDIVREM_EXTRA_WORDS
Definition: bignum.c:102
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base)
Definition: bignum.c:4018
#define U32(a)
Definition: bignum.c:169
static void big2str_karatsuba(struct big2str_struct *b2s, BDIGIT *xds, size_t xn, size_t wn, int power_level, size_t taillen)
Definition: bignum.c:4717
#define BDIGITS(x)
Definition: bignum.c:75
#define LSHIFTX(d, n)
Definition: bignum.c:70
#define RBASIC(obj)
Definition: ruby.h:1204
static BDIGIT_DBL_SIGNED bigdivrem_mulsub(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1487
VALUE rb_obj_hide(VALUE obj)
Definition: object.c:51
RUBY_EXTERN VALUE rb_cInteger
Definition: ruby.h:1891
#define INTEGER_PACK_MSWORD_FIRST
Definition: intern.h:138
#define FIX2INT(x)
Definition: ruby.h:686
#define bad(x)
Definition: _sdbm.c:124
static void bary_small_rshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift, BDIGIT higher_bdigit)
Definition: bignum.c:414
static int bytes_2comp(unsigned char *buf, size_t len)
Definition: bignum.c:612
static size_t integer_unpack_num_bdigits_small(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
Definition: bignum.c:958
VALUE rb_num_coerce_relop(VALUE, VALUE, ID)
Definition: numeric.c:524
VALUE rb_big_mul_karatsuba(VALUE x, VALUE y)
Definition: bignum.c:1849
#define INFINITY
Definition: missing.h:149
static void bigdivrem_restoring(BDIGIT *zds, size_t zn, BDIGIT *yds, size_t yn)
Definition: bignum.c:2598
size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
Definition: bignum.c:3231
VALUE rb_big_even_p(VALUE num)
Definition: bignum.c:6763
VALUE rb_big_gt(VALUE x, VALUE y)
Definition: bignum.c:5416
#define isnan(x)
Definition: win32.h:346
#define ST2FIX(h)
Definition: ruby.h:1579
static void shift(struct cparse_params *v, long act, VALUE tok, VALUE val)
Definition: cparse.c:684
static VALUE big_lshift(VALUE x, unsigned long shift)
Definition: bignum.c:4582
#define DBL_MANT_DIG
Definition: acosh.c:19
static void big2str_2bdigits(struct big2str_struct *b2s, BDIGIT *xds, size_t xn, size_t taillen)
Definition: bignum.c:4678
#define FIXABLE(f)
Definition: ruby.h:368
#define BARY_ARGS(ary)
Definition: bignum.c:104
#define BARY_SHORT_MUL(z, x, y)
Definition: bignum.c:108
#define CHAR_BIT
Definition: ruby.h:196
#define DBL_MAX_EXP
Definition: numeric.c:45
#define RB_FLOAT_TYPE_P(obj)
Definition: ruby.h:523
#define INTEGER_PACK_BIG_ENDIAN
Definition: intern.h:152
static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
Definition: bignum.c:1862
#define LONG2NUM(x)
Definition: ruby.h:1573
char * ptr
Definition: bignum.c:4663
VALUE rb_big_comp(VALUE x)
Definition: bignum.c:5491
unsigned int uint32_t
Definition: sha2.h:101
register unsigned int len
Definition: zonetab.h:51
#define StringValueCStr(v)
Definition: ruby.h:571
static int integer_unpack_single_bdigit(BDIGIT u, size_t size, int flags, BDIGIT *dp)
Definition: bignum.c:1062
#define INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION
Definition: intern.h:144
static void bary_short_mul(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:2299
#define RSTRING_PTR(str)
Definition: ruby.h:982
#define GMP_MUL_DIGITS
Definition: bignum.c:134
#define RGENGC_WB_PROTECTED_BIGNUM
Definition: ruby.h:801
#define INTEGER_PACK_NEGATIVE
Definition: intern.h:147
#define PRIxBDIGIT
Definition: bigdecimal.h:58
VALUE rb_equal(VALUE, VALUE)
Definition: object.c:86
VALUE rb_big_uminus(VALUE x)
Definition: bignum.c:5481
RUBY_EXTERN int ffs(int)
Definition: ffs.c:6
#define BIGNUM_SIGN(b)
Definition: internal.h:498
#define RFLOAT_VALUE(v)
Definition: ruby.h:940
int size
Definition: encoding.c:57
double rb_big_fdiv_double(VALUE x, VALUE y)
Definition: bignum.c:6139
#define INT2FIX(i)
Definition: ruby.h:232
VALUE rb_str2inum(VALUE str, int base)
Definition: bignum.c:4494
VALUE rb_big_remainder(VALUE x, VALUE y)
Definition: bignum.c:6050
static void bary_mul_single(BDIGIT *zds, size_t zn, BDIGIT x, BDIGIT y)
Definition: bignum.c:1440
RUBY_EXTERN double round(double)
Definition: numeric.c:79
#define SIZE_MAX
Definition: ruby.h:276
VALUE rb_big_and(VALUE x, VALUE y)
Definition: bignum.c:6283
static int nlz(BDIGIT x)
Definition: bignum.c:159
static int bary_2comp(BDIGIT *ds, size_t n)
Definition: bignum.c:449
static int bary_mul_precheck(BDIGIT **zdsp, size_t *znp, const BDIGIT **xdsp, size_t *xnp, const BDIGIT **ydsp, size_t *ynp)
Definition: bignum.c:2326
#define BDIGIT_DBL_MAX
Definition: bignum.c:84
static VALUE base36_power_cache[35][MAX_BASE36_POWER_TABLE_ENTRIES]
Definition: bignum.c:4599
static VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret)
Definition: bignum.c:4614
#define swap16(x)
Definition: internal.h:100
#define BIGNUM_NEGATE(b)
Definition: internal.h:504
#define FL_WB_PROTECTED
Definition: ruby.h:1216
static void bary_sq_fast(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn)
Definition: bignum.c:1555
static void get2comp(VALUE x)
Definition: bignum.c:3022
static BDIGIT abs2twocomp(VALUE *xp, long *n_ret)
Definition: bignum.c:3039
VALUE rb_big_divrem_normal(VALUE x, VALUE y)
Definition: bignum.c:2698
VALUE rb_big_norm(VALUE x)
Definition: bignum.c:3136
#define LONG2FIX(i)
Definition: ruby.h:234
#define SIZEOF_VALUE
Definition: ruby.h:88
#define BDIGIT_DBL
Definition: bigdecimal.h:47
VALUE rb_big_mul_toom3(VALUE x, VALUE y)
Definition: bignum.c:2246
#define RTEST(v)
Definition: ruby.h:450
void rb_thread_check_ints(void)
Definition: thread.c:1215
VALUE rb_big_lshift(VALUE x, VALUE y)
Definition: bignum.c:6544
#define PRIuSIZE
Definition: ruby.h:177
VALUE rb_uint2inum(VALUE n)
Definition: bignum.c:3185
static void big_extend_carry(VALUE x)
Definition: bignum.c:3014
#define NEGFIXABLE(f)
Definition: ruby.h:367
static VALUE bigadd(VALUE x, VALUE y, int sign)
Definition: bignum.c:5724
static mulfunc_t bary_mul_karatsuba_start
Definition: bignum.c:145
#define bdigit_roomof(n)
Definition: bignum.c:103
VALUE rb_big_pow(VALUE x, VALUE y)
Definition: bignum.c:6174
#define BIGNUM_POSITIVE_P(b)
Definition: internal.h:502
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Definition: bignum.c:3366
size_t yn
Definition: bignum.c:2519
static void bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
Definition: bignum.c:2826
VALUE rb_int2big(SIGNED_VALUE n)
Definition: bignum.c:3164
VALUE rb_big_aref(VALUE x, VALUE y)
Definition: bignum.c:6604
#define INTEGER_PACK_MSBYTE_FIRST
Definition: intern.h:140
VALUE rb_integer_float_eq(VALUE x, VALUE y)
Definition: bignum.c:5314
static int bary_sub_one(BDIGIT *zds, size_t zn)
Definition: bignum.c:1370
VALUE rb_big_le(VALUE x, VALUE y)
Definition: bignum.c:5434
#define BIGUP(x)
Definition: bignum.c:80
static int bary_mulsub_1xN(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1513
#define TAKE_LOWBITS(n)
static double big2dbl(VALUE x)
Definition: bignum.c:5199
VALUE rb_big_lt(VALUE x, VALUE y)
Definition: bignum.c:5428
#define StringValuePtr(v)
Definition: ruby.h:570
RUBY_EXTERN VALUE rb_eFloatDomainError
Definition: ruby.h:1936
VALUE rb_int2inum(SIGNED_VALUE n)
Definition: bignum.c:3192
static void bary_mul_karatsuba(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn)
Definition: bignum.c:1680
static void invalid_radix(int base)
Definition: bignum.c:3700
#define BIGLO(x)
Definition: bignum.c:82
void rb_warning(const char *fmt,...)
Definition: error.c:250
volatile VALUE stop
Definition: bignum.c:2521
#define BARY_ADD(z, x, y)
Definition: bignum.c:106
void rb_big_2comp(VALUE x)
Definition: bignum.c:3033
static BDIGIT_DBL bary2bdigitdbl(const BDIGIT *ds, size_t n)
Definition: bignum.c:360
#define r2
VALUE rb_big_rshift(VALUE x, VALUE y)
Definition: bignum.c:6574
static void validate_integer_pack_format(size_t numwords, size_t wordsize, size_t nails, int flags, int supported_flags)
Definition: bignum.c:490
void void xfree(void *)
#define GMP_STR2BIG_DIGITS
Definition: bignum.c:140
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
Definition: bignum.c:3994
static int bigzero_p(VALUE x)
Definition: bignum.c:2897
static int bary_muladd_1xN(BDIGIT *zds, size_t zn, BDIGIT x, const BDIGIT *yds, size_t yn)
Definition: bignum.c:1452
#define rb_intern(str)
#define conv_digit(c)
Definition: bignum.c:3688
VALUE rb_usascii_str_new(const char *, long)
Definition: string.c:742
#define mod(x, y)
Definition: date_strftime.c:28
static VALUE str2big_karatsuba(int sign, const char *digits_start, const char *digits_end, size_t num_digits, size_t num_bdigits, int digits_per_bdigits_dbl, int base)
Definition: bignum.c:3851
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:20
#define NULL
Definition: _sdbm.c:102
int hbase2_numdigits
Definition: bignum.c:4661
#define FIX2LONG(x)
Definition: ruby.h:363
#define Qundef
Definition: ruby.h:439
VALUE rb_str2big_karatsuba(VALUE arg, int base, int badcheck)
Definition: bignum.c:4302
static VALUE bigsub(VALUE x, VALUE y)
Definition: bignum.c:5517
static VALUE bigsq(VALUE x)
Definition: bignum.c:5809
#define INTEGER_PACK_BYTEORDER_MASK
Definition: bignum.c:484
#define TOOM3_MUL_DIGITS
Definition: bignum.c:136
VALUE rb_big_abs(VALUE x)
Definition: bignum.c:6685
#define INTEGER_PACK_FORCE_BIGNUM
Definition: intern.h:146
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
void rb_warn(const char *fmt,...)
Definition: error.c:221
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:1509
#define SIZET2NUM(v)
Definition: ruby.h:264
static void rb_big_realloc(VALUE big, size_t len)
Definition: bignum.c:2938
VALUE rb_eArgError
Definition: error.c:763
VALUE rb_big_or(VALUE x, VALUE y)
Definition: bignum.c:6402
VALUE rb_cstr2inum(const char *str, int base)
Definition: bignum.c:4488
#define NUM2LONG(x)
Definition: ruby.h:648
void rb_cmperr(VALUE x, VALUE y)
Definition: compar.c:24
#define BDIGIT
Definition: bigdecimal.h:46
#define BIGNUM_EMBED_FLAG
Definition: internal.h:506
static VALUE bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
Definition: bignum.c:5885
static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
Definition: bignum.c:5978
static int bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:627
#define CLEAR_LOWBITS(d, numbits)
Definition: bignum.c:71
VALUE rb_big_xor(VALUE x, VALUE y)
Definition: bignum.c:6496
#define DBL2NUM(dbl)
Definition: ruby.h:941
#define ISSPACE(c)
Definition: ruby.h:2124
#define StringValue(v)
Definition: ruby.h:569
#define PUSH_BITS(data, numbits)
VALUE rb_num_coerce_cmp(VALUE, VALUE, ID)
Definition: numeric.c:516
#define KARATSUBA_MUL_DIGITS
Definition: bignum.c:135
#define SIGNED_VALUE
Definition: ruby.h:87
#define BIGDN(x)
Definition: bignum.c:81
static size_t absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
Definition: bignum.c:3292