Upstream instruction fixes: 64-bit literals, exact normalized ends, flat offsets, rare forms

Ports of 7d04943 (a signed 64-bit operand sign-extends its literal),
645391b (a normalized buffer format's ends load as exactly 1.0 and -1.0),
40f30be (FLAT offsets ignore bit 11), 3d71ad1, 344704e, 5d6889e, 78c1267,
c3a04e2, e3fe5bf and the tests 259ba31, 8610ffb, 41b2472, 45ed8e3, 8d67fa5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:24:50 -06:00
1 parent 25f78a079c
commit a780306f4c
5 files changed
+529 -14

No files matched your search

@@ -259,6 +259,39 @@ fn run_cases_on(
assert!(ran > 0, "no SM 6.6 device available ({devices:?})");
}
#[test]
fn scalar_quad_masks_pack_nibbles_and_set_scc() {
let pack_words = |opcode: u32| {
[
0xBE90_0000 | opcode << 8, // s_quadmask s16, s0 (pairs for b64)
sop2(0x0a, 18, 129, 128), // s_cselect_b32 s18, 1, 0
sop2(0x1e, 18, 18, 144), // s_lshl_b32 s18, s18, 16
sop2(0x10, 16, 16, 18), // s_or_b32 s16, s16, s18
vop1(1, 0, sgpr(16)), // v_mov_b32 v0, s16
]
};
let cases = [
([0, 0, 0, 0], [0, 0]),
([0x8000_0420, 0x0100_0001, 0, 0], [0x1_0086, 0x1_4186]),
([0, 0x8000_0000, 0, 0], [0, 0x1_8000]),
([u32::MAX, u32::MAX, 0, 0], [0x1_00ff, 0x1_ffff]),
];
let inputs = cases.map(|(input, _)| input);
for (wide, opcode) in [0x2c, 0x2d].into_iter().enumerate() {
let shader = build_valu_cs(&pack_words(opcode));
run_cases(&shader, &inputs, |backend, i, _, got| {
assert_eq!(got, cases[i].1[wide], "{backend}, opcode {opcode:#x}");
});
}
let high = build_valu_cs(&[
0xBE90_2D00, // s_quadmask_b64 s[16:17], s[0:1]
vop1(1, 0, sgpr(17)),
]);
run_cases(&high, &inputs, |backend, _, _, got| {
assert_eq!(got, 0, "{backend}: high dword");
});
}
fn is_f16_nan(bits: u16) -> bool {
(bits & 0x7FFF) > 0x7C00
}
@@ -287,6 +320,33 @@ fn f16_to_f32(bits: u16) -> f32 {
f32::from_bits(f32_bits)
}
#[test]
fn normalized_buffer_formats_load_their_ends_exactly() {
// tbuffer_load_format_x.
let x = 0;
// (format, dword stored, value loaded)
let cases: &[(u8, u32, f32)] = &[
(1, 0xFF, 1.0), // 8_UNORM
(7, 0xFFFF, 1.0), // 16_UNORM
(2, 0x7F, 1.0), // 8_SNORM: the top, the one above the lowest, the lowest
(2, 0x81, -1.0),
(2, 0x80, -1.0),
(8, 0x7FFF, 1.0), // 16_SNORM
(8, 0x8001, -1.0),
(8, 0x8000, -1.0),
];
for &(format, stored, want) in cases {
let shader = tbuffer_load_of_v1(x, format);
run_cases(&shader, &[[stored, 0, 0, 0]], |backend, _, _, got| {
assert_eq!(
got,
want.to_bits(),
"{backend}: format {format} of {stored:#x} = {got:#010x}, want {want}",
);
});
}
}
#[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.
@@ -486,6 +546,77 @@ fn max_and_min_order_negative_zero_below_positive_zero() {
}
}
/// The f16 forms go through the same `max_min` helper, after widening to f32.
#[test]
fn f16_max_and_min_order_negative_zero_below_positive_zero() {
let (pz, nz) = (0_u32, 0x8000_u32);
let cases = [
[nz, pz, nz, 0],
[pz, nz, nz, 0],
[nz, pz, pz, 0],
[pz, nz, pz, 0],
];
let ops = [
("v_max_f16", 0x139, pz),
("v_min_f16", 0x13A, nz),
("v_max3_f16", 0x354, pz),
("v_min3_f16", 0x351, 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| {
// The f16 result is the low half of v0.
assert_eq!(got & 0xFFFF, want, "{backend}: {name} {inp:x?}");
});
}
}
#[test]
fn v_min3_max3_med3_integer_forms_match_rdna2() {
// v_{min3,max3,med3}_{i32,u32} v0, v1, v2, v3 (VOP3 0x152..0x159).
type Pick = fn([u32; 3]) -> u32;
let ops: [(&str, u16, Pick); 6] = [
("v_min3_i32", 0x152, |t| {
t.into_iter().min_by_key(|&x| x as i32).unwrap()
}),
("v_min3_u32", 0x153, |t| t.into_iter().min().unwrap()),
("v_max3_i32", 0x155, |t| {
t.into_iter().max_by_key(|&x| x as i32).unwrap()
}),
("v_max3_u32", 0x156, |t| t.into_iter().max().unwrap()),
("v_med3_i32", 0x158, |mut t| {
t.sort_by_key(|&x| x as i32);
t[1]
}),
("v_med3_u32", 0x159, |mut t| {
t.sort_unstable();
t[1]
}),
];
// Every ordering of three values, with the sign bit set on some so that
// the signed and unsigned answers differ.
let values = [0u32, 1, 0x7FFF_FFFF, 0x8000_0000, 0xFFFF_FFFF];
let mut cases = Vec::new();
for a in values {
for b in values {
for c in values {
cases.push([a, b, c, 0]);
}
}
}
for (name, op, pick) in ops {
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
run_cases(&shader, &cases, |backend, _, inp, got| {
let want = pick([inp[0], inp[1], inp[2]]);
assert_eq!(
got, want,
"{backend}: {name}({:#010x}, {:#010x}, {:#010x}) = {got:#010x}, want {want:#010x}",
inp[0], inp[1], inp[2],
);
});
}
}
#[test]
fn v_med3_f16_matches_rdna2_with_nan() {
// v_med3_f16 v0, v1, v2, v3 (VOP3 opcode 0x357)
@@ -579,6 +710,31 @@ fn v_cmp_lg_is_false_for_nan() {
}
}
#[test]
fn v_cvt_off_f32_i4_uses_the_signed_low_nibble() {
// v_cvt_off_f32_i4 v0, v1 (VOP1 opcode 0x00e)
let shader = build_valu_cs(&[vop1(0x00e, 0, vgpr(1))]);
let nibble = |n: u32| (((n as i32) << 28) >> 28) as f32 / 16.0;
let mut cases: Vec<u32> = (0..16).collect();
// Bits above the low nibble are ignored.
cases.extend([0xABCD_EF05, 0xFFFF_FFF8, 0x1234_5670, 0x8000_000F, 0x0000_00F7]);
let inputs: Vec<[u32; 4]> = cases.iter().map(|&x| [x, 0, 0, 0]).collect();
run_cases(&shader, &inputs, |backend, i, inp, got| {
let want = nibble(cases[i]).to_bits();
assert_eq!(
got, want,
"{backend}: v_cvt_off_f32_i4({:#010x}) = {got:#010x} ({:?}), want {want:#010x}",
inp[0],
f32::from_bits(got),
);
});
// Spot-check the reference itself: 0x5 -> 0.3125, 0x8 -> -0.5, 0xF -> -0.0625.
assert_eq!(nibble(0xABCD_EF05), 0.3125);
assert_eq!(nibble(8), -0.5);
assert_eq!(nibble(0xF), -0.0625);
}
#[test]
fn v_fract_f32_never_returns_one() {
// v_fract_f32 v0, v1 (VOP1 opcode 0x020)
@@ -865,6 +1021,40 @@ fn v_cvt_u16_and_i16_f16_saturate() {
}
}
#[test]
fn v_cvt_f16_u16_and_i16_widen_the_integer_first() {
// v_mov_b32 v0, 0; v_cvt_f16_u16 / v_cvt_f16_i16 v0, v1 (VOP1 0x050 / 0x051).
// Only integers f16 holds exactly: how the others round is the narrowing's
// business, not this conversion's. `None`: the result depends on rounding.
let cases: &[(u16, Option<u16>, Option<u16>)] = &[
// (u16 / i16 input bits, f16 of the unsigned value, f16 of the signed value)
(0x0000, Some(0x0000), Some(0x0000)), // 0
(0x0001, Some(0x3C00), Some(0x3C00)), // 1.0
(0x07FF, Some(0x67FF), Some(0x67FF)), // 2047, the last integer below 2048 that f16 holds
(0x8000, Some(0x7800), Some(0xF800)), // 32768 / -32768
(0xFC00, Some(0x7BE0), Some(0xE400)), // 64512 / -1024
(0xFFFE, None, Some(0xC000)), // 65534 does not fit f16 / -2
(0xFFFF, None, Some(0xBC00)), // 65535 does not fit f16 / -1
];
for (op, what, pick) in [
(0x050u16, "v_cvt_f16_u16", 0usize),
(0x051, "v_cvt_f16_i16", 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 };
let Some(want) = want else { return };
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))]);
@@ -946,6 +1136,38 @@ fn v_sin_and_v_cos_f16_reduce_large_turns() {
});
}
/// A 32-bit literal in a signed 64-bit operand is sign-extended (an
/// unsigned one's is zero-extended): -100 compared with `v[1:2]`, and
/// shifted by `s_ashr_i64`.
#[test]
fn signed_64_bit_operands_sign_extend_their_literal() {
const MINUS_100: u32 = -100_i32 as u32;
let check = |name: &str, body: &[u32], cases: &[([u32; 4], u32)]| {
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}: {name} {inp:x?}");
});
};
// v_cmp_*_i64 s6, v[1:2], -100 (VOP3, literal src1), then
// v_cndmask_b32 v0, 0, 1, s6.
let compare = |op: u16| {
let [a, b] = vop3(op, 6, vgpr(1), 255, 0);
let [c, d] = vop3(0x101, 0, 0x80, 0x81, sgpr(6));
[a, b, MINUS_100, c, d]
};
// An i64 as the two input dwords of `v[1:2]`.
let pair = |n: i64| [n as u32, (n >> 32) as u32, 0, 0];
let eq = [(pair(-100), 1), ([MINUS_100, 0, 0, 0], 0), (pair(0), 0)];
check("v_cmp_eq_i64", &compare(0x0A2), &eq);
let lt = [(pair(-200), 1), (pair(-50), 0), (pair(0), 0)];
check("v_cmp_lt_i64", &compare(0x0A1), &lt);
// s_ashr_i64 s[6:7], -100, 4; v_mov_b32 v0, s7: the high dword.
let ashr = [sop2(0x23, 6, 255, 0x84), MINUS_100, vop1(0x001, 0, sgpr(7))];
check("s_ashr_i64", &ashr, &[([0; 4], u32::MAX)]);
}
#[test]
fn bfe_signed_and_unsigned_match_rdna2() {
// v_bfe_i32 v0, v1, v2, v3 (VOP3 opcode 0x149)