aboutsummaryrefslogtreecommitdiff
path: root/src/big_int.cpp
blob: dc3d50589af30bc53d52ad822e3b67751b7fffd8 (plain)
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
struct BigInt {
	union {
		u64  word;
		u64 *words;
	} d;
	i32 len;
	b32 neg;
};


BigInt const BIG_INT_ZERO = {{0}, 0, false};
BigInt const BIG_INT_ONE = {{1}, 1, false};
BigInt const BIG_INT_NEG_ONE = {{1}, 1, true};


gb_global Arena global_big_int_arena = {0};

#if defined(GB_COMPILER_MSVC) && defined(GB_ARCH_64_BIT)
// URL(bill): https://stackoverflow.com/questions/8453146/128-bit-division-intrinsic-in-visual-c/8456388#8456388
u8 udiv128_data[] = {
	0x48, 0x89, 0xD0, // mov rax,rdx
	0x48, 0x89, 0xCA, // mov rdx,rcx
	0x49, 0xF7, 0xF0, // div r8
	0x49, 0x89, 0x11, // mov [r9],rdx
	0xC3              // ret
};
u64 (__fastcall *unsafe_udiv128)(u64 numhi, u64 numlo, u64 den, u64* rem) = (u64 (__fastcall *)(u64, u64, u64, u64*))&udiv128_data[0];
#endif

void global_big_int_init(void) {
	arena_init(&global_big_int_arena, heap_allocator());

#if defined(GB_COMPILER_MSVC) && defined(GB_ARCH_64_BIT)
	DWORD dummy;
	VirtualProtect(udiv128_data, sizeof(udiv128_data), PAGE_EXECUTE_READWRITE, &dummy);
#endif
}

// IMPORTANT NOTE LEAK(bill): This entire BigInt library leaks memory like there is no tomorrow
// However, this isn't really a problem as the vast majority of BigInt operations will not use
// more than 1 word.
// I could track how much this does leaks because I use an arena_allocator but I doubt I will require
// it any time soon
gb_inline gbAllocator big_int_allocator(void) {
	return arena_allocator(&global_big_int_arena);
}

void big_int_alloc(BigInt *dst, isize word_len, isize word_cap) {
	GB_ASSERT_MSG(word_len <= word_cap, "%td %td", word_len, word_cap);
	if (word_cap < dst->len) {
		dst->len = cast(i32)word_len;
	} else {
		dst->len = cast(i32)word_len;
		dst->d.words = gb_alloc_array(big_int_allocator(), u64, word_cap);
	}
}

void big_int_from_u64(BigInt *dst, u64 x);
void big_int_from_i64(BigInt *dst, i64 x);
void big_int_init    (BigInt *dst, BigInt const *src);
void big_int_from_string(BigInt *dst, String const &s);

void big_int_dealloc(BigInt *dst) {
	if (dst->len > 1) {
		gb_free(big_int_allocator(), dst->d.words);
	}
	gb_zero_item(dst);
}

BigInt big_int_make(BigInt const *b, bool abs=false);
BigInt big_int_make_abs(BigInt const *b);
BigInt big_int_make_u64(u64 x);
BigInt big_int_make_i64(i64 x);

u64    big_int_to_u64   (BigInt const *x);
i64    big_int_to_i64   (BigInt const *x);
f64    big_int_to_f64   (BigInt const *x);
String big_int_to_string(gbAllocator allocator, BigInt const *x, u64 base = 10);

gb_inline u64 const *big_int_ptr(BigInt const *b) {
	if (b->len <= 1) {
		return &b->d.word;
	}
	return b->d.words;
}
gb_inline u64 *big_int_ptr(BigInt *b) {
	if (b->len <= 1) {
		return &b->d.word;
	}
	return b->d.words;
}

void big_int_add    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_sub    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_shl    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_shr    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_mul    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y);

void big_int_quo_rem(BigInt const *x, BigInt const *y, BigInt *q, BigInt *r);
void big_int_quo    (BigInt *z, BigInt const *x, BigInt const *y);
void big_int_rem    (BigInt *z, BigInt const *x, BigInt const *y);

void big_int_euclidean_div(BigInt *z, BigInt const *x, BigInt const *y);
void big_int_euclidean_mod(BigInt *z, BigInt const *x, BigInt const *y);

void big_int_and    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_and_not(BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_xor    (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_or     (BigInt *dst, BigInt const *x, BigInt const *y);
void big_int_not    (BigInt *dst, BigInt const *x, u64 bit_count, bool is_signed);


void big_int_add_eq(BigInt *dst, BigInt const *x);
void big_int_sub_eq(BigInt *dst, BigInt const *x);
void big_int_shl_eq(BigInt *dst, BigInt const *x);
void big_int_shr_eq(BigInt *dst, BigInt const *x);
void big_int_mul_eq(BigInt *dst, BigInt const *x);

void big_int_quo_eq(BigInt *dst, BigInt const *x);
void big_int_rem_eq(BigInt *dst, BigInt const *x);


void big_int_add_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_add(dst, &res, x);
}
void big_int_sub_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_sub(dst, &res, x);
}
void big_int_shl_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_shl(dst, &res, x);
}
void big_int_shr_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_shr(dst, &res, x);
}
void big_int_mul_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_mul(dst, &res, x);
}
void big_int_quo_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_quo(dst, &res, x);
}
void big_int_rem_eq(BigInt *dst, BigInt const *x) {
	BigInt res = {};
	big_int_init(&res, dst);
	big_int_rem(dst, &res, x);
}



