936 lines
33 KiB
Rust
936 lines
33 KiB
Rust
//! 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),
|
|
) {
|
|
// One test at a time: with these tests on the hardware adapter and WARP
|
|
// from several test threads at once, now and then none of their
|
|
// dispatches ever completes and the test binary hangs.
|
|
static ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
|
let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
|
|
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 packed_add_selects_halves_of_inline_constants() {
|
|
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
|
|
// The same GFX10 source field (248) has a different bit pattern for a
|
|
// packed floating-point and a packed integer instruction.
|
|
let cases = [
|
|
// v_pk_add_f16 v0, 0x3118, v1
|
|
([0xcc0f4000, 0x1a0202f8], 0x0000_3118),
|
|
// v_pk_add_f16 v0, 0x3118, v1 op_sel_hi:[0,1] (use low twice)
|
|
([0xcc0f4000, 0x120202f8], 0x3118_3118),
|
|
// v_pk_add_u16 v0, 0x3e22f983, v1
|
|
([0xcc0a4000, 0x1a0202f8], 0x3e22_f983),
|
|
];
|
|
for (words, expected) in cases {
|
|
let shader = build_valu_cs(&words);
|
|
run_cases(&shader, &[[0; 4]], |backend, _, _, got| {
|
|
assert_eq!(got, expected, "{backend}: {words:08x?}");
|
|
});
|
|
}
|
|
}
|
|
|
|
#[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",
|
|
),
|
|
}
|
|
});
|
|
}
|
|
|
|
/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and
|
|
/// FMin compare and select, which can give either zero.
|
|
#[test]
|
|
fn max_and_min_order_negative_zero_below_positive_zero() {
|
|
let (pz, nz) = (0_u32, 0x8000_0000_u32);
|
|
// Each case has a -0 and a +0 among its first two operands.
|
|
let cases = [
|
|
[nz, pz, nz, 0],
|
|
[pz, nz, nz, 0],
|
|
[nz, pz, pz, 0],
|
|
[pz, nz, pz, 0],
|
|
];
|
|
let ops = [
|
|
("v_max_f32", 0x110, pz),
|
|
("v_min_f32", 0x10F, nz),
|
|
("v_max3_f32", 0x154, pz),
|
|
("v_min3_f32", 0x151, nz),
|
|
];
|
|
for (name, op, want) in ops {
|
|
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
|
|
run_cases(&shader, &cases, |backend, _, inp, got| {
|
|
assert_eq!(got, want, "{backend}: {name} {inp:x?}");
|
|
});
|
|
}
|
|
}
|
|
|
|
#[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",
|
|
),
|
|
}
|
|
});
|
|
}
|
|
|
|
/// `v_cmp_lg_*` is "less or greater": ordered and not equal, so false when
|
|
/// either operand is a NaN (RDNA2 ISA; the same on gfx1036 hardware).
|
|
#[test]
|
|
fn v_cmp_lg_is_false_for_nan() {
|
|
// v_cmp_lg_{f32,f16}_e64 s20, v1, v2 (VOPC 0x005 / 0x0cd in VOP3);
|
|
// v_cndmask_b32_e64 v0, 0, 1, s20 (VOP3 0x101)
|
|
let nan = f32::NAN.to_bits();
|
|
let (inf, ninf) = (f32::INFINITY.to_bits(), f32::NEG_INFINITY.to_bits());
|
|
let f = f32::to_bits;
|
|
let f32_cases: &[([u32; 4], u32)] = &[
|
|
([f(1.0), f(2.0), 0, 0], 1),
|
|
([f(2.0), f(1.0), 0, 0], 1),
|
|
([f(1.0), f(1.0), 0, 0], 0),
|
|
([f(0.0), f(-0.0), 0, 0], 0),
|
|
([inf, ninf, 0, 0], 1),
|
|
([inf, inf, 0, 0], 0),
|
|
([nan, f(1.0), 0, 0], 0),
|
|
([f(1.0), nan, 0, 0], 0),
|
|
([nan, nan, 0, 0], 0),
|
|
([0x7F80_0001, f(0.0), 0, 0], 0), // a signalling NaN
|
|
];
|
|
let f16_cases: &[([u32; 4], u32)] = &[
|
|
([0x3C00, 0x4000, 0, 0], 1),
|
|
([0x4000, 0x3C00, 0, 0], 1),
|
|
([0x3C00, 0x3C00, 0, 0], 0),
|
|
([0x0000, 0x8000, 0, 0], 0),
|
|
([0x7C00, 0xFC00, 0, 0], 1),
|
|
([0x7E00, 0x3C00, 0, 0], 0),
|
|
([0x3C00, 0x7E00, 0, 0], 0),
|
|
([0x7E00, 0x7E00, 0, 0], 0),
|
|
([0x7C01, 0x0000, 0, 0], 0), // a signalling NaN
|
|
([0xABCD_3C00, 0x1234_7E00, 0, 0], 0), // only the low halves are read
|
|
];
|
|
for (op, what, cases) in [
|
|
(0x005u16, "v_cmp_lg_f32", f32_cases),
|
|
(0x0cd, "v_cmp_lg_f16", f16_cases),
|
|
] {
|
|
let mut body = vop3(op, 20, vgpr(1), vgpr(2), 0x80).to_vec();
|
|
body.extend_from_slice(&vop3(0x101, 0, 0x80, 0x81, sgpr(20)));
|
|
let shader = build_valu_cs(&body);
|
|
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
|
|
run_cases(&shader, &inputs, |backend, i, inp, got| {
|
|
let want = cases[i].1;
|
|
assert_eq!(
|
|
got, want,
|
|
"{backend}: {what}({:#010x}, {:#010x}) = {got}, want {want}",
|
|
inp[0], inp[1],
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
#[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],
|
|
),
|
|
}
|
|
});
|
|
}
|
|
|
|
/// RDNA2's flt16_to_snorm16 / flt16_to_unorm16: clamp, scale by 32767 or
|
|
/// 65535, round the exact product to nearest even; NaN gives 0.
|
|
fn cvt_norm16_reference(bits: u16, signed: bool) -> u16 {
|
|
let x = f16_to_f32(bits);
|
|
if x.is_nan() {
|
|
return 0;
|
|
}
|
|
let (lo, scale) = if signed {
|
|
(-1.0, 32767.0)
|
|
} else {
|
|
(0.0, 65535.0)
|
|
};
|
|
// f64 holds the product of an f16 and a 16-bit scale exactly.
|
|
let p = f64::from(x.clamp(lo, 1.0)) * scale;
|
|
p.round_ties_even() as i32 as u16
|
|
}
|
|
|
|
#[test]
|
|
fn v_cvt_norm_i16_and_u16_f16_round_the_exact_product() {
|
|
// Every 251st f16 pattern, plus the values where an f32 product would
|
|
// round before the rounding to an integer, ties, and the edges.
|
|
let mut bits: Vec<u16> = (0..=u16::MAX).step_by(251).collect();
|
|
bits.extend([
|
|
0x0000, 0x8000, 0x0001, 0x8001, 0x03FF, 0x0400, 0x3800, 0xB800, 0x3801, 0xB801, 0x37FF,
|
|
0x3BFF, 0xBBFF, 0x3C00, 0xBC00, 0x3C01, 0x7BFF, 0x7C00, 0xFC00, 0x7E00, 0xFE00, 0x7C01,
|
|
0x3A9B, 0xBA9B, 0x2E66, 0x1400,
|
|
]);
|
|
for (op, signed, what) in [
|
|
(0x063u16, true, "v_cvt_norm_i16_f16"),
|
|
(0x064, false, "v_cvt_norm_u16_f16"),
|
|
] {
|
|
// v_cvt_norm_*16_f16 v0, v1 (VOP1); v0's high half starts at 0.
|
|
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
|
|
let inputs: Vec<[u32; 4]> = bits.iter().map(|&b| [u32::from(b), 0, 0, 0]).collect();
|
|
run_cases(&shader, &inputs, |backend, i, _, got| {
|
|
let want = cvt_norm16_reference(bits[i], signed);
|
|
assert_eq!(
|
|
got,
|
|
u32::from(want),
|
|
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
|
|
bits[i],
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
#[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_cvt_u16_and_i16_f16_saturate() {
|
|
// v_mov_b32 v0, 0; v_cvt_u16_f16 / v_cvt_i16_f16 v0, v1 (VOP1 0x052 / 0x053).
|
|
let cases: &[(u16, u16, u16)] = &[
|
|
// (f16 input, u16 result, i16 result)
|
|
(0x0000, 0, 0), // 0
|
|
(0x8000, 0, 0), // -0
|
|
(0x3E00, 1, 1), // 1.5 -> 1 (toward zero)
|
|
(0xBE00, 0, 0xFFFF), // -1.5 -> 0 / -1
|
|
(0x57D0, 125, 125), // 125.0
|
|
(0x7800, 32768, 32767), // 32768 saturates i16
|
|
(0x77FF, 32752, 32752), // 32752, the largest f16 below 32768
|
|
(0xF800, 0, 0x8000), // -32768 fits i16
|
|
(0xF801, 0, 0x8000), // -32800 saturates i16
|
|
(0x7BFF, 65504, 32767), // max finite f16
|
|
(0xFBFF, 0, 0x8000), // -65504
|
|
(0x7C00, 0xFFFF, 32767), // +inf
|
|
(0xFC00, 0, 0x8000), // -inf
|
|
(0x7E00, 0, 0), // NaN
|
|
(0xFE00, 0, 0), // -NaN
|
|
(0x0001, 0, 0), // smallest subnormal
|
|
];
|
|
for (op, what, pick) in [
|
|
(0x052u16, "v_cvt_u16_f16", 0usize),
|
|
(0x053, "v_cvt_i16_f16", 1),
|
|
] {
|
|
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
|
|
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| [u32::from(c.0), 0, 0, 0]).collect();
|
|
run_cases(&shader, &inputs, |backend, i, _, got| {
|
|
let want = if pick == 0 { cases[i].1 } else { cases[i].2 };
|
|
assert_eq!(
|
|
got,
|
|
u32::from(want),
|
|
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
|
|
cases[i].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],
|
|
);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn v_cmp_class_f16_matches_rdna2() {
|
|
// v_cmp_class_f16_e64 s6, v1, v2 (VOP3 opcode 0x08f); v_cndmask_b32_e64 v0, 0, 1, s6
|
|
let cmp_lo = build_valu_cs(&[
|
|
vop3(0x08f, 6, vgpr(1), vgpr(2), 0)[0],
|
|
vop3(0x08f, 6, vgpr(1), vgpr(2), 0)[1],
|
|
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0],
|
|
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1],
|
|
]);
|
|
// Same with op_sel:[1,0] (high half of v1) + neg on src0 (bit 29 of w1).
|
|
let [mut w0, mut w1] = vop3(0x08f, 6, vgpr(1), vgpr(2), 0);
|
|
w0 |= 1 << 11; // opsel[0] = 1 (high half)
|
|
w1 |= 1 << 29; // neg[0] = 1
|
|
let cmp_hi_neg = build_valu_cs(&[
|
|
w0,
|
|
w1,
|
|
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0],
|
|
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1],
|
|
]);
|
|
|
|
// All 10 IEEE-754 binary16 classes: bit k corresponds to class k.
|
|
let classes: &[(u32, u32)] = &[
|
|
(0x7C01, 0), // sNaN (+sNaN, quiet bit 0x0200 clear)
|
|
(0xFDFF, 0), // -sNaN (sign ignored for NaNs)
|
|
(0x7E00, 1), // +qNaN
|
|
(0xFE01, 1), // -qNaN
|
|
(0xFC00, 2), // -inf
|
|
(0xBC00, 3), // -normal (-1.0)
|
|
(0x8001, 4), // -denormal
|
|
(0x8000, 5), // -0.0
|
|
(0x0000, 6), // +0.0
|
|
(0x0001, 7), // +denormal
|
|
(0x3C00, 8), // +normal (+1.0)
|
|
(0x7C00, 9), // +inf
|
|
];
|
|
let mut cases = Vec::new();
|
|
for &(bits, cls) in classes {
|
|
// Exact class bit -> true; complement of class bit across all 10 bits -> false.
|
|
cases.push(([bits, 1u32 << cls, 0, 0], 1u32));
|
|
cases.push(([bits, 0x3FF & !(1u32 << cls), 0, 0], 0u32));
|
|
}
|
|
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
|
|
run_cases(&cmp_lo, &inputs, |backend, i, inp, got| {
|
|
let want = cases[i].1;
|
|
assert_eq!(
|
|
got, want,
|
|
"{backend}: v_cmp_class_f16({:#06x}, {:#05x}) = {got}, want {want}",
|
|
inp[0], inp[1],
|
|
);
|
|
});
|
|
|
|
// High half with neg: high half 0x3C00 (+1.0) negated -> -normal (class 3);
|
|
// high half 0x7C01 (+sNaN) negated -> still sNaN (class 0, not quieted).
|
|
let hi_cases: &[([u32; 4], u32)] = &[
|
|
([0x3C00_0000, 1 << 3, 0, 0], 1),
|
|
([0x3C00_0000, 1 << 8, 0, 0], 0),
|
|
([0x7C01_FFFF, 1 << 0, 0, 0], 1),
|
|
([0x7C01_FFFF, 1 << 1, 0, 0], 0),
|
|
];
|
|
let hi_in: Vec<[u32; 4]> = hi_cases.iter().map(|c| c.0).collect();
|
|
run_cases(&cmp_hi_neg, &hi_in, |backend, i, inp, got| {
|
|
let want = hi_cases[i].1;
|
|
assert_eq!(
|
|
got, want,
|
|
"{backend}: v_cmp_class_f16(-hi({:#010x}), {:#05x}) = {got}, want {want}",
|
|
inp[0], inp[1],
|
|
);
|
|
});
|
|
}
|