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