void big_int_normalize(BigInt *dst) {
	if (dst->len == 1 && dst->d.word == 0) {
		dst->len = 0;
		return;
	}
	u64 const *words = big_int_ptr(dst);

	i32 count_minus_one = -1;
	for (i32 i = 0; i < dst->len; i++) {
		u64 word = words[i];
		if (word != 0) {
			count_minus_one = i;
		}
	}

	if (count_minus_one < 0) {
		dst->neg = false;
		if (words[0] == 0) {
			dst->len = 0;
			return;
		}
	}
	dst->len = count_minus_one+1;
	if (count_minus_one == 0) {
		dst->d.word = words[0];
	}
}

i64 big_int_sign(BigInt const *x) {
	if (x->len == 0) {
		return 0;
	}
	return x->neg ? -1 : +1;
}


void big_int_from_u64(BigInt *dst, u64 x) {
	if (x == 0) {
		dst->len = 0;
		dst->neg = false;
	} else {
		dst->len = 1;
		dst->d.word = x;
		dst->neg = false;
	}
}
void big_int_from_i64(BigInt *dst, i64 x) {
	if (x >= 0) {
		big_int_from_u64(dst, cast(u64)x);
		return;
	} else {
		dst->len = 1;
		dst->d.word = (cast(u64)(-(x+1ll))) + 1ull;
		dst->neg = true;
	}

}
void big_int_init(BigInt *dst, BigInt const *src) {
	if (dst == src) {
		return;
	}
	if (src->len == 0) {
		big_int_from_u64(dst, 0);
		return;
	} else if (src->len == 1) {
		dst->len = 1;
		dst->d.word = src->d.word;
		dst->neg = src->neg;
		return;
	}


	dst->neg = src->neg;
	big_int_alloc(dst, src->len, src->len);
	u64 const *s = big_int_ptr(src);
	gb_memmove(dst->d.words, s, gb_size_of(u64)*dst->len);
	big_int_normalize(dst);
}

BigInt big_int_make(BigInt const *b, bool abs) {
	BigInt i = {};
	big_int_init(&i, b);
	if (abs) i.neg = false;
	return i;
}
BigInt big_int_make_abs(BigInt const *b) {
	return big_int_make(b, true);
}
BigInt big_int_alias_abs(BigInt const *b) {
	BigInt x = *b;
	x.neg = false;
	return x;
}


BigInt big_int_make_u64(u64 x) {
	BigInt i = {};
	big_int_from_u64(&i, x);
	return i;
}
BigInt big_int_make_i64(i64 x) {
	BigInt i = {};
	big_int_from_i64(&i, x);
	return i;
}


void big_int_from_string(BigInt *dst, String const &s) {
	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);
	big_int_init(dst, &BIG_INT_ZERO);

	for (isize i = 0; i < len; i++) {
		Rune r = cast(Rune)text[i];
		if (r == '_') {
			continue;
		}
		u64 v = u64_digit_value(r);
		if (v >= base) {
			break;
		}
		BigInt val = big_int_make_u64(v);
		big_int_mul_eq(dst, &b);
		big_int_add_eq(dst, &val);
	}
	big_int_normalize(dst);
}



u64 big_int_to_u64(BigInt const *x) {
	GB_ASSERT(!x->neg);
	switch (x->len) {
	case 0: return 0;
	case 1: return x->d.word;
	}
	GB_PANIC("BigInt too big for u64");
	return 0;
}

i64 big_int_to_i64(BigInt const *x) {
	switch (x->len) {
	case 0:
		return 0;
	case 1:
		if (x->neg) {
			if (x->d.word <= 9223372036854775808ull) { // 2^63 - 1
				return (-cast(i64)(x->d.word-1ull)) - 1ll;
			} else {
				GB_PANIC("BigInt too big for i64");
			}
		} else {
			return cast(i64)x->d.word;
		}
		break;
	}

	GB_PANIC("BigInt too big for i64");
	return 0;
}

f64 big_int_to_f64(BigInt const *x) {
	if (x->neg) {
		i64 i = big_int_to_i64(x);
		return cast(f64)i;
	}
	u64 u = big_int_to_u64(x);
	return cast(f64)u;
}

