Debian Codegraph @

js/src/jit-test/tests/wasm/regress/bug1761850.js

raw
1 // Testing runtime execution of select + comparison operations.
2 // Normally they are folded into shorter/faster sequence than select alone.
3
4 const floatOps = {
5 lt(a, b) { return a < b ? 0 : 1; },
6 le(a, b) { return a <= b ? 0 : 1; },
7 gt(a, b) { return a > b ? 0 : 1; },
8 ge(a, b) { return a >= b ? 0 : 1; },
9 eq(a, b) { return a === b ? 0 : 1; },
10 ne(a, b) { return a !== b ? 0 : 1; },
11 }
12
13 for (let ty of ['f32', 'f64']) {
14 for (let op of ['lt', 'le', 'gt', 'ge', 'eq', 'ne']) {
15 const module = new WebAssembly.Module(wasmTextToBinary(`(module
16 (memory (export "memory") 1 1)
17 (func (export "test") (result i32)
18 i32.const 128
19 i32.load8_u
20 i32.const 129
21 i32.load8_u
22 i32.const 0
23 ${ty}.load
24 i32.const ${ty == 'f32' ? 4 : 8}
25 ${ty}.load
26 ${ty}.${op}
27 select
28 )
29 (data (i32.const 128) "\\00\\01"))`));
30 const instance = new WebAssembly.Instance(module);
31 const arr = new (ty == 'f32' ? Float32Array : Float64Array)(instance.exports.memory.buffer);
32 for (let [a, b] of cross(
33 [0, 1, -1e100, Infinity, -Infinity, 1e100, -1e-10, 1/-Infinity, NaN]
34 )) {
35 arr[0] = a; arr[1] = b;
36 assertEq(instance.exports.test(), floatOps[op](arr[0], arr[1]))
37 }
38 }
39 }
40
41 const intOps = {
42 lt(a, b) { return a < b ? 0 : 1; },
43 le(a, b) { return a <= b ? 0 : 1; },
44 gt(a, b) { return a > b ? 0 : 1; },
45 ge(a, b) { return a >= b ? 0 : 1; },
46 eq(a, b) { return a === b ? 0 : 1; },
47 ne(a, b) { return a !== b ? 0 : 1; },
48 }
49
50 for (let [ty, signed] of [['i32', true], ['i32', false], ['i64', true], ['i64', false]]) {
51 for (let op of ['lt', 'le', 'gt', 'ge', 'eq', 'ne']) {
52 const module = new WebAssembly.Module(wasmTextToBinary(`(module
53 (memory (export "memory") 1 1)
54 (func (export "test") (result i32)
55 i32.const 128
56 i32.load8_u
57 i32.const 129
58 i32.load8_u
59 i32.const 0
60 ${ty}.load
61 i32.const ${ty == 'i32' ? 4 : 8}
62 ${ty}.load
63 ${ty}.${op}${op[0] == 'l' || op[0] == 'g' ? (signed ? '_s' : '_u') : ''}
64 select
65 )
66 (data (i32.const 128) "\\00\\01"))`));
67 const instance = new WebAssembly.Instance(module);
68 const arr = new (ty == 'i32' ? (signed ? Int32Array : Uint32Array) :
69 (signed ? BigInt64Array : BigUint64Array))
70 (instance.exports.memory.buffer);
71 const c = ty == 'i32' ? (a => a|0) : BigInt;
72 for (let [a, b] of cross(
73 [c(0), ~c(0), c(1), ~c(1), c(1) << c(8), ~c(1) << c(12)]
74 )) {
75 arr[0] = a; arr[1] = b;
76 assertEq(instance.exports.test(), intOps[op](arr[0], arr[1]))
77 }
78 }
79 }