diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index cec11cb..1b95cb0 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2467,8 +2467,8 @@ impl Em { "v_fract_f64" => format!("fract_d({})", f64v(0)), "v_rndne_f64" => format!("round_ne_d({})", f64v(0)), "v_frexp_mant_f64" => format!("frexp_mant_d({})", f64v(0)), - "v_sqrt_f64" => format!("sqrt({})", f64v(0)), - "v_rsq_f64" => format!("(1.0 / sqrt({}))", f64v(0)), + "v_sqrt_f64" => format!("sqrt_d({})", f64v(0)), + "v_rsq_f64" => format!("((double)1 / sqrt_d({}))", f64v(0)), "v_trunc_f64" => format!("(double)(int64_t){}", f64v(0)), "v_ldexp_f64" => format!("({} * (double)exp2((float)(int){}))", f64v(0), lo_hi[1].0), "v_cvt_f32_f64" => { @@ -4382,7 +4382,16 @@ int frexp_exp_d(double x) { return (int)top - 1073; } double floor_d(double x) { double t = (double)(int64_t)x; return t > x ? t - 1.0 : t; } -double ceil_d(double x) { return (isinf(x) || isnan(x)) ? x : -floor_d(-x); } +// no f64 negation here: -floor_d(-x) gave -0.0 for 1.5625 on an AMD driver +// HLSL has no f64 sqrt (it computes in f32): the f32 root refined by two Newton steps in f64; +// zero, negative, inf, NaN and values outside the f32 range keep the f32 result +double sqrt_d(double x) { + double y = (double)sqrt((float)x); + if (!(y > 0.0) || isinf(y)) return y; + y = y + (x - y * y) / (y + y); + return y + (x - y * y) / (y + y); +} +double ceil_d(double x) { if (isinf(x) || isnan(x) || abs(x) >= 4503599627370496.0) return x; double t = (double)(int64_t)x; return t < x ? t + 1.0 : t; } double fract_d(double x) { return (isinf(x) || isnan(x)) ? x - x : min(x - floor_d(x), asdouble(0xFFFFFFFFu, 0x3FEFFFFFu)); } double round_ne_d(double x) { uint l, h; asuint(x, l, h); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 91588a9..f938e01 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -202,6 +202,8 @@ fn gpu_v_swaprel_b32() { ("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 32)], None), // exec = s[32:33]: all lanes ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 129)], None), // m0 = 1 ("V_SWAPREL_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", V + 6)], None), // v7 <-> v7: no-op + // relative targets lie within the program's VGPRs (as in gpu_v_movrel): v15 used directly + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 15), ("SRC0", V + 15)], None), ]); let mut st = KernelState::default(); st.sgpr[30] = 0x5555_5555; @@ -293,7 +295,9 @@ fn unary_f64_math_vop1_and_vop3() { assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); assert_eq!(t.hlsl.matches("ceil_d(").count(), 3, "{}", t.hlsl); // helper plus two calls assert_eq!(t.hlsl.matches("fract_d(").count(), 3, "{}", t.hlsl); // helper plus two calls - assert_eq!(t.hlsl.matches("1.0 / sqrt(").count(), 2, "{}", t.hlsl); + // f64 roots through sqrt_d (HLSL's sqrt is f32): V_SQRT / V_RSQ, twice each + assert_eq!(t.hlsl.matches("sqrt_d(").count(), 1 + 4, "{}", t.hlsl); // helper plus four calls + assert_eq!(t.hlsl.matches("(double)1 / sqrt_d(").count(), 2, "{}", t.hlsl); if super::dxc::location().is_ok() { super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); }