| 1 |
// |jit-test| skip-if: true |
| 2 |
|
| 3 |
// Common code for the ad-hack test cases. |
| 4 |
|
| 5 |
function get(arr, loc, len) { |
| 6 |
let res = []; |
| 7 |
for ( let i=0; i < len; i++ ) { |
| 8 |
res.push(arr[loc+i]); |
| 9 |
} |
| 10 |
return res; |
| 11 |
} |
| 12 |
|
| 13 |
function getUnaligned(arr, width, loc, len) { |
| 14 |
assertEq(arr.constructor, Uint8Array); |
| 15 |
assertEq(width <= 4, true); |
| 16 |
let res = []; |
| 17 |
for ( let i=0; i < len; i++ ) { |
| 18 |
let x = 0; |
| 19 |
for ( let j=width-1; j >=0; j-- ) |
| 20 |
x = (x << 8) | arr[loc+i*width+j]; |
| 21 |
res.push(x); |
| 22 |
} |
| 23 |
return res; |
| 24 |
} |
| 25 |
|
| 26 |
function set(arr, loc, vals) { |
| 27 |
for ( let i=0; i < vals.length; i++ ) { |
| 28 |
if (arr instanceof BigInt64Array) { |
| 29 |
arr[loc+i] = BigInt(vals[i]); |
| 30 |
} else { |
| 31 |
arr[loc+i] = vals[i]; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
function setUnaligned(arr, width, loc, vals) { |
| 37 |
assertEq(arr.constructor, Uint8Array); |
| 38 |
assertEq(width <= 4, true); |
| 39 |
for ( let i=0; i < vals.length; i++ ) { |
| 40 |
let x = vals[i]; |
| 41 |
for ( let j=0 ; j < width ; j++ ) { |
| 42 |
arr[loc+i*width + j] = x & 255; |
| 43 |
x >>= 8; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
function equal(a, b) { |
| 49 |
return a === b || isNaN(a) && isNaN(b); |
| 50 |
} |
| 51 |
|
| 52 |
function upd(xs, at, val) { |
| 53 |
let ys = Array.from(xs); |
| 54 |
ys[at] = val; |
| 55 |
return ys; |
| 56 |
} |
| 57 |
|
| 58 |
// The following operations are not always generalized fully, they are just |
| 59 |
// functional enough for the existing test cases to pass. |
| 60 |
|
| 61 |
function sign_extend(n, bits) { |
| 62 |
if (bits < 32) { |
| 63 |
n = Number(n); |
| 64 |
return (n << (32 - bits)) >> (32 - bits); |
| 65 |
} |
| 66 |
if (typeof n == "bigint") { |
| 67 |
if (bits == 32) |
| 68 |
return Number(n & 0xFFFF_FFFFn) | 0; |
| 69 |
assertEq(bits, 64); |
| 70 |
n = (n & 0xFFFF_FFFF_FFFF_FFFFn) |
| 71 |
if (n > 0x7FFF_FFFF_FFFF_FFFFn) |
| 72 |
return n - 0x1_0000_0000_0000_0000n; |
| 73 |
return n; |
| 74 |
} |
| 75 |
assertEq(bits, 32); |
| 76 |
return n|0; |
| 77 |
} |
| 78 |
|
| 79 |
function zero_extend(n, bits) { |
| 80 |
if (bits < 32) { |
| 81 |
return n & ((1 << bits) - 1); |
| 82 |
} |
| 83 |
if (n < 0) |
| 84 |
n = 0x100000000 + n; |
| 85 |
return n; |
| 86 |
} |
| 87 |
|
| 88 |
function signed_saturate(z, bits) { |
| 89 |
let min = -(1 << (bits-1)); |
| 90 |
if (z <= min) { |
| 91 |
return min; |
| 92 |
} |
| 93 |
let max = (1 << (bits-1)) - 1; |
| 94 |
if (z > max) { |
| 95 |
return max; |
| 96 |
} |
| 97 |
return z; |
| 98 |
} |
| 99 |
|
| 100 |
function unsigned_saturate(z, bits) { |
| 101 |
if (z <= 0) { |
| 102 |
return 0; |
| 103 |
} |
| 104 |
let max = (1 << bits) - 1; |
| 105 |
if (z > max) { |
| 106 |
return max; |
| 107 |
} |
| 108 |
return z; |
| 109 |
} |
| 110 |
|
| 111 |
function shl(count, width) { |
| 112 |
if (width == 64) { |
| 113 |
count = BigInt(count); |
| 114 |
return (v) => { |
| 115 |
v = BigInt(v); |
| 116 |
if (v < 0) |
| 117 |
v = (1n << 64n) + v; |
| 118 |
let r = (v << count) & ((1n << 64n) - 1n); |
| 119 |
if (r & (1n << 63n)) |
| 120 |
r = -((1n << 64n) - r); |
| 121 |
return r; |
| 122 |
} |
| 123 |
} else { |
| 124 |
return (v) => { |
| 125 |
let mask = (width == 32) ? -1 : ((1 << width) - 1); |
| 126 |
return (v << count) & mask; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
function popcount(n) { |
| 132 |
n = n - ((n >> 1) & 0x55555555) |
| 133 |
n = (n & 0x33333333) + ((n >> 2) & 0x33333333) |
| 134 |
return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24 |
| 135 |
} |
| 136 |
|
| 137 |
function jsValueToWasmName(x) { |
| 138 |
if (typeof x == "number") { |
| 139 |
if (x == 0) return 1 / x < 0 ? "-0" : "0"; |
| 140 |
if (isNaN(x)) return "+nan"; |
| 141 |
if (!isFinite(x)) return (x < 0 ? "-" : "+") + "inf"; |
| 142 |
} |
| 143 |
return x; |
| 144 |
} |
| 145 |
|
| 146 |
// For each input array, a set of arrays of the proper length for v128, with |
| 147 |
// values in range but possibly of the wrong signedness (eg, for Int8Array, 128 |
| 148 |
// is in range but is really -128). Also a unary operator `rectify` that |
| 149 |
// transforms the value to the proper sign and bitwidth. |
| 150 |
|
| 151 |
Int8Array.inputs = [iota(16).map((x) => (x+1) * (x % 3 == 0 ? -1 : 1)), |
| 152 |
iota(16).map((x) => (x*2+3) * (x % 3 == 1 ? -1 : 1)), |
| 153 |
[1,2,128,127,1,4,128,127,1,2,129,125,1,2,254,0], |
| 154 |
[2,1,127,128,5,1,127,128,2,1,126,130,2,1,1,255], |
| 155 |
iota(16).map((x) => ((x + 37) * 8 + 12) % 256), |
| 156 |
iota(16).map((x) => ((x + 12) * 4 + 9) % 256)]; |
| 157 |
Int8Array.rectify = (x) => sign_extend(x,8); |
| 158 |
Int8Array.layoutName = 'i8x16'; |
| 159 |
|
| 160 |
Uint8Array.inputs = Int8Array.inputs; |
| 161 |
Uint8Array.rectify = (x) => zero_extend(x,8); |
| 162 |
Uint8Array.layoutName = 'i8x16'; |
| 163 |
|
| 164 |
Int16Array.inputs = [iota(8).map((x) => (x+1) * (x % 3 == 0 ? -1 : 1)), |
| 165 |
iota(8).map((x) => (x*2+3) * (x % 3 == 1 ? -1 : 1)), |
| 166 |
[1,2,32768,32767,1,4,32768,32767], |
| 167 |
[2,1,32767,32768,5,1,32767,32768], |
| 168 |
[1,2,128,127,1,4,128,127].map((x) => (x << 8) + x*2), |
| 169 |
[2,1,127,128,1,1,128,128].map((x) => (x << 8) + x*3)]; |
| 170 |
Int16Array.rectify = (x) => sign_extend(x,16); |
| 171 |
Int16Array.layoutName = 'i16x8'; |
| 172 |
|
| 173 |
Uint16Array.inputs = Int16Array.inputs; |
| 174 |
Uint16Array.rectify = (x) => zero_extend(x,16); |
| 175 |
Uint16Array.layoutName = 'i16x8'; |
| 176 |
|
| 177 |
Int32Array.inputs = [iota(4).map((x) => (x+1) * (x % 3 == 0 ? -1 : 1)), |
| 178 |
iota(4).map((x) => (x*2+3) * (x % 3 == 1 ? -1 : 1)), |
| 179 |
[1,2,32768 << 16,32767 << 16], |
| 180 |
[2,1,32767 << 16,32768 << 16], |
| 181 |
[1,2,128,127].map((x) => (x << 24) + (x << 8) + x*3), |
| 182 |
[2,1,127,128].map((x) => (x << 24) + (x << 8) + x*4)]; |
| 183 |
Int32Array.rectify = (x) => sign_extend(x,32); |
| 184 |
Int32Array.layoutName = 'i32x4'; |
| 185 |
|
| 186 |
Uint32Array.inputs = Int32Array.inputs; |
| 187 |
Uint32Array.rectify = (x) => zero_extend(x,32); |
| 188 |
Uint32Array.layoutName = 'i32x4'; |
| 189 |
|
| 190 |
BigInt64Array.inputs = [[1,2],[2,1],[-1,-2],[-2,-1],[2n ** 32n, 2n ** 32n - 5n], |
| 191 |
[(2n ** 38n) / 5n, (2n ** 41n) / 7n], |
| 192 |
[-((2n ** 38n) / 5n), (2n ** 41n) / 7n]]; |
| 193 |
BigInt64Array.rectify = (x) => BigInt(x); |
| 194 |
BigInt64Array.layoutName = 'i64x2'; |
| 195 |
|
| 196 |
Float32Array.inputs = [[1, -1, 1e10, -1e10], |
| 197 |
[-1, -2, -1e10, 1e10], |
| 198 |
[5.1, -1.1, -4.3, -0], |
| 199 |
...permute([1, -10, NaN, Infinity])]; |
| 200 |
Float32Array.rectify = (x) => Math.fround(x); |
| 201 |
Float32Array.layoutName = 'f32x4'; |
| 202 |
|
| 203 |
Float64Array.inputs = Float32Array.inputs.map((x) => x.slice(0, 2)) |
| 204 |
Float64Array.rectify = (x) => x; |
| 205 |
Float64Array.layoutName = 'f64x2'; |
| 206 |
|
| 207 |
// Tidy up all the inputs |
| 208 |
for ( let A of [Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, |
| 209 |
Float32Array, Float64Array]) { |
| 210 |
A.inputs = A.inputs.map((xs) => xs.map(A.rectify)); |
| 211 |
} |