js/src/jit-test/tests/asm.js/testZOOB.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/asm.js/testZOOB.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +load(libdir + "asm.js");
     1.5 +
     1.6 +setIonCheckGraphCoherency(false);
     1.7 +setCachingEnabled(false);
     1.8 +
     1.9 +// constants
    1.10 +var buf = new ArrayBuffer(4096);
    1.11 +
    1.12 +// An unshifted literal constant byte index in the range 0 to 2^31-1 inclusive should give a link failure.
    1.13 +assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b);  function f() {return arr[0x7fffffff]|0 } return f'), this, null, buf);
    1.14 +assertAsmLinkFail(asmCompile('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x1fffffff]|0 } return f'), this, null, buf);
    1.15 +
    1.16 +
    1.17 +// An unshifted literal constant byte index outside the range 0 to 2^31-1 inclusive should cause an error compiling.
    1.18 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x20000000]|0 } return f');
    1.19 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x3fffffff]|0 } return f');
    1.20 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x40000000]|0 } return f');
    1.21 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x7fffffff]|0 } return f');
    1.22 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x80000000]|0 } return f');
    1.23 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x8fffffff]|0 } return f');
    1.24 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0xffffffff]|0 } return f');
    1.25 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000]|0 } return f');
    1.26 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b);  function f() {return arr[0x80000000]|0 } return f');
    1.27 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b);  function f() {return arr[0xffffffff]|0 } return f');
    1.28 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b);  function f() {return arr[0x100000000]|0 } return f');
    1.29 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int16Array(b); function f() {return arr[-1]|0 } return f');
    1.30 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[-2]|0 } return f');
    1.31 +
    1.32 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[10-12]|0 } return f');
    1.33 +
    1.34 +// An intish shifted literal constant index should not fail to compile or link.
    1.35 +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);
    1.36 +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);
    1.37 +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);
    1.38 +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);
    1.39 +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);
    1.40 +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);
    1.41 +// Unsigned (intish) folded constant index.
    1.42 +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);
    1.43 +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);
    1.44 +
    1.45 +// A non-intish shifted literal constant index should cause an error compiling.
    1.46 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b); function f() {return arr[0x100000000>>0]|0 } return f');
    1.47 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int32Array(b); function f() {return arr[0x100000000>>2]|0 } return f');
    1.48 +
    1.49 +// Folded non-intish constant expressions should cause an error compiling.
    1.50 +assertAsmTypeFail('glob', 'imp', 'b', USE_ASM + 'var arr=new glob.Int8Array(b);  function f() {return arr[0xffffffff+1]|0 } return f');
    1.51 +
    1.52 +
    1.53 +
    1.54 +function testInt(ctor, shift, scale, disp) {
    1.55 +    var ab = new ArrayBuffer(4096);
    1.56 +    var arr = new ctor(ab);
    1.57 +    for (var i = 0; i < arr.length; i++)
    1.58 +        arr[i] = i;
    1.59 +    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);
    1.60 +    for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097])
    1.61 +        assertEq(f(i), arr[((i<<scale)+disp)>>shift]|0);
    1.62 +
    1.63 +    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)]) {
    1.64 +        for (var j of [-8,-4,-1,0,1,4,8])
    1.65 +            assertEq(f(i+j), arr[(((i+j)<<scale)+disp)>>shift]|0);
    1.66 +    }
    1.67 +
    1.68 +    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);
    1.69 +    for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) {
    1.70 +        var index = ((i<<scale)+disp)>>shift;
    1.71 +        var v = arr[index]|0;
    1.72 +        arr[index] = 0;
    1.73 +        f(i, v);
    1.74 +        assertEq(arr[index]|0, v);
    1.75 +    }
    1.76 +
    1.77 +    for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) {
    1.78 +        for (var j of [-8,-4,-1,0,1,4,8]) {
    1.79 +            var index = (((i+j)<<scale)+disp)>>shift;
    1.80 +            var v = arr[index]|0;
    1.81 +            arr[index] = 0;
    1.82 +            f(i+j, v);
    1.83 +            assertEq(arr[index]|0, v);
    1.84 +        }
    1.85 +    }
    1.86 +}
    1.87 +
    1.88 +function testFloat(ctor, shift, scale, disp, coercion) {
    1.89 +    var ab = new ArrayBuffer(4096);
    1.90 +    var arr = new ctor(ab);
    1.91 +    for (var i = 0; i < arr.length; i++)
    1.92 +        arr[i] = i;
    1.93 +    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);
    1.94 +    for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097])
    1.95 +        assertEq(f(i), +arr[((i<<scale)+disp)>>shift]);
    1.96 +
    1.97 +    for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) {
    1.98 +        for (var j of [-8,-4,-1,0,1,4,8])
    1.99 +            assertEq(f(i+j), +arr[(((i+j)<<scale)+disp)>>shift]);
   1.100 +    }
   1.101 +
   1.102 +    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);
   1.103 +    for (var i of [0,1,2,3,4,1023,1024,1025,4095,4096,4097]) {
   1.104 +        var index = ((i<<scale)+disp)>>shift;
   1.105 +        var v = +arr[index];
   1.106 +        arr[index] = 0;
   1.107 +        f(i, v);
   1.108 +        assertEq(+arr[index], v);
   1.109 +    }
   1.110 +
   1.111 +    for (var i of [-Math.pow(2,31), Math.pow(2,31)-1, Math.pow(2,32)]) {
   1.112 +        for (var j of [-8,-4,-1,0,1,4,8]) {
   1.113 +            var index = (((i+j)<<scale)+disp)>>shift;
   1.114 +            var v = +arr[index];
   1.115 +            arr[index] = 0;
   1.116 +            f(i+j, v);
   1.117 +            assertEq(+arr[index], v);
   1.118 +        }
   1.119 +    }
   1.120 +}
   1.121 +
   1.122 +function testFloat32(ctor, shift, scale, disp) {
   1.123 +    testFloat(ctor, shift, scale, disp, "toF");
   1.124 +}
   1.125 +function testFloat64(ctor, shift, scale, disp) {
   1.126 +    testFloat(ctor, shift, scale, disp, "+");
   1.127 +}
   1.128 +
   1.129 +function test(tester, ctor, shift) {
   1.130 +    for (scale of [0,1,2,3]) {
   1.131 +        for (disp of [0,1,8,Math.pow(2,31)-1,Math.pow(2,31),Math.pow(2,32)-1])
   1.132 +            tester(ctor, shift, scale, disp);
   1.133 +    }
   1.134 +}
   1.135 +
   1.136 +test(testInt, Int8Array, 0);
   1.137 +test(testInt, Uint8Array, 0);
   1.138 +test(testInt, Int16Array, 1);
   1.139 +test(testInt, Uint16Array, 1);
   1.140 +test(testInt, Int32Array, 2);
   1.141 +test(testInt, Uint32Array, 2);
   1.142 +test(testFloat32, Float32Array, 2);
   1.143 +test(testFloat64, Float64Array, 3);

mercurial