tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testZOOB.js (4100B)


      1 // |jit-test| skip-if: !isAsmJSCompilationAvailable()
      2 load(libdir + "asm.js");
      3 load(libdir + "asserts.js");
      4 
      5 // This test runs a lot of code and is very slow with --ion-eager. Use a minimum
      6 // Ion warmup trigger of 5 to avoid timeouts.
      7 if (getJitCompilerOptions()["ion.warmup.trigger"] < 5)
      8    setJitCompilerOption("ion.warmup.trigger", 5);
      9 
     10 var ab = new ArrayBuffer(BUF_MIN);
     11 
     12 // Compute a set of interesting indices.
     13 indices = [0]
     14 for (var i of [4,1024,BUF_MIN,Math.pow(2,30),Math.pow(2,31),Math.pow(2,32),Math.pow(2,33)]) {
     15    for (var j of [-2,-1,0,1,2]) {
     16        for (var k of [1,-1])
     17            indices.push((i+j)*k);
     18    }
     19 }
     20 
     21 function testInt(ctor, shift, scale, disp) {
     22    var arr = new ctor(ab);
     23 
     24    var c = asmCompile('glob', 'imp', 'b',
     25                       USE_ASM +
     26                       'var arr=new glob.' + ctor.name + '(b); ' +
     27                       'function load(i) {i=i|0; return arr[((i<<' + scale + ')+' + disp + ')>>' + shift + ']|0 } ' +
     28                       'function store(i,j) {i=i|0;j=j|0; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = j } ' +
     29                       'function storeZero(i) {i=i|0; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = 0 } ' +
     30                       'function storeNegOne(i) {i=i|0; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = -1 } ' +
     31                       'return { load: load, store: store, storeZero: storeZero, storeNegOne: storeNegOne }');
     32    var f = asmLink(c, this, null, ab);
     33 
     34    var v = arr[0];
     35    arr[0] = -1;
     36    var negOne = arr[0]|0;
     37    arr[0] = v;
     38 
     39    for (var i of indices) {
     40        var index = ((i<<scale)+disp)>>shift;
     41        v = arr[index]|0;
     42 
     43        // Loads
     44        assertEq(f.load(i), v);
     45 
     46        // Stores of immediates
     47        arr[index] = 1;
     48        f.storeZero(i);
     49        assertEq(arr[index]|0, 0);
     50        f.storeNegOne(i);
     51        assertEq(arr[index]|0, index>>>0 < arr.length ? negOne : 0);
     52 
     53        // Stores
     54        arr[index] = ~v;
     55        f.store(i, v);
     56        assertEq(arr[index]|0, v);
     57    }
     58 }
     59 
     60 function testFloat(ctor, shift, scale, disp, coercion) {
     61    var arr = new ctor(ab);
     62 
     63    var c = asmCompile('glob', 'imp', 'b',
     64                       USE_ASM +
     65                       'var arr=new glob.' + ctor.name + '(b); ' +
     66                       'var toF = glob.Math.fround; ' +
     67                       'function load(i) {i=i|0; return ' + coercion + '(arr[((i<<' + scale + ')+' + disp + ')>>' + shift + ']) } ' +
     68                       'function store(i,j) {i=i|0;j=+j; arr[((i<<' + scale + ')+' + disp + ')>>' + shift + '] = j } ' +
     69                       'return { load: load, store: store }');
     70    var f = asmLink(c, this, null, ab);
     71 
     72    for (var i of indices) {
     73        var index = ((i<<scale)+disp)>>shift;
     74        var v = +arr[index];
     75 
     76        // Loads
     77        assertEq(f.load(i), v);
     78 
     79        // Stores
     80        arr[index] = ~v;
     81        f.store(i, v);
     82        assertEq(+arr[index], v);
     83    }
     84 }
     85 
     86 function testFloat32(ctor, shift, scale, disp) {
     87    testFloat(ctor, shift, scale, disp, "toF");
     88 }
     89 function testFloat64(ctor, shift, scale, disp) {
     90    testFloat(ctor, shift, scale, disp, "+");
     91 }
     92 
     93 function assertEqX4(observed, expected) {
     94    assertEq(observed.x, expected.x);
     95    assertEq(observed.y, expected.y);
     96    assertEq(observed.z, expected.z);
     97    assertEq(observed.w, expected.w);
     98 }
     99 
    100 function test(tester, ctor, shift) {
    101    var arr = new ctor(ab);
    102    for (var i = 0; i < arr.length; i++)
    103        arr[i] = Math.imul(i, Math.imul((i & 1), 2) - 1);
    104    for (scale of [0,1,2,3]) {
    105        for (disp of [0,1,2,8,Math.pow(2,30),Math.pow(2,31)-1,Math.pow(2,31),Math.pow(2,32)-1])
    106            tester(ctor, shift, scale, disp);
    107    }
    108    for (var i = 0; i < arr.length; i++) {
    109        var v = arr[i];
    110        arr[i] = Math.imul(i, Math.imul((i & 1), 2) - 1);
    111        assertEq(arr[i], v);
    112    }
    113 }
    114 
    115 test(testInt, Int8Array, 0);
    116 test(testInt, Uint8Array, 0);
    117 test(testInt, Int16Array, 1);
    118 test(testInt, Uint16Array, 1);
    119 test(testInt, Int32Array, 2);
    120 test(testInt, Uint32Array, 2);
    121 test(testFloat32, Float32Array, 2);
    122 test(testFloat64, Float64Array, 3);