summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/asm.js/testFloatingPoint.js
blob: bad22028e050e32a2618282933358eb872818c5c (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
133
134
load(libdir + "asm.js");

assertEq(asmLink(asmCompile(USE_ASM + "function f() { return 1.1 } return f"))(), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; return +(+(i|0) + .1) } return f"))(1), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(d) { d=+d; return +d } return f"))(1.1), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return +(d+e) } return f"))(1.0, .1), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(i,e) { i=i|0;e=+e; return +(+~~i+e) } return f"))(1, .1), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(d,i) { d=+d;i=i|0; return +(d + +(i|0)) } return f"))(.1, 1), 1.1);
assertEq(asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return +(d-e) } return f"))(1.1, .8), (1.1-.8));
assertEq(asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return +(d*e) } return f"))(1.1, 2.2), (1.1*2.2));
assertEq(asmLink(asmCompile(USE_ASM + "function g() { var i=2; return (~~(i=(i+1)|0))|0 } return g"))(), 3);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d<e)|0 } return f"));
assertEq(f(1.1, 2.2), 1);
assertEq(f(1.1, 1.1), 0);
assertEq(f(2.1, 1.1), 0);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d<=e)|0 } return f"));
assertEq(f(1.1, 2.2), 1);
assertEq(f(1.1, 1.1), 1);
assertEq(f(2.1, 1.1), 0);
assertEq(f(NaN, 1.1), 0);
assertEq(f(1.1, NaN), 0);
assertEq(f(NaN, NaN), 0);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d>e)|0 } return f"));
assertEq(f(2.1, 1.1), 1);
assertEq(f(1.1, 1.1), 0);
assertEq(f(1.1, 2.1), 0);
assertEq(f(NaN, 1.1), 0);
assertEq(f(1.1, NaN), 0);
assertEq(f(NaN, NaN), 0);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d>=e)|0 } return f"));
assertEq(f(2.1, 1.1), 1);
assertEq(f(1.0, 1.1), 0);
assertEq(f(1.1, 2.1), 0);
assertEq(f(NaN, 1.1), 0);
assertEq(f(1.1, NaN), 0);
assertEq(f(NaN, NaN), 0);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d==e)|0 } return f"));
assertEq(f(2.1, 1.1), 0);
assertEq(f(1.1, 1.1), 1);
assertEq(f(1.1, 2.1), 0);
assertEq(f(NaN, 1.1), 0);
assertEq(f(1.1, NaN), 0);
assertEq(f(NaN, NaN), 0);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return (d!=e)|0 } return f"));
assertEq(f(2.1, 1.1), 1);
assertEq(f(1.1, 1.1), 0);
assertEq(f(1.1, 2.1), 1);
assertEq(f(NaN, 1.1), 1);
assertEq(f(1.1, NaN), 1);
assertEq(f(NaN, NaN), 1);

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return +(d/e) } return f"));
assertEq(f(1.1, .1), (1.1/.1));
assertEq(f(1.1, 0),  (1.1/0));
assertEq(f(1.1, -0), (1.1/-0));

var f = asmLink(asmCompile(USE_ASM + "function f(d,e) { d=+d;e=+e; return +(d%e) } return f"));
assertEq(f(1.1, .1), (1.1%.1));
assertEq(f(1.1, 0),  (1.1%0));
assertEq(f(1.1, -0), (1.1%-0));

var f = asmLink(asmCompile(USE_ASM + "function f(d) { d=+d; var i = 0; i = ~~d; return i|0 } return f"));
assertEq(f(1.0), 1);
assertEq(f(1.9), 1);
assertEq(f(1.9999), 1);
assertEq(f(2.0), 2);
assertEq(f(Math.pow(2,40)), ~~Math.pow(2,40));
assertEq(f(-Math.pow(2,40)), ~~-Math.pow(2,40));
assertEq(f(4000000000), ~~4000000000);
assertEq(f(-4000000000), ~~-4000000000);
assertEq(f(NaN), 0);
assertEq(f(Infinity), 0);
assertEq(f(-Infinity), 0);

assertAsmTypeFail(USE_ASM + "function f(i,j) { i=i|0;j=j|0; return +((i|0)/(j|0)) } return f");
assertAsmTypeFail(USE_ASM + "function f(i,j) { i=i|0;j=j|0; return +(i+j) } return f");
assertAsmTypeFail(USE_ASM + "function f(i,j) { i=i|0;j=j|0; return +(i-j) } return f");

var f = asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=j|0; return +(((i|0)/(j|0))|0) } return f"));
assertEq(f(1,0), 0);
assertEq(f(-Math.pow(2,31),-1), -Math.pow(2,31));

var f = asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=j|0; return +(((i|0)%(j|0))|0) } return f"));
assertEq(f(1,0), 0);
assertEq(f(-Math.pow(2,31),-1), 0);

var {f,g} = asmLink(asmCompile(USE_ASM + "function f() { return 3.5 } function g(d) { d=+d; return +(d+3.5) } return {f:f,g:g}"));
assertEq(f(), 3.5);
assertEq(g(1), 1+3.5);

var buf = new ArrayBuffer(BUF_MIN);
var f64 = new Float64Array(buf);
var i32 = new Int32Array(buf);
var u32 = new Uint32Array(buf);
var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function f() { return +f64[0] } return f'), this, null, buf);
f64[0] = 0;
assertEq(f(), 0);
f64[0] = -1;
assertEq(f(), -1);
f64[0] = 1;
assertEq(f(), 1);
f64[0] = Infinity;
assertEq(f(), Infinity);
f64[0] = -Infinity;
assertEq(f(), -Infinity);

function ffi(d) { str = String(d) }
var g = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'var ffi=imp.ffi; function g() { ffi(+f64[0]) } return g'), this, {ffi:ffi}, buf);
var h = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + HEAP_IMPORTS + 'function g() { return +(+f64[0] < 0.0 ? -+f64[0] : +f64[0]) } return g'), this, null, buf)

function ffi1() { return 2.6 }
assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + "var ffi1=imp.ffi1; function g() { var i=0,j=0.0; i=ffi1()|0; j=+ffi1(); return +(+(i|0)+j) } return g"), null, {ffi1:ffi1})(), 2+2.6);

// that sounds dangerous!
var a = [0,1,0xffff0000,0x7fff0000,0xfff80000,0x7ff80000,0xfffc0000,0x7ffc0000,0xffffffff,0x0000ffff,0x00008fff7];
for (i of a) {
    for (j of a) {
        u32[0] = i;
        u32[1] = j;

        assertEq(f(), f64[0]);

        g();
        assertEq(str, String(f64[0]));

        assertEq(h(), Math.abs(f64[0]));
    }
}