Debian Codegraph @

js/src/jit-test/tests/ion/pow-base-power-of-two.js

raw
1 // Lowering provides a specialisation when the base operand is a constant which
2 // is a power of two.
3
4 load(libdir + "math.js");
5
6 function test(x, y, z) {
7 function pow(x, y) { return `Math.pow(${x}, ${y})` };
8 function exp(x, y) { return `((${x}) ** ${y})` };
9
10 function make(fn, x, y, z) {
11 return Function(`
12 // Load from array to prevent constant-folding.
13 // (Ion is currently not smart enough to realise that both array
14 // values are the same.)
15 var ys = [${y}, ${y}];
16 var zs = [${z}, ${z}];
17 for (var i = 0; i < 1000; ++i) {
18 assertNear(${fn(x, "ys[i & 1]")}, zs[i & 1]);
19 }
20 `);
21 }
22
23 function double(v) {
24 // NB: numberToDouble() always returns a double value.
25 return `numberToDouble(${v})`;
26 }
27
28 function addTests(fn) {
29 tests.push(make(fn, x, y, z));
30 tests.push(make(fn, x, double(y), z));
31 tests.push(make(fn, double(x), y, z));
32 tests.push(make(fn, double(x), double(y), z));
33 }
34
35 var tests = [];
36 addTests(pow);
37 addTests(exp);
38
39 for (var i = 0; i < tests.length; ++i) {
40 for (var j = 0; j < 2; ++j) {
41 tests[i]();
42 }
43 }
44 }
45
46 function* range(a, b, fn) {
47 for (var i = a; i <= b; ++i) {
48 yield fn(i);
49 }
50 }
51
52 // Only 2^i with |i| ∈ {1..8} currently triggers the optimisation, but also test
53 // the next power-of-two values, 1 and 0, and negative base-of-two values.
54 var values = [
55 ...range(1, 10, i => 2 ** i),
56 1,
57 0,
58 ...range(1, 4, i => -(2 ** i)),
59 ];
60
61 for (var x of values) {
62 test(x, 0, 1);
63 test(x, 1, x);
64 test(x, 2, x * x);
65
66 // 0**(negative) is Infinity, 1**(negative) is 1.
67 if (Math.abs(x) > 1) {
68 test(x, -1076, 0);
69 }
70
71 // (negative)**(odd-negative) is -0.
72 if (x > 1) {
73 test(x, -1075, 0);
74 }
75 }