forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaml_bigarray.js
More file actions
587 lines (587 loc) · 18.8 KB
/
Copy pathcaml_bigarray.js
File metadata and controls
587 lines (587 loc) · 18.8 KB
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
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2014 Jérôme Vouillon, Hugo Heuzard, Andy Ray
// Laboratoire PPS - CNRS Université Paris Diderot
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, with linking exception;
// either version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Copyright (c) 2015-2016 Bloomberg LP. All rights reserved.
// Hongbo Zhang (hzhang295@bloomberg.net)
'use strict';
// Bigarray.
// Note this is temporary and does not get tested,
// we will replace this part with ocaml itself instead
//
// - all bigarray types including Int64 and Complex.
// - fortran + c layouts
// - sub/slice/reshape
// - retain fast path for 1d array access
//
// Note; int64+complex support if provided by allocating a second TypedArray
// Note; accessor functions are selected when the bigarray is created. It is assumed
// that this results in just a function pointer and will thus be fast.
var caml_exceptions_1 = require('./caml_exceptions');
function caml_ba_get_size(dims) {
var n_dims = dims.length;
var size = 1;
for (var i = 0; i < n_dims; i++) {
if (dims[i] < 0)
caml_exceptions_1.caml_invalid_argument("Bigarray.create: negative dimension");
size = size * dims[i];
}
return size;
}
function index_offset_c(n_dims, dims, index) {
var ofs = 0;
if (n_dims != index.length) {
caml_exceptions_1.caml_invalid_argument("Bigarray.get/set: bad number of dimensions");
}
;
for (var i = 0; i < n_dims; i++) {
if (index[i] < 0 || index[i] >= dims[i]) {
caml_exceptions_1.caml_array_bound_error(0);
}
ofs = ofs * dims[i] + index[i];
}
return ofs;
}
function index_offset_fortran(n_dims, dims, index) {
var ofs = 0;
if (n_dims != index.length) {
caml_exceptions_1.caml_invalid_argument("Bigarray.get/set: wrong number of indices");
}
for (var i = n_dims - 1; i >= 0; i--) {
if (index[i] < 1 || index[i] > dims[i]) {
caml_exceptions_1.caml_array_bound_error(0);
}
ofs = ofs * dims[i] + (index[i] - 1);
}
return ofs;
}
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_exceptions_1.caml_array_bound_error(0);
}
return data[i];
}
function get1_fortran(i) {
if (i < 1 || i > dim0) {
caml_exceptions_1.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_exceptions_1.caml_array_bound_error(0);
data[i] = v;
}
function set1_fortran(i, v) {
if (i < 1 || i > dim0)
caml_exceptions_1.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_exceptions_1.caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_exceptions_1.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_exceptions_1.caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_exceptions_1.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_exceptions_1.caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
for (var i = 0; i < n_dims; i++) {
if (dims[i] != from.nth_dim(i)) {
caml_exceptions_1.caml_invalid_argument("Bigarray.blit: dimension mismatch");
}
}
data.set(from.data);
};
break;
}
function sub(ofs, len) {
if (ofs < 0 || len < 0) {
caml_exceptions_1.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_exceptions_1.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_exceptions_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_exceptions_1.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_exceptions_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_exceptions_1.caml_invalid_argument("Bigarray.reshape: negative dimension");
num_elts = num_elts * new_dim[i];
}
// Check that sizes agree
if (num_elts != size)
caml_exceptions_1.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_exceptions_1.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);
}
exports.caml_ba_create = caml_ba_create;
function caml_ba_kind(ba) {
return ba.kind;
}
exports.caml_ba_kind = caml_ba_kind;
function caml_ba_layout(ba) {
return ba.layout;
}
exports.caml_ba_layout = caml_ba_layout;
/**
* ('a,'b,'c) t -> int
* @param ba
* @returns {number}
*/
function caml_ba_num_dims(ba) {
return ba.num_dims;
}
exports.caml_ba_num_dims = caml_ba_num_dims;
function caml_ba_dim(ba, dim) {
return ba.nth_dim(dim);
}
exports.caml_ba_dim = caml_ba_dim;
function caml_ba_dim_1(ba) {
return ba.nth_dim(0);
}
exports.caml_ba_dim_1 = caml_ba_dim_1;
function caml_ba_dim_2(ba) {
return ba.nth_dim(1);
}
exports.caml_ba_dim_2 = caml_ba_dim_2;
function caml_ba_dim_3(ba) {
return ba.nth_dim(2);
}
exports.caml_ba_dim_3 = caml_ba_dim_3;
function caml_ba_get_generic(ba, index) {
return ba.get(index);
}
exports.caml_ba_get_generic = caml_ba_get_generic;
function caml_ba_get_1(ba, i0) {
return ba.get1(i0);
}
exports.caml_ba_get_1 = caml_ba_get_1;
function caml_ba_get_2(ba, i0, i1) {
return ba.get([i0, i1]);
}
exports.caml_ba_get_2 = caml_ba_get_2;
function caml_ba_get_3(ba, i0, i1, i2) {
return ba.get([i0, i1, i2]);
}
exports.caml_ba_get_3 = caml_ba_get_3;
function caml_ba_set_generic(ba, index, v) {
return ba.set(index, v);
}
exports.caml_ba_set_generic = caml_ba_set_generic;
function caml_ba_set_1(ba, i0, v) {
return ba.set1(i0, v);
}
exports.caml_ba_set_1 = caml_ba_set_1;
function caml_ba_set_2(ba, i0, i1, v) {
return ba.set([i0, i1], v);
}
exports.caml_ba_set_2 = caml_ba_set_2;
function caml_ba_set_3(ba, i0, i1, i2, v) {
return ba.set([i0, i1, i2], v);
}
exports.caml_ba_set_3 = caml_ba_set_3;
function caml_ba_blit(src, dst) {
dst.blit(src);
return 0;
}
exports.caml_ba_blit = caml_ba_blit;
function caml_ba_fill(ba, init) {
ba.fill(init);
return 0;
}
exports.caml_ba_fill = caml_ba_fill;
function caml_ba_sub(ba, ofs, len) {
return ba.sub(ofs, len);
}
exports.caml_ba_sub = caml_ba_sub;
function caml_ba_slice(ba, vind) {
return ba.slice(vind);
}
exports.caml_ba_slice = caml_ba_slice;
function caml_ba_reshape(ba, vind) {
return ba.reshape(vind);
}
exports.caml_ba_reshape = caml_ba_reshape;
function caml_ba_map_file_bytecode() {
throw Error("caml_ba_map_file_bytecode not implemented");
}