bool bi__alias(BigInt const *dst, BigInt const *src) {
	if (dst == src) {
		return true;
	}
	if (dst->len > 1 && src->len > 1) {
		return dst->d.words == src->d.words;
	}
	return false;
}


void big_int_neg(BigInt *dst, BigInt const *x) {
	big_int_init(dst, x);
	dst->neg = !dst->neg;
	big_int_normalize(dst);
}


int big_int_cmp(BigInt const *x, BigInt const *y) {
	if (x == y) {
		return 0;
	} else if (x->neg && !y->neg) {
		return -1;
	} else if (!x->neg && y->neg) {
		return +1;
	} else if (x->len > y->len) {
		return x->neg ? -1 : +1;
	} else if (y->len > x->len) {
		return x->neg ? +1 : -1;
	} else if (x->len == 0) {
		return 0;
	}

	u64 const *xd = big_int_ptr(x);
	u64 const *yd = big_int_ptr(y);

	for (i32 i = x->len; i >= 0; i--) {
		u64 a = xd[i];
		u64 b = yd[i];

		if (a > b) {
			return x->neg ? -1 : +1;
		}
		if (a < b) {
			return x->neg ? +1 : -1;
		}
	}

	return 0;
}

int big_int_cmp_zero(BigInt const *x) {
	if (x->len == 0) {
		return 0;
	}
	return x->neg ? -1 : +1;
}

bool big_int_is_zero(BigInt const *x) {
	if (x->len == 0) {
		return true;
	}
	return false;
}




void big_int_add(BigInt *dst, BigInt const *x, BigInt const *y) {
	if (x->len == 0) {
		big_int_init(dst, y);
		return;
	}
	if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (x->neg == y->neg) {
		dst->neg = x->neg;

		u64 const *x_words = big_int_ptr(x);
		u64 const *y_words = big_int_ptr(y);
		u64 overflow = cast(u64)add_overflow_u64(x_words[0], y_words[0], &dst->d.word);
		if (overflow == 0 && x->len == 1 && y->len == 1) {
			dst->len = 1;
			big_int_normalize(dst);
			return;
		}

		u64 first_word = dst->d.word;
		big_int_alloc(dst, 0, gb_max(x->len, y->len)+1);
		dst->d.words[0] = first_word;

		i32 i = 1;
		for (;;) {
			u64 v = overflow;
			overflow = 0;

			bool found_word = false;
			if (i < x->len) {
				found_word = true;
				overflow += add_overflow_u64(v, x_words[i], &v);
			}
			if (i < y->len) {
				found_word = true;
				overflow += add_overflow_u64(v, y_words[i], &v);
			}

			dst->d.words[i] = v;
			i += 1;

			if (!found_word) {
				dst->len = i;
				big_int_normalize(dst);
				return;
			}
		}
	} else {
		BigInt const *pos = nullptr;
		BigInt const *neg = nullptr;
		if (x->neg) {
			neg = x;
			pos = y;
		} else {
			GB_ASSERT(y->neg);
			pos = x;
			neg = y;
		}

		BigInt neg_abs = {};
		big_int_neg(&neg_abs, neg);
		BigInt const *bigger  = nullptr;
		BigInt const *smaller = nullptr;

		int cmp = big_int_cmp(pos, &neg_abs);
		dst->neg = cmp < 0;
		switch (cmp) {
		case 0:
			big_int_from_u64(dst, 0);
			return;
		case -1:
			bigger  = &neg_abs;
			smaller = pos;
			break;
		case +1:
			bigger  = pos;
			smaller = &neg_abs;
			break;
		default:
			GB_PANIC("Invalid bit_int_cmp value");
			return;
		}

		u64 const *bigger_words  = big_int_ptr(bigger);
		u64 const *smaller_words = big_int_ptr(smaller);
		u64 overflow = cast(u64)sub_overflow_u64(bigger_words[0], smaller_words[0], &dst->d.word);
		if (overflow == 0 && bigger->len == 1 && smaller->len == 1) {
			dst->len = 1;
			big_int_normalize(dst);
			return;
		}

		u64 first_word = dst->d.word;
		big_int_alloc(dst, 0, bigger->len);
		dst->d.words[0] = first_word;

		i32 i = 0;
		while (i < bigger->len) {
			u64 v = bigger_words[i];
			u64 prev_overflow = overflow;
			overflow = 0;

			bool found_word = false;
			if (i < smaller->len) {
				found_word = true;
				overflow += sub_overflow_u64(v, smaller_words[i], &v);
			}
			if (sub_overflow_u64(v, prev_overflow, &v)) {
				found_word = true;
				overflow += 1;
			}
			dst->d.words[i] = v;
			i += 1;

			if (!found_word) {
				break;
			}
		}

		GB_ASSERT(overflow == 0);
		dst->len = i;
		big_int_normalize(dst);
		return;
	}
}


void big_int_sub(BigInt *dst, BigInt const *x, BigInt const *y) {
	BigInt neg_y = {};
	big_int_neg(&neg_y, y);
	big_int_add(dst, x, &neg_y);
	big_int_normalize(dst);
	return;
}


