mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Recompiler: round f16 conversions and products to nearest-even by default
Use the existing rounding helper for v_cvt_f16_f32 and scalar and packed half multiplication. A finite product of two half operands fits in binary32, so the final binary16 conversion rounds the exact product.
This commit is contained in:
@@ -426,15 +426,21 @@ fn is_f64_rounded_op(name: &str) -> bool {
|
||||
}
|
||||
|
||||
/// HLSL expression converting float expression `e` to a 16-bit half (`0..=0xFFFF`).
|
||||
/// Uses `f32_to_f16_rm(..., xcl_fp16_rm)` only when the shader changes `MODE[3:2]`.
|
||||
fn to_f16_expr(dynamic_rm: bool, e: &str) -> String {
|
||||
if dynamic_rm {
|
||||
format!("f32_to_f16_rm({e}, xcl_fp16_rm)")
|
||||
to_f16_rounded_expr(true, e)
|
||||
} else {
|
||||
format!("(f32tof16({e}) & 0xFFFFu)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Round an exact binary32 input in the selected RDNA mode. Legacy `f32tof16`
|
||||
/// uses round-toward-zero. The default RDNA mode is round-to-nearest-even.
|
||||
fn to_f16_rounded_expr(dynamic_rm: bool, e: &str) -> String {
|
||||
let rm = if dynamic_rm { "xcl_fp16_rm" } else { "0u" };
|
||||
format!("f32_to_f16_rm({e}, {rm})")
|
||||
}
|
||||
|
||||
impl Em {
|
||||
pub fn new(opt: Options) -> Self {
|
||||
Em {
|
||||
@@ -2566,7 +2572,12 @@ impl Em {
|
||||
let dyn_rm = self.dynamic_f16_rm;
|
||||
let fr = |e: String| {
|
||||
let s = if clamp { format!("saturate({e})") } else { e };
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
// The product of two binary16 operands fits exactly in binary32.
|
||||
if name == "v_pk_mul_f16" {
|
||||
to_f16_rounded_expr(dyn_rm, &s)
|
||||
} else {
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
}
|
||||
};
|
||||
let t = self.t();
|
||||
// dot products: one 32-bit result
|
||||
@@ -2796,7 +2807,11 @@ impl Em {
|
||||
if m.clamp {
|
||||
s = format!("saturate({s})");
|
||||
}
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
if name == "v_mul_f16" {
|
||||
to_f16_rounded_expr(dyn_rm, &s)
|
||||
} else {
|
||||
to_f16_expr(dyn_rm, &s)
|
||||
}
|
||||
};
|
||||
let dst = outs.first().map(|o| Self::dst_of(d, o)).unwrap_or(Dst::Null);
|
||||
let cur = match &dst {
|
||||
@@ -3117,7 +3132,7 @@ impl Em {
|
||||
"v_cvt_f16_f32" => {
|
||||
// the modifiers in f32 before the rounding: the same f16 (scaling by a power of
|
||||
// two is exact, and 0 and 1 are f16 values), except where the f16 is denormal
|
||||
let h = to_f16_expr(self.dynamic_f16_rm, &Self::out_mods(&m, &fs(0)));
|
||||
let h = to_f16_rounded_expr(self.dynamic_f16_rm, &Self::out_mods(&m, &fs(0)));
|
||||
if m.opsel >> 3 & 1 == 1 {
|
||||
format!("({cur} & 0xFFFFu) | ({h} << 16)")
|
||||
} else if d.variant == Variant::Sdwa {
|
||||
|
||||
@@ -6207,6 +6207,51 @@ fn gpu_nonfused_mad_and_mac_prevent_fma_contraction() {
|
||||
assert_eq!(v1[16], 0x2880_0000, "v_fma_f32 cancellation control should stay fused");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_f16_defaults_to_round_to_nearest_even() {
|
||||
if !gpu_ready() {
|
||||
return;
|
||||
}
|
||||
let code = prog(&[
|
||||
("V_CVT_F16_F32", "ENC_VOP1", &[("VDST", 10), ("SRC0", V)], None),
|
||||
("V_MUL_F16", "ENC_VOP2", &[("VDST", 11), ("SRC0", V + 1), ("VSRC1", 2)], None),
|
||||
("V_PK_MUL_F16", "ENC_VOP3P", &[("VDST", 12), ("SRC0", V + 1), ("SRC1", V + 2), ("OP_SEL_HI", 3)], None),
|
||||
]);
|
||||
let opt = Options { wave64: false, hw_wave: 32, ..Options::default() };
|
||||
let t = translate(&code, &opt).unwrap();
|
||||
assert!(t.complete(), "{:?}", t.unsupported_why);
|
||||
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap();
|
||||
|
||||
let cases: [(f32, u16); 7] = [
|
||||
(f32::from_bits(3075.0f32.to_bits() - 1), 0x6A01),
|
||||
(3075.0, 0x6A02),
|
||||
(f32::from_bits(3075.0f32.to_bits() + 1), 0x6A02),
|
||||
(3081.0, 0x6A04),
|
||||
(-3075.0, 0xEA02),
|
||||
(65520.0, 0x7C00),
|
||||
(f32::from_bits(0x3300_0001), 0x0001),
|
||||
];
|
||||
let mut st = KernelState::default();
|
||||
st.sgpr[127] = 0;
|
||||
for (lane, &(value, _)) in cases.iter().enumerate() {
|
||||
st.vgpr[lane][0] = value.to_bits();
|
||||
}
|
||||
// 3072 * (1 + 2^-10) is the exact binary16 midpoint 3075.
|
||||
st.vgpr[0][1] = 0xEA00_6A00;
|
||||
st.vgpr[0][2] = 0x3C01_3C01;
|
||||
|
||||
let r = runner::run(&dxil, &st, &[]).unwrap();
|
||||
let actual: [u16; 7] = std::array::from_fn(|lane| r.vgpr[lane][10] as u16);
|
||||
let expected = cases.map(|(_, bits)| bits);
|
||||
let product = r.vgpr[0][11] as u16;
|
||||
let packed = r.vgpr[0][12];
|
||||
assert_eq!(
|
||||
(actual, product, packed),
|
||||
(expected, 0x6A02, 0xEA02_6A02),
|
||||
"conversion {actual:04x?}, multiplication {product:04x}, packed multiplication {packed:08x}",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gpu_s_round_mode_controls_f16_rounding_across_modes() {
|
||||
if !gpu_ready() {
|
||||
|
||||
Reference in New Issue
Block a user