-
Notifications
You must be signed in to change notification settings - Fork 8
/
runtime.js
1228 lines (1120 loc) · 32 KB
/
runtime.js
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
/* eslint-disable no-unused-vars */
// We represent a [Z.t] as a javascript 32bit integers or as a BigInt.
// Like in ZArith, we guarantee that:
// - If the number fits in a 32bit integer, it is stored in a 32bit integer,
// not a BigInt.
// - Conversely, if the number does not fit in a 32bit integer, it is stored in
// a BigInt. We almost could get away with using primitive integers up to 53
// bits, but this would require giving up marshaling (because the js_of_ocaml
// runtime assumes that numbers are either 32bit integers or floats, and
// refuses to marshal floats that are not exactly 32bit integers), and so we
// don't. I am not sure this would bring much performance benefits due to the
// additional checks required anyways (the checks for 32bit integers happen
// in ZArith's OCaml code, so we can't replace them with checks for 53 bit
// integers).
//
// Note that some functions in this module call other functions as utility in
// a way that doesn't respect these invariants, and so some functions can see
// non-normalized numbers as input.
//Provides: ml_z_normalize
function ml_z_normalize(x) {
// This is the primary reason runtime and runtime_wasm are separate despite looking.
// very similar. JavaScript uses 32bit as its cutoff between number and BigInt, and
// WebAssembly uses 31bit. With some amount of effort we could merge a lot of this
// logic.
if (typeof x === "number") {
if (x === (x | 0)) return x;
return BigInt(x);
}
if (-2147483648 <= x && x <= 2147483647) return Number(x) | 0;
return x;
}
//external mul_overflows: int -> int -> bool
//Provides: ml_z_mul_overflows
function ml_z_mul_overflows(x, y) {
let z = x * y;
return +(z !== (z | 0));
}
//external init: unit -> unit
//Provides: ml_z_init
//Requires: caml_zarith_marshal, caml_zarith_unmarshal, caml_custom_ops
//Requires: ml_z_hash, ml_z_compare
function ml_z_init(unit) {
caml_custom_ops['_z'] =
{
serialize: caml_zarith_marshal,
deserialize: caml_zarith_unmarshal,
hash: ml_z_hash,
compare: ml_z_compare,
};
Object.defineProperty(BigInt.prototype, 'caml_custom', { value: '_z' });
return 0
}
//external neg: t -> t
//Provides: ml_z_neg const
//Requires: ml_z_normalize
function ml_z_neg(z1) {
return ml_z_normalize(-z1);
}
//external add: t -> t -> t
//Provides: ml_z_add const
//Requires: ml_z_normalize
function ml_z_add(z1, z2) {
return ml_z_normalize(BigInt(z1) + BigInt(z2));
}
//external sub: t -> t -> t
//Provides: ml_z_sub const
//Requires: ml_z_normalize
function ml_z_sub(z1, z2) {
return ml_z_normalize(BigInt(z1) - BigInt(z2));
}
//external mul: t -> t -> t
//Provides: ml_z_mul const
//Requires: ml_z_normalize
function ml_z_mul(z1, z2) {
return ml_z_normalize(BigInt(z1) * BigInt(z2));
}
//external div: t -> t -> t
//Provides: ml_z_div
//Requires: caml_raise_zero_divide, ml_z_normalize
function ml_z_div(z1, z2) {
if (z2 == 0) caml_raise_zero_divide();
return ml_z_normalize(BigInt(z1) / BigInt(z2));
}
//external cdiv: t -> t -> t
//Provides: ml_z_cdiv
//Requires: ml_z_div, ml_z_sign, ml_z_normalize
function ml_z_cdiv(z1, z2) {
let z1_pos = ml_z_sign(z1);
let z2_pos = ml_z_sign(z2);
if (z1_pos * z2_pos > 0) /* Multiplication is like a signwise xor */ {
if (BigInt(z1) % BigInt(z2) !== 0n) {
return ml_z_normalize(BigInt(z1) / BigInt(z2) + 1n);
}
}
return ml_z_div(z1, z2);
}
//external fdiv: t -> t -> t
//Provides: ml_z_fdiv
//Requires: ml_z_div, ml_z_sign, ml_z_normalize
function ml_z_fdiv(z1, z2) {
let z1_pos = ml_z_sign(z1);
let z2_pos = ml_z_sign(z2);
if (z1_pos * z2_pos < 0) /* Multiplication is like a signwise xor */ {
if (BigInt(z1) % BigInt(z2) !== 0n) {
return ml_z_normalize(BigInt(z1) / BigInt(z2) - 1n);
}
}
return ml_z_div(z1, z2);
}
//external rem: t -> t -> t
//Provides: ml_z_rem
//Requires: caml_raise_zero_divide, ml_z_normalize
function ml_z_rem(z1, z2) {
if (z2 == 0) caml_raise_zero_divide();
return ml_z_normalize(BigInt(z1) % BigInt(z2));
}
//external div_rem: t -> t -> (t * t)
//Provides: ml_z_div_rem
//Requires: ml_z_div, ml_z_rem
function ml_z_div_rem(z1, z2) {
return [0, ml_z_div(z1, z2), ml_z_rem(z1, z2)]
}
//external succ: t -> t
//Provides: ml_z_succ const
//Requires: ml_z_normalize
function ml_z_succ(z1) {
return ml_z_normalize(BigInt(z1) + 1n);
}
//external pred: t -> t
//Provides: ml_z_pred const
//Requires: ml_z_normalize
function ml_z_pred(z1) {
return ml_z_normalize(BigInt(z1) - 1n);
}
//external abs: t -> t
//Provides: ml_z_abs const
//Requires: ml_z_normalize
function ml_z_abs(z1) {
if (z1 < 0) return ml_z_normalize(-z1);
return z1;
}
//external logand: t -> t -> t
//Provides: ml_z_logand const
//Requires: ml_z_normalize
function ml_z_logand(z1, z2) {
return ml_z_normalize(BigInt(z1) & BigInt(z2));
}
//external logor: t -> t -> t
//Provides: ml_z_logor const
//Requires: ml_z_normalize
function ml_z_logor(z1, z2) {
return ml_z_normalize(BigInt(z1) | BigInt(z2));
}
//external logxor: t -> t -> t
//Provides: ml_z_logxor const
//Requires: ml_z_normalize
function ml_z_logxor(z1, z2) {
return ml_z_normalize(BigInt(z1) ^ BigInt(z2));
}
//external lognot: t -> t
//Provides: ml_z_lognot const
//Requires: ml_z_normalize
function ml_z_lognot(z1) {
return ml_z_normalize(~z1);
}
//external shift_left: t -> int -> t
//Provides: ml_z_shift_left const
//Requires: ml_z_normalize
function ml_z_shift_left(z1, amt) {
return ml_z_normalize(BigInt(z1) << BigInt(amt));
}
//external shift_right: t -> int -> t
//Provides: ml_z_shift_right const
//Requires: ml_z_normalize
function ml_z_shift_right(z1, amt) {
return ml_z_normalize(BigInt(z1) >> BigInt(amt));
}
//external shift_right_trunc: t -> int -> t
//Provides: ml_z_shift_right_trunc const
//Requires: ml_z_normalize
function ml_z_shift_right_trunc(z1, z2) {
return ml_z_normalize(BigInt(z1) / (1n << BigInt(z2)));
}
//external of_int32: int32 -> t
//Provides: ml_z_of_int32 const
function ml_z_of_int32(i) {
return i | 0;
}
//external of_nativeint: nativeint -> t
//Provides: ml_z_of_nativeint const
function ml_z_of_nativeint(i) {
return i | 0;
}
//external of_int64: int64 -> t
//Provides: ml_z_of_int64 const
//Requires: caml_int64_compare, caml_int64_neg, ml_z_normalize
//Requires: caml_int64_create_lo_hi,caml_int64_hi32,caml_int64_lo32
function ml_z_of_int64(i64) {
let neg = false;
if (caml_int64_compare(i64, caml_int64_create_lo_hi(0, 0)) < 0) {
neg = true;
i64 = caml_int64_neg(i64)
}
let lo = caml_int64_lo32(i64) >>> 0;
let hi = caml_int64_hi32(i64) >>> 0;
let x = BigInt(lo) + (BigInt(hi) << 32n);
if (neg) { x = -x }
return ml_z_normalize(x)
}
//external of_float: float -> t
//Provides: ml_z_of_float
//Requires: caml_raise_constant, caml_named_value, ml_z_normalize
function ml_z_of_float(f1) {
if (f1 == Infinity || f1 == -Infinity || f1 != f1)
caml_raise_constant(caml_named_value("ml_z_overflow"));
return ml_z_normalize(f1 < 0 ? Math.ceil(f1) : Math.floor(f1));
}
//external to_int: t -> int
//Provides: ml_z_to_int
//Requires: caml_raise_constant, caml_named_value
function ml_z_to_int(z1) {
if (typeof z1 === "number" && z1 === (z1 | 0)) return z1;
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
//external to_int32: t -> int32
//Provides: ml_z_to_int32
//Requires: ml_z_to_int
function ml_z_to_int32(z1) { return ml_z_to_int(z1) }
//external to_int32_unsigned: t -> int32
//Provides: ml_z_to_int32_unsigned
//Requires: ml_z_fits_int32_unsigned, ml_z_normalize, caml_raise_constant
//Requires: caml_named_value
function ml_z_to_int32_unsigned(z1) {
if (ml_z_fits_int32_unsigned(z1)) {
return Number(BigInt.asIntN(32, BigInt(z1))) | 0;
}
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
//external to_nativeint_unsigned: t -> nativeint
//Provides: ml_z_to_nativeint_unsigned
//Requires: ml_z_to_int32_unsigned
function ml_z_to_nativeint_unsigned(z1) {
return ml_z_to_int32_unsigned(z1);
}
//external to_int64: t -> int64
//Provides: ml_z_to_int64
//Requires: ml_z_fits_int64, caml_raise_constant, caml_named_value
//Requires: caml_int64_create_lo_hi
function ml_z_to_int64(z1) {
if (!ml_z_fits_int64(z1)) {
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
let z1n = BigInt(z1)
let mask = 0xffffffffn
let lo = Number(z1n & mask);
let hi = Number((z1n >> 32n) & mask);
let x = caml_int64_create_lo_hi(lo, hi);
return x;
}
//external to_int64_unsigned: t -> int64
//Provides: ml_z_to_int64_unsigned
//Requires: ml_z_fits_int64_unsigned, caml_raise_constant, caml_named_value
//Requires: caml_int64_create_lo_hi
function ml_z_to_int64_unsigned(z1) {
if (!ml_z_fits_int64_unsigned(z1)) {
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
let z1n = BigInt.asIntN(64, BigInt(z1))
let mask = 0xffffffffn
let lo = Number(z1n & mask);
let hi = Number((z1n >> 32n) & mask);
let x = caml_int64_create_lo_hi(lo, hi);
return x;
}
//external testbit: t -> int -> bool
//Provides: ml_z_testbit const
function ml_z_testbit(z, pos) {
return Number((BigInt(z) >> BigInt(pos)) & 1n);
}
//external to_nativeint: t -> nativeint
//Provides: ml_z_to_nativeint
//Requires: ml_z_to_int
function ml_z_to_nativeint(z1) { return ml_z_to_int(z1) }
//external format: string -> t -> string
//Provides: ml_z_format
//Requires: caml_jsbytes_of_string, caml_failwith, caml_string_of_jsbytes
//Requires: ml_z_normalize
function ml_z_format(fmt, z1) {
let z1n = BigInt(z1);
fmt = caml_jsbytes_of_string(fmt);
// https://github.com/ocaml/Zarith/blob/d0555d451ce295c4497f24a8d9993f8dd23097df/z.mlip#L297
let base = 10;
let cas = 0;
let width = 0;
let alt = 0;
let dir = 0;
let sign = '';
let pad = ' ';
let idx = 0;
let prefix = "";
while (fmt[idx] == '%') idx++;
for (; ; idx++) {
if (fmt[idx] == '#') alt = 1;
else if (fmt[idx] == '0') pad = '0';
else if (fmt[idx] == '-') dir = 1;
else if (fmt[idx] == ' ' || fmt[idx] == '+') sign = fmt[idx];
else break;
}
if (z1n < 0) { sign = '-'; z1n = -z1n }
for (; fmt[idx] >= '0' && fmt[idx] <= '9'; idx++)
width = 10 * width + (+fmt[idx]);
switch (fmt[idx]) {
case 'i': case 'd': case 'u': break;
case 'b': base = 2; if (alt) prefix = "0b"; break;
case 'o': base = 8; if (alt) prefix = "0o"; break;
case 'x': base = 16; if (alt) prefix = "0x"; break;
case 'X': base = 16; if (alt) prefix = "0X"; cas = 1; break;
default:
caml_failwith("Unsupported format '" + fmt + "'");
}
if (dir) pad = ' ';
let res = z1n.toString(base);
if (cas === 1) {
res = res.toUpperCase();
}
let size = res.length;
if (pad == ' ') {
if (dir) {
res = sign + prefix + res;
for (; res.length < width;) res = res + pad;
} else {
res = sign + prefix + res;
for (; res.length < width;) res = pad + res;
}
} else {
let pre = sign + prefix;
for (; res.length + pre.length < width;) res = pad + res;
res = pre + res;
}
return caml_string_of_jsbytes(res);
}
//Provides: jsoo_z_of_js_string_base
//Requires: caml_invalid_argument, ml_z_normalize
function jsoo_z_of_js_string_base(base, s) {
if (base == 0) { // https://github.com/ocaml/Zarith/blob/b8dbaf48a7927061df699ad7ce642bb4f1fe5308/caml_z.c#L598
base = 10;
let p = 0;
let sign = 1;
if (s[p] == '-') { sign = -1; p++ }
else if (s[p] == '+') { p++ }
if (s[p] == '0') {
p++;
if (s.length == p) {
return 0;
} else {
let bc = s[p];
if (bc == 'o' || bc == 'O') {
base = 8;
} else if (bc == 'x' || bc == 'X') {
base = 16;
} else if (bc == 'b' || bc == 'B') {
base = 2;
}
if (base != 10) {
s = s.substring(p + 1);
if (sign == -1) s = "-" + s;
}
}
}
}
function digit(code) {
if (code >= 48 && code <= 57) return code - 48;
if (code >= 97 && code <= 102) return code - 97 + 10;
if (code >= 65 && code <= 70) return code - 65 + 10;
}
let i = 0;
if (s[i] == '+') {
//remove leading '+'
s = s.substring(1);
}
else if (s[i] == '-') i++;
if (s[i] == '_') caml_invalid_argument("Z.of_substring_base: invalid digit");
s = s.replace(/_/g, '');
//normalize "empty" numbers
if (s == '-' || s == '') s = '0';
for (; i < s.length; i++) {
let c = digit(s.charCodeAt(i));
if (c == undefined || c >= base)
caml_invalid_argument("Z.of_substring_base: invalid digit");
}
if (base === 10) return ml_z_normalize(BigInt(s));
let neg = false;
i = 0;
if (s[i] == '-') { neg = true; i++; }
let n = 0n;
for (; i < s.length; i++) {
n *= BigInt(base);
n += BigInt(digit(s.charCodeAt(i)));
}
if (neg) n = -n;
return ml_z_normalize(n);
}
//external of_substring_base: int -> string -> pos:int -> len:int -> t
//Provides: ml_z_of_substring_base
//Requires: jsoo_z_of_js_string_base, caml_jsbytes_of_string
//Requires: caml_invalid_argument, caml_ml_string_length
function ml_z_of_substring_base(base, s, pos, len) {
s = caml_jsbytes_of_string(s);
if (pos != 0 || len != s.length) {
if (s.length - pos < len) {
caml_invalid_argument("Z.of_substring_base: invalid offset or length");
}
s = s.slice(pos, pos + len);
}
return jsoo_z_of_js_string_base(base, s);
}
//external compare: t -> t -> int
//Provides: ml_z_compare const
function ml_z_compare(z1, z2) {
return z1 == z2 ? 0 : z1 > z2 ? 1 : -1;
}
//external equal: t -> t -> bool
//Provides: ml_z_equal const
function ml_z_equal(z1, z2) {
return z1 == z2 ? 1 : 0;
}
//external sign: t -> int
//Provides: ml_z_sign const
function ml_z_sign(z1) {
return z1 == 0 ? 0 : z1 > 0 ? 1 : -1;
}
//external gcd: t -> t -> t
//Provides: ml_z_gcd
//Requires: ml_z_normalize
function ml_z_gcd(z1, z2) {
let a = BigInt(z1);
if (a < 0) a = -a;
let b = BigInt(z2);
if (b < 0) b = -b;
if (a === b) return ml_z_normalize(a);
if (a === 0n) return ml_z_normalize(b);
if (b === 0n) return ml_z_normalize(a);
let c = 1n, d, t;
function min(a, b) { return a < b ? a : b }
while ((a & 1n) === 0n && (b & 1n) === 0n) {
d = min(a & -a, b & -b);
a /= d;
b /= d;
c *= d;
}
while ((a & 1n) === 0n) {
a /= a & -a;
}
do {
while ((b & 1n) === 0n) {
b /= b & -b;
}
if (a > b) {
t = b; b = a; a = t;
}
b -= a;
} while (b !== 0n);
return ml_z_normalize(c === 1n ? a : a * c);
}
//external numbits: t -> int
//Provides: ml_z_numbits const
function ml_z_numbits(z1) {
if (z1 < 0) z1 = -z1;
let n = 0;
let upperBound = 1n;
while (upperBound <= z1) {
n += 1;
upperBound = upperBound << 1n;
}
return n; // 2^{n-1} <= |x| < 2^n
}
//external fits_int: t -> bool
//Provides: ml_z_fits_int const
function ml_z_fits_int(z1) {
return typeof z1 === "number" ? +(z1 === (z1 | 0)) : 0;
}
//external fits_int32: t -> bool
//Provides: ml_z_fits_int32
//Requires: ml_z_fits_int
function ml_z_fits_int32(z1) {
return ml_z_fits_int(z1);
}
//external fits_int32_unsigned: t -> bool
//Provides: ml_z_fits_int32_unsigned
function ml_z_fits_int32_unsigned(z1) {
return 0 <= z1 && z1 <= 4294967295;
}
//external fits_int64_unsigned: t -> bool
//Provides: ml_z_fits_int64_unsigned
function ml_z_fits_int64_unsigned(z1) {
return 0 <= z1 && z1 <= 18446744073709551615n;
}
//external fits_nativeint_unsigned: t -> bool
//Provides: ml_z_fits_nativeint_unsigned
//Requires: ml_z_fits_int32_unsigned
function ml_z_fits_nativeint_unsigned(z1) {
return ml_z_fits_int32_unsigned(z1);
}
//external fits_int64: t -> bool
//Provides: ml_z_fits_int64
function ml_z_fits_int64(z1) {
if (z1 <= 9223372036854775807n && z1 >= -9223372036854775808n)
return 1
else
return 0
}
//external fits_nativeint: t -> bool
//Provides: ml_z_fits_nativeint
//Requires: ml_z_fits_int
function ml_z_fits_nativeint(z1) {
return ml_z_fits_int(z1);
}
//external powm: t -> t -> t -> t
//Provides: ml_z_powm
//Requires: ml_z_normalize, ml_z_invert, caml_raise_zero_divide
//Requires: jsoo_bigint_mod_pow
function ml_z_powm(z1, z2, z3) {
if (z3 == 0) caml_raise_zero_divide();
if (z3 == 1 || z3 == -1) return 0;
if (z2 == 0) return 1;
let z3n = BigInt(z3);
if (z2 < 0) {
let inv = BigInt(ml_z_invert(z1, z3));
let r = jsoo_bigint_mod_pow(inv, BigInt(-z2), z3n);
if (r < 0) r = r + (z3n < 0 ? -z3n : z3n);
return ml_z_normalize(r);
} else {
let r = jsoo_bigint_mod_pow(BigInt(z1), BigInt(z2), z3n);
if (r < 0) r = r + (z3n < 0 ? -z3n : z3n);
return ml_z_normalize(r);
}
}
//external pown: t -> t -> t
//Provides: ml_z_pow
//Requires: caml_failwith, ml_z_normalize, caml_invalid_argument
function ml_z_pow(z1, i1) {
if (i1 < 0) {
caml_invalid_argument("Z.pow: exponent must be nonnegative");
}
return ml_z_normalize(BigInt(z1) ** BigInt(i1));
}
//external hash: t -> int
//Provides: ml_z_hash const
//Requires: caml_hash_mix_int
function ml_z_hash(z1) {
let z1n = BigInt(z1);
let neg = z1n < 0;
if (neg) z1n = -z1n;
let acc = 0, len = 1;
let left = z1n;
while (left >= 2 ** 32) {
acc = caml_hash_mix_int(acc, Number(left & 0xffffffffn));
left >>= 32n;
len += 1;
}
acc = caml_hash_mix_int(acc, Number(left) | 0);
if (len % 2 !== 0) {
acc = caml_hash_mix_int(acc, 0);
}
if (neg) {
acc = acc + 1
}
return acc | 0
}
//external to_bits: t -> string
//Provides: ml_z_to_bits const
//Requires: caml_string_of_jsbytes, caml_str_repeat
function ml_z_to_bits(z1) {
let z1n = BigInt(z1);
if (z1n < 0) z1n = -z1n;
let res = "";
while (z1n !== 0n) {
res += String.fromCharCode(Number(z1n % 256n) | 0);
z1n /= 256n;
}
while (res.length % 4 != 0) {
res += String.fromCharCode(0);
}
return caml_string_of_jsbytes(res);
}
//external of_bits: string -> t
//Provides: ml_z_of_bits const
//Requires: caml_string_unsafe_get, caml_ml_string_length, ml_z_normalize
function ml_z_of_bits(z1) {
let r = 0n;
let base = 1n;
for (let i = 0; i < caml_ml_string_length(z1); i++) {
let d = caml_string_unsafe_get(z1, i);
r += base * BigInt(d);
base *= 256n;
}
return ml_z_normalize(r);
}
//external powm_sec: t -> t -> t -> t
//Provides: ml_z_powm_sec
//Requires: caml_failwith, ml_z_powm, caml_invalid_argument
function ml_z_powm_sec(z1, z2, z3) {
if (z1 < 0) z3 = -z3;
// powm_sec requires that the exponent be positive
if (z2 < 1) {
caml_invalid_argument("Z.powm_sec: exponent must be positive");
}
if ((BigInt(z3) & 1n) !== 1n) {
caml_invalid_argument("Z.powm_sec: modulus must be odd");
}
return ml_z_powm(z1, z2, z3)
}
//external root: t -> int -> t
//Provides: ml_z_root
//Requires: ml_z_normalize, ml_z_numbits, caml_invalid_argument
function ml_z_root(z, i) {
if (i % 2 === 0 && z < 0) {
caml_invalid_argument("Z.root: even root of a negative number");
}
if (z == 0 || z == 1) {
return Number(z) | 0;
}
z = BigInt(z);
i = BigInt(i);
var log2z = ml_z_numbits(z);
var i_minus_1 = i - 1n;
// Start with an upper bound of the root
var x = 1n << ((BigInt(log2z) + i_minus_1) / i);
while (1) {
// Use Newton's method to get a better approximation of the root
var next = ((i_minus_1 * x) + (z / (x ** i_minus_1))) / i;
// The sequence is strictly decreasing until we reach the result
// See https://github.com/waldemarhorwat/integer-roots for a proof
if (x <= next) {
return ml_z_normalize(x);
}
x = next
}
}
//external rootrem: t -> int -> t * t
//Provides: ml_z_rootrem
//Requires: ml_z_root, ml_z_normalize, caml_invalid_argument
function ml_z_rootrem(z, i) {
if (i % 2 === 0 && z < 0) {
caml_invalid_argument("Z.rootrem: even root of a negative number");
}
let ans = ml_z_root(z, i);
return [0, ans, ml_z_normalize(BigInt(z) - (BigInt(ans) ** BigInt(i)))];
}
//external invert: t -> t -> t
//Provides: ml_z_invert
//Requires: caml_raise_zero_divide, ml_z_gcdext_intern, ml_z_normalize
function ml_z_invert(a, n) {
// Because [a.modInv(n)] produces different results for edge cases,
// we wrote our own implementation based on gcdext_intern.
if (n == 1 || n == -1)
return 0;
if (n == 0 && (a == 1 || a == -1)) {
return a;
}
if (n == 0 || a == 0) {
caml_raise_zero_divide();
}
let x = ml_z_gcdext_intern(a, n);
let r = BigInt(x[2]);
a = BigInt(a);
n = BigInt(n);
if (n < 0) n = -n;
let tmp = (a * r) % n;
if (tmp < 0) tmp += n;
if (r < 0) r += n;
if (tmp == 1) {
return ml_z_normalize(r);
}
caml_raise_zero_divide();
}
//external perfect_power: t -> bool
//Provides: ml_z_perfect_power
//Requires: caml_failwith, ml_z_numbits, ml_z_root, ml_z_pow
function ml_z_perfect_power(z) {
// Return true if op is a perfect power, i.e., if there exist integers a and
// b, with b > 1, such that op = a^b.
// Otherwise false.
if (z == 0 || z == 1 || z == -1) return 1;
let zp = z < 0 ? -z : z;
let log2z = ml_z_numbits(zp);
for (let b = 2; b <= log2z; b++) {
if (z < 0 && b % 2 == 0) continue;
let p = ml_z_root(zp, b);
if (z < 0) p = -p;
let r = ml_z_pow(p, b);
if (z == r) {
return 1;
}
}
return 0;
}
//external perfect_square: t -> bool
//Provides: ml_z_perfect_square
//Requires: ml_z_root
function ml_z_perfect_square(z) {
if (z < 0) return 0;
let root = BigInt(ml_z_root(z, 2));
if (root * root == z) {
return 1;
} else {
return 0
}
}
//Must have 0 <= low <= high
//Provides: jsoo_bigint_rand_between
function jsoo_bigint_rand_between(low, high) {
let range = high - low + 1n;
let base = 1e7, bigbase = BigInt(base);
let result = 0n, restricted = true;
let digits = [];
while (range >= bigbase) {
digits.push(Number(range % bigbase));
range /= bigbase;
}
digits.push(Number(range));
digits.reverse();
for (let i = 0; i < digits.length; i++) {
let top = restricted ? digits[i] | 0 : base;
range /= bigbase;
let digit = Math.floor(Math.random() * top);
result *= bigbase;
result += BigInt(digit);
if (digit < top) restricted = false;
}
return low + result;
}
//Provides: jsoo_bigint_mod_pow
function jsoo_bigint_mod_pow(self, exp, mod) {
if (mod === 0n) throw new Error("Cannot take modPow with modulo 0");
if (exp < 0) throw new Error("Cannot take modPow with negative exponent");
let r = 1n, base = self % mod;
while (exp > 0) {
if (base === 0n) return 0n;
if ((exp & 1n) === 1n) r = (r * base) % mod;
exp >>= 1n;
base = (base * base) % mod;
}
return r;
}
//external probab_prime: t -> int -> int
//Provides: ml_z_probab_prime const
//Requires: jsoo_bigint_mod_pow, jsoo_bigint_rand_between
//Note: called with [1n] from [ml_z_primorial]
function ml_z_probab_prime(z, i) {
if (z < 0) z = -z;
// Test for basic primes (multiples of 2, 3, 5)
if (z == 1) return 0;
if (z == 2 || z == 3 || z == 5) return 1;
let n = BigInt(z);
if (n % 2n === 0n || n % 3n === 0n || n % 5n === 0n) return 0;
if (z < 49) return 1;
// Miller-Rabin test
let nPrev = n - 1n,
b = nPrev,
r = 0,
a, d, j, x;
while (b % 2n === 0n) b /= 2n, r++;
next: for (j = 0; j < i; j++) {
a = jsoo_bigint_rand_between(2n, n - 2n);
x = jsoo_bigint_mod_pow(a, b, n);
if (x === 1n || x === nPrev) continue;
for (d = r - 1; d != 0; d--) {
x = (x * x) % n;
if (x === 1n) return 0;
if (x === nPrev) continue next;
}
return 0;
}
return 1;
}
//external nextprime: t -> t
//Provides: ml_z_nextprime const
//Requires: ml_z_normalize, ml_z_probab_prime
function ml_z_nextprime(z1) {
// Interestingly, the zarith next_prime only returns
// probabalistic primes. We do the same, with the
// same probablistic parameter of 25.
// https://fossies.org/dox/gmp-6.1.2/mpz_2nextprime_8c_source.html
if (z1 < 1 || z1 == 1) {
return 2;
}
let z1n = BigInt(z1)
if ((z1n & 1n) === 1n) {
z1n += 2n;
} else {
z1n += 1n;
}
for (; ;) {
if (ml_z_probab_prime(z1n, 25)) {
return ml_z_normalize(z1n);
} else {
z1n += 2n;
}
}
}
//external c_extract: t -> int -> int -> t
//Provides: ml_z_extract
//Requires: caml_failwith, ml_z_normalize
function ml_z_extract(z1, pos, len) {
return ml_z_normalize((BigInt(z1) >> BigInt(pos)) & ((1n << BigInt(len)) - 1n));
}
//external c_extract_small: t -> int -> int -> t
//Provides: ml_z_extract_small
function ml_z_extract_small(z1, pos, len) {
return Number(BigInt.asIntN(32, z1 >> BigInt(pos))) & ((1 << len) - 1);
}
//external gcdext_intern: t -> t -> (t * t * bool)
//Provides: ml_z_gcdext_intern
//Requires: caml_raise_zero_divide, ml_z_normalize
function ml_z_gcdext_intern(z1, z2) {
if (z1 == 0) caml_raise_zero_divide();
let a = BigInt(z1);
let b = BigInt(z2);
let x = 0n;
let lastx = 1n;
let y = 1n;
let lasty = 1n;
let q, t, r;
while (b !== 0n) {
q = a / b;
r = a - q * b;
t = x;
x = lastx - q * x;
lastx = t;
t = y;
y = lasty - q * y;
lasty = t;
a = b;
b = r;
}
if (a < 0)
return [0, ml_z_normalize(-a), ml_z_normalize(-lastx), 1]
else
return [0, ml_z_normalize(a), ml_z_normalize(lastx), 1]
}
//external sqrt: t -> t
//Provides: ml_z_sqrt
//Requires: ml_z_root, caml_invalid_argument
function ml_z_sqrt(z1) {
if (z1 < 0) {
caml_invalid_argument("Z.sqrt: square root of a negative number");
}
return ml_z_root(z1, 2);
}
//external sqrt_rem: t -> (t * t)
//Provides: ml_z_sqrt_rem
//Requires: ml_z_root, caml_invalid_argument, ml_z_normalize
function ml_z_sqrt_rem(z) {
if (z < 0) {
caml_invalid_argument("Z.sqrt_rem: square root of a negative number");
}
let root = ml_z_root(z, 2);
let rootn = BigInt(root);
let diff = BigInt(z) - rootn * rootn;
return [0, root, ml_z_normalize(diff)]
}
//external trailing_zeros: t -> int
//Provides: ml_z_trailing_zeros const
function ml_z_trailing_zeros(z) {
if (z == 0) {
// max_int in 32bit
return 0x7fffffff;
}
if (z < 0) z = -z;
let zn = BigInt(z);
let i = 0;
zn = (zn ^ (zn - 1n)) >> 1n;
for (i = 0; zn !== 0n; i++) zn >>= 1n;
return i;
}
//external popcount: t -> int
//Provides: ml_z_popcount
//Requires: caml_raise_constant, caml_named_value
function ml_z_popcount(z) {
if (z < 0) {
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
z = BigInt(z);
let i;
for (i = 0; z !== 0n; i++) {
z &= z - 1n;
}
if (i !== (i | 0)) caml_raise_constant(caml_named_value("ml_z_overflow"));
return i | 0;
}
//external hamdist: t -> t -> int
//Provides: ml_z_hamdist
//Requires: ml_z_popcount, caml_invalid_argument, caml_raise_constant
//Requires: caml_named_value, ml_z_normalize
function ml_z_hamdist(z1, z2) {
if ((z1 < 0) !== (z2 < 0)) {
caml_raise_constant(caml_named_value("ml_z_overflow"));
}
if ((typeof z1 == 'bignum' || typeof z2 == 'bignum') && (z1 < 0 || z2 < 0)) {
caml_invalid_argument("Z.hamdist: negative arguments");
}
return ml_z_popcount(BigInt(z1) ^ BigInt(z2));
}
//external size: t -> int
//Provides: ml_z_size const
function ml_z_size(z1) {
// Claim to be a 32-bit architecture.
if (z1 < 0) z1 = -z1;