This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
ecmascript_simd.js
1281 lines (1119 loc) · 34.5 KB
/
ecmascript_simd.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
/*
vim: set ts=8 sts=2 et sw=2 tw=79:
Copyright (C) 2013
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
// A conforming SIMD.js implementation may contain the following deviations to
// normal JS numeric behavior:
// - Subnormal numbers may or may not be flushed to zero on input or output of
// any SIMD operation.
// Many of the operations in SIMD.js have semantics which correspond to scalar
// operations in JS, however there are a few differences:
// - Vector shifts don't mask the shift count.
// - Conversions from float to int32 throw on error.
// - Load and store operations throw when out of bounds.
(function(global) {
if (typeof global.SIMD === "undefined") {
// SIMD module.
global.SIMD = {};
}
if (typeof module !== "undefined") {
// For CommonJS modules
module.exports = global.SIMD;
}
var SIMD = global.SIMD;
// Buffers for bit casting and coercing lane values to those representable in
// the underlying lane type.
var _f32x4 = new Float32Array(4);
var _f64x2 = new Float64Array(_f32x4.buffer);
var _i32x4 = new Int32Array(_f32x4.buffer);
var _i16x8 = new Int16Array(_f32x4.buffer);
var _i8x16 = new Int8Array(_f32x4.buffer);
var _ui32x4 = new Uint32Array(_f32x4.buffer);
var _ui16x8 = new Uint16Array(_f32x4.buffer);
var _ui8x16 = new Uint8Array(_f32x4.buffer);
function convertValue(buffer, value) {
buffer[0] = value;
return buffer[0];
}
function convertArray(buffer, array) {
for (var i = 0; i < array.length; i++)
array[i] = convertValue(buffer, array[i]);
return array;
}
// Utility functions.
function isInt32(o) {
return (o | 0) === o;
}
function isTypedArray(o) {
return (o instanceof Int8Array) ||
(o instanceof Uint8Array) ||
(o instanceof Uint8ClampedArray) ||
(o instanceof Int16Array) ||
(o instanceof Uint16Array) ||
(o instanceof Int32Array) ||
(o instanceof Uint32Array) ||
(o instanceof Float32Array) ||
(o instanceof Float64Array);
}
function minNum(x, y) {
return x != x ? y :
y != y ? x :
Math.min(x, y);
}
function maxNum(x, y) {
return x != x ? y :
y != y ? x :
Math.max(x, y);
}
function clamp(a, min, max) {
if (a < min)
return min;
if (a > max)
return max;
return a;
}
// SIMD implementation functions
function simdCoerceIndex(index) {
index = +index;
if (index != Math.floor(index))
throw new RangeError("SIMD index must be an integer");
return index;
}
function simdCheckLaneIndex(index, lanes) {
if (!isInt32(index))
throw new TypeError('Lane index must be an int32');
if (index < 0 || index >= lanes)
throw new RangeError('Lane index must be in bounds');
}
// Global lanes array for constructing SIMD values.
var lanes = [];
function simdCreate(type) {
return type.fn.apply(type.fn, lanes);
}
function simdToString(type, a) {
a = type.fn.check(a);
var str = "SIMD." + type.name + "(";
str += type.fn.extractLane(a, 0);
for (var i = 1; i < type.lanes; i++) {
str += ", " + type.fn.extractLane(a, i);
}
return str + ")";
}
function simdToLocaleString(type, a) {
a = type.fn.check(a);
var str = "SIMD." + type.name + "(";
str += type.fn.extractLane(a, 0).toLocaleString();
for (var i = 1; i < type.lanes; i++) {
str += ", " + type.fn.extractLane(a, i).toLocaleString();
}
return str + ")";
}
function simdSplat(type, s) {
for (var i = 0; i < type.lanes; i++)
lanes[i] = s;
return simdCreate(type);
}
function simdReplaceLane(type, a, i, s) {
a = type.fn.check(a);
simdCheckLaneIndex(i, type.lanes);
for (var j = 0; j < type.lanes; j++)
lanes[j] = type.fn.extractLane(a, j);
lanes[i] = s;
return simdCreate(type);
}
function simdFrom(toType, fromType, a) {
a = fromType.fn.check(a);
for (var i = 0; i < fromType.lanes; i++) {
var v = Math.trunc(fromType.fn.extractLane(a, i));
if (toType.minVal !== undefined &&
!(toType.minVal <= v && v <= toType.maxVal)) {
throw new RangeError("Can't convert value");
}
lanes[i] = v;
}
return simdCreate(toType);
}
function simdFromBits(toType, fromType, a) {
a = fromType.fn.check(a);
var newValue = new toType.fn();
newValue.s_ = new toType.view(a.s_.buffer);
return newValue;
}
function simdSelect(type, selector, a, b) {
selector = type.boolType.fn.check(selector);
a = type.fn.check(a);
b = type.fn.check(b);
for (var i = 0; i < type.lanes; i++) {
lanes[i] = type.boolType.fn.extractLane(selector, i) ?
type.fn.extractLane(a, i) : type.fn.extractLane(b, i);
}
return simdCreate(type);
}
function simdSwizzle(type, a, indices) {
a = type.fn.check(a);
for (var i = 0; i < indices.length; i++) {
simdCheckLaneIndex(indices[i], type.lanes);
lanes[i] = type.fn.extractLane(a, indices[i]);
}
return simdCreate(type);
}
function simdShuffle(type, a, b, indices) {
a = type.fn.check(a);
b = type.fn.check(b);
for (var i = 0; i < indices.length; i++) {
simdCheckLaneIndex(indices[i], 2 * type.lanes);
lanes[i] = indices[i] < type.lanes ?
type.fn.extractLane(a, indices[i]) :
type.fn.extractLane(b, indices[i] - type.lanes);
}
return simdCreate(type);
}
function unaryNeg(a) { return -a; }
function unaryBitwiseNot(a) { return ~a; }
function unaryLogicalNot(a) { return !a; }
function simdUnaryOp(type, op, a) {
a = type.fn.check(a);
for (var i = 0; i < type.lanes; i++)
lanes[i] = op(type.fn.extractLane(a, i));
return simdCreate(type);
}
function binaryAnd(a, b) { return a & b; }
function binaryOr(a, b) { return a | b; }
function binaryXor(a, b) { return a ^ b; }
function binaryAdd(a, b) { return a + b; }
function binarySub(a, b) { return a - b; }
function binaryMul(a, b) { return a * b; }
function binaryDiv(a, b) { return a / b; }
var binaryImul;
if (typeof Math.imul !== 'undefined') {
binaryImul = Math.imul;
} else {
binaryImul = function(a, b) {
var ah = (a >>> 16) & 0xffff;
var al = a & 0xffff;
var bh = (b >>> 16) & 0xffff;
var bl = b & 0xffff;
// the shift by 0 fixes the sign on the high part
// the final |0 converts the unsigned value into a signed value
return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0)|0);
};
}
function simdBinaryOp(type, op, a, b) {
a = type.fn.check(a);
b = type.fn.check(b);
for (var i = 0; i < type.lanes; i++)
lanes[i] = op(type.fn.extractLane(a, i), type.fn.extractLane(b, i));
return simdCreate(type);
}
function binaryEqual(a, b) { return a == b; }
function binaryNotEqual(a, b) { return a != b; }
function binaryLess(a, b) { return a < b; }
function binaryLessEqual(a, b) { return a <= b; }
function binaryGreater(a, b) { return a > b; }
function binaryGreaterEqual(a, b) { return a >= b; }
function simdRelationalOp(type, op, a, b) {
a = type.fn.check(a);
b = type.fn.check(b);
for (var i = 0; i < type.lanes; i++)
lanes[i] = op(type.fn.extractLane(a, i), type.fn.extractLane(b, i));
return simdCreate(type.boolType);
}
function simdAnyTrue(type, a) {
a = type.fn.check(a);
for (var i = 0; i < type.lanes; i++)
if (type.fn.extractLane(a, i)) return true;
return false;
}
function simdAllTrue(type, a) {
a = type.fn.check(a);
for (var i = 0; i < type.lanes; i++)
if (!type.fn.extractLane(a, i)) return false;
return true;
}
function binaryShiftLeft(a, bits) { return a << bits; }
function binaryShiftRightArithmetic(a, bits) { return a >> bits; }
function binaryShiftRightLogical(a, bits) { return a >>> bits; }
function simdShiftOp(type, op, a, bits) {
a = type.fn.check(a);
for (var i = 0; i < type.lanes; i++)
lanes[i] = op(type.fn.extractLane(a, i), bits);
return simdCreate(type);
}
function simdLoad(type, tarray, index, count) {
if (!isTypedArray(tarray))
throw new TypeError("The 1st argument must be a typed array.");
index = simdCoerceIndex(index);
var bpe = tarray.BYTES_PER_ELEMENT;
var bytes = count * type.laneSize;
if (index < 0 || (index * bpe + bytes) > tarray.byteLength)
throw new RangeError("The value of index is invalid.");
var newValue = type.fn();
var dst = new Uint8Array(newValue.s_.buffer);
var src = new Uint8Array(tarray.buffer, tarray.byteOffset + index * bpe, bytes);
for (var i = 0; i < bytes; i++) {
dst[i] = src[i];
}
var typeBytes = type.lanes * type.laneSize;
for (var i = bytes; i < typeBytes; i++) {
dst[i] = 0;
}
return newValue;
}
function simdStore(type, tarray, index, a, count) {
if (!isTypedArray(tarray))
throw new TypeError("The 1st argument must be a typed array.");
index = simdCoerceIndex(index);
var bpe = tarray.BYTES_PER_ELEMENT;
var bytes = count * type.laneSize;
if (index < 0 || (index * bpe + bytes) > tarray.byteLength)
throw new RangeError("The value of index is invalid.");
a = type.fn.check(a);
// The underlying buffers are copied byte by byte, to avoid float
// canonicalization.
var src = new Uint8Array(a.s_.buffer);
var dst = new Uint8Array(tarray.buffer, tarray.byteOffset + index * bpe, bytes);
for (var i = 0; i < bytes; i++) {
dst[i] = src[i];
}
return a;
}
// Constructors and extractLane functions are closely related and must be
// polyfilled together.
// Float32x4
if (typeof SIMD.Float32x4 === "undefined" ||
typeof SIMD.Float32x4.extractLane === "undefined") {
SIMD.Float32x4 = function(s0, s1, s2, s3) {
if (!(this instanceof SIMD.Float32x4)) {
return new SIMD.Float32x4(s0, s1, s2, s3);
}
this.s_ = convertArray(_f32x4, new Float32Array([s0, s1, s2, s3]));
}
SIMD.Float32x4.extractLane = function(v, i) {
v = SIMD.Float32x4.check(v);
simdCheckLaneIndex(i, 4);
return v.s_[i];
}
}
// Miscellaneous functions that aren't easily parameterized on type.
if (typeof SIMD.Float32x4.swizzle === "undefined") {
SIMD.Float32x4.swizzle = function(a, s0, s1, s2, s3) {
return simdSwizzle(float32x4, a, [s0, s1, s2, s3]);
}
}
if (typeof SIMD.Float32x4.shuffle === "undefined") {
SIMD.Float32x4.shuffle = function(a, b, s0, s1, s2, s3) {
return simdShuffle(float32x4, a, b, [s0, s1, s2, s3]);
}
}
// Int32x4
if (typeof SIMD.Int32x4 === "undefined" ||
typeof SIMD.Int32x4.extractLane === "undefined") {
SIMD.Int32x4 = function(s0, s1, s2, s3) {
if (!(this instanceof SIMD.Int32x4)) {
return new SIMD.Int32x4(s0, s1, s2, s3);
}
this.s_ = convertArray(_i32x4, new Int32Array([s0, s1, s2, s3]));
}
SIMD.Int32x4.extractLane = function(v, i) {
v = SIMD.Int32x4.check(v);
simdCheckLaneIndex(i, 4);
return v.s_[i];
}
}
if (typeof SIMD.Int32x4.swizzle === "undefined") {
SIMD.Int32x4.swizzle = function(a, s0, s1, s2, s3) {
return simdSwizzle(int32x4, a, [s0, s1, s2, s3]);
}
}
if (typeof SIMD.Int32x4.shuffle === "undefined") {
SIMD.Int32x4.shuffle = function(a, b, s0, s1, s2, s3) {
return simdShuffle(int32x4, a, b, [s0, s1, s2, s3]);
}
}
// Int16x8
if (typeof SIMD.Int16x8 === "undefined" ||
typeof SIMD.Int16x8.extractLane === "undefined") {
SIMD.Int16x8 = function(s0, s1, s2, s3, s4, s5, s6, s7) {
if (!(this instanceof SIMD.Int16x8)) {
return new SIMD.Int16x8(s0, s1, s2, s3, s4, s5, s6, s7);
}
this.s_ = convertArray(_i16x8, new Int16Array([s0, s1, s2, s3, s4, s5, s6, s7]));
}
SIMD.Int16x8.extractLane = function(v, i) {
v = SIMD.Int16x8.check(v);
simdCheckLaneIndex(i, 8);
return v.s_[i];
}
}
if (typeof SIMD.Int16x8.swizzle === "undefined") {
SIMD.Int16x8.swizzle = function(a, s0, s1, s2, s3, s4, s5, s6, s7) {
return simdSwizzle(int16x8, a, [s0, s1, s2, s3, s4, s5, s6, s7]);
}
}
if (typeof SIMD.Int16x8.shuffle === "undefined") {
SIMD.Int16x8.shuffle = function(a, b, s0, s1, s2, s3, s4, s5, s6, s7) {
return simdShuffle(int16x8, a, b, [s0, s1, s2, s3, s4, s5, s6, s7]);
}
}
// Int8x16
if (typeof SIMD.Int8x16 === "undefined" ||
typeof SIMD.Int8x16.extractLane === "undefined") {
SIMD.Int8x16 = function(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
if (!(this instanceof SIMD.Int8x16)) {
return new SIMD.Int8x16(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15);
}
this.s_ = convertArray(_i8x16, new Int8Array([s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]));
}
SIMD.Int8x16.extractLane = function(v, i) {
v = SIMD.Int8x16.check(v);
simdCheckLaneIndex(i, 16);
return v.s_[i];
}
}
if (typeof SIMD.Int8x16.swizzle === "undefined") {
SIMD.Int8x16.swizzle = function(a, s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
return simdSwizzle(int8x16, a, [s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]);
}
}
if (typeof SIMD.Int8x16.shuffle === "undefined") {
SIMD.Int8x16.shuffle = function(a, b, s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
return simdShuffle(int8x16, a, b, [s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]);
}
}
// Uint32x4
if (typeof SIMD.Uint32x4 === "undefined" ||
typeof SIMD.Uint32x4.extractLane === "undefined") {
SIMD.Uint32x4 = function(s0, s1, s2, s3) {
if (!(this instanceof SIMD.Uint32x4)) {
return new SIMD.Uint32x4(s0, s1, s2, s3);
}
this.s_ = convertArray(_ui32x4, new Uint32Array([s0, s1, s2, s3]));
}
SIMD.Uint32x4.extractLane = function(v, i) {
v = SIMD.Uint32x4.check(v);
simdCheckLaneIndex(i, 4);
return v.s_[i];
}
}
if (typeof SIMD.Uint32x4.swizzle === "undefined") {
SIMD.Uint32x4.swizzle = function(a, s0, s1, s2, s3) {
return simdSwizzle(uint32x4, a, [s0, s1, s2, s3]);
}
}
if (typeof SIMD.Uint32x4.shuffle === "undefined") {
SIMD.Uint32x4.shuffle = function(a, b, s0, s1, s2, s3) {
return simdShuffle(uint32x4, a, b, [s0, s1, s2, s3]);
}
}
// Uint16x8
if (typeof SIMD.Uint16x8 === "undefined" ||
typeof SIMD.Uint16x8.extractLane === "undefined") {
SIMD.Uint16x8 = function(s0, s1, s2, s3, s4, s5, s6, s7) {
if (!(this instanceof SIMD.Uint16x8)) {
return new SIMD.Uint16x8(s0, s1, s2, s3, s4, s5, s6, s7);
}
this.s_ = convertArray(_ui16x8, new Uint16Array([s0, s1, s2, s3, s4, s5, s6, s7]));
}
SIMD.Uint16x8.extractLane = function(v, i) {
v = SIMD.Uint16x8.check(v);
simdCheckLaneIndex(i, 8);
return v.s_[i];
}
}
if (typeof SIMD.Uint16x8.swizzle === "undefined") {
SIMD.Uint16x8.swizzle = function(a, s0, s1, s2, s3, s4, s5, s6, s7) {
return simdSwizzle(uint16x8, a, [s0, s1, s2, s3, s4, s5, s6, s7]);
}
}
if (typeof SIMD.Uint16x8.shuffle === "undefined") {
SIMD.Uint16x8.shuffle = function(a, b, s0, s1, s2, s3, s4, s5, s6, s7) {
return simdShuffle(uint16x8, a, b, [s0, s1, s2, s3, s4, s5, s6, s7]);
}
}
// Uint8x16
if (typeof SIMD.Uint8x16 === "undefined" ||
typeof SIMD.Uint8x16.extractLane === "undefined") {
SIMD.Uint8x16 = function(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
if (!(this instanceof SIMD.Uint8x16)) {
return new SIMD.Uint8x16(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15);
}
this.s_ = convertArray(_ui8x16, new Uint8Array([s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]));
}
SIMD.Uint8x16.extractLane = function(v, i) {
v = SIMD.Uint8x16.check(v);
simdCheckLaneIndex(i, 16);
return v.s_[i];
}
}
if (typeof SIMD.Uint8x16.swizzle === "undefined") {
SIMD.Uint8x16.swizzle = function(a, s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
return simdSwizzle(uint8x16, a, [s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]);
}
}
if (typeof SIMD.Uint8x16.shuffle === "undefined") {
SIMD.Uint8x16.shuffle = function(a, b, s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
return simdShuffle(uint8x16, a, b, [s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15]);
}
}
// Bool32x4
if (typeof SIMD.Bool32x4 === "undefined" ||
typeof SIMD.Bool32x4.extractLane === "undefined") {
SIMD.Bool32x4 = function(s0, s1, s2, s3) {
if (!(this instanceof SIMD.Bool32x4)) {
return new SIMD.Bool32x4(s0, s1, s2, s3);
}
this.s_ = [!!s0, !!s1, !!s2, !!s3];
}
SIMD.Bool32x4.extractLane = function(v, i) {
v = SIMD.Bool32x4.check(v);
simdCheckLaneIndex(i, 4);
return v.s_[i];
}
}
// Bool16x8
if (typeof SIMD.Bool16x8 === "undefined" ||
typeof SIMD.Bool16x8.extractLane === "undefined") {
SIMD.Bool16x8 = function(s0, s1, s2, s3, s4, s5, s6, s7) {
if (!(this instanceof SIMD.Bool16x8)) {
return new SIMD.Bool16x8(s0, s1, s2, s3, s4, s5, s6, s7);
}
this.s_ = [!!s0, !!s1, !!s2, !!s3, !!s4, !!s5, !!s6, !!s7];
}
SIMD.Bool16x8.extractLane = function(v, i) {
v = SIMD.Bool16x8.check(v);
simdCheckLaneIndex(i, 8);
return v.s_[i];
}
}
// Bool8x16
if (typeof SIMD.Bool8x16 === "undefined" ||
typeof SIMD.Bool8x16.extractLane === "undefined") {
SIMD.Bool8x16 = function(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15) {
if (!(this instanceof SIMD.Bool8x16)) {
return new SIMD.Bool8x16(s0, s1, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, s12, s13, s14, s15);
}
this.s_ = [!!s0, !!s1, !!s2, !!s3, !!s4, !!s5, !!s6, !!s7,
!!s8, !!s9, !!s10, !!s11, !!s12, !!s13, !!s14, !!s15];
}
SIMD.Bool8x16.extractLane = function(v, i) {
v = SIMD.Bool8x16.check(v);
simdCheckLaneIndex(i, 16);
return v.s_[i];
}
}
// Type data to generate the remaining functions.
var float32x4 = {
name: "Float32x4",
fn: SIMD.Float32x4,
lanes: 4,
laneSize: 4,
buffer: _f32x4,
view: Float32Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"add", "sub", "mul", "div", "neg", "abs", "min", "max", "minNum", "maxNum",
"reciprocalApproximation", "reciprocalSqrtApproximation", "sqrt",
"load", "load1", "load2", "load3", "store", "store1", "store2", "store3"],
}
var int32x4 = {
name: "Int32x4",
fn: SIMD.Int32x4,
lanes: 4,
laneSize: 4,
minVal: -0x80000000,
maxVal: 0x7FFFFFFF,
buffer: _i32x4,
notFn: unaryBitwiseNot,
view: Int32Array,
mulFn: binaryImul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul", "neg",
"shiftLeftByScalar", "shiftRightByScalar",
"load", "load1", "load2", "load3", "store", "store1", "store2", "store3"],
}
var int16x8 = {
name: "Int16x8",
fn: SIMD.Int16x8,
lanes: 8,
laneSize: 2,
minVal: -0x8000,
maxVal: 0x7FFF,
buffer: _i16x8,
notFn: unaryBitwiseNot,
view: Int16Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul", "neg",
"shiftLeftByScalar", "shiftRightByScalar",
"addSaturate", "subSaturate",
"load", "store"],
}
var int8x16 = {
name: "Int8x16",
fn: SIMD.Int8x16,
lanes: 16,
laneSize: 1,
minVal: -0x80,
maxVal: 0x7F,
buffer: _i8x16,
notFn: unaryBitwiseNot,
view: Int8Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul", "neg",
"shiftLeftByScalar", "shiftRightByScalar",
"addSaturate", "subSaturate",
"load", "store"],
}
var uint32x4 = {
name: "Uint32x4",
fn: SIMD.Uint32x4,
lanes: 4,
laneSize: 4,
minVal: 0,
maxVal: 0xFFFFFFFF,
unsigned: true,
buffer: _ui32x4,
notFn: unaryBitwiseNot,
view: Uint32Array,
mulFn: binaryImul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul",
"shiftLeftByScalar", "shiftRightByScalar",
"load", "load1", "load2", "load3", "store", "store1", "store2", "store3"],
}
var uint16x8 = {
name: "Uint16x8",
fn: SIMD.Uint16x8,
lanes: 8,
laneSize: 2,
unsigned: true,
minVal: 0,
maxVal: 0xFFFF,
buffer: _ui16x8,
notFn: unaryBitwiseNot,
view: Uint16Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul",
"shiftLeftByScalar", "shiftRightByScalar",
"addSaturate", "subSaturate",
"load", "store"],
}
var uint8x16 = {
name: "Uint8x16",
fn: SIMD.Uint8x16,
lanes: 16,
laneSize: 1,
unsigned: true,
minVal: 0,
maxVal: 0xFF,
buffer: _ui8x16,
notFn: unaryBitwiseNot,
view: Uint8Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"and", "or", "xor", "not",
"add", "sub", "mul",
"shiftLeftByScalar", "shiftRightByScalar",
"addSaturate", "subSaturate",
"load", "store"],
}
var bool32x4 = {
name: "Bool32x4",
fn: SIMD.Bool32x4,
lanes: 4,
laneSize: 4,
notFn: unaryLogicalNot,
fns: ["check", "splat", "replaceLane",
"allTrue", "anyTrue", "and", "or", "xor", "not"],
}
var bool16x8 = {
name: "Bool16x8",
fn: SIMD.Bool16x8,
lanes: 8,
laneSize: 2,
notFn: unaryLogicalNot,
fns: ["check", "splat", "replaceLane",
"allTrue", "anyTrue", "and", "or", "xor", "not"],
}
var bool8x16 = {
name: "Bool8x16",
fn: SIMD.Bool8x16,
lanes: 16,
laneSize: 1,
notFn: unaryLogicalNot,
fns: ["check", "splat", "replaceLane",
"allTrue", "anyTrue", "and", "or", "xor", "not"],
}
// Each SIMD type has a corresponding Boolean SIMD type, which is returned by
// relational ops.
float32x4.boolType = int32x4.boolType = uint32x4.boolType = bool32x4;
int16x8.boolType = uint16x8.boolType = bool16x8;
int8x16.boolType = uint8x16.boolType = bool8x16;
// SIMD from<type> types.
float32x4.from = [int32x4, uint32x4];
int32x4.from = [float32x4, uint32x4];
int16x8.from = [uint16x8];
int8x16.from = [uint8x16];
uint32x4.from = [float32x4, int32x4];
uint16x8.from = [int16x8];
uint8x16.from = [int8x16];
// SIMD from<type>Bits types.
float32x4.fromBits = [int32x4, int16x8, int8x16, uint32x4, uint16x8, uint8x16];
int32x4.fromBits = [float32x4, int16x8, int8x16, uint32x4, uint16x8, uint8x16];
int16x8.fromBits = [float32x4, int32x4, int8x16, uint32x4, uint16x8, uint8x16];
int8x16.fromBits = [float32x4, int32x4, int16x8, uint32x4, uint16x8, uint8x16];
uint32x4.fromBits = [float32x4, int32x4, int16x8, int8x16, uint16x8, uint8x16];
uint16x8.fromBits = [float32x4, int32x4, int16x8, int8x16, uint32x4, uint8x16];
uint8x16.fromBits = [float32x4, int32x4, int16x8, int8x16, uint32x4, uint16x8];
var simdTypes = [float32x4,
int32x4, int16x8, int8x16,
uint32x4, uint16x8, uint8x16,
bool32x4, bool16x8, bool8x16];
// SIMD Phase2 types.
if (typeof simdPhase2 !== 'undefined') {
// Float64x2
if (typeof SIMD.Float64x2 === "undefined" ||
typeof SIMD.Float64x2.extractLane === "undefined") {
SIMD.Float64x2 = function(s0, s1) {
if (!(this instanceof SIMD.Float64x2)) {
return new SIMD.Float64x2(s0, s1);
}
this.s_ = convertArray(_f64x2, new Float64Array([s0, s1]));
}
SIMD.Float64x2.extractLane = function(v, i) {
v = SIMD.Float64x2.check(v);
simdCheckLaneIndex(i, 2);
return v.s_[i];
}
}
if (typeof SIMD.Float64x2.swizzle === "undefined") {
SIMD.Float64x2.swizzle = function(a, s0, s1) {
return simdSwizzle(float64x2, a, [s0, s1]);
}
}
if (typeof SIMD.Float64x2.shuffle === "undefined") {
SIMD.Float64x2.shuffle = function(a, b, s0, s1) {
return simdShuffle(float64x2, a, b, [s0, s1]);
}
}
// Bool64x2
if (typeof SIMD.Bool64x2 === "undefined" ||
typeof SIMD.Bool64x2.extractLane === "undefined") {
SIMD.Bool64x2 = function(s0, s1) {
if (!(this instanceof SIMD.Bool64x2)) {
return new SIMD.Bool64x2(s0, s1);
}
this.s_ = [!!s0, !!s1];
}
SIMD.Bool64x2.extractLane = function(v, i) {
v = SIMD.Bool64x2.check(v);
simdCheckLaneIndex(i, 2);
return v.s_[i];
}
}
var float64x2 = {
name: "Float64x2",
fn: SIMD.Float64x2,
lanes: 2,
laneSize: 8,
buffer: _f64x2,
view: Float64Array,
mulFn: binaryMul,
fns: ["check", "splat", "replaceLane", "select",
"equal", "notEqual", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual",
"add", "sub", "mul", "div", "neg", "abs", "min", "max", "minNum", "maxNum",
"reciprocalApproximation", "reciprocalSqrtApproximation", "sqrt",
"load", "store"],
}
var bool64x2 = {
name: "Bool64x2",
fn: SIMD.Bool64x2,
lanes: 2,
laneSize: 8,
notFn: unaryLogicalNot,
fns: ["check", "splat", "replaceLane",
"allTrue", "anyTrue", "and", "or", "xor", "not"],
}
float64x2.boolType = bool64x2;
float32x4.fromBits.push(float64x2);
int32x4.fromBits.push(float64x2);
int16x8.fromBits.push(float64x2);
int8x16.fromBits.push(float64x2);
uint32x4.fromBits.push(float64x2);
uint16x8.fromBits.push(float64x2);
uint8x16.fromBits.push(float64x2);
float64x2.fromBits = [float32x4, int32x4, int16x8, int8x16,
uint32x4, uint16x8, uint8x16];
simdTypes.push(float64x2);
simdTypes.push(bool64x2);
}
// SIMD prototype functions.
var prototypeFns = {
valueOf:
function(type) {
return function() {
throw new TypeError(type.name + " cannot be converted to a number");
}
},
toString:
function(type) {
return function() {
return simdToString(type, this);
}
},
toLocaleString:
function(type) {
return function() {
return simdToLocaleString(type, this);
}
},
};
// SIMD constructor functions.
var simdFns = {
check:
function(type) {
return function(a) {
if (!(a instanceof type.fn)) {
throw new TypeError("Argument is not a " + type.name + ".");
}
return a;
}
},
splat:
function(type) {
return function(s) { return simdSplat(type, s); }
},
replaceLane:
function(type) {
return function(a, i, s) { return simdReplaceLane(type, a, i, s); }
},
allTrue:
function(type) {
return function(a) { return simdAllTrue(type, a); }
},
anyTrue:
function(type) {
return function(a) { return simdAnyTrue(type, a); }
},
and:
function(type) {
return function(a, b) {
return simdBinaryOp(type, binaryAnd, a, b);
}
},
or:
function(type) {
return function(a, b) {
return simdBinaryOp(type, binaryOr, a, b);
}
},
xor:
function(type) {
return function(a, b) {
return simdBinaryOp(type, binaryXor, a, b);
}
},
not:
function(type) {
return function(a) {
return simdUnaryOp(type, type.notFn, a);
}
},
equal:
function(type) {
return function(a, b) {
return simdRelationalOp(type, binaryEqual, a, b);
}
},
notEqual:
function(type) {