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
|
// 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.
//
// Bigarray.
//
// - 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.
//Provides: caml_ba_init const
function caml_ba_init() {
return 0;
}
//Provides: caml_ba_init_views
//Requires: caml_ba_views
function caml_ba_init_views() {
if (!caml_ba_views) {
var g = joo_global_object;
caml_ba_views = [
[
g.Float32Array, g.Float64Array, g.Int8Array, g.Uint8Array,
g.Int16Array, g.Uint16Array, g.Int32Array, g.Int32Array,
g.Int32Array, g.Int32Array, g.Float32Array, g.Float64Array, g.Uint8Array],
[
0 /* General */, 0 /* General */, 0 /* General */, 0 /* General */,
0 /* General */, 0 /* General */, 0 /* General */, 1 /* Int64 */,
0 /* General */, 0 /* General */, 2 /* Complex */, 2 /* Complex */, 0 /* General */]
];
}
}
//Provides: caml_ba_get_size
//Requires: caml_invalid_argument
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_invalid_argument("Bigarray.create: negative dimension");
size = size * dims[i];
}
return size;
}
//Provides: caml_ba_views
var caml_ba_views;
//Provides: caml_ba_create_from
//Requires: caml_ba_get_size
//Requires: caml_invalid_argument
//Requires: caml_array_bound_error
function caml_ba_create_from(data, data2, data_type, kind, layout, dims) {
var n_dims = dims.length;
var size = caml_ba_get_size(dims);
//
// Functions to compute the offsets for C or Fortran layout arrays
// from the given array of indices.
//
function offset_c(index) {
var ofs = 0;
if (n_dims != index.length)
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_array_bound_error();
ofs = (ofs * dims[i]) + index[i];
}
return ofs;
}
function offset_fortran(index) {
var ofs = 0;
if (n_dims != index.length)
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_array_bound_error();
ofs = (ofs * dims[i]) + (index[i] - 1);
}
return ofs;
}
var offset = layout == 0 ? offset_c : offset_fortran;
var dim0 = dims[0];
//
// Element get functions.
//
function get_std(index) {
var ofs = offset(index);
var v = data[ofs];
return v;
}
function get_int64(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];
}
function get_complex(index) {
var off = offset(index);
var r = data[off];
var i = data2[off];
return [254, r, i];
}
var get = data_type == 1 /* Int64 */ ? get_int64 : (data_type == 2 /* Complex */ ? get_complex : get_std);
function get1_c(i) {
if (i < 0 || i >= dim0)
caml_array_bound_error();
return data[i];
}
function get1_fortran(i) {
if (i < 1 || i > dim0)
caml_array_bound_error();
return data[i - 1];
}
function get1_any(i) {
return get([i]);
}
var get1 = data_type == 0 /* General */ ? (layout == 0 ? get1_c : get1_fortran) : get1_any;
//
// Element set functions
//
function set_std_raw(off, v) {
data[off] = v;
}
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[1];
data2[off] = v[2];
}
function set_std(index, v) {
var ofs = offset(index);
return set_std_raw(ofs, v);
}
function set_int64(index, v) {
return set_int64_raw(offset(index), v);
}
function set_complex(index, v) {
return set_complex_raw(offset(index), v);
}
var set = data_type == 1 /* Int64 */ ? set_int64 : (data_type == 2 /* Complex */ ? set_complex : set_std);
function set1_c(i, v) {
if (i < 0 || i >= dim0)
caml_array_bound_error();
data[i] = v;
}
function set1_fortran(i, v) {
if (i < 1 || i > dim0)
caml_array_bound_error();
data[i - 1] = v;
}
function set1_any(i, v) {
set([i], v);
}
var set1 = data_type == 0 /* General */ ? (layout == 0 ? set1_c : set1_fortran) : set1_any;
//
// other
//
function nth_dim(i) {
if (i < 0 || i >= n_dims)
caml_invalid_argument("Bigarray.dim");
return dims[i];
}
function fill(v) {
if (data_type == 0 /* General */)
for (var i = 0; i < data.length; i++)
set_std_raw(i, v);
if (data_type == 1 /* Int64 */)
for (var i = 0; i < data.length; i++)
set_int64_raw(i, v);
if (data_type == 2 /* Complex */)
for (var i = 0; i < data.length; i++)
set_complex_raw(i, v);
}
function blit(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);
if (data_type != 0 /* General */)
data2.set(from.data2);
}
function sub(ofs, len) {
var changed_dim;
var mul = 1;
if (layout == 0) {
for (var i = 1; i < n_dims; i++)
mul = mul * dims[i];
changed_dim = 0;
} else {
for (var i = 0; i < (n_dims - 1); i++)
mul = mul * dims[i];
changed_dim = n_dims - 1;
ofs = ofs - 1;
}
if (ofs < 0 || len < 0 || (ofs + len) > dims[changed_dim])
caml_invalid_argument("Bigarray.sub: bad sub-array");
var new_data = data.subarray(ofs * mul, (ofs + len) * mul);
var new_data2 = data_type == 0 /* General */ ? null : data2.subarray(ofs * mul, (ofs + len) * mul);
var new_dims = [];
for (var i = 0; i < n_dims; i++)
new_dims[i] = dims[i];
new_dims[changed_dim] = len;
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) {
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 {
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 compare(b, total) {
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:
case 1:
case 10:
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:
case 3:
case 4:
case 5:
case 6:
case 8:
case 9:
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;
}
return {
data: data,
data2: data2,
data_type: data_type,
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: compare
};
}
//Provides: caml_ba_create
//Requires: caml_ba_create_from
//Requires: caml_js_from_array
//Requires: caml_ba_views
//Requires: caml_ba_init_views
//Requires: caml_invalid_argument
//Requires: caml_ba_get_size
function caml_ba_create(kind, layout, dims_ml) {
// Initialize TypedArray views
caml_ba_init_views();
// set up dimensions and calculate size
var dims = caml_js_from_array(dims_ml);
//var n_dims = dims.length;
var size = caml_ba_get_size(dims);
// Allocate TypedArray
var view = caml_ba_views[0][kind];
if (!view)
caml_invalid_argument("Bigarray.create: unsupported kind");
var data = new view(size);
// 2nd TypedArray for int64, complex32 and complex64
var data_type = caml_ba_views[1][kind];
var data2 = null;
if (data_type != 0 /* General */) {
data2 = new view(size);
}
return caml_ba_create_from(data, data2, data_type, kind, layout, dims);
}
//Provides: caml_ba_kind
function caml_ba_kind(ba) {
return ba.kind;
}
//Provides: caml_ba_layout
function caml_ba_layout(ba) {
return ba.layout;
}
//Provides: caml_ba_num_dims
function caml_ba_num_dims(ba, _dim) {
return ba.num_dims;
}
//Provides: caml_ba_dim
function caml_ba_dim(ba, dim) {
return ba.nth_dim(dim);
}
//Provides: caml_ba_dim_1
function caml_ba_dim_1(ba) {
return ba.nth_dim(0);
}
//Provides: caml_ba_dim_2
function caml_ba_dim_2(ba) {
return ba.nth_dim(1);
}
//Provides: caml_ba_dim_3
function caml_ba_dim_3(ba) {
return ba.nth_dim(2);
}
//Provides: caml_ba_get_generic
//Requires: caml_js_from_array
function caml_ba_get_generic(ba, index) {
return ba.get(caml_js_from_array(index));
}
//Provides: caml_ba_get_1
function caml_ba_get_1(ba, i0) {
return ba.get1(i0);
}
//Provides: caml_ba_get_2
function caml_ba_get_2(ba, i0, i1) {
return ba.get([i0, i1]);
}
//Provides: caml_ba_get_3
function caml_ba_get_3(ba, i0, i1, i2) {
return ba.get([i0, i1, i2]);
}
//Provides: caml_ba_set_generic
//Requires: caml_js_from_array
function caml_ba_set_generic(ba, index, v) {
return ba.set(caml_js_from_array(index), v);
}
//Provides: caml_ba_set_1
function caml_ba_set_1(ba, i0, v) {
return ba.set1(i0, v);
}
//Provides: caml_ba_set_2
function caml_ba_set_2(ba, i0, i1, v) {
return ba.set([i0, i1], v);
}
//Provides: caml_ba_set_3
function caml_ba_set_3(ba, i0, i1, i2, v) {
return ba.set([i0, i1, i2], v);
}
//Provides: caml_ba_blit
function caml_ba_blit(src, dst) {
dst.blit(src);
return 0;
}
//Provides: caml_ba_fill
function caml_ba_fill(ba, init) {
ba.fill(init);
return 0;
}
//Provides: caml_ba_sub
function caml_ba_sub(ba, ofs, len) {
return ba.sub(ofs, len);
}
//Provides: caml_ba_slice
//Requires: caml_js_from_array
function caml_ba_slice(ba, vind) {
return ba.slice(caml_js_from_array(vind));
}
//Provides: caml_ba_reshape
//Requires: caml_js_from_array
function caml_ba_reshape(ba, vind) {
return ba.reshape(caml_js_from_array(vind));
}
|