1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
#include "libtommath/tommath.h"
#if 0
void *MP_MALLOC(size_t size) {
return malloc(size);
}
void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize) {
return realloc(mem, newsize);
}
void *MP_CALLOC(size_t nmemb, size_t size) {
return calloc(nmemb, size);
}
void MP_FREE(void *mem, size_t size) {
free(mem);
}
#else
void *MP_MALLOC(size_t size) {
return gb_alloc(permanent_allocator(), cast(isize)size);
}
void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize) {
return gb_resize(permanent_allocator(), mem, cast(isize)oldsize, cast(isize)newsize);
}
void *MP_CALLOC(size_t nmemb, size_t size) {
size_t total = nmemb*size;
return gb_alloc(permanent_allocator(), cast(isize)total);
}
void MP_FREE(void *mem, size_t size) {
// DO NOTHING
}
#endif
#ifndef MAX_BIG_INT_SHIFT
#define MAX_BIG_INT_SHIFT 1024
#endif
typedef mp_int BigInt;
gb_internal void big_int_from_u64(BigInt *dst, u64 x);
gb_internal void big_int_from_i64(BigInt *dst, i64 x);
gb_internal void big_int_init (BigInt *dst, BigInt const *src);
gb_internal void big_int_from_string(BigInt *dst, String const &s, bool *success);
gb_internal void big_int_dealloc(BigInt *dst) {
mp_clear(dst);
}
gb_internal BigInt big_int_make(BigInt const *b, bool abs=false);
gb_internal BigInt big_int_make_abs(BigInt const *b);
gb_internal BigInt big_int_make_u64(u64 x);
gb_internal BigInt big_int_make_i64(i64 x);
gb_internal u64 big_int_to_u64 (BigInt const *x);
gb_internal i64 big_int_to_i64 (BigInt const *x);
gb_internal f64 big_int_to_f64 (BigInt const *x);
gb_internal String big_int_to_string(gbAllocator allocator, BigInt const *x, u64 base = 10);
gb_internal void big_int_add (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_sub (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_shl (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_shr (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_mul (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y);
gb_internal void big_int_exp_u64(BigInt *dst, BigInt const *x, u64 y, bool *success);
gb_internal void big_int_quo_rem(BigInt const *x, BigInt const *y, BigInt *q, BigInt *r);
gb_internal void big_int_quo (BigInt *z, BigInt const *x, BigInt const *y);
gb_internal void big_int_rem (BigInt *z, BigInt const *x, BigInt const *y);
gb_internal void big_int_and (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_and_not(BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_xor (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_or (BigInt *dst, BigInt const *x, BigInt const *y);
gb_internal void big_int_not (BigInt *dst, BigInt const *x, i32 bit_count, bool is_signed);
gb_internal void big_int_add_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_sub_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_shl_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_shr_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_mul_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_quo_eq(BigInt *dst, BigInt const *x);
gb_internal void big_int_rem_eq(BigInt *dst, BigInt const *x);
gb_internal bool big_int_is_neg(BigInt const *x);
gb_internal void big_int_neg(BigInt *dst, BigInt const *x);
gb_internal void big_int_add_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_add(dst, &res, x);
}
gb_internal void big_int_sub_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_sub(dst, &res, x);
}
gb_internal void big_int_shl_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_shl(dst, &res, x);
}
gb_internal void big_int_shr_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_shr(dst, &res, x);
}
gb_internal void big_int_mul_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_mul(dst, &res, x);
}
gb_internal void big_int_quo_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_quo(dst, &res, x);
}
gb_internal void big_int_rem_eq(BigInt *dst, BigInt const *x) {
BigInt res = {};
big_int_init(&res, dst);
big_int_rem(dst, &res, x);
}
gb_internal i64 big_int_sign(BigInt const *x) {
if (mp_iszero(x)) {
return 0;
}
return x->sign == MP_ZPOS ? +1 : -1;
}
gb_internal void big_int_from_u64(BigInt *dst, u64 x) {
mp_init_u64(dst, x);
}
gb_internal void big_int_from_i64(BigInt *dst, i64 x) {
mp_init_i64(dst, x);
}
gb_internal void big_int_init(BigInt *dst, BigInt const *src) {
if (dst == src) {
return;
}
mp_init_copy(dst, src);
}
gb_internal BigInt big_int_make(BigInt const *b, bool abs) {
BigInt i = {};
big_int_init(&i, b);
if (abs) mp_abs(&i, &i);
return i;
}
gb_internal BigInt big_int_make_abs(BigInt const *b) {
return big_int_make(b, true);
}
gb_internal BigInt big_int_make_u64(u64 x) {
BigInt i = {};
big_int_from_u64(&i, x);
return i;
}
gb_internal BigInt big_int_make_i64(i64 x) {
BigInt i = {};
big_int_from_i64(&i, x);
return i;
}
gb_internal void big_int_from_string(BigInt *dst, String const &s, bool *success) {
*success = true;
bool is_negative = false;
u64 base = 10;
bool has_prefix = false;
if (s.len > 2 && s[0] == '0') {
switch (s[1]) {
case 'b': base = 2; has_prefix = true; break;
case 'o': base = 8; has_prefix = true; break;
case 'd': base = 10; has_prefix = true; break;
case 'z': base = 12; has_prefix = true; break;
case 'x': base = 16; has_prefix = true; break;
case 'h': base = 16; has_prefix = true; break;
}
}
u8 *text = s.text;
isize len = s.len;
if (has_prefix) {
text += 2;
len -= 2;
}
BigInt b = {};
big_int_from_u64(&b, base);
mp_zero(dst);
isize i = 0;
for (; i < len; i++) {
Rune r = cast(Rune)text[i];
if (r == '-') {
if (is_negative) {
// NOTE(Jeroen): Can't have a doubly negative number.
*success = false;
return;
}
is_negative = true;
continue;
}
if (r == '_') {
continue;
}
u64 v = u64_digit_value(r);
if (v >= base) {
// NOTE(Jeroen): Can still be a valid integer if the next character is an `e` or `E`.
if (r != 'e' && r != 'E') {
*success = false;
}
break;
}
BigInt val = big_int_make_u64(v);
big_int_mul_eq(dst, &b);
big_int_add_eq(dst, &val);
}
if (i < len && (text[i] == 'e' || text[i] == 'E')) {
i += 1;
GB_ASSERT(base == 10);
GB_ASSERT(text[i] != '-');
if (text[i] == '+') {
i += 1;
}
u64 exp = 0;
for (; i < len; i++) {
char r = cast(char)text[i];
if (r == '_') {
continue;
}
u64 v = 0;
if (gb_char_is_digit(r)) {
v = u64_digit_value(r);
} else {
*success = false;
break;
}
exp *= 10;
exp += v;
}
BigInt tmp = {};
mp_init(&tmp);
big_int_exp_u64(&tmp, &b, exp, success);
big_int_mul_eq(dst, &tmp);
}
if (is_negative) {
big_int_neg(dst, dst);
}
}
gb_internal u64 big_int_to_u64(BigInt const *x) {
GB_ASSERT(x->sign == 0);
return mp_get_u64(x);
}
gb_internal i64 big_int_to_i64(BigInt const *x) {
return mp_get_i64(x);
}
gb_internal f64 big_int_to_f64(BigInt const *x) {
return mp_get_double(x);
}
gb_internal void big_int_neg(BigInt *dst, BigInt const *x) {
mp_neg(x, dst);
}
gb_internal int big_int_cmp(BigInt const *x, BigInt const *y) {
return mp_cmp(x, y);
}
gb_internal int big_int_cmp_zero(BigInt const *x) {
if (mp_iszero(x)) {
return 0;
}
return x->sign ? -1 : +1;
}
gb_internal bool big_int_is_zero(BigInt const *x) {
return mp_iszero(x);
}
gb_internal void big_int_add(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_add(x, y, dst);
}
gb_internal void big_int_sub(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_sub(x, y, dst);
}
gb_internal void big_int_shl(BigInt *dst, BigInt const *x, BigInt const *y) {
u32 yy = mp_get_u32(y);
mp_mul_2d(x, yy, dst);
}
gb_internal void big_int_shr(BigInt *dst, BigInt const *x, BigInt const *y) {
u32 yy = mp_get_u32(y);
BigInt d = {};
mp_div_2d(x, yy, dst, &d);
big_int_dealloc(&d);
}
gb_internal void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y) {
BigInt d = {};
big_int_from_u64(&d, y);
mp_mul(x, &d, dst);
big_int_dealloc(&d);
}
gb_internal void big_int_exp_u64(BigInt *dst, BigInt const *x, u64 y, bool *success) {
if (y > INT_MAX) {
*success = false;
return;
}
// Note: The cutoff for square-multiply being faster than the naive
// for loop is when exp > 4, but it probably isn't worth adding
// a fast path.
mp_err err = mp_expt_n(x, int(y), dst);
*success = err == MP_OKAY;
}
gb_internal void big_int_mul(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_mul(x, y, dst);
}
gb_internal u64 leading_zeros_u64(u64 x) {
#if defined(GB_COMPILER_MSVC)
#if defined(GB_ARCH_64_BIT)
return __lzcnt64(x);
#else
u64 y, n;
n = 0;
y = x;
L:
if (x < 0) {
return n;
}
if (y == 0) {
return 64-n;
}
n++;
x <<= 1;
y >>= 1;
goto L;
#endif
#else
return cast(u64)__builtin_clzll(cast(unsigned long long)x);
#endif
}
// `big_int_quo_rem` sets z to the quotient x/y and r to the remainder x%y
// and returns the pair (z, r) for y != 0.
// if y == 0, a division-by-zero run-time panic occurs.
//
// q = x/y with the result truncated to zero
// r = x - y*q
gb_internal void big_int_quo_rem(BigInt const *x, BigInt const *y, BigInt *q_, BigInt *r_) {
mp_div(x, y, q_, r_);
}
gb_internal void big_int_quo(BigInt *z, BigInt const *x, BigInt const *y) {
BigInt r = {};
big_int_quo_rem(x, y, z, &r);
big_int_dealloc(&r);
}
gb_internal void big_int_rem(BigInt *z, BigInt const *x, BigInt const *y) {
BigInt q = {};
big_int_quo_rem(x, y, &q, z);
big_int_dealloc(&q);
}
gb_internal void big_int_euclidean_mod(BigInt *z, BigInt const *x, BigInt const *y) {
BigInt y0 = {};
big_int_init(&y0, y);
BigInt q = {};
big_int_quo_rem(x, y, &q, z);
if (z->sign) {
if (y0.sign) {
big_int_sub(z, z, &y0);
} else {
big_int_add(z, z, &y0);
}
}
}
gb_internal void big_int_and(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_and(x, y, dst);
}
gb_internal void big_int_and_not(BigInt *dst, BigInt const *x, BigInt const *y) {
if (mp_iszero(x)) {
big_int_init(dst, y);
return;
}
if (mp_iszero(y)) {
big_int_init(dst, x);
return;
}
if (x->sign == y->sign) {
if (x->sign) {
// (-x) &~ (-y) == ~(x-1) &~ ~(y-1) == ~(x-1) & (y-1) == (y-1) &~ (x-1)
BigInt x1 = big_int_make_abs(x);
BigInt y1 = big_int_make_abs(y);
mp_decr(&x1);
mp_decr(&y1);
BigInt ny1 = {};
mp_complement(&y1, &ny1);
mp_and(&x1, &ny1, dst);
big_int_dealloc(&x1);
big_int_dealloc(&y1);
big_int_dealloc(&ny1);
return;
}
BigInt ny = {};
mp_complement(y, &ny);
mp_and(x, &ny, dst);
big_int_dealloc(&ny);
return;
}
if (x->sign) {
// (-x) &~ y == ~(x-1) &~ y == ~(x-1) & ~y == ~((x-1) | y) == -(((x-1) | y) + 1)
BigInt x1 = big_int_make_abs(x);
BigInt y1 = big_int_make_abs(y);
mp_decr(&x1);
BigInt z1 = {};
big_int_or(&z1, &x1, &y1);
mp_add_d(&z1, 1, dst);
big_int_dealloc(&x1);
big_int_dealloc(&y1);
big_int_dealloc(&z1);
return;
}
// x &~ (-y) == x &~ ~(y-1) == x & (y-1)
BigInt x1 = big_int_make_abs(x);
BigInt y1 = big_int_make_abs(y);
mp_decr(&y1);
big_int_and(dst, &x1, &y1);
big_int_dealloc(&x1);
big_int_dealloc(&y1);
return;
}
gb_internal void big_int_xor(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_xor(x, y, dst);
}
gb_internal void big_int_or(BigInt *dst, BigInt const *x, BigInt const *y) {
mp_or(x, y, dst);
}
gb_internal void debug_print_big_int(BigInt const *x) {
TEMPORARY_ALLOCATOR_GUARD();
String s = big_int_to_string(temporary_allocator(), x, 10);
gb_printf_err("[DEBUG] %.*s\n", LIT(s));
}
gb_internal void big_int_not(BigInt *dst, BigInt const *x, i32 bit_count, bool is_signed) {
GB_ASSERT(bit_count >= 0);
if (bit_count == 0) {
big_int_from_u64(dst, 0);
return;
}
if (big_int_is_neg(x)) {
// ~x == -x - 1
big_int_neg(dst, x);
mp_decr(dst);
mp_mod_2d(dst, bit_count, dst);
return;
}
BigInt pow2b = {};
mp_2expt(&pow2b, bit_count);
BigInt mask = {};
mp_2expt(&mask, bit_count);
mp_decr(&mask);
BigInt v = {};
mp_init_copy(&v, x);
mp_mod_2d(&v, bit_count, &v);
mp_xor(&v, &mask, dst);
if (is_signed) {
BigInt pmask = {};
BigInt pmask_minus_one = {};
mp_2expt(&pmask, bit_count-1);
mp_sub_d(&pmask, 1, &pmask_minus_one);
BigInt a = {};
BigInt b = {};
big_int_and(&a, dst, &pmask_minus_one);
big_int_and(&b, dst, &pmask);
big_int_sub(dst, &a, &b);
big_int_dealloc(&a);
big_int_dealloc(&b);
}
big_int_dealloc(&pow2b);
big_int_dealloc(&mask);
big_int_dealloc(&v);
}
gb_internal bool big_int_is_neg(BigInt const *x) {
if (x == nullptr) {
return false;
}
return x->sign != MP_ZPOS;
}
gb_internal char digit_to_char(u8 digit) {
GB_ASSERT(digit < 16);
if (digit <= 9) {
return digit + '0';
} else if (digit <= 15) {
return digit + 'a';
}
return '0';
}
gb_internal String big_int_to_string(gbAllocator allocator, BigInt const *x, u64 base) {
GB_ASSERT(base <= 16);
if (mp_iszero(x)) {
u8 *buf = gb_alloc_array(allocator, u8, 1);
buf[0] = '0';
return make_string(buf, 1);
}
Array<char> buf = {};
array_init(&buf, allocator, 0, 32);
if (x->used >= 498) { // 2^498 ~ 10^150
mp_int val = {};
mp_abs(x, &val);
int exp = 0;
mp_err err = mp_log_n(&val, 10, &exp);
GB_ASSERT(err == MP_OKAY);
GB_ASSERT(exp >= 100);
mp_int thousand_below = {};
mp_int thousand_above = {};
mp_init_i32(&thousand_below, 10);
mp_expt_n(&thousand_below, exp-3, &thousand_below);
mp_div(&val, &thousand_below, &thousand_above, nullptr);
double mant = 1.0e-3 * mp_get_double(&thousand_above);
char val_buf[256] = {};
isize n = gb_snprintf(val_buf, gb_size_of(val_buf)-1, "~ %s%.fe%u", (x->sign ? "-" : ""), mant, exp);
array_add_elems(&buf, val_buf, n-1);
} else {
BigInt v = {};
mp_init_copy(&v, x);
if (v.sign) {
array_add(&buf, '-');
mp_abs(&v, &v);
}
isize first_word_idx = buf.count;
BigInt r = {};
BigInt b = {};
big_int_from_u64(&b, base);
u8 digit = 0;
while (big_int_cmp(&v, &b) >= 0) {
big_int_quo_rem(&v, &b, &v, &r);
digit = cast(u8)big_int_to_u64(&r);
array_add(&buf, digit_to_char(digit));
}
big_int_rem(&r, &v, &b);
digit = cast(u8)big_int_to_u64(&r);
array_add(&buf, digit_to_char(digit));
big_int_dealloc(&r);
big_int_dealloc(&b);
for (isize i = first_word_idx; i < buf.count/2; i++) {
isize j = buf.count + first_word_idx - i - 1;
char tmp = buf[i];
buf[i] = buf[j];
buf[j] = tmp;
}
}
return make_string(cast(u8 *)buf.data, buf.count);
}
gb_internal int big_int_log2(BigInt const *x) {
return mp_count_bits(x) - 1;
}
|