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