tor-browser

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

binaryarith-mod-int32.js (1486B)


      1 setJitCompilerOption("ion.forceinlineCaches", 1);
      2 
      3 function runTest(dividend, divisor) {
      4  function test(dividend, divisor, expected) {
      5    for (var i = 0; i < dividend.length; i++) {
      6      var x = dividend[i];
      7      for (var j = 0; j < divisor.length; j++) {
      8        var y = divisor[j];
      9        var result = x % y;
     10 
     11        assertEq(result, expected[i * divisor.length + j]);
     12      }
     13    }
     14  }
     15 
     16  var f = Function(`return ${test}`)();
     17 
     18  // Seed the IC to ensure we're using Int32Mod.
     19  var ones = Array(8).fill(1);
     20  var zeros = Array(8 * 8).fill(0);
     21  for (var i = 0; i < 2; ++i) {
     22    f(ones, ones, zeros);
     23  }
     24 
     25  var expected = dividend.map(x => divisor.map(y => x % y)).flat();
     26  for (var i = 0; i < 10; ++i) {
     27    f(dividend, divisor, expected);
     28  }
     29 }
     30 
     31 const positiveInt32 = [
     32  1, 2, 3, 4, 5, 6, 7, 8,
     33  2**31 - 8,
     34  2**31 - 7,
     35  2**31 - 6,
     36  2**31 - 5,
     37  2**31 - 4,
     38  2**31 - 3,
     39  2**31 - 2,
     40  2**31 - 1,
     41 ];
     42 
     43 const negativeInt32 = [
     44  -1, -2, -3, -4, -5, -6, -7, -8,
     45  -(2**31 - 8),
     46  -(2**31 - 7),
     47  -(2**31 - 6),
     48  -(2**31 - 5),
     49  -(2**31 - 4),
     50  -(2**31 - 3),
     51  -(2**31 - 2),
     52  -(2**31 - 1),
     53  -(2**31),
     54 ];
     55 
     56 const zero = [0];
     57 
     58 const cases = [positiveInt32, zero, negativeInt32];
     59 
     60 for (let a of cases) {
     61  for (let b of cases) {
     62    runTest(a, b);
     63  }
     64 }
     65 
     66 // Test some "interesting" cases.
     67 
     68 // |-2147483648 % -1| may internally compute |-2147483648 / -1|, which isn't an int32 and
     69 // may trigger a floating point exception on some architectures.
     70 runTest([-2147483648], [-1]);