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
|
if (!this.hasOwnProperty("SIMD"))
quit();
function booleanBinaryX4(op, v, w) {
var arr = [];
var [varr, warr] = [simdToArray(v), simdToArray(w)];
for (var i = 0; i < 4; i++)
arr[i] = op(varr[i], warr[i]);
return arr;
}
function binaryX(op, v, w) {
var arr = [];
var [varr, warr] = [simdToArray(v), simdToArray(w)];
[varr, warr] = [varr.map(Math.fround), warr.map(Math.fround)];
for (var i = 0; i < varr.length; i++)
arr[i] = op(varr[i], warr[i]);
return arr.map(Math.fround);
}
function unaryX4(op, v, coerceFunc) {
var arr = [];
var varr = simdToArray(v).map(coerceFunc);
for (var i = 0; i < 4; i++)
arr[i] = op(varr[i]);
return arr.map(coerceFunc);
}
function assertNear(a, b) {
assertEq((a != a && b != b) || Math.abs(a - b) < 0.001, true);
}
function GetType(v) {
var pt = Object.getPrototypeOf(v);
switch (pt) {
case SIMD.Int8x16.prototype: return SIMD.Int8x16;
case SIMD.Int16x8.prototype: return SIMD.Int16x8;
case SIMD.Int32x4.prototype: return SIMD.Int32x4;
case SIMD.Uint8x16.prototype: return SIMD.Uint8x16;
case SIMD.Uint16x8.prototype: return SIMD.Uint16x8;
case SIMD.Uint32x4.prototype: return SIMD.Uint32x4;
case SIMD.Float32x4.prototype: return SIMD.Float32x4;
case SIMD.Bool8x16.prototype: return SIMD.Bool8x16;
case SIMD.Bool16x8.prototype: return SIMD.Bool16x8;
case SIMD.Bool32x4.prototype: return SIMD.Bool32x4;
}
throw "unexpected SIMD type";
}
function GetLength(t) {
switch (t) {
case SIMD.Int8x16: return 16;
case SIMD.Int16x8: return 8;
case SIMD.Int32x4: return 4;
case SIMD.Uint8x16: return 16;
case SIMD.Uint16x8: return 8;
case SIMD.Uint32x4: return 4;
case SIMD.Float32x4: return 4;
case SIMD.Bool8x16: return 16;
case SIMD.Bool16x8: return 8;
case SIMD.Bool32x4: return 4;
}
throw "unexpected SIMD type";
}
function assertEqVec(v, w) {
var typeV = GetType(v);
var lengthV = GetLength(typeV);
var ext = typeV.extractLane;
assertEq(GetType(w), typeV);
for (var i = 0; i < lengthV; i++)
assertEq(ext(v, i), ext(w, i));
}
function assertEqVecArr(v, w) {
var typeV = GetType(v);
var lengthV = GetLength(typeV);
var ext = typeV.extractLane;
assertEq(w.length, lengthV);
for (var i = 0; i < lengthV; i++)
assertEq(ext(v, i), w[i]);
}
function assertEqX4(vec, arr, ...opts) {
var assertFunc;
if (opts.length == 1 && typeof opts[0] !== 'undefined') {
assertFunc = opts[0];
} else {
assertFunc = assertEq;
}
var Type = GetType(vec);
assertFunc(Type.extractLane(vec, 0), arr[0]);
assertFunc(Type.extractLane(vec, 1), arr[1]);
assertFunc(Type.extractLane(vec, 2), arr[2]);
assertFunc(Type.extractLane(vec, 3), arr[3]);
}
function simdToArray(vec) {
var Type = GetType(vec);
var Length = GetLength(Type);
var a = [];
for (var i = 0; i < Length; i++)
a.push(Type.extractLane(vec, i));
return a;
}
|