summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/asm.js/testSIMD-bitcasts.js
blob: c5a5fb2bcada3e77def3a6e6ee45c8097222ce66 (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
74
75
76
77
78
79
80
81
82
83
84
load(libdir + "asm.js");
load(libdir + "simd.js");
load(libdir + "asserts.js");

// Set to true to see more JS debugging spew.
const DEBUG = false;

if (!isSimdAvailable()) {
    DEBUG && print("won't run tests as simd extensions aren't activated yet");
    quit(0);
}

// Test all bit-casts and normal loads and stores.
var heap = new ArrayBuffer(BUF_MIN);
var asU8 = new Uint8Array(heap);
var allTypes = [
    "Int8x16",
    "Int16x8",
    "Int32x4",
    "Uint8x16",
    "Uint16x8",
    "Uint32x4",
    "Float32x4"
];

// Generate a load bit-cast store test function that performs:
//
// function f(a, b) {
//     vec = src.load(H, a);
//     cast = dst.from«src»Bits(vec);
//     store(H, b, cast);
// }
//
// Here, `H` is the heap provided by `heap`.
function test_func(src, dst) {
    text = `
        "use asm";
        var src = glob.SIMD.${src};
        var dst = glob.SIMD.${dst};
        var ld = src.load;
        var st = dst.store;
        var bc = dst.from${src}Bits;

        var H = new glob.Uint8Array(heap);

        function f(a, b) {
            a = a|0;
            b = b|0;

            st(H, b, bc(ld(H, a)));
        }

        return f;
    `;
    return asmLink(asmCompile('glob', 'ffi', 'heap', text), this, null, heap);
}

function assertBuf16(a, b) {
    for (let i=0; i < 16; i++) {
        assertEq(asU8[a+i], asU8[b+i]);
    }
}

for (let src of allTypes) {
    for (let dst of allTypes) {
        // Skip identity conversions.
        if (src == dst) continue;

        print(src, dst);
        let f = test_func(src, dst);
        // Initialize with pseudo-random data.
        for (let i = 0; i < 64; i++) {
            asU8[i] = (i + 17) * 97;
        }

        // Aligned load/store.
        f(0, 16);
        assertBuf16(0, 16);

        // Unaligned access.
        f(1, 27);
        assertBuf16(1, 27);
    }
}