void big_int_shl(BigInt *dst, BigInt const *x, BigInt const *y) {
	GB_ASSERT(!y->neg);

	if (x->len == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	if (x->len == 1 && x->d.word == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (y->len == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	if (y->len != 1) {
		GB_PANIC("SHL value greater than 64 bits!");
	}

	u64 const *xd = big_int_ptr(x);
	u64 shift_amount = big_int_to_u64(y);
	if (x->len == 1 && shift_amount < 64) {
		dst->d.word = xd[0] << shift_amount;
		if (dst->d.word > xd[0]) {
			dst->len = 1;
			dst->neg = x->neg;
			big_int_normalize(dst);
			return;
		}
	}

	u64 word_shift_len = shift_amount / 64;
	u64 remaining_shift_len = shift_amount % 64;

	big_int_alloc(dst, cast(i32)word_shift_len, x->len + word_shift_len + 1);
	GB_ASSERT((x->len + word_shift_len + 1) > 1);

	u64 carry = 0;
	for (i32 i = 0; i < x->len; i++) {
		u64 word = xd[i];
		dst->d.words[dst->len] = carry | (word << remaining_shift_len);
		dst->len += 1;
		if (remaining_shift_len > 0) {
			carry = word >> (64 - remaining_shift_len);
		} else {
			carry = 0;
		}
	}
	big_int_normalize(dst);
}

void big_int_shr(BigInt *dst, BigInt const *x, BigInt const *y) {
	GB_ASSERT(!y->neg);

	if (x->len == 0) {
		big_int_from_u64(dst, 0);
		return;
	}
	if (x->len == 1 && x->d.word == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (y->len != 1) {
		GB_PANIC("SHR value greater than 64 bits!");
	}

	u64 const *xd = big_int_ptr(x);
	u64 shift_amount = big_int_to_u64(y);

	if (x->len == 1) {
		dst->d.word = xd[0] >> shift_amount;
		dst->len = 1;
		dst->neg = x->neg;
		big_int_normalize(dst);
		return;
	}

	u64 word_shift_len = shift_amount / 64ull;
	u64 remaining_shift_len = shift_amount % 64ull;

	if (word_shift_len >= x->len) {
		big_int_from_u64(dst, 0);
		return;
	}

	i32 len = cast(i32)(x->len - word_shift_len);
	i32 cap = gb_max(len, dst->len);
	big_int_alloc(dst, len, cap);
	GB_ASSERT(dst->len >= 1);

	u64 carry = 0;
	for (i32 src_idx = x->len - 1; src_idx >= 0; src_idx--) {
		u64 v = xd[src_idx];
		u64 dst_idx = src_idx - word_shift_len;

		dst->d.words[dst_idx] = carry | (v >> remaining_shift_len);

		carry = v << (64ull - remaining_shift_len);
	}
	big_int_normalize(dst);
}

void big_int_mul_u64(BigInt *dst, BigInt const *x, u64 y) {
	BigInt v64 = {};
	big_int_from_u64(&v64, 64);

	big_int_from_u64(dst, 0);

	u64 const *xd = big_int_ptr(x);
	for (i32 i = x->len-1; i >= 0; i--) {
		BigInt shifted = {};
		big_int_shl(&shifted, dst, &v64);

		u64 result_u64 = 0;
		u64 carry_u64 = 0;
		mul_overflow_u64(y, xd[i], &result_u64, &carry_u64);

		BigInt result;
		BigInt carry;
		BigInt carry_shifted;
		big_int_from_u64(&result, result_u64);
		big_int_from_u64(&carry,  carry_u64);
		big_int_shl(&carry_shifted, &carry, &v64);

		BigInt tmp;
		big_int_add(&tmp, &shifted, &carry_shifted);
		big_int_add(dst, &tmp, &result);
	}
	big_int_normalize(dst);
}


void big_int_mul(BigInt *z, BigInt const *x, BigInt const *y) {
	if (x->len == 0 || y->len == 0) {
		return big_int_from_u64(z, 0);
	}
	u64 const *xd = big_int_ptr(x);
	u64 const *yd = big_int_ptr(y);

	u64 carry = 0;
	mul_overflow_u64(xd[0], yd[0], &z->d.word, &carry);
	if (carry == 0 && x->len == 1 && y->len == 1) {
		z->neg = (x->neg != y->neg);
		z->len = 1;
		big_int_normalize(z);
		return;
	}

	big_int_from_u64(z, 0);
	i32 len = x->len+y->len;
	big_int_alloc(z, len, len);
	u64 *zd = big_int_ptr(z);

	for (i32 i = 0; i < y->len; i++) {
		u64 d = yd[i];
		if (d != 0) {
			u64 *z = zd+i;
			i32 n = x->len;
			u64 c = 0;
			for (i32 j = 0; j < n; j++) {
				u64 z1 = 0;
				u64 z00 = 0;
				mul_overflow_u64(xd[j], d, &z00, &z1);
				u64 z0 = z00 + z[j];
				if (z0 < z00) {
					z1 += 1;
				}
				z[j] = z0 + c;
				c = 0;
				if (z[j] < z0) {
					c = 1;
				}
				c += z1;
			}

			zd[n+i] = c;
		}
	}

	z->neg = (x->neg != y->neg);
	big_int_normalize(z);
}


u64 leading_zeros_u64(u64 x) {
#if defined(GB_COMPILER_MSVC)
	return __lzcnt64(x);
#else
	return cast(u64)__builtin_clzll(cast(unsigned long long)x);
#endif
}


void bi__divWW(u64 u1, u64 u0, u64 y, u64 *q, u64 *r) {
#if defined(GB_COMPILER_MSVC) && defined(GB_ARCH_64_BIT)
	*q = unsafe_udiv128(u1, u0, y, r);
#else
	// NOTE(bill): q = (u1<<64 + u0 - r)/y
	// Hacker's Delight page 152
	if (u1 >= y) {
		*q = ~cast(u64)0ull;
		*r = ~cast(u64)0ull;
		return;
	}


	u64 s = leading_zeros_u64(y);
	y <<= s;

	static u64 const B = 1ull<<32ull;
	static u64 const M = B-1ull;

	u64 vn1  = y >> 32ull;
	u64 vn0  = y & M;
	u64 un32 = (u1<<s) | (u0>>(64ull-s));
	u64 un10 = u0 << s;
	u64 un1  = un10 >> 32ull;
	u64 un0  = un10 & M;
	u64 q1   = un32 / vn1;
	u64 rhat = un32 - (q1*vn1);

	while ((q1 >= B) || ((q1*vn0) > (B*rhat+un1))) {
		q1 -= 1;
		rhat += vn1;
		if (rhat >= B) {
			break;
		}
	}

	u64 un21 = (un32*B) + un1 - (q1*y);
	u64 q0 = un21 / vn1;
	rhat = un21 - (q0*vn1);

	while ((q0 >= B) || ((q0*vn0) > (B*rhat+un0))) {
		q0 -= 1;
		rhat += vn1;
		if (rhat >= B) {
			break;
		}
	}

	*q = q1*B + q0;
	*r = (un21*B + un0 - q0*y) >> s;
#endif
}


void bi__divWVW(BigInt *z, u64 xn, BigInt const *x, u64 y, u64 *r_) {
	GB_ASSERT(x->len >= z->len);
	u64 r = xn;
	u64 const *xd = big_int_ptr(x);
	u64 *zd = big_int_ptr(z);
	for (i32 i = z->len-1; i >= 0; i--) {
		u64 u1 = r;
		u64 u0 = xd[i];
		bi__divWW(r, xd[i], y, &zd[i], &r);
	}
	if (r_) *r_ = r;
}

void bi__divW(BigInt const *x, u64 y, BigInt *q_, u64 *r_) {
	BigInt q = {};
	u64 r = 0;
	i32 m = x->len;
	if (y == 0) {
		GB_PANIC("division by zero");
	} else if (y == 1) {
		q = *x;
	} else if (m == 0) {
		// okay
	} else {
		big_int_alloc(&q, m, m);
		bi__divWVW(&q, 0, x, y, &r);
		big_int_normalize(&q);
	}
	if (q_) *q_ = q;
	if (r_) *r_ = r;
}

u64 shlVU(BigInt *z, BigInt const *x, u64 s) {
	u64 c = 0;
	i32 n = z->len;

	u64 const *xd = big_int_ptr(x);
	u64 *zd = big_int_ptr(z);

	if (n > 0) {
		u64 s1 = 64 - s;
		u64 w1 = xd[n-1];
		c = w1 >> s1;
		for (i32 i = n-1; i > 0; i--) {
			u64 w = w1;
			w1 = xd[i-1];
			zd[i] = (w<<s) | (w1>>s1);
		}
		zd[0] = w1 << s;
	}
	return c;
}

u64 mulAddVWW(BigInt *z, BigInt const *x, u64 y, u64 r) {
	u64 c = r;
	u64 const *xd = big_int_ptr(x);
	u64 *zd = big_int_ptr(z);

	for (i32 i = 0; i < z->len; i++) {
		u64 a, b;
		mul_overflow_u64(xd[i], y, &a, &b);
		zd[i] = b + c;
		if (zd[i] < b) {
			a += 1;
		}
		c = a;
	}
	return c;
}

bool bi__greater_than(u64 x1, u64 x2, u64 y1, u64 y2) {
	return x1 > y1 || (x1 == x2 && x2 > y2);
}

void bi__div_large(BigInt const *a, BigInt const *b, BigInt *q, BigInt *r) {
	i32 n = b->len;
	i32 m = a->len - n;

	BigInt u = *a;
	BigInt v = *b;

	big_int_alloc(q, m+1, m+1);

	BigInt qhatv = {{cast(u64)n+1ull}, 1, false};

	big_int_alloc(&u, a->len+1, a->len+1);

	u64 *ud = big_int_ptr(&u);
	u64 *vd = big_int_ptr(&v);

	u64 shift = leading_zeros_u64(vd[n-1]);
	if (shift > 0) {
		BigInt v1 = {{cast(u64)n}, 1, false};
		shlVU(&v1, &v, shift);
		v = v1;
		vd = big_int_ptr(&v);
	}

	BigInt uu = u;
	uu.len = a->len;
	ud[a->len] = shlVU(&uu, a, shift);
}

void big_int_quo_rem_unsigned(BigInt const *a, BigInt const *b, BigInt *q_, BigInt *r_) {
	if (b->len == 0) {
		GB_PANIC("division by zero");
	} else if (b->len == 1 && b->d.word == 0) {
		GB_PANIC("division by zero");
	}

	BigInt x = *a; x.neg = false;
	BigInt y = *b; y.neg = false;
	BigInt q = {};
	BigInt r = {};

	int cmp = big_int_cmp(&x, &y);

	if (cmp < 0) {
		q = BIG_INT_ZERO;
		r = x;
		goto end;
	} else if (cmp == 0) {
		q = BIG_INT_ONE;
		r = BIG_INT_ZERO;
		goto end;
	}

	if (y.len == 1) {
		if (y.d.word == 0) {
			GB_PANIC("division by zero");
		} else if (y.d.word == 1) {
			q = x;
			r = BIG_INT_ZERO;
			goto end;
		} else if (x.len == 0) {
			q = BIG_INT_ZERO;
			r = BIG_INT_ZERO;
			goto end;
		}
		u64 rr = 0;
		bi__divW(&x, y.d.word, &q, &rr);
		big_int_from_u64(&r, rr);
		goto end;
	}

	GB_PANIC("Division of a large denominator not yet supported");
	bi__div_large(&x, &y, &q, &r);

end:
	big_int_normalize(&q);
	big_int_normalize(&r);
	if (q_) *q_ = q;
	if (r_) *r_ = r;
	return;
}

// `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
void big_int_quo_rem(BigInt const *x, BigInt const *y, BigInt *q_, BigInt *r_) {
	BigInt q = {};
	BigInt r = {};

	big_int_quo_rem_unsigned(x, y, &q, &r);
	q.neg = q.len > 0 && x->neg != y->neg;
	r.neg = r.len > 0 && x->neg;

	if (q_) *q_ = q;
	if (r_) *r_ = r;
}

void big_int_quo(BigInt *z, BigInt const *x, BigInt const *y) {
	BigInt r = {};
	big_int_quo_rem_unsigned(x, y, z, &r);
	z->neg = z->len > 0 && x->neg != y->neg;
}

void big_int_rem(BigInt *z, BigInt const *x, BigInt const *y) {
	BigInt q = {};
	big_int_quo_rem_unsigned(x, y, &q, z);
	z->neg = z->len > 0 && x->neg;
}



void big_int_euclidean_div(BigInt *z, BigInt const *x, BigInt const *y) {
	BigInt r = {};
	big_int_quo_rem(x, y, z, &r);
	if (r.neg) {
		if (y->neg) {
			big_int_add(z, z, &BIG_INT_ONE);
		} else {
			big_int_sub(z, z, &BIG_INT_ONE);
		}
	}
}


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->neg) {
		if (y0.neg) {
			big_int_sub(z, z, &y0);
		} else {
			big_int_add(z, z, &y0);
		}
	}
}


void big_int_and(BigInt *dst, BigInt const *x, BigInt const *y) {
	if (x->len == 0 || y->len == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	if (x->neg == y->neg) {
		if (x->neg) {
			// (-x) & (-y) == ~(x-1) & ~(x-y) == ~((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1)
			BigInt x1 = big_int_make_abs(x);
			BigInt y1 = big_int_make_abs(y);
			BigInt z1 = {};

			big_int_sub_eq(&x1, &BIG_INT_ONE);
			big_int_sub_eq(&y1, &BIG_INT_ONE);
			big_int_or(&z1, &x1, &y1);
			big_int_add(dst, &z1, &BIG_INT_ONE);
			dst->neg = true; // NOTE(bill): dst cannot be 0 as x and y are both negative
			big_int_normalize(dst);
			return;
		}
		u64 const *xd = big_int_ptr(x);
		u64 const *yd = big_int_ptr(y);

		if (x->len == 1 && y->len == 1) {
			dst->len = 1;
			dst->d.word = xd[0] & yd[0];
			return;
		}

		i32 len = gb_max(x->len, y->len);
		big_int_alloc(dst, len, len);
		GB_ASSERT(dst->len > 1);

		i32 i = 0;
		for (; i < x->len && i < y->len; i++) {
			dst->d.words[i] = xd[i] & yd[i];
		}
		for (; i < len; i++) {
			dst->d.words[i] = 0;
		}
		dst->neg = false;
		big_int_normalize(dst);
		return;
	}
	if (x->neg) {
		BigInt const *tmp = x;
		x = y;
		y = tmp;
		// NOTE(bill): AND is symmetric
	}


	// x & (-y) == x &~ (y-1)

	dst->neg = false;
	BigInt x1 = big_int_make_abs(x);
	BigInt y1 = big_int_make_abs(y);
	big_int_sub_eq(&y1, &BIG_INT_ONE);
	big_int_and_not(dst, &x1, &y1);
	big_int_normalize(dst);
}

void big_int__and_not_abs(BigInt *dst, BigInt const *x, BigInt const *y) {
	u64 const *xd = big_int_ptr(x);
	u64 const *yd = big_int_ptr(y);

	if (x->len == 1 && y->len == 1) {
		dst->len = 1;
		dst->d.word = xd[0] & (~yd[0]);
		return;
	}

	i32 len = gb_max(x->len, y->len);
	big_int_alloc(dst, len, len);
	GB_ASSERT(dst->len > 1);

	i32 i = 0;
	for (; i < x->len && i < y->len; i++) {
		dst->d.words[i] = xd[i] & (~yd[i]);
	}
	if (i < x->len) {
		for (; i < len; i++) {
			dst->d.words[i] = xd[i];
		}
	}
	if (i < y->len) {
		for (; i < len; i++) {
			dst->d.words[i] = yd[i];
		}
	}
	big_int_normalize(dst);
}

void big_int_and_not(BigInt *dst, BigInt const *x, BigInt const *y) {
	if (x->len == 0) {
		big_int_init(dst, y);
		return;
	}
	if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (x->neg == y->neg) {
		if (x->neg) {
			// (-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);
			big_int_sub_eq(&x1, &BIG_INT_ONE);
			big_int_sub_eq(&y1, &BIG_INT_ONE);

			big_int__and_not_abs(dst, &y1, &x1);
			dst->neg = false;
			big_int_normalize(dst);
			return;
		}

		big_int__and_not_abs(dst, x, y);
		dst->neg = false;
		big_int_normalize(dst);
		return;
	}

	if (x->neg) {
		// (-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);
		big_int_sub_eq(&x1, &BIG_INT_ONE);

		BigInt z1 = {};
		big_int_or(&z1, &x1, &y1);
		big_int_add(dst, &z1, &BIG_INT_ONE);
		dst->neg = true;
		big_int_normalize(dst);
		return;
	}

	// x &~ (-y) == x &~ ~(y-1) == x & (y-1)
	BigInt x1 = big_int_make_abs(x);
	BigInt y1 = big_int_make_abs(y);
	big_int_sub_eq(&y1, &BIG_INT_ONE);
	big_int_and(dst, &x1, &y1);
	dst->neg = false;
	big_int_normalize(dst);
	return;
}


void big_int__xor_abs(BigInt *dst, BigInt const *x, BigInt const *y) {
	u64 const *xd = big_int_ptr(x);
	u64 const *yd = big_int_ptr(y);

	if (x->len == 1 && y->len == 1) {
		dst->len = 1;
		dst->d.word = xd[0] ^ yd[0];
		big_int_normalize(dst);
		return;
	}

	i32 len = gb_max(x->len, y->len);
	big_int_alloc(dst, len, len);
	GB_ASSERT(dst->len > 1);

	i32 i = 0;
	for (; i < x->len && i < y->len; i++) {
		dst->d.words[i] = xd[i] ^ yd[i];
	}
	for (; i < len; i++) {
		if (i < x->len) {
			dst->d.words[i] = xd[i];
		}
		if (i < y->len) {
			dst->d.words[i] = yd[i];
		}
	}
	big_int_normalize(dst);
}

void big_int_xor(BigInt *dst, BigInt const *x, BigInt const *y) {
	if (x->len == 0) {
		big_int_init(dst, y);
		return;
	} else if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (x->neg == y->neg) {
		if (x->neg) {
			// (-x) ^ (-y) == ~(x-1) ^ ~(y-1) == (x-1) ^ (y-1)
			BigInt x1 = big_int_make_abs(x);
			BigInt y1 = big_int_make_abs(y);
			big_int_sub_eq(&x1, &BIG_INT_ONE);
			big_int_sub_eq(&y1, &BIG_INT_ONE);
			big_int__xor_abs(dst, &x1, &y1);
			dst->neg = false;
			return;
		}

		big_int__xor_abs(dst, x, y);
		dst->neg = false;
		return;
	}

	// x->neg != y->neg
	if (x->neg) {
		BigInt const *tmp = x;
		x = y;
		y = tmp;
	}

	// x ^ (-y) == x ^ ~(y-1) == ~(x ^ (y-1)) == -((x ^ (y-1)) + 1)

	dst->neg = false;
	BigInt y1 = big_int_make_abs(y);
	big_int_sub_eq(&y1, &BIG_INT_ONE);
	big_int__xor_abs(dst, x, &y1);
	big_int_add_eq(dst, &BIG_INT_ONE);
	dst->neg = true;
	return;
}


void big_int_or(BigInt *dst, BigInt const *x, BigInt const *y) {
	if (x->len == 0) {
		big_int_init(dst, y);
		return;
	} else if (y->len == 0) {
		big_int_init(dst, x);
		return;
	}

	if (x->neg == y->neg) {
		if (x->neg) {
			// (-x) | (-y) == ~(x-1) | ~(y-1) == ~((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1)
			BigInt x1 = big_int_make_abs(x);
			BigInt y1 = big_int_make_abs(y);
			big_int_sub_eq(&x1, &BIG_INT_ONE);
			big_int_sub_eq(&y1, &BIG_INT_ONE);
			big_int_and(dst, &x1, &y1);
			big_int_add_eq(dst, &BIG_INT_ONE);
			dst->neg = true;
			return;
		}

		dst->neg = x->neg;
		u64 const *xd = big_int_ptr(x);
		u64 const *yd = big_int_ptr(y);

		if (x->len == 1 && y->len == 1) {
			dst->len = 1;
			dst->d.word = xd[0] | yd[0];
			return;
		}

		i32 len = gb_max(x->len, y->len);
		big_int_alloc(dst, len, len);
		GB_ASSERT(dst->len > 1);

		for (i32 i = 0; i < len; i++) {
			u64 word = 0;
			if (i < x->len) {
				word |= xd[i];
			}
			if (i < y->len) {
				word |= yd[i];
			}

			dst->d.words[i] = word;
		}
		big_int_normalize(dst);
	}

	if (x->neg) {
		BigInt const *tmp = x;
		x = y;
		y = tmp;
	}

	// x | (-y) == x | ~(y-1) == ~((y-1) &~ x) == -(~((y-1) &~ x) + 1)
	BigInt y1 = big_int_make_abs(y);
	big_int_sub_eq(&y1, &BIG_INT_ONE);
	big_int__and_not_abs(dst, &y1, x);
	big_int_add_eq(dst, &BIG_INT_ONE);
	dst->neg = true;
	return;
}


void big_int_not(BigInt *dst, BigInt const *x, u64 bit_count, bool is_signed) {
	if (bit_count == 0) {
		big_int_from_u64(dst, 0);
		return;
	}

	// TODO(bill): Is this fast enough?

	dst->neg = false;
	u64 const *xd = big_int_ptr(x);
	if (bit_count <= 64) {
		dst->len = 1;
		if (x->len == 0) {
			if (bit_count == 64) {
				dst->d.word = ~cast(u64)0ull;
			} else {
				dst->d.word = (1ull << bit_count) - 1ull;
			}
		} else if (x->len == 1) {
			dst->d.word = ~xd[0];
			if (bit_count != 64) {
				u64 mask = (1ull << bit_count) - 1ull;
				dst->d.word &= mask;
			}
		}
	} else {
		dst->len = cast(i32)((bit_count+63ull) / 64ull);
		GB_ASSERT(dst->len >= x->len);
		big_int_alloc(dst, dst->len, dst->len);
		GB_ASSERT(dst->len > 1);

		i32 i = 0;
		for (; i < x->len; i++) {
			dst->d.words[i] = ~xd[i];
		}
		for (; i < dst->len; i++) {
			dst->d.words[i] = ~cast(u64)0ull;
		}

		i32 word_idx = cast(i32)(cast(u64)dst->len - (bit_count/64ull)-1ull);
		u32 word_bit_idx = bit_count % 64;
		if (word_idx < dst->len) {
			u64 mask = (1ull << word_bit_idx) - 1ull;
			dst->d.words[word_idx] &= mask;
		}
	}

	big_int_normalize(dst);

	if (is_signed) {
		BigInt prec = big_int_make_u64(bit_count-1);
		BigInt mask = {};
		BigInt mask_minus_one = {};
		big_int_shl(&mask, &BIG_INT_ONE, &prec);
		big_int_sub(&mask_minus_one, &mask, &BIG_INT_ONE);

		BigInt a = {};
		BigInt b = {};
		big_int_and(&a, dst, &mask_minus_one);
		big_int_and(&b, dst, &mask);
		big_int_sub(dst, &a, &b);
	}
}


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';
}

String big_int_to_string(gbAllocator allocator, BigInt const *x, u64 base) {
	GB_ASSERT(base <= 16);

	if ((x->len == 0) && (x->len == 1 && x->d.word == 0)) {
		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);

	BigInt v = *x;

	if (v.neg) {
		array_add(&buf, '-');
		v.neg = false;
		big_int_normalize(&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));


	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);
}