Explorar el Código

shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU tests

Fixes a real-hardware NVIDIA bug in fract() from 36eb0b4 and adds synthetic GFX10 -> DXIL -> GPU/WARP regression tests for the RDNA2 VALU lowerings from #2:

- fract(): select(OGT(r, k), k, r) is folded by NVIDIA's DXIL compiler into FMNMX (minNum), which swallows NaN and turns v_fract_f32(NaN) into 0x3f7fffff (and v_fract_f16(NaN) into 0x3bff). Comparing with pred::OGE against a distinct threshold (1.0 for f32, 1 - 2^-12 for f16) prevents the minNum fold and preserves NaN and +/-inf -> NaN on both NVIDIA hardware and WARP.
- valu_tests.rs: builds minimal synthetic GFX10 compute shaders, lowers them through the full pipeline (ir::build -> resource::track -> io::raise -> structure -> lower), validates with dxil.dll when present, and executes them via Host::dispatch on both hardware GPU and WARP without requiring console dxc. Covers v_fract_f32/f16, v_med3_f32/f16, v_sin/v_cos_f32/f16, and v_bfe_i32/u32 + s_bfe_i32.

Co-authored-by: BlueKaiman <[email protected]>
alessiodeia hace 3 días
padre
commit
2e5001cf9d

+ 9 - 6
crates/victory-shader/src/emit/lower.rs

