float16-as-float32-specialization-for-float16array.js (1717B)
1 function float32(f16, i) { 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[0] = y; 13 } 14 15 function float64(f16, i) { 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[0] = 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 float32(f16, i); 67 let x = u16[0]; 68 69 float32_baseline(f16, i); 70 assertEq(x, u16[0]); 71 72 float64(f16, i); 73 let y = u16[0]; 74 75 float64_baseline(f16, i); 76 assertEq(y, u16[0]); 77 78 if (x !== y) { 79 n++; 80 } 81 } 82 assertEq(n, 1);