michael@0: load(libdir + "asm.js"); michael@0: michael@0: setIonCheckGraphCoherency(false); michael@0: setCachingEnabled(false); michael@0: michael@0: // constants michael@0: var buf = new ArrayBuffer(4096); michael@0: michael@0: // An unshifted literal constant byte index in the range 0 to 2^31-1 inclusive should give a link failure. michael@0: assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x7fffffff]|0 } return f'), this, null, buf); michael@0: assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x1fffffff]|0 } return f'), this, null, buf); michael@0: michael@0: michael@0: // An unshifted literal constant byte index outside the range 0 to 2^31-1 inclusive should cause an error compiling. michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x20000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x3fffffff]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x40000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x7fffffff]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x80000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x8fffffff]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0xffffffff]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x80000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x100000000]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int16Array(b); function f() {return arr[-1]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[-2]|0 } return f'); michael@0: michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[10-12]|0 } return f'); michael@0: michael@0: // An intish shifted literal constant index should not fail to compile or link. michael@0: 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); michael@0: 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); michael@0: 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); michael@0: 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); michael@0: 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); michael@0: 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); michael@0: // Unsigned (intish) folded constant index. michael@0: 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); michael@0: 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); michael@0: michael@0: // A non-intish shifted literal constant index should cause an error compiling. michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x100000000>>0]|0 } return f'); michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000>>2]|0 } return f'); michael@0: michael@0: // Folded non-intish constant expressions should cause an error compiling. michael@0: assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0xffffffff+1]|0 } return f'); michael@0: michael@0: michael@0: michael@0: function testInt(ctor, shift, scale, disp) { michael@0: var ab = new ArrayBuffer(4096); michael@0: var arr = new ctor(ab); michael@0: for (var i = 0; i < arr.length; i++) michael@0: arr[i] = i; michael@0: 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); michael@0: for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) michael@0: assertEq(f(i), arr[((i<>shift]|0); michael@0: michael@0: 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)]) { michael@0: for (var j of [-8,-4,-1,0,1,4,8]) michael@0: assertEq(f(i+j), arr[(((i+j)<>shift]|0); michael@0: } michael@0: michael@0: 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); michael@0: for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) { michael@0: var index = ((i<>shift; michael@0: var v = arr[index]|0; michael@0: arr[index] = 0; michael@0: f(i, v); michael@0: assertEq(arr[index]|0, v); michael@0: } michael@0: michael@0: for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { michael@0: for (var j of [-8,-4,-1,0,1,4,8]) { michael@0: var index = (((i+j)<>shift; michael@0: var v = arr[index]|0; michael@0: arr[index] = 0; michael@0: f(i+j, v); michael@0: assertEq(arr[index]|0, v); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function testFloat(ctor, shift, scale, disp, coercion) { michael@0: var ab = new ArrayBuffer(4096); michael@0: var arr = new ctor(ab); michael@0: for (var i = 0; i < arr.length; i++) michael@0: arr[i] = i; michael@0: 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); michael@0: for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) michael@0: assertEq(f(i), +arr[((i<>shift]); michael@0: michael@0: for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { michael@0: for (var j of [-8,-4,-1,0,1,4,8]) michael@0: assertEq(f(i+j), +arr[(((i+j)<>shift]); michael@0: } michael@0: michael@0: 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); michael@0: for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) { michael@0: var index = ((i<>shift; michael@0: var v = +arr[index]; michael@0: arr[index] = 0; michael@0: f(i, v); michael@0: assertEq(+arr[index], v); michael@0: } michael@0: michael@0: for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) { michael@0: for (var j of [-8,-4,-1,0,1,4,8]) { michael@0: var index = (((i+j)<>shift; michael@0: var v = +arr[index]; michael@0: arr[index] = 0; michael@0: f(i+j, v); michael@0: assertEq(+arr[index], v); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function testFloat32(ctor, shift, scale, disp) { michael@0: testFloat(ctor, shift, scale, disp, "toF"); michael@0: } michael@0: function testFloat64(ctor, shift, scale, disp) { michael@0: testFloat(ctor, shift, scale, disp, "+"); michael@0: } michael@0: michael@0: function test(tester, ctor, shift) { michael@0: for (scale of [0,1,2,3]) { michael@0: for (disp of [0,1,8,Math.pow(2,31)-1,Math.pow(2,31),Math.pow(2,32)-1]) michael@0: tester(ctor, shift, scale, disp); michael@0: } michael@0: } michael@0: michael@0: test(testInt, Int8Array, 0); michael@0: test(testInt, Uint8Array, 0); michael@0: test(testInt, Int16Array, 1); michael@0: test(testInt, Uint16Array, 1); michael@0: test(testInt, Int32Array, 2); michael@0: test(testInt, Uint32Array, 2); michael@0: test(testFloat32, Float32Array, 2); michael@0: test(testFloat64, Float64Array, 3);