Debian Codegraph @

js/src/jit-test/tests/cacheir/bigint-compare-number.js

raw
1 // Same test as bigint-compare-double, except that the inputs are allowed to be any number, i.e.
2 // either Int32 or Double.
3
4 // Test various combinations of (BigInt R Number) and ensures the result is consistent with
5 // (Number R⁻¹ BigInt). This test doesn't aim to cover the overall correctness of (BigInt R Number),
6 // but merely ensures the possible combinations are properly handled in CacheIR.
7
8 var xs = [
9 // Definitely heap digits.
10 -(2n ** 2000n),
11 -(2n ** 1000n),
12
13 // -(2n**64n)
14 -18446744073709551617n,
15 -18446744073709551616n,
16 -18446744073709551615n,
17
18 // -(2n**63n)
19 -9223372036854775809n,
20 -9223372036854775808n,
21 -9223372036854775807n,
22
23 // -(2**32)
24 -4294967297n,
25 -4294967296n,
26 -4294967295n,
27
28 // -(2**31)
29 -2147483649n,
30 -2147483648n,
31 -2147483647n,
32
33 -1n,
34 0n,
35 1n,
36
37 // 2**31
38 2147483647n,
39 2147483648n,
40 2147483649n,
41
42 // 2**32
43 4294967295n,
44 4294967296n,
45 4294967297n,
46
47 // 2n**63n
48 9223372036854775807n,
49 9223372036854775808n,
50 9223372036854775809n,
51
52 // 2n**64n
53 18446744073709551615n,
54 18446744073709551616n,
55 18446744073709551617n,
56
57 // Definitely heap digits.
58 2n ** 1000n,
59 2n ** 2000n,
60 ];
61
62 // Compute the Number approximation of the BigInt values.
63 var ys = xs.map(x => Number(x));
64
65 // Compute if the Number approximation of the BigInt values is exact.
66 // (The larger test values are all powers of two, so we can keep this function simple.)
67 var zs = xs.map(x => {
68 var isNegative = x < 0n;
69 if (isNegative) {
70 x = -x;
71 }
72 var s = x.toString(2);
73 if (s.length <= 53 || (s.length <= 1024 && /^1+0+$/.test(s))) {
74 return 0;
75 }
76 if (s.length <= 1024 && /^1+$/.test(s)) {
77 return isNegative ? -1 : 1;
78 }
79 if (s.length <= 1024 && /^1+0+1$/.test(s)) {
80 return isNegative ? 1 : -1;
81 }
82 return NaN;
83 });
84
85 function testLooseEqual() {
86 for (var i = 0; i < 100; ++i) {
87 var j = i % xs.length;
88 var x = xs[j];
89 var y = ys[j];
90 var z = zs[j];
91
92 assertEq(x == y, z === 0);
93 assertEq(y == x, z === 0);
94 }
95 }
96 testLooseEqual();
97
98 function testLooseNotEqual() {
99 for (var i = 0; i < 100; ++i) {
100 var j = i % xs.length;
101 var x = xs[j];
102 var y = ys[j];
103 var z = zs[j];
104
105 assertEq(x != y, z !== 0);
106 assertEq(y != x, z !== 0);
107 }
108 }
109 testLooseNotEqual();
110
111 function testLessThan() {
112 for (var i = 0; i < 100; ++i) {
113 var j = i % xs.length;
114 var x = xs[j];
115 var y = ys[j];
116 var z = zs[j];
117
118 if (z === 0) {
119 assertEq(x < y, false);
120 assertEq(y < x, false);
121 } else if (z > 0) {
122 assertEq(x < y, true);
123 assertEq(y < x, false);
124 } else if (z < 0) {
125 assertEq(x < y, false);
126 assertEq(y < x, true);
127 } else {
128 assertEq(x < y, y > 0);
129 assertEq(y < x, y < 0);
130 }
131 }
132 }
133 testLessThan();
134
135 function testLessThanEquals() {
136 for (var i = 0; i < 100; ++i) {
137 var j = i % xs.length;
138 var x = xs[j];
139 var y = ys[j];
140 var z = zs[j];
141
142 if (z === 0) {
143 assertEq(x <= y, true);
144 assertEq(y <= x, true);
145 } else if (z > 0) {
146 assertEq(x <= y, true);
147 assertEq(y <= x, false);
148 } else if (z < 0) {
149 assertEq(x <= y, false);
150 assertEq(y <= x, true);
151 } else {
152 assertEq(x <= y, y > 0);
153 assertEq(y <= x, y < 0);
154 }
155 }
156 }
157 testLessThanEquals();
158
159 function testGreaterThan() {
160 for (var i = 0; i < 100; ++i) {
161 var j = i % xs.length;
162 var x = xs[j];
163 var y = ys[j];
164 var z = zs[j];
165
166 if (z === 0) {
167 assertEq(x > y, false);
168 assertEq(y > x, false);
169 } else if (z > 0) {
170 assertEq(x > y, false);
171 assertEq(y > x, true);
172 } else if (z < 0) {
173 assertEq(x > y, true);
174 assertEq(y > x, false);
175 } else {
176 assertEq(x > y, y < 0);
177 assertEq(y > x, y > 0);
178 }
179 }
180 }
181 testGreaterThan();
182
183 function testGreaterThanEquals() {
184 for (var i = 0; i < 100; ++i) {
185 var j = i % xs.length;
186 var x = xs[j];
187 var y = ys[j];
188 var z = zs[j];
189
190 if (z === 0) {
191 assertEq(x >= y, true);
192 assertEq(y >= x, true);
193 } else if (z > 0) {
194 assertEq(x >= y, false);
195 assertEq(y >= x, true);
196 } else if (z < 0) {
197 assertEq(x >= y, true);
198 assertEq(y >= x, false);
199 } else {
200 assertEq(x >= y, y < 0);
201 assertEq(y >= x, y > 0);
202 }
203 }
204 }
205 testGreaterThanEquals();