From 3eaaad8182bffefaae87410b5637d4099008e406 Mon Sep 17 00:00:00 2001 From: acaracargo <58486686+acaracargo@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:36:40 +0300 Subject: [PATCH 1/2] Tests: f32 transcendentals on the GPU against f64 references v_rcp, v_rsq, v_sqrt, v_log, v_exp, v_sin and v_cos (f32) had no GPU test. 26 inputs: zeros of both signs, infinities, NaN, negative inputs, overflow, denormal-free small values and sin / cos arguments up to 255 turns. Special results are compared exactly, the rest within 2^-21 relative (rcp, rsq, sqrt, log, exp) or 2^-10 absolute (sin, cos; D3D allows 8e-4). rsq(-0) is left out: IEEE says -inf, and what the console gives is not settled here. The sin / cos argument is in turns. The translation multiplies it by 6.2831853 before sin(), so the error grows with the number of turns: sin(1 turn) is -1.9e-7 instead of 0, and the worst absolute error over the inputs is 6.8e-5 (sin) and 9.6e-5 (cos), at 200-255 turns. Past about 50 turns the radian argument also leaves D3D's +-100pi accuracy range, so other drivers may do worse. Not changed here. Measured: RTX 4060 (wave32), Windows 11 Pro 26300, Rust 1.100, debug. cargo test --workspace -- --test-threads=4: 678 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware. Worst relative errors: rcp 0, rsq 8.4e-8, sqrt 7.4e-8, log 1.2e-7, exp 1.1e-7. Scaling sin by pi or translating v_log_f32 to log() in emit.rs fails the test. Assisted-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/tests.rs | 66 ++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 596a74198..152904937 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1382,6 +1382,72 @@ fn gpu_retail_vs_blit_gen_amb_cube() { } } +/// The f32 transcendentals against f64 references: special values (zeros, infinities, NaN, +/// negative inputs) exactly, the rest within a tolerance (`TOL`, relative, or absolute for sin / +/// cos, whose argument is in revolutions: sin(2π·x), also for many revolutions inside the +/// ISA's ±256 range). +#[test] +fn gpu_transcendental_f32() { + if !gpu_ready() { + return; + } + let xs: [f32; 26] = [ + 1.0, 2.0, 0.5, 3.0, 1e-3, 1e10, 7.25, 0.0, -0.0, f32::INFINITY, f32::NEG_INFINITY, f32::NAN, -1.0, 0.25, 0.125, + 1.75, -0.375, 10.25, 100.25, 200.25, 255.75, -200.125, 16.0, -16.0, 1e-30, 0.1, + ]; + const OPS: [&str; 7] = ["V_RCP_F32", "V_RSQ_F32", "V_SQRT_F32", "V_LOG_F32", "V_EXP_F32", "V_SIN_F32", "V_COS_F32"]; + let code = program_of(&OPS.iter().enumerate().map(|(i, n)| ins(n, "ENC_VOP1", &[("VDST", 10 + i as u64), ("SRC0", V + 1)], None)).collect::>()); + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = xs[l % xs.len()].to_bits(); + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + let turn = |x: f64, f: fn(f64) -> f64| f(2.0 * std::f64::consts::PI * (x - x.floor())); + let mut bad = Vec::new(); + let mut worst = [0f64; 7]; + for (l, v) in r.vgpr.iter().enumerate().take(xs.len()) { + let x = xs[l] as f64; + for (i, op) in OPS.iter().enumerate() { + let want = match i { + 0 => 1.0 / x, + 1 => 1.0 / x.sqrt(), + 2 => x.sqrt(), + 3 => x.log2(), + 4 => x.exp2(), + 5 => if x.is_finite() { turn(x, f64::sin) } else { f64::NAN }, + _ => if x.is_finite() { turn(x, f64::cos) } else { f64::NAN }, + }; + let got = f32::from_bits(v[10 + i]) as f64; + if i == 1 && xs[l].to_bits() == 0x8000_0000 { + continue; // rsq(-0): -inf by IEEE, not settled for the console + } + if (i >= 5) && x.abs() > 256.0 { + continue; // outside the ISA's sin / cos domain + } + let want = want as f32 as f64; // the f32 result, so an overflow is an infinity + let ok = if want.is_nan() { + got.is_nan() + } else if want.is_infinite() || (want == 0.0 && i < 5) { + got == want + } else { + let err = if i >= 5 { (got - want).abs() } else { ((got - want) / want).abs() }; + worst[i] = worst[i].max(err); + err <= TOL[i] + }; + if !ok { + bad.push(format!("{op}({x:e}) = {got:e} ({:#010x}), want {want:e}", v[10 + i])); + } + } + } + println!("worst error: {:?}", OPS.iter().zip(worst).collect::>()); + assert!(bad.is_empty(), "{}", bad.join("\n")); +} +/// `gpu_transcendental_f32` tolerances: 2^-21 relative (4 ulp) for rcp / rsq / sqrt / log / exp, 2^-10 +/// absolute for sin / cos (D3D allows 8e-4 for sin / cos; the console reduces the argument in +/// revolutions, the translation scales it to radians first, so the error grows with the turns: +/// about 1e-4 at 255 turns on an RTX 4060). +const TOL: [f64; 7] = [4.77e-7, 4.77e-7, 4.77e-7, 4.77e-7, 4.77e-7, 9.77e-4, 9.77e-4]; + #[test] fn gpu_sdwa_dpp_and_cross_lane() { if !gpu_ready() { From 232ee73f36c7b8fa0c4e9fe30c96e29a4b3063f0 Mon Sep 17 00:00:00 2001 From: acaracargo <58486686+acaracargo@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:39:40 +0300 Subject: [PATCH 2/2] Tests: f16 transcendentals on the GPU against f64 references v_rcp, v_rsq, v_sqrt, v_log, v_exp, v_sin and v_cos (f16) on 25 binary16 inputs (zeros, infinities, NaN, negative inputs, the smallest normal, the largest finite value, sin / cos arguments up to 100 turns), with another value in the high half of the source. D[15:0] is compared with the f64 result: special results exactly, the rest within 2^-9 relative or 2^-8 absolute for sin / cos. Results past the binary16 range are left out: in the default mode the f16 result goes through the legacy f32tof16, which rounds toward zero, so v_exp_f16(16) gives 65504 (0x7bff) where round-to-nearest gives +inf. That is the default-mode rounding reported in #111 for add / sub / fma; it affects these instructions too. The worst errors measured (rcp 8.2e-4 relative, about 1.6 ulp) come from the same truncation. Measured: RTX 4060 (wave32), Windows 11 Pro 26300, Rust 1.100, debug. cargo test --workspace -- --test-threads=4: 679 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware. Translating v_exp_f16 to exp() or v_cos_f16 without the 2pi scale in emit.rs fails the test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WsgnReN2iZK9RUNckZAhrv --- crates/xcl/src/gpu/recomp/tests.rs | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 152904937..d70acb33e 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1442,6 +1442,70 @@ fn gpu_transcendental_f32() { println!("worst error: {:?}", OPS.iter().zip(worst).collect::>()); assert!(bad.is_empty(), "{}", bad.join("\n")); } +/// The f16 transcendentals the same way as `gpu_transcendental_f32`, on binary16 inputs (with +/// another value in the high half of the source), D[15:0] against the f64 result: 2^-9 relative +/// (2 ulp), or 2^-8 absolute for sin / cos. Results past the binary16 range are left out (#111). +#[test] +fn gpu_transcendental_f16() { + if !gpu_ready() { + return; + } + let xs: [u16; 25] = [ + 0x3C00, 0x4000, 0x3800, 0x4200, 0x1419, 0x7BFF, 0x4740, 0x0000, 0x8000, 0x7C00, 0xFC00, 0x7E00, 0xBC00, 0x3400, + 0x3000, 0x3F00, 0xB600, 0x4920, 0x5644, 0x4C00, 0xCC00, 0x2E66, 0x0400, 0x4880, 0x4C40, + ]; + const OPS: [&str; 7] = ["V_RCP_F16", "V_RSQ_F16", "V_SQRT_F16", "V_LOG_F16", "V_EXP_F16", "V_SIN_F16", "V_COS_F16"]; + let code = program_of(&OPS.iter().enumerate().map(|(i, n)| ins(n, "ENC_VOP1", &[("VDST", 10 + i as u64), ("SRC0", V + 1)], None)).collect::>()); + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = 0xBEEF_0000 | xs[l % xs.len()] as u32; + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + let turn = |x: f64, f: fn(f64) -> f64| f(2.0 * std::f64::consts::PI * (x - x.floor())); + let mut bad = Vec::new(); + let mut worst = [0f64; 7]; + for (l, v) in r.vgpr.iter().enumerate().take(xs.len()) { + let x = f16_to_f64(xs[l]); + for (i, op) in OPS.iter().enumerate() { + if i == 1 && xs[l] == 0x8000 { + continue; // rsq(-0), as in gpu_transcendental_f32 + } + let want = match i { + 0 => 1.0 / x, + 1 => 1.0 / x.sqrt(), + 2 => x.sqrt(), + 3 => x.log2(), + 4 => x.exp2(), + 5 => if x.is_finite() { turn(x, f64::sin) } else { f64::NAN }, + _ => if x.is_finite() { turn(x, f64::cos) } else { f64::NAN }, + }; + if i >= 5 && x.abs() > 256.0 { + continue; // outside the ISA's sin / cos domain + } + if x.is_finite() && want.abs() >= 65520.0 { + // an overflow is +-inf in the default round-to-nearest mode, but the default f16 + // conversion (legacy f32tof16) rounds toward zero and gives 65504 (#111) + continue; + } + let got = f16_to_f64(v[10 + i] as u16); + let ok = if want.is_nan() { + got.is_nan() + } else if want.is_infinite() || (want == 0.0 && i < 5) { + got == want + } else { + let err = if i >= 5 { (got - want).abs() } else { ((got - want) / want).abs() }; + worst[i] = worst[i].max(err); + err <= if i >= 5 { 3.91e-3 } else { 1.96e-3 } + }; + if !ok { + bad.push(format!("{op}({x:e}) = {got:e} ({:#06x}), want {want:e}", v[10 + i] & 0xFFFF)); + } + } + } + println!("worst error: {:?}", OPS.iter().zip(worst).collect::>()); + assert!(bad.is_empty(), "{}", bad.join("\n")); +} + /// `gpu_transcendental_f32` tolerances: 2^-21 relative (4 ulp) for rcp / rsq / sqrt / log / exp, 2^-10 /// absolute for sin / cos (D3D allows 8e-4 for sin / cos; the console reduces the argument in /// revolutions, the translation scales it to radians first, so the error grows with the turns: