summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/float.js
blob: b07a3e14825c3c85faabf470d600579414eba6f0 (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
load(libdir + "wasm.js");

wasmFullPass('(module (func (result f32) (f32.const -1)) (export "run" 0))', -1);
wasmFullPass('(module (func (result f32) (f32.const 1)) (export "run" 0))', 1);
wasmFullPass('(module (func (result f64) (f64.const -2)) (export "run" 0))', -2);
wasmFullPass('(module (func (result f64) (f64.const 2)) (export "run" 0))', 2);
wasmFullPass('(module (func (result f64) (f64.const 4294967296)) (export "run" 0))', 4294967296);
wasmFullPass('(module (func (result f32) (f32.const 1.5)) (export "run" 0))', 1.5);
wasmFullPass('(module (func (result f64) (f64.const 2.5)) (export "run" 0))', 2.5);
wasmFullPass('(module (func (result f64) (f64.const 10e2)) (export "run" 0))', 10e2);
wasmFullPass('(module (func (result f32) (f32.const 10e2)) (export "run" 0))', 10e2);
wasmFullPass('(module (func (result f64) (f64.const -0x8000000000000000)) (export "run" 0))', -0x8000000000000000);
wasmFullPass('(module (func (result f64) (f64.const -9223372036854775808)) (export "run" 0))', -9223372036854775808);
wasmFullPass('(module (func (result f64) (f64.const 1797693134862315708145274e284)) (export "run" 0))', 1797693134862315708145274e284);

function testUnary(type, opcode, op, expect) {
    wasmFullPass('(module (func (param ' + type + ') (result ' + type + ') (' + type + '.' + opcode + ' (get_local 0))) (export "run" 0))',
                 expect,
                 {},
                 op);
}

function testBinary(type, opcode, lhs, rhs, expect) {
    wasmFullPass('(module (func (param ' + type + ') (param ' + type + ') (result ' + type + ') (' + type + '.' + opcode + ' (get_local 0) (get_local 1))) (export "run" 0))',
                 expect,
                 {},
                 lhs, rhs);
}

function testComparison(type, opcode, lhs, rhs, expect) {
    wasmFullPass('(module (func (param ' + type + ') (param ' + type + ') (result i32) (' + type + '.' + opcode + ' (get_local 0) (get_local 1))) (export "run" 0))',
                 expect,
                 {},
                 lhs, rhs);
}

testUnary('f32', 'abs', -40, 40);
testUnary('f32', 'neg', 40, -40);
testUnary('f32', 'floor', 40.9, 40);
testUnary('f32', 'ceil', 40.1, 41);
testUnary('f32', 'nearest', -41.5, -42);
testUnary('f32', 'trunc', -41.5, -41);
testUnary('f32', 'sqrt', 40, 6.324555397033691);

testBinary('f32', 'add', 40, 2, 42);
testBinary('f32', 'sub', 40, 2, 38);
testBinary('f32', 'mul', 40, 2, 80);
testBinary('f32', 'div', 40, 3, 13.333333015441895);
testBinary('f32', 'min', 40, 2, 2);
testBinary('f32', 'max', 40, 2, 40);
testBinary('f32', 'copysign', 40, -2, -40);

testComparison('f32', 'eq', 40, 40, 1);
testComparison('f32', 'ne', 40, 40, 0);
testComparison('f32', 'lt', 40, 40, 0);
testComparison('f32', 'le', 40, 40, 1);
testComparison('f32', 'gt', 40, 40, 0);
testComparison('f32', 'ge', 40, 40, 1);

testUnary('f64', 'abs', -40, 40);
testUnary('f64', 'neg', 40, -40);
testUnary('f64', 'floor', 40.9, 40);
testUnary('f64', 'ceil', 40.1, 41);
testUnary('f64', 'nearest', -41.5, -42);
testUnary('f64', 'trunc', -41.5, -41);
testUnary('f64', 'sqrt', 40, 6.324555320336759);

testBinary('f64', 'add', 40, 2, 42);
testBinary('f64', 'sub', 40, 2, 38);
testBinary('f64', 'mul', 40, 2, 80);
testBinary('f64', 'div', 40, 3, 13.333333333333334);
testBinary('f64', 'min', 40, 2, 2);
testBinary('f64', 'max', 40, 2, 40);
testBinary('f64', 'copysign', 40, -2, -40);

testComparison('f64', 'eq', 40, 40, 1);
testComparison('f64', 'ne', 40, 40, 0);
testComparison('f64', 'lt', 40, 40, 0);
testComparison('f64', 'le', 40, 40, 1);
testComparison('f64', 'gt', 40, 40, 0);
testComparison('f64', 'ge', 40, 40, 1);

wasmFailValidateText('(module (func (param i32) (result f32) (f32.sqrt (get_local 0))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param f32) (result i32) (f32.sqrt (get_local 0))))', mismatchError("f32", "i32"));
wasmFailValidateText('(module (func (param i32) (result i32) (f32.sqrt (get_local 0))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param i32) (result f64) (f64.sqrt (get_local 0))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (param f64) (result i32) (f64.sqrt (get_local 0))))', mismatchError("f64", "i32"));
wasmFailValidateText('(module (func (param i32) (result i32) (f64.sqrt (get_local 0))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (f32.sqrt (nop))))', /popping value from empty stack/);

wasmFailValidateText('(module (func (param i32) (param f32) (result f32) (f32.add (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param f32) (param i32) (result f32) (f32.add (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param f32) (param f32) (result i32) (f32.add (get_local 0) (get_local 1))))', mismatchError("f32", "i32"));
wasmFailValidateText('(module (func (param i32) (param i32) (result i32) (f32.add (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param i32) (param f64) (result f64) (f64.add (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (param f64) (param i32) (result f64) (f64.add (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (param f64) (param f64) (result i32) (f64.add (get_local 0) (get_local 1))))', mismatchError("f64", "i32"));
wasmFailValidateText('(module (func (param i32) (param i32) (result i32) (f64.add (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));

wasmFailValidateText('(module (func (param i32) (param f32) (result f32) (f32.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param f32) (param i32) (result f32) (f32.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param f32) (param f32) (result f32) (f32.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f32"));
wasmFailValidateText('(module (func (param i32) (param f64) (result f64) (f64.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (param f64) (param i32) (result f64) (f64.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));
wasmFailValidateText('(module (func (param f64) (param f64) (result f64) (f64.eq (get_local 0) (get_local 1))))', mismatchError("i32", "f64"));