Debian Codegraph @

js/src/jit-test/tests/atomics/bigint-compareExchange.js

raw
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 testCompareExchange() {
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 // Comparison fails.
70 assertEq(Atomics.compareExchange(int64, 0, 1n, value), 0n);
71 assertEq(int64[0], 0n);
72
73 assertEq(Atomics.compareExchange(uint64, 0, 1n, value), 0n);
74 assertEq(uint64[0], 0n);
75
76 // Comparison succeeds, the new value is written into the memory location.
77 assertEq(Atomics.compareExchange(int64, 0, 0n, value), 0n);
78 assertEq(int64[0], BigInt.asIntN(64, value));
79
80 assertEq(Atomics.compareExchange(uint64, 0, 0n, value), 0n);
81 assertEq(uint64[0], BigInt.asUintN(64, value));
82
83 // Comparison succeeds, the memory location is reset.
84 assertEq(Atomics.compareExchange(int64, 0, value, 0n), BigInt.asIntN(64, value));
85 assertEq(int64[0], 0n);
86
87 assertEq(Atomics.compareExchange(uint64, 0, value, 0n), BigInt.asUintN(64, value));
88 assertEq(uint64[0], 0n);
89 }
90 }
91
92 // Test with variable index.
93 for (let i = 0; i < 20; ++i) {
94 for (let j = 0; j < bigIntValues.length; ++j) {
95 let value = bigIntValues[j];
96 let idx = j & 1;
97
98 // Comparison fails.
99 assertEq(Atomics.compareExchange(int64, idx, 1n, value), 0n);
100 assertEq(int64[idx], 0n);
101
102 assertEq(Atomics.compareExchange(uint64,idx, 1n, value), 0n);
103 assertEq(uint64[idx], 0n);
104
105 // Comparison succeeds, the new value is written into the memory location.
106 assertEq(Atomics.compareExchange(int64, idx, 0n, value), 0n);
107 assertEq(int64[idx], BigInt.asIntN(64, value));
108
109 assertEq(Atomics.compareExchange(uint64, idx, 0n, value), 0n);
110 assertEq(uint64[idx], BigInt.asUintN(64, value));
111
112 // Comparison succeeds, the memory location is reset.
113 assertEq(Atomics.compareExchange(int64, idx, value, 0n), BigInt.asIntN(64, value));
114 assertEq(int64[idx], 0n);
115
116 assertEq(Atomics.compareExchange(uint64, idx, value, 0n), BigInt.asUintN(64, value));
117 assertEq(uint64[idx], 0n);
118 }
119 }
120 }
121 for (let i = 0; i < 2; ++i) testCompareExchange();