forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_bigarray.js
728 lines (673 loc) · 20.2 KB
/
caml_bigarray.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
// Generated CODE, PLEASE EDIT WITH CARE
'use strict';
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
function caml_array_bound_error() {
throw [
Caml_builtin_exceptions.invalid_argument,
"index out of bounds"
];
}
function caml_invalid_argument(s) {
throw [
Caml_builtin_exceptions.invalid_argument,
s
];
}
function caml_ba_get_size(dims) {
var size = 1;
for(var i = 0 ,i_finish = dims.length - 1 | 0; i<= i_finish; ++i){
var v = dims[i];
if (v < 0) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Bigarray.create: negative dimension"
];
}
else {
size = size * v;
}
}
return size;
}
function index_offset_c(n_dims, dims, index) {
var offs = 0;
if (n_dims !== index.length) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Bigarray.get/set: bad number of dimensions"
];
}
for(var i = 0 ,i_finish = n_dims - 1 | 0; i<= i_finish; ++i){
var index_j = index[i];
var dim_j = dims[i];
if (index_j < 0 || index_j >= dim_j) {
throw [
Caml_builtin_exceptions.invalid_argument,
"index out of bounds"
];
}
else {
offs = offs * dim_j + index_j;
}
}
return offs;
}
function index_offset_fortran(n_dims, dims, index) {
var offs = 0;
if (n_dims !== index.length) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Bigarray.get/set: bad number of dimensions"
];
}
for(var i = n_dims - 1 | 0; i>= 0; --i){
var index_j = index[i];
var dim_j = dims[i];
if (index_j < 1 || index_j > dim_j) {
throw [
Caml_builtin_exceptions.invalid_argument,
"index out of bounds"
];
}
else {
offs = offs * dim_j + (index_j - 1);
}
}
return offs;
}
function $$bigarray_compare(b, total, layout, n_dims, kind, nth_dim, data, data2) {
if (layout != b.layout)
return b.layout - layout;
if (n_dims != b.num_dims)
return b.num_dims - n_dims;
for (var i = 0; i < n_dims; i++)
if (nth_dim(i) != b.nth_dim(i))
return (nth_dim(i) < b.nth_dim(i)) ? -1 : 1;
switch (kind) {
case 0: //float32
case 1: //float64
case 10: //complex32
case 11:
var x, y;
for (var i = 0; i < data.length; i++) {
x = data[i];
y = b.data[i];
//first array
if (x < y)
return -1;
if (x > y)
return 1;
if (x != y) {
if (x != y) {
if (!total)
return NaN;
if (x == x)
return 1;
if (y == y)
return -1;
}
}
if (data2) {
//second array
x = data2[i];
y = b.data2[i];
if (x < y)
return -1;
if (x > y)
return 1;
if (x != y) {
if (x != y) {
if (!total)
return NaN;
if (x == x)
return 1;
if (y == y)
return -1;
}
}
}
}
;
break;
case 2: //int8
case 3: //uint8
case 4: //int16
case 5: //uint16
case 6: //int32
case 8: //int
case 9: //nativeint
case 12:
for (var i = 0; i < data.length; i++) {
if (data[i] < b.data[i])
return -1;
if (data[i] > b.data[i])
return 1;
}
;
break;
case 7:
for (var i = 0; i < data.length; i++) {
if (data2[i] < b.data2[i])
return -1;
if (data2[i] > b.data2[i])
return 1;
if (data[i] < b.data[i])
return -1;
if (data[i] > b.data[i])
return 1;
}
;
break;
}
return 0;
}
function $$caml_ba_create_from(data, data2, data_type, kind, layout, dims) {
var n_dims = dims.length;
var size = caml_ba_get_size(dims);
var offset;
switch (layout) {
case 0 /* C_layout */:
offset = function (index) {
return index_offset_c(n_dims, dims, index);
};
break;
case 1 /* Fortan_layout */:
offset = function (index) {
return index_offset_fortran(n_dims, dims, index);
};
break;
}
var dim0 = dims[0];
function get1_c(i) {
if (i < 0 || i >= dim0) {
caml_array_bound_error(0);
}
return data[i];
}
function get1_fortran(i) {
if (i < 1 || i > dim0) {
caml_array_bound_error(0);
}
return data[i - 1];
}
function set_int64_raw(off, v) {
data[off] = v[1] | ((v[2] & 0xff) << 24);
data2[off] = ((v[2] >>> 8) & 0xffff) | (v[3] << 16);
}
function set_complex_raw(off, v) {
data[off] = v[0];
data2[off] = v[1];
}
function set1_c(i, v) {
if (i < 0 || i >= dim0)
caml_array_bound_error(0);
data[i] = v;
}
function set1_fortran(i, v) {
if (i < 1 || i > dim0)
caml_array_bound_error(0);
data[i - 1] = v;
}
var get;
var get1;
var set;
var set1;
var fill;
var blit;
switch (data_type) {
case 1 /* Int64 */:
get = function (index) {
var off = offset(index);
var l = data[off];
var h = data2[off];
return [255,
l & 0xffffff,
((l >>> 24) & 0xff) | ((h & 0xffff) << 8),
(h >>> 16) & 0xffff];
};
set = function (index, v) {
return set_int64_raw(offset(index), v);
};
get1 = function (i) {
return get([i]);
};
set1 = function (i, v) {
set([i], v);
};
fill = function (v) {
for (var i = 0; i < data.length; i++) {
set_int64_raw(i, v);
}
};
blit = function (from) {
if (n_dims != from.num_dims) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
}
data.set(from.data);
data2.set(from.data2);
};
break;
case 2 /* Complex */:
get = function (index) {
var off = offset(index);
return [data[off], data2[off]];
};
set = function (index, v) { return set_complex_raw(offset(index), v); };
get1 = function (i) {
return get([i]);
};
set1 = function (i, v) {
set([i], v);
};
fill = function (v) {
for (var i = 0; i < data.length; i++) {
set_complex_raw(i, v);
}
};
blit = function (from) {
if (n_dims != from.num_dims) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
}
data.set(from.data);
data2.set(from.data2);
};
break;
case 0 /* General */:
get = function (index) {
return data[offset(index)];
};
set = function (index, v) {
data[offset(index)] = v;
};
if (layout === 0 /* C_layout */) {
get1 = get1_c;
set1 = set1_c;
}
else {
get1 = get1_fortran;
set1 = set1_fortran;
}
fill = function (v) {
for (var i = 0; i < data.length; i++) {
data[i] = v;
}
};
blit = function (from) {
if (n_dims != from.num_dims) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
}
data.set(from.data);
};
break;
}
function sub(ofs, len) {
if (ofs < 0 || len < 0) {
caml_invalid_argument("Bigarray.sub: bad sub-array");
}
var mul = 1;
var new_dims = [];
if (layout == 0 /* C_layout */) {
for (var i = 1; i < n_dims; i++) {
mul *= dims[i];
new_dims[i] = dims[i];
}
new_dims[0] = len;
if (ofs + len > dims[0]) {
caml_invalid_argument("Bigarray.sub: bad sub-array");
}
}
else {
for (var i = 0; i < (n_dims - 1); i++) {
mul *= dims[i];
new_dims[i] = dims[i];
}
new_dims[n_dims - 1] = len;
ofs = ofs - 1;
if (ofs + len > dims[n_dims - 1]) {
caml_invalid_argument("Bigarray.sub: bad sub-array");
}
}
var new_data = data.subarray(ofs * mul, (ofs + len) * mul);
var new_data2 = null;
if (data_type !== 0 /* General */) {
new_data2 = data2.subarray(ofs * mul, (ofs + len) * mul);
}
return $$caml_ba_create_from(new_data, new_data2, data_type, kind, layout, new_dims);
}
function slice(vind) {
var num_inds = vind.length;
var index = [];
var sub_dims = [];
var ofs;
if (num_inds >= n_dims)
caml_invalid_argument("Bigarray.slice: too many indices");
// Compute offset and check bounds
if (layout === 0 /* C_layout */) {
// We slice from the left
for (var i = 0; i < num_inds; i++)
index[i] = vind[i];
for (; i < n_dims; i++)
index[i] = 0;
ofs = offset(index);
sub_dims = dims.slice(num_inds);
}
else {
// We slice from the right
for (var i = 0; i < num_inds; i++)
index[n_dims - num_inds + i] = vind[i];
for (var i = 0; i < n_dims - num_inds; i++)
index[i] = 1;
ofs = offset(index);
sub_dims = dims.slice(0, num_inds);
}
var size = caml_ba_get_size(sub_dims);
var new_data = data.subarray(ofs, ofs + size);
var new_data2 = data_type == 0 /* General */ ? null : data2.subarray(ofs, ofs + size);
return $$caml_ba_create_from(new_data, new_data2, data_type, kind, layout, sub_dims);
}
function reshape(vdim) {
var new_dim = [];
var num_dims = vdim.length;
if (num_dims < 1) {
caml_invalid_argument("Bigarray.reshape: bad number of dimensions");
}
var num_elts = 1;
for (var i = 0; i < num_dims; i++) {
new_dim[i] = vdim[i];
if (new_dim[i] < 0)
caml_invalid_argument("Bigarray.reshape: negative dimension");
num_elts = num_elts * new_dim[i];
}
// Check that sizes agree
if (num_elts != size)
caml_invalid_argument("Bigarray.reshape: size mismatch");
return $$caml_ba_create_from(data, data2, data_type, kind, layout, new_dim);
}
function nth_dim(i) {
if (i < 0 || i >= n_dims)
caml_invalid_argument("Bigarray.dim");
return dims[i];
}
return {
data: data,
data2: data2,
num_dims: n_dims,
nth_dim: nth_dim,
kind: kind,
layout: layout,
size: size,
sub: sub,
slice: slice,
blit: blit,
fill: fill,
reshape: reshape,
get: get,
get1: get1,
set: set,
set1: set1,
compare: function (b, total) {
return $$bigarray_compare(b, total, layout, n_dims, kind, nth_dim, data, data2);
}
};
}
/**
* ('a,'b) kind -> 'c layout -> int array -> ('a,'b,'c) t
* type ('a, 'b) kind =
* Float32 : (float, float32_elt) kind
* | Float64 : (float, float64_elt) kind
* | Int8_signed : (int, int8_signed_elt) kind
* | Int8_unsigned : (int, int8_unsigned_elt) kind
* | Int16_signed : (int, int16_signed_elt) kind
* | Int16_unsigned : (int, int16_unsigned_elt) kind
* | Int32 : (int32, int32_elt) kind
* | Int64 : (int64, int64_elt) kind
* | Int : (int, int_elt) kind
* | Nativeint : (nativeint, nativeint_elt) kind
* | Complex32 : (Complex.t, complex32_elt) kind
* | Complex64 : (Complex.t, complex64_elt) kind
* | Char : (char, int8_unsigned_elt) kind
* @param kind
* @param layout
* @param dims_ml
* @returns {Bigarray}
*/
function $$caml_ba_create(kind, layout, dims) {
var size = caml_ba_get_size(dims);
var data;
var data2;
var data_type = 0 /* General */;
switch (kind) {
case 0 /* Float32 */:
data = new Float32Array(size);
break;
case 1 /* Float64 */:
data = new Float64Array(size);
break;
case 2 /* Int8_signed */:
data = new Int8Array(size);
break;
case 3 /* Int8_unsigned */:
data = new Uint8Array(size);
break;
case 4 /* Int16_signed */:
data = new Int16Array(size);
break;
case 5 /* Int16_unsigned */:
data = new Uint16Array(size);
break;
case 6 /* Int32 */:
data = new Int32Array(size);
break;
case 7 /* Int64 */:
data = new Int32Array(size);
data2 = new Int32Array(size);
data_type = 1 /* Int64 */;
break;
case 8 /* Int */:
data = new Int32Array(size);
break;
case 9 /* Nativeint */:
data = new Int32Array(size);
break;
case 10 /* Complex32 */:
data = new Float32Array(size);
data2 = new Float32Array(size);
data_type = 2 /* Complex */;
break;
case 11 /* Complex64 */:
data = new Float64Array(size);
data2 = new Float64Array(size);
data_type = 2 /* Complex */;
break;
case 12 /* Char */:
data = new Uint8Array(size);
break;
}
return $$caml_ba_create_from(data, data2, data_type, kind, layout, dims);
}
function $$caml_ba_kind(ba) {
return ba.kind;
}
function $$caml_ba_layout(ba) {
return ba.layout;
}
/**
* ('a,'b,'c) t -> int
* @param ba
* @returns {number}
*/
function $$caml_ba_num_dims(ba) {
return ba.num_dims;
}
function $$caml_ba_dim(ba, dim) {
return ba.nth_dim(dim);
}
function $$caml_ba_dim_1(ba) {
return ba.nth_dim(0);
}
function $$caml_ba_dim_2(ba) {
return ba.nth_dim(1);
}
function $$caml_ba_dim_3(ba) {
return ba.nth_dim(2);
}
function $$caml_ba_get_generic(ba, index) {
return ba.get(index);
}
function $$caml_ba_get_1(ba, i0) {
return ba.get1(i0);
}
function $$caml_ba_get_2(ba, i0, i1) {
return ba.get([i0, i1]);
}
function $$caml_ba_get_3(ba, i0, i1, i2) {
return ba.get([i0, i1, i2]);
}
function $$caml_ba_set_generic(ba, index, v) {
return ba.set(index, v);
}
function $$caml_ba_set_1(ba, i0, v) {
return ba.set1(i0, v);
}
function $$caml_ba_set_2(ba, i0, i1, v) {
return ba.set([i0, i1], v);
}
function $$caml_ba_set_3(ba, i0, i1, i2, v) {
return ba.set([i0, i1, i2], v);
}
function $$caml_ba_blit(src, dst) {
dst.blit(src);
return 0;
}
function $$caml_ba_fill(ba, init) {
ba.fill(init);
return 0;
}
function $$caml_ba_sub(ba, ofs, len) {
return ba.sub(ofs, len);
}
function $$caml_ba_slice(ba, vind) {
return ba.slice(vind);
}
function $$caml_ba_reshape(ba, vind) {
return ba.reshape(vind);
}
;
function caml_ba_map_file_bytecode() {
throw [
Caml_builtin_exceptions.failure,
"caml_ba_map_file_bytecode not implemented"
];
}
function caml_ba_create(prim, prim$1, prim$2) {
return $$caml_ba_create(prim, prim$1, prim$2);
}
function caml_ba_get_generic(prim, prim$1) {
return $$caml_ba_get_generic(prim, prim$1);
}
function caml_ba_set_generic(prim, prim$1, prim$2) {
$$caml_ba_set_generic(prim, prim$1, prim$2);
return /* () */0;
}
function caml_ba_num_dims(prim) {
return $$caml_ba_num_dims(prim);
}
function caml_ba_dim(prim, prim$1) {
return $$caml_ba_dim(prim, prim$1);
}
function caml_ba_kind(prim) {
return $$caml_ba_kind(prim);
}
function caml_ba_layout(prim) {
return $$caml_ba_layout(prim);
}
function caml_ba_sub(prim, prim$1, prim$2) {
return $$caml_ba_sub(prim, prim$1, prim$2);
}
function caml_ba_slice(prim, prim$1) {
return $$caml_ba_slice(prim, prim$1);
}
function caml_ba_blit(prim, prim$1) {
$$caml_ba_blit(prim, prim$1);
return /* () */0;
}
function caml_ba_fill(prim, prim$1) {
$$caml_ba_fill(prim, prim$1);
return /* () */0;
}
function caml_ba_reshape(prim, prim$1) {
return $$caml_ba_reshape(prim, prim$1);
}
function caml_ba_get_1(prim, prim$1) {
return $$caml_ba_get_1(prim, prim$1);
}
function caml_ba_set_1(prim, prim$1, prim$2) {
$$caml_ba_set_1(prim, prim$1, prim$2);
return /* () */0;
}
function caml_ba_set_2(prim, prim$1, prim$2, prim$3) {
$$caml_ba_set_2(prim, prim$1, prim$2, prim$3);
return /* () */0;
}
function caml_ba_get_2(prim, prim$1, prim$2) {
return $$caml_ba_get_2(prim, prim$1, prim$2);
}
function caml_ba_dim_1(prim) {
return $$caml_ba_dim_1(prim);
}
function caml_ba_dim_2(prim) {
return $$caml_ba_dim_2(prim);
}
function caml_ba_dim_3(prim) {
return $$caml_ba_dim_3(prim);
}
function caml_ba_get_3(prim, prim$1, prim$2, prim$3) {
return $$caml_ba_get_3(prim, prim$1, prim$2, prim$3);
}
function caml_ba_set_3(prim, prim$1, prim$2, prim$3, prim$4) {
$$caml_ba_set_3(prim, prim$1, prim$2, prim$3, prim$4);
return /* () */0;
}
exports.caml_array_bound_error = caml_array_bound_error;
exports.caml_invalid_argument = caml_invalid_argument;
exports.caml_ba_get_size = caml_ba_get_size;
exports.index_offset_c = index_offset_c;
exports.index_offset_fortran = index_offset_fortran;
exports.caml_ba_create = caml_ba_create;
exports.caml_ba_get_generic = caml_ba_get_generic;
exports.caml_ba_set_generic = caml_ba_set_generic;
exports.caml_ba_num_dims = caml_ba_num_dims;
exports.caml_ba_dim = caml_ba_dim;
exports.caml_ba_kind = caml_ba_kind;
exports.caml_ba_layout = caml_ba_layout;
exports.caml_ba_sub = caml_ba_sub;
exports.caml_ba_slice = caml_ba_slice;
exports.caml_ba_blit = caml_ba_blit;
exports.caml_ba_fill = caml_ba_fill;
exports.caml_ba_reshape = caml_ba_reshape;
exports.caml_ba_get_1 = caml_ba_get_1;
exports.caml_ba_set_1 = caml_ba_set_1;
exports.caml_ba_set_2 = caml_ba_set_2;
exports.caml_ba_get_2 = caml_ba_get_2;
exports.caml_ba_dim_1 = caml_ba_dim_1;
exports.caml_ba_dim_2 = caml_ba_dim_2;
exports.caml_ba_dim_3 = caml_ba_dim_3;
exports.caml_ba_get_3 = caml_ba_get_3;
exports.caml_ba_set_3 = caml_ba_set_3;
exports.caml_ba_map_file_bytecode = caml_ba_map_file_bytecode;
/* Not a pure module */