summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/bug1693500.js
blob: 932dba1a57af88d1dd33e6fcc2268d972c889a24 (plain)
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
// |jit-test| skip-if: !wasmSimdEnabled() || wasmCompileMode() != "ion" || (!getBuildConfiguration().x86 && !getBuildConfiguration().x64) || getBuildConfiguration().simulator

const avx = isAvxPresent();
for (let [n1, n2, numInstr] of [
    [0x123456789abn, 0xffeeddccbbaa9988n, avx ? 6 : 7],
    [42n, 0xFFFFFFFFn, avx ? 3 : 4],
    [0n, 0n, 1],
    [1n, 1n, 1],
    ...iota(63).map(i => [2n << BigInt(i), 2n << BigInt(i), 1]),
    ...iota(63).reduce((acc, i) => {
        const base = 2n << BigInt(i);
        return acc.concat(iota(i + 1).map(j => [
            base | (1n << BigInt(j)), base | (1n << BigInt(j)),
            (avx ? 2 : 3) + (j == 0 ? 0 : 1)]));
    }, []),
    ...iota(63).map(i => [~(1n << BigInt(i)), ~(1n << BigInt(i)), avx ? 5 : 6]),
    [0x7fffffffffffffffn, 0x7fffffffffffffffn, avx ? 5 : 6],
    [-1n, -1n, 3],
]) {
    var wasm = wasmTextToBinary(`(module
        (memory (export "memory") 1 1)
        (func $t (export "t") (param v128) (result v128)
            local.get 0
            v128.const i64x2 ${n1} ${n2}
            i64x2.mul
        )
        (func $t0 (param v128 v128) (result v128)
            local.get 0
            local.get 1
            i64x2.mul
        )
        (func (export "run") (result i32)
            i32.const 16
            i32.const 0
            v128.load
            call $t
            v128.store

            v128.const i64x2 ${n1} ${n2}
            i32.const 0
            v128.load
            call $t0

            i32.const 16
            v128.load
            i64x2.eq
            i64x2.all_true
        )
    )`)

    var ins = new WebAssembly.Instance(new WebAssembly.Module(wasm));
    var mem64 = new BigInt64Array(ins.exports.memory.buffer, 0, 4);

    for (let [t1, t2] of [
        [1n, 1n], [-1n, -2n], [0n, 0n], [0x123456789abn, 0xffeeddccbbaa9988n],
        [0x100001111n, -0xFF0011n], [5555n, 0n],
    ]) {
        mem64[0] = t1; mem64[1] = t2;
        assertEq(ins.exports.run(), 1);
    }

    if (hasDisassembler() && getBuildConfiguration().x64) {
        const dis = wasmDis(ins.exports.t, {asString: true,});
        const lines = getFuncBody(dis).trim().split('\n');
        assertEq(lines.length, numInstr);
    }
}

// Utils.
function getFuncBody(dis) {
    const parts = dis.split(/mov %rsp, %rbp\n|^[0-9A-Fa-f ]+pop %rbp/gm);
    return parts.at(-2).replace(/[0-9A-F]{8} (?: [0-9a-f]{2})+[\s\n]+/g, "");
}