tor-browser

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

bigint-exchange.js (2299B)


      1 // |jit-test| test-join=--spectre-mitigations=off
      2 
      3 // These do not test atomicity, just that code generation for BigInt values
      4 // works correctly.
      5 
      6 const bigIntValues = [
      7  // Definitely heap digits.
      8  -(2n ** 2000n),
      9  -(2n ** 1000n),
     10 
     11  // -(2n**64n)
     12  -18446744073709551617n,
     13  -18446744073709551616n,
     14  -18446744073709551615n,
     15 
     16  // -(2n**63n)
     17  -9223372036854775809n,
     18  -9223372036854775808n,
     19  -9223372036854775807n,
     20 
     21  // -(2**32)
     22  -4294967297n,
     23  -4294967296n,
     24  -4294967295n,
     25 
     26  // -(2**31)
     27  -2147483649n,
     28  -2147483648n,
     29  -2147483647n,
     30 
     31  -1n,
     32  0n,
     33  1n,
     34 
     35  // 2**31
     36  2147483647n,
     37  2147483648n,
     38  2147483649n,
     39 
     40  // 2**32
     41  4294967295n,
     42  4294967296n,
     43  4294967297n,
     44 
     45  // 2n**63n
     46  9223372036854775807n,
     47  9223372036854775808n,
     48  9223372036854775809n,
     49 
     50  // 2n**64n
     51  18446744073709551615n,
     52  18446744073709551616n,
     53  18446744073709551617n,
     54 
     55  // Definitely heap digits.
     56  2n ** 1000n,
     57  2n ** 2000n,
     58 ];
     59 
     60 function testExchange() {
     61  const int64 = new BigInt64Array(2);
     62  const uint64 = new BigUint64Array(2);
     63 
     64  // Test with constant index.
     65  for (let i = 0; i < 20; ++i) {
     66    for (let j = 0; j < bigIntValues.length; ++j) {
     67      let value = bigIntValues[j];
     68 
     69      assertEq(Atomics.exchange(int64, 0, value), 0n);
     70      assertEq(int64[0], BigInt.asIntN(64, value));
     71 
     72      assertEq(Atomics.exchange(uint64, 0, value), 0n);
     73      assertEq(uint64[0], BigInt.asUintN(64, value));
     74 
     75      assertEq(Atomics.exchange(int64, 0, 0n), BigInt.asIntN(64, value));
     76      assertEq(int64[0], 0n);
     77 
     78      assertEq(Atomics.exchange(uint64, 0, 0n), BigInt.asUintN(64, value));
     79      assertEq(uint64[0], 0n);
     80    }
     81  }
     82 
     83  // Test with variable index.
     84  for (let i = 0; i < 20; ++i) {
     85    for (let j = 0; j < bigIntValues.length; ++j) {
     86      let value = bigIntValues[j];
     87      let idx = j & 1;
     88 
     89      assertEq(Atomics.exchange(int64, idx, value), 0n);
     90      assertEq(int64[idx], BigInt.asIntN(64, value));
     91 
     92      assertEq(Atomics.exchange(uint64, idx, value), 0n);
     93      assertEq(uint64[idx], BigInt.asUintN(64, value));
     94 
     95      assertEq(Atomics.exchange(int64, idx, 0n), BigInt.asIntN(64, value));
     96      assertEq(int64[idx], 0n);
     97 
     98      assertEq(Atomics.exchange(uint64, idx, 0n), BigInt.asUintN(64, value));
     99      assertEq(uint64[idx], 0n);
    100    }
    101  }
    102 }
    103 for (let i = 0; i < 2; ++i) testExchange();