forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_int64.js
604 lines (558 loc) · 12.5 KB
/
caml_int64.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
'use strict';
var Caml = require("./caml.js");
function mk(lo, hi) {
return [
hi,
(lo >>> 0)
];
}
var min_int = [
-2147483648,
0
];
var max_int = [
2147483647,
4294967295
];
var one = [
0,
1
];
var zero = [
0,
0
];
var neg_one = [
-1,
4294967295
];
function neg_signed(x) {
return (x & -2147483648) !== 0;
}
function non_neg_signed(x) {
return (x & -2147483648) === 0;
}
function succ(param) {
var x_lo = param[1];
var x_hi = param[0];
var lo = x_lo + 1 | 0;
return [
x_hi + (
lo === 0 ? 1 : 0
) | 0,
(lo >>> 0)
];
}
function neg(param) {
var other_lo = (param[1] ^ -1) + 1 | 0;
return [
(param[0] ^ -1) + (
other_lo === 0 ? 1 : 0
) | 0,
(other_lo >>> 0)
];
}
function add_aux(param, y_lo, y_hi) {
var x_lo = param[1];
var lo = x_lo + y_lo | 0;
var overflow = neg_signed(x_lo) && (neg_signed(y_lo) || non_neg_signed(lo)) || neg_signed(y_lo) && non_neg_signed(lo) ? 1 : 0;
return [
param[0] + y_hi + overflow | 0,
(lo >>> 0)
];
}
function add(self, param) {
return add_aux(self, param[1], param[0]);
}
function equal(x, y) {
if (x[1] === y[1]) {
return x[0] === y[0];
} else {
return false;
}
}
function equal_null(x, y) {
if (y !== null) {
return Caml.i64_eq(x, y);
} else {
return false;
}
}
function equal_undefined(x, y) {
if (y !== undefined) {
return Caml.i64_eq(x, y);
} else {
return false;
}
}
function equal_nullable(x, y) {
if (y == null) {
return false;
} else {
return Caml.i64_eq(x, y);
}
}
function sub_aux(x, lo, hi) {
var y_lo = ((lo ^ -1) + 1 >>> 0);
var y_hi = (hi ^ -1) + (
y_lo === 0 ? 1 : 0
) | 0;
return add_aux(x, y_lo, y_hi);
}
function sub(self, param) {
return sub_aux(self, param[1], param[0]);
}
function lsl_(x, numBits) {
if (numBits === 0) {
return x;
}
var lo = x[1];
if (numBits >= 32) {
return [
(lo << (numBits - 32 | 0)),
0
];
} else {
return [
(lo >>> (32 - numBits | 0)) | (x[0] << numBits),
((lo << numBits) >>> 0)
];
}
}
function lsr_(x, numBits) {
if (numBits === 0) {
return x;
}
var hi = x[0];
var offset = numBits - 32 | 0;
if (offset === 0) {
return [
0,
(hi >>> 0)
];
} else if (offset > 0) {
return [
0,
(hi >>> offset)
];
} else {
return [
(hi >>> numBits),
(((hi << (-offset | 0)) | (x[1] >>> numBits)) >>> 0)
];
}
}
function asr_(x, numBits) {
if (numBits === 0) {
return x;
}
var hi = x[0];
if (numBits < 32) {
return [
(hi >> numBits),
(((hi << (32 - numBits | 0)) | (x[1] >>> numBits)) >>> 0)
];
} else {
return [
hi >= 0 ? 0 : -1,
((hi >> (numBits - 32 | 0)) >>> 0)
];
}
}
function is_zero(param) {
if (param[0] !== 0) {
return false;
} else {
return param[1] === 0;
}
}
function mul(_this, _other) {
while(true) {
var other = _other;
var $$this = _this;
var lo;
var this_hi = $$this[0];
var exit = 0;
var exit$1 = 0;
var exit$2 = 0;
if (this_hi !== 0) {
exit$2 = 4;
} else {
if ($$this[1] === 0) {
return zero;
}
exit$2 = 4;
}
if (exit$2 === 4) {
if (other[0] !== 0) {
exit$1 = 3;
} else {
if (other[1] === 0) {
return zero;
}
exit$1 = 3;
}
}
if (exit$1 === 3) {
if (this_hi !== -2147483648 || $$this[1] !== 0) {
exit = 2;
} else {
lo = other[1];
}
}
if (exit === 2) {
var other_hi = other[0];
var lo$1 = $$this[1];
var exit$3 = 0;
if (other_hi !== -2147483648 || other[1] !== 0) {
exit$3 = 3;
} else {
lo = lo$1;
}
if (exit$3 === 3) {
var other_lo = other[1];
if (this_hi < 0) {
if (other_hi >= 0) {
return neg(mul(neg($$this), other));
}
_other = neg(other);
_this = neg($$this);
continue ;
}
if (other_hi < 0) {
return neg(mul($$this, neg(other)));
}
var a48 = (this_hi >>> 16);
var a32 = this_hi & 65535;
var a16 = (lo$1 >>> 16);
var a00 = lo$1 & 65535;
var b48 = (other_hi >>> 16);
var b32 = other_hi & 65535;
var b16 = (other_lo >>> 16);
var b00 = other_lo & 65535;
var c48 = 0;
var c32 = 0;
var c16 = 0;
var c00 = a00 * b00;
c16 = (c00 >>> 16) + a16 * b00;
c32 = (c16 >>> 16);
c16 = (c16 & 65535) + a00 * b16;
c32 = c32 + (c16 >>> 16) + a32 * b00;
c48 = (c32 >>> 16);
c32 = (c32 & 65535) + a16 * b16;
c48 = c48 + (c32 >>> 16);
c32 = (c32 & 65535) + a00 * b32;
c48 = c48 + (c32 >>> 16);
c32 = c32 & 65535;
c48 = c48 + (a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48) & 65535;
return [
c32 | (c48 << 16),
((c00 & 65535 | ((c16 & 65535) << 16)) >>> 0)
];
}
}
if ((lo & 1) === 0) {
return zero;
} else {
return min_int;
}
};
}
function xor(param, param$1) {
return [
param[0] ^ param$1[0],
((param[1] ^ param$1[1]) >>> 0)
];
}
function or_(param, param$1) {
return [
param[0] | param$1[0],
((param[1] | param$1[1]) >>> 0)
];
}
function and_(param, param$1) {
return [
param[0] & param$1[0],
((param[1] & param$1[1]) >>> 0)
];
}
function to_float(param) {
return param[0] * 0x100000000 + param[1];
}
function of_float(x) {
if (isNaN(x) || !isFinite(x)) {
return zero;
}
if (x <= -9.22337203685477581e+18) {
return min_int;
}
if (x + 1 >= 9.22337203685477581e+18) {
return max_int;
}
if (x < 0) {
return neg(of_float(- x));
}
var hi = x / 4294967296 | 0;
var lo = x % 4294967296 | 0;
return [
hi,
(lo >>> 0)
];
}
function isSafeInteger(param) {
var hi = param[0];
var top11Bits = (hi >> 21);
if (top11Bits === 0) {
return true;
} else if (top11Bits === -1) {
return !(param[1] === 0 && hi === -2097152);
} else {
return false;
}
}
function to_string(self) {
if (isSafeInteger(self)) {
return String(to_float(self));
}
if (self[0] < 0) {
if (Caml.i64_eq(self, min_int)) {
return "-9223372036854775808";
} else {
return "-" + to_string(neg(self));
}
}
var approx_div1 = of_float(Math.floor(to_float(self) / 10));
var lo = approx_div1[1];
var hi = approx_div1[0];
var match = sub_aux(sub_aux(self, (lo << 3), (lo >>> 29) | (hi << 3)), (lo << 1), (lo >>> 31) | (hi << 1));
var rem_lo = match[1];
var rem_hi = match[0];
if (rem_lo === 0 && rem_hi === 0) {
return to_string(approx_div1) + "0";
}
if (rem_hi < 0) {
var rem_lo$1 = ((rem_lo ^ -1) + 1 >>> 0);
var delta = Math.ceil(rem_lo$1 / 10);
var remainder = 10 * delta - rem_lo$1;
return to_string(sub_aux(approx_div1, delta | 0, 0)) + String(remainder | 0);
}
var delta$1 = Math.floor(rem_lo / 10);
var remainder$1 = rem_lo - 10 * delta$1;
return to_string(add_aux(approx_div1, delta$1 | 0, 0)) + String(remainder$1 | 0);
}
function div(_self, _other) {
while(true) {
var other = _other;
var self = _self;
var self_hi = self[0];
var exit = 0;
var exit$1 = 0;
if (other[0] !== 0 || other[1] !== 0) {
exit$1 = 2;
} else {
throw {
RE_EXN_ID: "Division_by_zero",
Error: new Error()
};
}
if (exit$1 === 2) {
if (self_hi !== -2147483648) {
if (self_hi !== 0) {
exit = 1;
} else {
if (self[1] === 0) {
return zero;
}
exit = 1;
}
} else if (self[1] !== 0) {
exit = 1;
} else {
if (Caml.i64_eq(other, one) || Caml.i64_eq(other, neg_one)) {
return self;
}
if (Caml.i64_eq(other, min_int)) {
return one;
}
var half_this = asr_(self, 1);
var approx = lsl_(div(half_this, other), 1);
var exit$2 = 0;
if (approx[0] !== 0) {
exit$2 = 3;
} else {
if (approx[1] === 0) {
if (other[0] < 0) {
return one;
} else {
return neg(one);
}
}
exit$2 = 3;
}
if (exit$2 === 3) {
var rem = sub(self, mul(other, approx));
return add(approx, div(rem, other));
}
}
}
if (exit === 1) {
var other_hi = other[0];
var exit$3 = 0;
if (other_hi !== -2147483648) {
exit$3 = 2;
} else {
if (other[1] === 0) {
return zero;
}
exit$3 = 2;
}
if (exit$3 === 2) {
if (self_hi < 0) {
if (other_hi >= 0) {
return neg(div(neg(self), other));
}
_other = neg(other);
_self = neg(self);
continue ;
}
if (other_hi < 0) {
return neg(div(self, neg(other)));
}
var res = zero;
var rem$1 = self;
while(Caml.i64_ge(rem$1, other)) {
var b = Math.floor(to_float(rem$1) / to_float(other));
var approx$1 = 1 > b ? 1 : b;
var log2 = Math.ceil(Math.log(approx$1) / Math.LN2);
var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
var approxRes = of_float(approx$1);
var approxRem = mul(approxRes, other);
while(approxRem[0] < 0 || Caml.i64_gt(approxRem, rem$1)) {
approx$1 = approx$1 - delta;
approxRes = of_float(approx$1);
approxRem = mul(approxRes, other);
};
if (is_zero(approxRes)) {
approxRes = one;
}
res = add(res, approxRes);
rem$1 = sub(rem$1, approxRem);
};
return res;
}
}
};
}
function mod_(self, other) {
return sub(self, mul(div(self, other), other));
}
function div_mod(self, other) {
var quotient = div(self, other);
return [
quotient,
sub(self, mul(quotient, other))
];
}
function compare(self, other) {
var y = other[0];
var x = self[0];
var v = x < y ? -1 : (
x === y ? 0 : 1
);
if (v !== 0) {
return v;
}
var y$1 = other[1];
var x$1 = self[1];
if (x$1 < y$1) {
return -1;
} else if (x$1 === y$1) {
return 0;
} else {
return 1;
}
}
function of_int32(lo) {
return [
lo < 0 ? -1 : 0,
(lo >>> 0)
];
}
function to_int32(x) {
return x[1] | 0;
}
function to_hex(x) {
var x_lo = x[1];
var x_hi = x[0];
var aux = function (v) {
return (v >>> 0).toString(16);
};
if (x_hi === 0 && x_lo === 0) {
return "0";
}
if (x_lo === 0) {
return aux(x_hi) + "00000000";
}
if (x_hi === 0) {
return aux(x_lo);
}
var lo = aux(x_lo);
var pad = 8 - lo.length | 0;
if (pad <= 0) {
return aux(x_hi) + lo;
} else {
return aux(x_hi) + ("0".repeat(pad) + lo);
}
}
function discard_sign(x) {
return [
2147483647 & x[0],
x[1]
];
}
function float_of_bits(x) {
return (function(lo,hi){ return (new Float64Array(new Int32Array([lo,hi]).buffer))[0]})(x[1], x[0]);
}
function bits_of_float(x) {
var match = (function(x){return new Int32Array(new Float64Array([x]).buffer)})(x);
return [
match[1],
(match[0] >>> 0)
];
}
exports.mk = mk;
exports.succ = succ;
exports.min_int = min_int;
exports.max_int = max_int;
exports.one = one;
exports.zero = zero;
exports.neg_one = neg_one;
exports.of_int32 = of_int32;
exports.to_int32 = to_int32;
exports.add = add;
exports.neg = neg;
exports.sub = sub;
exports.lsl_ = lsl_;
exports.lsr_ = lsr_;
exports.asr_ = asr_;
exports.is_zero = is_zero;
exports.mul = mul;
exports.xor = xor;
exports.or_ = or_;
exports.and_ = and_;
exports.equal = equal;
exports.equal_null = equal_null;
exports.equal_undefined = equal_undefined;
exports.equal_nullable = equal_nullable;
exports.to_float = to_float;
exports.of_float = of_float;
exports.div = div;
exports.mod_ = mod_;
exports.compare = compare;
exports.float_of_bits = float_of_bits;
exports.bits_of_float = bits_of_float;
exports.div_mod = div_mod;
exports.to_hex = to_hex;
exports.discard_sign = discard_sign;
exports.to_string = to_string;
/* No side effect */