summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/builtin.js
blob: 65d673d9a7015fc9c19e2fc13d542f2ab6df1f7e (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let values = [-Infinity, Infinity, NaN, 0, -0, -0.1, 0.1, 0.5, -1.6, 1.6, 13.37];

function unary(name) {
    print(name);

    let imports = {
        math: {
            func: Math[name],
        }
    };

    let f32 = x => Math.fround(Math[name](Math.fround(x)));
    let f64 = Math[name];

    let i = wasmEvalText(`(module
        (import "math" "func" (func $f32 (param f32) (result f32)))
        (import "math" "func" (func $f64 (param f64) (result f64)))

        (table $t 10 funcref)
        (type $f_f (func (param f32) (result f32)))
        (type $d_d (func (param f64) (result f64)))
        (elem (i32.const 0) $f32 $f64)

        (func (export "f32") (param f32) (result f32)
            local.get 0
            call $f32
        )
        (func (export "f32_t") (param f32) (result f32)
            local.get 0
            i32.const 0
            call_indirect (type $f_f)
        )
        (func (export "f64") (param f64) (result f64)
            local.get 0
            call $f64
        )
        (func (export "f64_t") (param f64) (result f64)
            local.get 0
            i32.const 1
            call_indirect (type $d_d)
        )
    )`, imports).exports;

    for (let v of values) {
        assertEq(i.f32(v), f32(v));
        assertEq(i.f32_t(v), f32(v));
        assertEq(i.f64(v), f64(v));
        assertEq(i.f64_t(v), f64(v));
    }
}

function binary(name) {
    print(name);

    let imports = {
        math: {
            func: Math[name]
        }
    };

    let f32 = (x, y) => Math.fround(Math[name](Math.fround(x), Math.fround(y)));
    let f64 = Math[name];

    let i = wasmEvalText(`(module
        (import "math" "func" (func $f32 (param f32) (param f32) (result f32)))
        (import "math" "func" (func $f64 (param f64) (param f64) (result f64)))

        (table $t 10 funcref)
        (type $ff_f (func (param f32) (param f32) (result f32)))
        (type $dd_d (func (param f64) (param f64) (result f64)))
        (elem (i32.const 0) $f32 $f64)

        (func (export "f32") (param f32) (param f32) (result f32)
            local.get 0
            local.get 1
            call $f32
        )
        (func (export "f32_t") (param f32) (param f32) (result f32)
            local.get 0
            local.get 1
            i32.const 0
            call_indirect (type $ff_f)
        )
        (func (export "f64") (param f64) (param f64) (result f64)
            local.get 0
            local.get 1
            call $f64
        )
        (func (export "f64_t") (param f64) (param f64) (result f64)
            local.get 0
            local.get 1
            i32.const 1
            call_indirect (type $dd_d)
        )
    )`, imports).exports;

    for (let v of values) {
        for (let w of values) {
            assertEq(i.f32(v, w), f32(v, w));
            assertEq(i.f64(v, w), f64(v, w));
        }
    }
}

unary('sin');
unary('sin');
unary('tan');
unary('cos');
unary('exp');
unary('log');
unary('asin');
unary('atan');
unary('acos');
unary('log10');
unary('log2');
unary('log1p');
unary('expm1');
unary('sinh');
unary('tanh');
unary('cosh');
unary('asinh');
unary('atanh');
unary('acosh');
unary('sign');
unary('trunc');
unary('cbrt');

binary('atan2');
binary('hypot');
binary('pow');

print('done');