@@ -1409,12 +1409,15 @@ impl<'a> Lower<'a> {
     /// `a - floor(a)`, which on the hardware never reaches 1.0: where it
     /// would round to it (a tiny negative `a`), the result is `below_one`,
     /// the largest value under 1.0 in the operation's width. Hosts differ
-    /// here (some give 1.0). NaN stays NaN.
-    fn fract(&mut self, a: V, below_one: f32) -> V {
+    /// here (some give 1.0). Comparing against a `threshold` distinct from
+    /// `below_one` prevents drivers (observed on NVIDIA) from folding
+    /// `r > k ? k : r` into `minNum` (`FMNMX`) and swallowing NaN.
+    fn fract(&mut self, a: V, threshold: f32, below_one: f32) -> V {
         let c = self.cur;
         let r = self.unary(22, a);
+        let limit = self.s.m.f32(threshold.to_bits());
         let k = self.s.m.f32(below_one.to_bits());
-        let over = self.s.m.cmp(c, pred::OGT, r, k);
+        let over = self.s.m.cmp(c, pred::OGE, r, limit);
         self.s.m.select(c, over, k, r)
     }
 
@@ -3711,7 +3714,7 @@ impl<'a> Lower<'a> {
                 };
                 let r = match op {
                     12 | 13 => self.trig_turns(a, op == 12),
-                    22 => self.fract(a, f32::from_bits(0x3F7F_FFFF)),
+                    22 => self.fract(a, 1.0, f32::from_bits(0x3F7F_FFFF)),
                     _ => self.unary(op, a),
                 };
                 float(self, r)
@@ -4312,8 +4315,8 @@ impl<'a> Lower<'a> {
                     };
                     match op {
                         12 | 13 => self.trig_turns(a, op == 12),
-                        // 0x3bff as a half: 1 - 2^-11.
-                        22 => self.fract(a, 1.0 - 1.0 / 2048.0),
+                        // Any f16 in (-2^-11, 0) has fract >= 1 - 2^-12; clamp to 0x3bff (1 - 2^-11).
+                        22 => self.fract(a, 1.0 - 1.0 / 4096.0, 1.0 - 1.0 / 2048.0),
                         _ => self.unary(op, a),
                     }
                 }

+ 2 - 0
crates/victory-shader/src/emit/mod.rs

@@ -86,6 +86,8 @@ mod sampler;
 mod shader;
 #[cfg(windows)]
 pub mod validator;
+#[cfg(all(test, windows))]
+mod valu_tests;
 mod wave;
 
 use crate::dxbc::{Container, FourCC};

+ 675 - 0
crates/victory-shader/src/emit/valu_tests.rs

@@ -0,0 +1,675 @@
+//! End-to-end tests of synthetic GFX10 VALU shaders lowered to DXIL, validated
+//! by `dxil.dll` when present, and executed on the host GPU and WARP.
+//!
+//! Covers RDNA2 hardware semantics that differ from naive DXIL lowering:
+//! `v_med3_f32`/`v_med3_f16` with NaN operands, `v_fract_f32`/`v_fract_f16`
+//! near `1.0`, and turn-range reduction for `v_sin_*`/`v_cos_*`.
+
+use super::host::{Dispatch, Host, Root, View};
+use super::{HostShader, lower, validator};
+use crate::native::{
+    ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
+};
+use crate::{io, ir, resource, structure};
+
+const S_ENDPGM: u32 = 0xBF81_0000;
+
+/// SOP1 `s_mov_b32 sdst, src0`.
+const fn s_mov_b32(sdst: u8, src0: u8) -> u32 {
+    0xBE80_0000 | (sdst as u32) << 16 | 0x03 << 8 | src0 as u32
+}
+
+/// SOP2 `op sdst, src0, src1`.
+const fn sop2(op: u8, sdst: u8, src0: u8, src1: u8) -> u32 {
+    0x8000_0000
+        | (op as u32 & 0x7F) << 23
+        | (sdst as u32 & 0x7F) << 16
+        | (src1 as u32) << 8
+        | src0 as u32
+}
+
+/// SMEM `s_load_dwordx4 s[dst:dst+3], s[base:base+1], 0` (soffset = null).
+const fn s_load_dwordx4(dst: u8, base: u8) -> [u32; 2] {
+    [
+        0x3D << 26 | 0x02 << 18 | (dst as u32) << 6 | (base as u32 >> 1),
+        125 << 25,
+    ]
+}
+
+/// VOP1 `op vdst, src0` (`src0`: 0..105 = SGPR, 256..511 = VGPR).
+const fn vop1(op: u16, vdst: u8, src0: u16) -> u32 {
+    0x7E00_0000 | (vdst as u32) << 17 | (op as u32) << 9 | (src0 as u32 & 0x1FF)
+}
+
+/// VOP3 `op vdst, src0, src1, src2` (no modifiers).
+const fn vop3(op: u16, vdst: u8, src0: u16, src1: u16, src2: u16) -> [u32; 2] {
+    [
+        0x35 << 26 | (op as u32 & 0x3FF) << 16 | vdst as u32,
+        (src0 as u32 & 0x1FF) | (src1 as u32 & 0x1FF) << 9 | (src2 as u32 & 0x1FF) << 18,
+    ]
+}
+
+/// MUBUF `buffer_store_dword vdata, off, s[srsrc:srsrc+3], 0 offset:byte_off`.
+const fn buffer_store_dword(vdata: u8, srsrc: u8, byte_off: u16) -> [u32; 2] {
+    [
+        0x38 << 26 | 0x1C << 18 | (byte_off as u32 & 0xFFF),
+        0x80 << 24 | ((srsrc as u32 >> 2) & 0x1F) << 16 | (vdata as u32) << 8,
+    ]
+}
+
+const fn sgpr(n: u8) -> u16 {
+    n as u16
+}
+
+const fn vgpr(n: u8) -> u16 {
+    256 + n as u16
+}
+
+/// Builds and lowers a 1-thread synthetic RDNA2 compute shader that receives
+/// 4 dwords of root constants in `s0..s3`, runs `body` (leaving its result in
+/// `v0`), and stores `v0` to dword 0 of the raw UAV at heap index 0.
+fn build_valu_cs(body: &[u32]) -> HostShader {
+    let mut words = Vec::new();
+    // s[4:5] = descriptor table address (heap index 0 -> byte offset 0).
+    words.push(s_mov_b32(5, 0x80)); // inline constant 0
+    words.extend_from_slice(&s_load_dwordx4(8, 4));
+    // Copy root constants s0..s3 into v1..v4 so VALU instructions operate on
+    // VGPRs just as they do in real shaders.
+    for i in 0..4u8 {
+        words.push(vop1(0x001, i + 1, sgpr(i))); // v_mov_b32
+    }
+    words.extend_from_slice(body);
+    words.extend_from_slice(&buffer_store_dword(0, 8, 0));
+    words.push(S_ENDPGM);
+
+    let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
+    let native = NativeProgram {
+        stage: HwStage::Cs,
+        wave64: false,
+        vgprs: 8,
+        layout: vec![
+            RootParam {
+                root: RootType::Constants,
+                table_offset_dwords: 0,
+                sgprs: 4,
+                base_register: 0,
+                user_data: 0,
+                slot: 0,
+            },
+            RootParam {
+                root: RootType::Table,
+                table_offset_dwords: 0,
+                sgprs: 1,
+                base_register: 0,
+                user_data: 4,
+                slot: 1,
+            },
+        ],
+        used: vec![UsedResource {
+            kind: ResourceType::Uav,
+            root: RootType::Table,
+            table_offset_dwords: 0,
+            lower_bound: 0,
+            stride: 0,
+            dwords: 4,
+            user_data: 4,
+            slot: 1,
+        }],
+        io: StageIo::Cs(ComputeIo {
+            rsrc2: 5 << 1, // 5 user SGPRs: s0..s4
+            threads: [1, 1, 1],
+        }),
+        code,
+    };
+
+    let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
+    let res = resource::track(&p, &native.layout, native.stage).expect("resource::track");
+    let io = io::raise(&p, &native).expect("io::raise");
+    let st = structure::structure(&p).expect("structure");
+    let lowered = lower::lower(&lower::Parts {
+        native: &native,
+        p: &p,
+        st: &st,
+        io: &io,
+        res: &res,
+        inputs: Vec::new(),
+        outputs: Vec::new(),
+        prims: Vec::new(),
+        mesh_stride: 0,
+    })
+    .expect("lower");
+
+    let shader = HostShader {
+        stage: HwStage::Cs,
+        dxil: lowered.dxil,
+        user_data: lowered.user_data,
+        samplers: lowered.samplers,
+        static_samplers: Vec::new(),
+        root_cbvs: lowered.root_cbvs,
+        root_srvs: lowered.root_srvs,
+    };
+
+    if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
+        let mut copy = shader.dxil.clone();
+        let ours = copy[4..20].to_vec();
+        v.validate(&mut copy).expect("DXIL validation failed");
+        assert_eq!(copy[4..20], ours[..], "container hash mismatch");
+    }
+
+    shader
+}
+
+/// Runs `shader` on every available SM 6.6 host (hardware GPU and WARP) for
+/// each 4-dword input set and invokes `check(backend, case_index, inputs, result_dword)`.
+fn run_cases(
+    shader: &HostShader,
+    cases: &[[u32; 4]],
+    mut check: impl FnMut(&str, usize, [u32; 4], u32),
+) {
+    let mut ran = 0;
+    for (label, warp) in [("hardware", false), ("warp", true)] {
+        let Ok(host) = Host::new(warp) else {
+            eprintln!("no SM 6.6 {label} device; skipped");
+            continue;
+        };
+        let (root, pso) = host
+            .compute_pipeline(shader)
+            .unwrap_or_else(|e| panic!("{label}: compute_pipeline failed: {e}"));
+        for (i, &inputs) in cases.iter().enumerate() {
+            let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
+            for u in &shader.user_data {
+                ud[u.index as usize] = match (u.root, u.slot) {
+                    (RootType::Constants, 0) => inputs[u.dword as usize],
+                    (RootType::Table, 1) => 0,
+                    other => panic!("unexpected user data {other:?}"),
+                };
+            }
+            let out = host
+                .dispatch(
+                    &root,
+                    &pso,
+                    &Dispatch {
+                        roots: vec![(0, Root::Constants(ud))],
+                        views: &[View::RawUav(1)],
+                        samplers: &[],
+                        groups: [1, 1, 1],
+                    },
+                )
+                .unwrap_or_else(|e| panic!("{label}: dispatch failed: {e}"));
+            check(label, i, inputs, out[0][0]);
+        }
+        ran += 1;
+    }
+    assert!(ran > 0, "neither hardware nor WARP SM 6.6 device available");
+}
+
+fn is_f16_nan(bits: u16) -> bool {
+    (bits & 0x7FFF) > 0x7C00
+}
+
+fn f16_to_f32(bits: u16) -> f32 {
+    let sign = u32::from(bits >> 15) << 31;
+    let exp = (bits >> 10) & 0x1F;
+    let mant = bits & 0x3FF;
+    let f32_bits = match exp {
+        0 => {
+            if mant == 0 {
+                sign
+            } else {
+                let mut m = u32::from(mant);
+                let mut e = 127 - 14;
+                while (m & 0x400) == 0 {
+                    m <<= 1;
+                    e -= 1;
+                }
+                sign | (e << 23) | ((m & 0x3FF) << 13)
+            }
+        }
+        0x1F => sign | (0xFF << 23) | (u32::from(mant) << 13),
+        _ => sign | ((u32::from(exp) + 112) << 23) | (u32::from(mant) << 13),
+    };
+    f32::from_bits(f32_bits)
+}
+
+#[test]
+fn v_med3_f32_matches_rdna2_with_nan() {
+    // v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)
+    let shader = build_valu_cs(&vop3(0x157, 0, vgpr(1), vgpr(2), vgpr(3)));
+    let nan = f32::NAN.to_bits();
+    let f = f32::to_bits;
+
+    let cases: &[([u32; 4], Option<f32>)] = &[
+        // Normal medians (all orderings).
+        ([f(1.0), f(2.0), f(3.0), 0], Some(2.0)),
+        ([f(3.0), f(1.0), f(2.0), 0], Some(2.0)),
+        ([f(-5.0), f(0.0), f(5.0), 0], Some(0.0)),
+        ([f(1.0), f(1.0), f(2.0), 0], Some(1.0)),
+        // One NaN: RDNA2 returns min3 of the non-NaN operands.
+        ([f(1.0), f(2.0), nan, 0], Some(1.0)),
+        ([nan, f(1.0), f(0.0), 0], Some(0.0)),
+        ([nan, f(0.0), f(1.0), 0], Some(0.0)),
+        ([f(2.0), nan, f(1.0), 0], Some(1.0)),
+        ([f(-3.0), nan, f(4.0), 0], Some(-3.0)),
+        // Two NaNs: returns the sole non-NaN operand.
+        ([nan, f(5.0), nan, 0], Some(5.0)),
+        ([nan, nan, f(-2.0), 0], Some(-2.0)),
+        ([f(7.0), nan, nan, 0], Some(7.0)),
+        // Three NaNs: returns NaN.
+        ([nan, nan, nan, 0], None),
+    ];
+
+    let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
+    run_cases(&shader, &inputs, |backend, i, inp, got| {
+        let expect = cases[i].1;
+        let got_f = f32::from_bits(got);
+        match expect {
+            Some(want) => assert_eq!(
+                got_f,
+                want,
+                "{backend}: v_med3_f32({:?}, {:?}, {:?}) = {got_f} ({got:#010x}), want {want}",
+                f32::from_bits(inp[0]),
+                f32::from_bits(inp[1]),
+                f32::from_bits(inp[2]),
+            ),
+            None => assert!(
+                got_f.is_nan(),
+                "{backend}: v_med3_f32(NaN, NaN, NaN) = {got_f} ({got:#010x}), want NaN",
+            ),
+        }
+    });
+}
+
+#[test]
+fn v_med3_f16_matches_rdna2_with_nan() {
+    // v_med3_f16 v0, v1, v2, v3 (VOP3 opcode 0x357)
+    let shader = build_valu_cs(&vop3(0x357, 0, vgpr(1), vgpr(2), vgpr(3)));
+    let h0 = 0x0000u32; // 0.0
+    let h1 = 0x3C00u32; // 1.0
+    let h2 = 0x4000u32; // 2.0
+    let h3 = 0x4200u32; // 3.0
+    let h5 = 0x4500u32; // 5.0
+    let hn2 = 0xC000u32; // -2.0
+    let nan = 0x7E00u32;
+
+    let cases: &[([u32; 4], Option<u16>)] = &[
+        ([h1, h2, h3, 0], Some(h2 as u16)),
+        ([hn2, h0, h3, 0], Some(h0 as u16)),
+        ([h1, h2, nan, 0], Some(h1 as u16)),
+        ([nan, h1, h0, 0], Some(h0 as u16)),
+        ([nan, h0, h1, 0], Some(h0 as u16)),
+        ([h2, nan, h1, 0], Some(h1 as u16)),
+        ([nan, h5, nan, 0], Some(h5 as u16)),
+        ([nan, nan, nan, 0], None),
+    ];
+
+    let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
+    run_cases(&shader, &inputs, |backend, i, inp, got| {
+        let expect = cases[i].1;
+        let got_h = got as u16;
+        match expect {
+            Some(want) => assert_eq!(
+                got_h, want,
+                "{backend}: v_med3_f16({:#06x}, {:#06x}, {:#06x}) = {got_h:#06x}, want {want:#06x}",
+                inp[0], inp[1], inp[2],
+            ),
+            None => assert!(
+                is_f16_nan(got_h),
+                "{backend}: v_med3_f16(NaN, NaN, NaN) = {got_h:#06x}, want NaN",
+            ),
+        }
+    });
+}
+
+#[test]
+fn v_fract_f32_never_returns_one() {
+    // v_fract_f32 v0, v1 (VOP1 opcode 0x020)
+    let shader = build_valu_cs(&[vop1(0x020, 0, vgpr(1))]);
+    let f = f32::to_bits;
+    let below_one = 0x3F7F_FFFFu32;
+
+    let cases: &[(u32, Option<u32>)] = &[
+        // Tiny negatives: must clamp strictly below 1.0 (not 0x3f800000).
+        (f(-1e-10), Some(below_one)),
+        (f(-1e-8), Some(below_one)),
+        (f(-2.0f32.powi(-25)), Some(below_one)),
+        (f(-2.0f32.powi(-24)), Some(below_one)),
+        // Normal values.
+        (f(0.0), Some(f(0.0))),
+        (f(-0.0), Some(f(0.0))),
+        (f(0.25), Some(f(0.25))),
+        (f(3.75), Some(f(0.75))),
+        (f(-0.25), Some(f(0.75))),
+        (f(-3.25), Some(f(0.75))),
+        (f(1.0), Some(f(0.0))),
+        (f(-1.0), Some(f(0.0))),
+        // Non-finite inputs yield NaN.
+        (f(f32::NAN), None),
+        (f(f32::INFINITY), None),
+        (f(f32::NEG_INFINITY), None),
+    ];
+
+    let inputs: Vec<[u32; 4]> = cases.iter().map(|&(x, _)| [x, 0, 0, 0]).collect();
+    run_cases(&shader, &inputs, |backend, i, inp, got| {
+        let expect = cases[i].1;
+        match expect {
+            Some(want) => assert_eq!(
+                got,
+                want,
+                "{backend}: v_fract_f32({:?} / {:#010x}) = {got:#010x} ({:?}), want {want:#010x}",
+                f32::from_bits(inp[0]),
+                inp[0],
+                f32::from_bits(got),
+            ),
+            None => assert!(
+                f32::from_bits(got).is_nan(),
+                "{backend}: v_fract_f32({:?}) = {got:#010x}, want NaN",
+                f32::from_bits(inp[0]),
+            ),
+        }
+    });
+}
+
+#[test]
+fn v_fract_f16_never_returns_one() {
+    // v_fract_f16 v0, v1 (VOP1 opcode 0x05f)
+    let shader = build_valu_cs(&[vop1(0x05f, 0, vgpr(1))]);
+    let below_one = 0x3BFFu16; // 1.0 - 2^-11
+
+    assert_eq!(f16_to_f32(0x9400), -2.0f32.powi(-10));
+    assert_eq!(f16_to_f32(0x9000), -2.0f32.powi(-11));
+    assert_eq!(f16_to_f32(0x8C00), -2.0f32.powi(-12));
+
+    let cases: &[(u16, Option<u16>)] = &[
+        // Negatives in (-2^-11, 0): must clamp to 0x3BFF, never 0x3C00 (1.0).
+        (0x8C00, Some(below_one)), // -2^-12
+        (0x8400, Some(below_one)), // -2^-14
+        (0x8001, Some(below_one)), // -2^-24 (smallest negative f16 subnormal)
+        (0x9000, Some(below_one)), // -2^-11 (exact 1 - 2^-11 = 0x3BFF)
+        (0x9400, Some(0x3BFE)),    // -2^-10 (exact 1 - 2^-10 = 0x3BFE)
+        // Normal values.
+        (0x0000, Some(0x0000)), // 0.0 -> 0.0
+        (0x3400, Some(0x3400)), // 0.25 -> 0.25
+        (0xB400, Some(0x3A00)), // -0.25 -> 0.75
+        (0x4300, Some(0x3800)), // 3.5 -> 0.5
+        (0x3C00, Some(0x0000)), // 1.0 -> 0.0
+        // Non-finite inputs yield NaN.
+        (0x7E00, None), // NaN
+        (0x7C00, None), // +inf
+        (0xFC00, None), // -inf
+    ];
+
+    let inputs: Vec<[u32; 4]> = cases
+        .iter()
+        .map(|&(x, _)| [u32::from(x), 0, 0, 0])
+        .collect();
+    run_cases(&shader, &inputs, |backend, i, inp, got| {
+        let expect = cases[i].1;
+        let got_h = got as u16;
+        match expect {
+            Some(want) => assert_eq!(
+                got_h, want,
+                "{backend}: v_fract_f16({:#06x}) = {got_h:#06x}, want {want:#06x}",
+                inp[0],
+            ),
+            None => assert!(
+                is_f16_nan(got_h),
+                "{backend}: v_fract_f16({:#06x}) = {got_h:#06x}, want NaN",
+                inp[0],
+            ),
+        }
+    });
+}
+
+#[test]
+fn v_sin_and_v_cos_f32_reduce_large_turns() {
+    let sin_shader = build_valu_cs(&[vop1(0x035, 0, vgpr(1))]);
+    let cos_shader = build_valu_cs(&[vop1(0x036, 0, vgpr(1))]);
+    let f = f32::to_bits;
+    let half_sqrt2 = std::f32::consts::FRAC_1_SQRT_2;
+
+    let sin_cases: &[(f32, Option<f32>)] = &[
+        (0.0, Some(0.0)),
+        (0.25, Some(1.0)),
+        (255.0, Some(0.0)),
+        (255.25, Some(1.0)),
+        (255.5, Some(0.0)),
+        (-255.25, Some(-1.0)),
+        (10_000.125, Some(half_sqrt2)),
+        (f32::NAN, None),
+        (f32::INFINITY, None),
+        (f32::NEG_INFINITY, None),
+    ];
+    let sin_inputs: Vec<[u32; 4]> = sin_cases.iter().map(|&(x, _)| [f(x), 0, 0, 0]).collect();
+    run_cases(&sin_shader, &sin_inputs, |backend, i, inp, got| {
+        let expect = sin_cases[i].1;
+        let got_f = f32::from_bits(got);
+        match expect {
+            Some(want) => assert!(
+                (got_f - want).abs() < 1e-5,
+                "{backend}: v_sin_f32({}) = {got_f}, want {want} (err = {})",
+                f32::from_bits(inp[0]),
+                (got_f - want).abs(),
+            ),
+            None => assert!(
+                got_f.is_nan(),
+                "{backend}: v_sin_f32({}) = {got_f}, want NaN",
+                f32::from_bits(inp[0]),
+            ),
+        }
+    });
+
+    let cos_cases: &[(f32, Option<f32>)] = &[
+        (0.0, Some(1.0)),
+        (0.25, Some(0.0)),
+        (255.0, Some(1.0)),
+        (255.25, Some(0.0)),
+        (255.5, Some(-1.0)),
+        (-255.5, Some(-1.0)),
+        (10_000.125, Some(half_sqrt2)),
+        (f32::NAN, None),
+        (f32::INFINITY, None),
+        (f32::NEG_INFINITY, None),
+    ];
+    let cos_inputs: Vec<[u32; 4]> = cos_cases.iter().map(|&(x, _)| [f(x), 0, 0, 0]).collect();
+    run_cases(&cos_shader, &cos_inputs, |backend, i, inp, got| {
+        let expect = cos_cases[i].1;
+        let got_f = f32::from_bits(got);
+        match expect {
+            Some(want) => assert!(
+                (got_f - want).abs() < 1e-5,
+                "{backend}: v_cos_f32({}) = {got_f}, want {want} (err = {})",
+                f32::from_bits(inp[0]),
+                (got_f - want).abs(),
+            ),
+            None => assert!(
+                got_f.is_nan(),
+                "{backend}: v_cos_f32({}) = {got_f}, want NaN",
+                f32::from_bits(inp[0]),
+            ),
+        }
+    });
+
+    // Main's `trig_turns` preserves sin(-0.0) = -0.0 and produces exact +0.0 at
+    // half-turns (sin) and quarter-turns (cos).
+    let exact_sin: &[(u32, u32)] = &[
+        (0x8000_0000, 0x8000_0000), // sin(-0.0) = -0.0
+        (f(255.0), 0),              // sin(255.0) = +0.0
+        (f(255.5), 0),              // sin(255.5) = +0.0
+    ];
+    let exact_sin_in: Vec<[u32; 4]> = exact_sin.iter().map(|&(x, _)| [x, 0, 0, 0]).collect();
+    run_cases(&sin_shader, &exact_sin_in, |backend, i, inp, got| {
+        let want = exact_sin[i].1;
+        assert_eq!(
+            got, want,
+            "{backend}: v_sin_f32({:#010x}) = {got:#010x}, want {want:#010x}",
+            inp[0],
+        );
+    });
+    let exact_cos: &[(u32, u32)] = &[
+        (f(0.25), 0),   // cos(0.25) = +0.0
+        (f(255.25), 0), // cos(255.25) = +0.0
+    ];
+    let exact_cos_in: Vec<[u32; 4]> = exact_cos.iter().map(|&(x, _)| [x, 0, 0, 0]).collect();
+    run_cases(&cos_shader, &exact_cos_in, |backend, i, inp, got| {
+        let want = exact_cos[i].1;
+        assert_eq!(
+            got, want,
+            "{backend}: v_cos_f32({:#010x}) = {got:#010x}, want {want:#010x}",
+            inp[0],
+        );
+    });
+}
+
+#[test]
+fn v_sin_and_v_cos_f16_reduce_large_turns() {
+    let sin_shader = build_valu_cs(&[vop1(0x060, 0, vgpr(1))]);
+    let cos_shader = build_valu_cs(&[vop1(0x061, 0, vgpr(1))]);
+
+    // Sanity-check the f16 bit encodings of large turns.
+    assert_eq!(f16_to_f32(0x5BF8), 255.0);
+    assert_eq!(f16_to_f32(0x5BFA), 255.25);
+    assert_eq!(f16_to_f32(0x5BFC), 255.5);
+    assert_eq!(f16_to_f32(0x7BFF), 65504.0);
+
+    let sin_cases: &[(u16, Option<f32>)] = &[
+        (0x5BF8, Some(0.0)),  // 255.0
+        (0x5BFA, Some(1.0)),  // 255.25
+        (0x5BFC, Some(0.0)),  // 255.5
+        (0xDBFA, Some(-1.0)), // -255.25
+        (0x7BFF, Some(0.0)),  // 65504.0 (largest finite f16, exact integer)
+        (0x7E00, None),       // NaN
+        (0x7C00, None),       // +inf
+        (0xFC00, None),       // -inf
+    ];
+    let sin_inputs: Vec<[u32; 4]> = sin_cases
+        .iter()
+        .map(|&(x, _)| [u32::from(x), 0, 0, 0])
+        .collect();
+    run_cases(&sin_shader, &sin_inputs, |backend, i, inp, got| {
+        let expect = sin_cases[i].1;
+        let got_h = got as u16;
+        match expect {
+            Some(want) => {
+                let got_f = f16_to_f32(got_h);
+                assert!(
+                    (got_f - want).abs() < 1e-3,
+                    "{backend}: v_sin_f16({:#06x} = {}) = {got_f} ({got_h:#06x}), want {want}",
+                    inp[0],
+                    f16_to_f32(inp[0] as u16),
+                );
+            }
+            None => assert!(
+                is_f16_nan(got_h),
+                "{backend}: v_sin_f16({:#06x}) = {got_h:#06x}, want NaN",
+                inp[0],
+            ),
+        }
+    });
+
+    let cos_cases: &[(u16, Option<f32>)] = &[
+        (0x5BF8, Some(1.0)),  // 255.0
+        (0x5BFA, Some(0.0)),  // 255.25
+        (0x5BFC, Some(-1.0)), // 255.5
+        (0x7BFF, Some(1.0)),  // 65504.0 (largest finite f16, exact integer)
+        (0x7E00, None),       // NaN
+        (0x7C00, None),       // +inf
+        (0xFC00, None),       // -inf
+    ];
+    let cos_inputs: Vec<[u32; 4]> = cos_cases
+        .iter()
+        .map(|&(x, _)| [u32::from(x), 0, 0, 0])
+        .collect();
+    run_cases(&cos_shader, &cos_inputs, |backend, i, inp, got| {
+        let expect = cos_cases[i].1;
+        let got_h = got as u16;
+        match expect {
+            Some(want) => {
+                let got_f = f16_to_f32(got_h);
+                assert!(
+                    (got_f - want).abs() < 1e-3,
+                    "{backend}: v_cos_f16({:#06x} = {}) = {got_f} ({got_h:#06x}), want {want}",
+                    inp[0],
+                    f16_to_f32(inp[0] as u16),
+                );
+            }
+            None => assert!(
+                is_f16_nan(got_h),
+                "{backend}: v_cos_f16({:#06x}) = {got_h:#06x}, want NaN",
+                inp[0],
+            ),
+        }
+    });
+}
+
+#[test]
+fn bfe_signed_and_unsigned_match_rdna2() {
+    // v_bfe_i32 v0, v1, v2, v3 (VOP3 opcode 0x149)
+    let v_signed = build_valu_cs(&vop3(0x149, 0, vgpr(1), vgpr(2), vgpr(3)));
+    // v_bfe_u32 v0, v1, v2, v3 (VOP3 opcode 0x148)
+    let v_unsigned = build_valu_cs(&vop3(0x148, 0, vgpr(1), vgpr(2), vgpr(3)));
+    // s_bfe_i32 s6, s0, s1 (SOP2 opcode 0x28: s1 = off[4:0] | width[22:16]); v_mov_b32 v0, s6
+    let s_signed = build_valu_cs(&[sop2(0x28, 6, 0, 1), vop1(0x001, 0, sgpr(6))]);
+
+    let signed_cases: &[([u32; 4], u32)] = &[
+        // Field crosses bit 31: extends from bit 31.
+        ([0xd2f9_0159, 28, 16, 0], 0xffff_fffd),
+        ([0x8000_0000, 31, 4, 0], 0xffff_ffff),
+        ([0x7000_0000, 28, 8, 0], 0x0000_0007),
+        // Normal fields inside 32 bits.
+        ([0x0000_0080, 4, 4, 0], 0xffff_fff8),
+        ([0x0000_0070, 4, 4, 0], 0x0000_0007),
+        // Edge widths: 0 yields 0; v_bfe masks width to 5 bits (32 -> 0),
+        // while 31 extracts all remaining bits from offset 4.
+        ([0xffff_ffff, 4, 0, 0], 0),
+        ([0x8000_0000, 4, 31, 0], 0xf800_0000),
+    ];
+    let v_in: Vec<[u32; 4]> = signed_cases.iter().map(|c| c.0).collect();
+    run_cases(&v_signed, &v_in, |backend, i, inp, got| {
+        let want = signed_cases[i].1;
+        assert_eq!(
+            got, want,
+            "{backend}: v_bfe_i32({:#010x}, {}, {}) = {got:#010x}, want {want:#010x}",
+            inp[0], inp[1], inp[2],
+        );
+    });
+    // v_bfe_i32 width is 5-bit (src2[4:0]), so width=32 wraps to 0.
+    run_cases(
+        &v_signed,
+        &[[0x8000_0000, 4, 32, 0]],
+        |backend, _, _, got| {
+            assert_eq!(got, 0, "{backend}: v_bfe_i32 width 32 should mask to 0");
+        },
+    );
+
+    // s_bfe_i32 width is 7-bit (src1[22:16]), so width=32 keeps all 32 bits.
+    let mut s_cases = signed_cases.to_vec();
+    s_cases.push(([0x8000_0000, 4, 32, 0], 0xf800_0000));
+    let s_in: Vec<[u32; 4]> = s_cases
+        .iter()
+        .map(|&(inp, _)| [inp[0], (inp[1] & 31) | ((inp[2] & 0x7F) << 16), 0, 0])
+        .collect();
+    run_cases(&s_signed, &s_in, |backend, i, inp, got| {
+        let want = s_cases[i].1;
+        assert_eq!(
+            got, want,
+            "{backend}: s_bfe_i32({:#010x}, {:#010x}) = {got:#010x}, want {want:#010x}",
+            inp[0], inp[1],
+        );
+    });
+
+    let unsigned_cases: &[([u32; 4], u32)] = &[
+        ([0xd2f9_0159, 28, 16, 0], 0x0000_000d),
+        ([0x8000_0000, 31, 4, 0], 0x0000_0001),
+        ([0xffff_ffff, 4, 0, 0], 0),
+        ([0x8000_0000, 4, 31, 0], 0x0800_0000),
+    ];
+    let u_in: Vec<[u32; 4]> = unsigned_cases.iter().map(|c| c.0).collect();
+    run_cases(&v_unsigned, &u_in, |backend, i, inp, got| {
+        let want = unsigned_cases[i].1;
+        assert_eq!(
+            got, want,
+            "{backend}: v_bfe_u32({:#010x}, {}, {}) = {got:#010x}, want {want:#010x}",
+            inp[0], inp[1], inp[2],
+        );
+    });
+}