|
1 load(libdir + "asm.js"); |
|
2 |
|
3 setIonCheckGraphCoherency(false); |
|
4 setCachingEnabled(false); |
|
5 |
|
6 // constants |
|
7 var buf = new ArrayBuffer(4096); |
|
8 |
|
9 // An unshifted literal constant byte index in the range 0 to 2^31-1 inclusive should give a link failure. |
|
10 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x7fffffff]|0 } return f'), this, null, buf); |
|
11 assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x1fffffff]|0 } return f'), this, null, buf); |
|
12 |
|
13 |
|
14 // An unshifted literal constant byte index outside the range 0 to 2^31-1 inclusive should cause an error compiling. |
|
15 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x20000000]|0 } return f'); |
|
16 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x3fffffff]|0 } return f'); |
|
17 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x40000000]|0 } return f'); |
|
18 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x7fffffff]|0 } return f'); |
|
19 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x80000000]|0 } return f'); |
|
20 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x8fffffff]|0 } return f'); |
|
21 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0xffffffff]|0 } return f'); |
|
22 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000]|0 } return f'); |
|
23 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x80000000]|0 } return f'); |
|
24 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff]|0 } return f'); |
|
25 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x100000000]|0 } return f'); |
|
26 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int16Array(b); function f() {return arr[-1]|0 } return f'); |
|
27 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[-2]|0 } return f'); |
|
28 |
|
29 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[10-12]|0 } return f'); |
|
30 |
|
31 // An intish shifted literal constant index should not fail to compile or link. |
|
32 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x3fffffff>>0]|0 } return f'), this, null, buf)(), 0); |
|
33 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x3fffffff>>2]|0 } return f'), this, null, buf)(), 0); |
|
34 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff>>0]|0 } return f'), this, null, buf)(), 0); |
|
35 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0xffffffff>>2]|0 } return f'), this, null, buf)(), 0); |
|
36 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[-1>>0]|0 } return f'), this, null, buf)(), 0); |
|
37 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[-1>>2]|0 } return f'), this, null, buf)(), 0); |
|
38 // Unsigned (intish) folded constant index. |
|
39 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff>>>0]|0 } return f'), this, null, buf)(), 0); |
|
40 assertEq(asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {arr[0] = 1; return arr[(0xffffffff+1)>>>0]|0 } return f'), this, null, buf)(), 1); |
|
41 |
|
42 // A non-intish shifted literal constant index should cause an error compiling. |
|
43 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x100000000>>0]|0 } return f'); |
|
44 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000>>2]|0 } return f'); |
|
45 |
|
46 // Folded non-intish constant expressions should cause an error compiling. |
|
47 assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff+1]|0 } return f'); |
|
48 |
|
49 |
|
50 |
|
51 function testInt(ctor, shift, scale, disp) { |
|
52 var ab = new ArrayBuffer(4096); |
|
53 var arr = new ctor(ab); |
|
54 for (var i = 0; i < arr.length; i++) |
|
55 arr[i] = i; |
|
56 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.' + ctor.name + '(b); function f(i) {i=i|0; return arr[((i<<' + scale + ')+' + disp + ')>>' + shift + ']|0 } return f'), this, null, ab); |
|
57 for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) |
|
58 assertEq(f(i), arr[((i<<scale)+disp)>>shift]|0); |
|
59 |
|
60 for (var i of [-Math.pow(2,28),Math.pow(2,28),-Math.pow(2,29),Math.pow(2,29),-Math.pow(2,30),Math.pow(2,30),-Math.pow(2,31),Math.pow(2,31),-Math.pow(2,32),Math.pow(2,32)]) { |
|
61 for (var j of [-8,-4,-1,0,1,4,8]) |
|
62 assertEq(f(i+j), arr[(((i+j)<<scale)+disp)>>shift]|0); |
|
63 } |
|
64 |
|
65 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.' + ctor.name + '(b); function f(i,j) {i=i|0;j=j|0; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = j } return f'), this, null, ab); |
|
66 for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) { |
|
67 var index = ((i<<scale)+disp)>>shift; |
|
68 var v = arr[index]|0; |
|
69 arr[index] = 0; |
|
70 f(i, v); |
|
71 assertEq(arr[index]|0, v); |
|
72 } |
|
73 |
|
74 for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { |
|
75 for (var j of [-8,-4,-1,0,1,4,8]) { |
|
76 var index = (((i+j)<<scale)+disp)>>shift; |
|
77 var v = arr[index]|0; |
|
78 arr[index] = 0; |
|
79 f(i+j, v); |
|
80 assertEq(arr[index]|0, v); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 function testFloat(ctor, shift, scale, disp, coercion) { |
|
86 var ab = new ArrayBuffer(4096); |
|
87 var arr = new ctor(ab); |
|
88 for (var i = 0; i < arr.length; i++) |
|
89 arr[i] = i; |
|
90 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.' + ctor.name + '(b); var toF = glob.Math.fround; function f(i) {i=i|0; return ' + coercion + '(arr[((i<<' + scale + ')+' + disp + ')>>' + shift + ']) } return f'), this, null, ab); |
|
91 for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) |
|
92 assertEq(f(i), +arr[((i<<scale)+disp)>>shift]); |
|
93 |
|
94 for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { |
|
95 for (var j of [-8,-4,-1,0,1,4,8]) |
|
96 assertEq(f(i+j), +arr[(((i+j)<<scale)+disp)>>shift]); |
|
97 } |
|
98 |
|
99 var f = asmLink(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.' + ctor.name + '(b); var toF = glob.Math.fround; function f(i,j) {i=i|0;j=+j; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = j } return f'), this, null, ab); |
|
100 for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) { |
|
101 var index = ((i<<scale)+disp)>>shift; |
|
102 var v = +arr[index]; |
|
103 arr[index] = 0; |
|
104 f(i, v); |
|
105 assertEq(+arr[index], v); |
|
106 } |
|
107 |
|
108 for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { |
|
109 for (var j of [-8,-4,-1,0,1,4,8]) { |
|
110 var index = (((i+j)<<scale)+disp)>>shift; |
|
111 var v = +arr[index]; |
|
112 arr[index] = 0; |
|
113 f(i+j, v); |
|
114 assertEq(+arr[index], v); |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 function testFloat32(ctor, shift, scale, disp) { |
|
120 testFloat(ctor, shift, scale, disp, "toF"); |
|
121 } |
|
122 function testFloat64(ctor, shift, scale, disp) { |
|
123 testFloat(ctor, shift, scale, disp, "+"); |
|
124 } |
|
125 |
|
126 function test(tester, ctor, shift) { |
|
127 for (scale of [0,1,2,3]) { |
|
128 for (disp of [0,1,8,Math.pow(2,31)-1,Math.pow(2,31),Math.pow(2,32)-1]) |
|
129 tester(ctor, shift, scale, disp); |
|
130 } |
|
131 } |
|
132 |
|
133 test(testInt, Int8Array, 0); |
|
134 test(testInt, Uint8Array, 0); |
|
135 test(testInt, Int16Array, 1); |
|
136 test(testInt, Uint16Array, 1); |
|
137 test(testInt, Int32Array, 2); |
|
138 test(testInt, Uint32Array, 2); |
|
139 test(testFloat32, Float32Array, 2); |
|
140 test(testFloat64, Float64Array, 3); |