float16-as-float32-specialization-for-float16array-hole.js (1907B)
1 function float32(f16, i, index) { 2 // Unbox to Int32. 3 i = i|0; 4 5 // Float32 addition. 6 let x = Math.fround(i + 0.1); 7 8 // Float32 square root. 9 let y = Math.fround(Math.sqrt(x)); 10 11 // Store as Float16. 12 f16[index] = y; 13 } 14 15 function float64(f16, i, index) { 16 // Unbox to Int32. 17 i = i|0; 18 19 // Float32 addition. 20 let x = Math.fround(i + 0.1); 21 22 // Float64 square root. 23 let y = Math.sqrt(x); 24 25 // Store as Float16. 26 f16[index] = y; 27 } 28 29 function toBaseline(f) { 30 let source = f.toString(); 31 assertEq(source.at(-1), "}"); 32 33 // Add with-statement to disable Ion compilation. 34 source = source.slice(0, -1) + "; with ({}); }"; 35 36 return Function(`return ${source};`)(); 37 } 38 39 // Different results are expected for these inputs: 40 // 41 // Input Float64-SQRT Float32-SQRT 42 // ----------------------------------- 43 // 1527 39.09375 39.0625 44 // 16464 128.375 128.25 45 // 18581 136.375 136.25 46 // 20826 144.375 144.25 47 // 23199 152.375 152.25 48 // 25700 160.375 160.25 49 // 28329 168.375 168.25 50 // 31086 176.375 176.25 51 // 52 // Limit execution to 1550 to avoid spending too much time on this single test. 53 // 54 // 1550 iterations should still be enough to allow tiering up to Ion, at least 55 // under eager compilation settings. 56 const LIMIT = 1550; 57 58 let float32_baseline = toBaseline(float32); 59 let float64_baseline = toBaseline(float64); 60 61 let f16 = new Float16Array(1); 62 let u16 = new Uint16Array(f16.buffer); 63 64 let n = 0; 65 for (let i = 0; i < LIMIT; ++i) { 66 // Call with out-of-bounds indices to trigger compilation with 67 // MStoreTypedArrayElementHole. 68 float32(f16, i, 100_000); 69 float64(f16, i, 100_000); 70 71 float32(f16, i, 0); 72 let x = u16[0]; 73 74 float32_baseline(f16, i, 0); 75 assertEq(x, u16[0]); 76 77 float64(f16, i, 0); 78 let y = u16[0]; 79 80 float64_baseline(f16, i, 0); 81 assertEq(y, u16[0]); 82 83 if (x !== y) { 84 n++; 85 } 86 } 87 assertEq(n, 1);