pow-constant-power.js (1685B)
1 // Ion provides specialisations when Math.pow() resp. the **-operator is used 2 // with a constant power of one of [-0.5, 0.5, 1, 2, 3, 4]. 3 4 function test(x, y, z) { 5 function pow(x, y) { return `Math.pow(${x}, ${y})` }; 6 function exp(x, y) { return `((${x}) ** ${y})` }; 7 8 function make(fn, x, y, z) { 9 return Function(` 10 // Load from array to prevent constant-folding. 11 // (Ion is currently not smart enough to realise that both array 12 // values are the same.) 13 var xs = [${x}, ${x}]; 14 var zs = [${z}, ${z}]; 15 for (var i = 0; i < 200; ++i) { 16 assertEq(${fn("xs[i & 1]", y)}, zs[i & 1]); 17 } 18 `); 19 } 20 21 function double(v) { 22 // NB: numberToDouble() always returns a double value. 23 return `numberToDouble(${v})`; 24 } 25 26 function addTests(fn) { 27 tests.push(make(fn, x, y, z)); 28 tests.push(make(fn, x, double(y), z)); 29 tests.push(make(fn, double(x), y, z)); 30 tests.push(make(fn, double(x), double(y), z)); 31 } 32 33 var tests = []; 34 addTests(pow); 35 addTests(exp); 36 37 for (var i = 0; i < tests.length; ++i) { 38 for (var j = 0; j < 2; ++j) { 39 tests[i](); 40 } 41 } 42 } 43 44 // Make sure the tests below test int32 and double return values. 45 46 // Math.pow(x, -0.5) 47 test( 1, -0.5, 1); 48 test(16, -0.5, 0.25); 49 50 // Math.pow(x, 0.5) 51 test(16, 0.5, 4); 52 test( 2, 0.5, Math.SQRT2); 53 54 // Math.pow(x, 1) 55 test(5, 1, 5); 56 test(0.5, 1, 0.5); 57 58 // Math.pow(x, 2) 59 test(5, 2, 25); 60 test(0.5, 2, 0.25); 61 62 // Math.pow(x, 3) 63 test(5, 3, 125); 64 test(0.5, 3, 0.125); 65 66 // Math.pow(x, 3) 67 test(5, 4, 625); 68 test(0.5, 4, 0.0625);