ref, validation: evaluate eleven SOP1 unary forms in both interpreters (#215)

Both interpreters now run s_not/brev/bcnt0/bcnt1/ff0/ff1/flbit(_b32)/
sext_i8/sext_i16/abs with SDST s0-s105. Not-found searches return -1,
s_flbit_i32 returns -1 for 0 and -1, abs keeps 0x80000000, and only NOT,
BCNT0, BCNT1 and ABS write SCC (D != 0). The reference uses host bit
intrinsics; the second interpreter works from an explicit bit array and
arithmetic, so they do not share an evaluation path.

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
This commit is contained in:
elforeign committed 2026-11-24 04:20:56 -06:00
1 parent b705104403
commit 84bb80d910
2 files changed
+168 -1

No files matched your search

+93
View File
@@ -51,6 +51,8 @@ pub enum Op {
Move(u8, Source),
Add(u8, u8, u8),
ScalarMove(u8, Source),
/// SOP1 unary operation writing an SGPR (s0-s105), and SCC for some.
ScalarUnary(ScalarUnary, u8, Source),
/// VOP1 move of a normal SGPR or `vcc_lo` (106) into a VGPR.
MoveScalar(u8, u8),
/// VOPC 32-bit integer compare writing the Wave32 mask to `vcc_lo`.
@@ -164,6 +166,34 @@ pub enum ScalarAlu {
ArithmeticShiftRight,
}
/// SOP1 unary operations decoded by the reference interpreter (RDNA 2 ISA
/// guide 12.3; SCC per Tables 12 and 14).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ScalarUnary {
/// D = ~S0, SCC = (D != 0).
Not,
/// D[31:0] = S0[0:31].
ReverseBits,
/// D = number of zero bits, SCC = (D != 0).
CountZeros,
/// D = number of one bits, SCC = (D != 0).
CountOnes,
/// D = index of the lowest zero bit, or -1.
FindFirstZero,
/// D = index of the lowest one bit, or -1.
FindFirstOne,
/// D = number of zeros above the highest one bit, or -1 for 0.
FindLastBit,
/// D = number of leading bits equal to the sign bit, or -1 for 0 and -1.
FindLastSignBit,
/// D = signext(S0[7:0]).
SignExtendByte,
/// D = signext(S0[15:0]).
SignExtendShort,
/// D = |S0| (0x80000000 stays), SCC = (D != 0).
Absolute,
}
/// VOP2 operations decoded by the reference interpreter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VectorAlu {
@@ -297,6 +327,31 @@ pub fn decode(code: &[u8]) -> Result<Vec<DecodedInstruction>, ReferenceError> {
let (source, length) = decode_scalar(*source, tail, offset)?;
(Op::ScalarMove(destination & 0x7f, source), length)
}
// SOP1: ENCODING 0b10_1111101 in bits 23-31, SDST in 16-22 (s0-s105
// only), OP in 8-15.
[
source,
op @ (7 | 11 | 13 | 15 | 17 | 19 | 21 | 23 | 25 | 26 | 52),
destination @ 0x80..=0xe9,
0xbe,
tail @ ..,
] => {
let kind = match op {
7 => ScalarUnary::Not,
11 => ScalarUnary::ReverseBits,
13 => ScalarUnary::CountZeros,
15 => ScalarUnary::CountOnes,
17 => ScalarUnary::FindFirstZero,
19 => ScalarUnary::FindFirstOne,
21 => ScalarUnary::FindLastBit,
23 => ScalarUnary::FindLastSignBit,
25 => ScalarUnary::SignExtendByte,
26 => ScalarUnary::SignExtendShort,
_ => ScalarUnary::Absolute,
};
let (source, length) = decode_scalar(*source, tail, offset)?;
(Op::ScalarUnary(kind, destination & 0x7f, source), length)
}
// SOPC: ENCODING 0b10_1111110 in bits 23-31, OP in 16-22 (guide
// 13.1.4); 0-5 signed and 6-11 unsigned EQ, LG, GT, GE, LT, LE.
[left, right, op @ 0..=11, 0xbf, tail @ ..] if (*left, *right) != (255, 255) => {
@@ -694,6 +749,44 @@ fn execute_wave(program: &[DecodedInstruction], input: &[u32]) -> Result<Vec<u32
}
scalars[usize::from(dst)] = Some(value);
}
Op::ScalarUnary(kind, dst, src) => {
if dst < 8 {
return Err(error(offset, "write to a reserved resource SGPR"));
}
let value = read_scalar(src)?;
// -1 (all ones) when no bit is found.
let found = |index: u32| if index == 32 { u32::MAX } else { index };
let result = match kind {
ScalarUnary::Not => !value,
ScalarUnary::ReverseBits => value.reverse_bits(),
ScalarUnary::CountZeros => value.count_zeros(),
ScalarUnary::CountOnes => value.count_ones(),
ScalarUnary::FindFirstZero => found(value.trailing_ones()),
ScalarUnary::FindFirstOne => found(value.trailing_zeros()),
ScalarUnary::FindLastBit => found(value.leading_zeros()),
ScalarUnary::FindLastSignBit => {
let same = if value >> 31 == 1 {
value.leading_ones()
} else {
value.leading_zeros()
};
found(same)
}
ScalarUnary::SignExtendByte => value as u8 as i8 as i32 as u32,
ScalarUnary::SignExtendShort => value as u16 as i16 as i32 as u32,
ScalarUnary::Absolute => (value as i32).wrapping_abs() as u32,
};
scalars[usize::from(dst)] = Some(result);
if matches!(
kind,
ScalarUnary::Not
| ScalarUnary::CountZeros
| ScalarUnary::CountOnes
| ScalarUnary::Absolute
) {
scc = Some(result != 0);
}
}
Op::CompareEqual(lhs, rhs) => {
scc = Some(read_scalar(lhs)? == read_scalar(rhs)?);
}
+75 -1
View File
@@ -8,7 +8,8 @@
//! field layout and executes them with an explicit program-counter machine, so
//! a defect in the production decoder or reference cannot cancel out here.
//!
//! Scope matches the demo fixtures: scalar move, the twelve SOPC 32-bit integer
//! Scope matches the demo fixtures: scalar move, eleven SOP1 unary forms
//! (NOT, BREV, BCNT0/1, FF0/1, FLBIT, FLBIT_I32, SEXT_I8/I16, ABS), the twelve SOPC 32-bit integer
//! compares, branch, SOP2 unsigned
//! and signed add and subtract, add-with-carry, subtract-with-borrow, AND, OR
//! and XOR, the stage C forms ANDN2, ORN2, NAND, NOR, XNOR, LSHL, LSHR and
@@ -52,6 +53,8 @@ enum CmpSrc {
#[derive(Clone, Copy, Debug)]
enum Insn {
SMov(u8, Src),
/// SOP1 unary opcode 7, 11, 13, 15, 17, 19, 21, 23, 25, 26 or 52.
SUnary(u32, u8, Src),
/// SOP2 opcode 0-5, 14, 16, 18 or an even opcode 20-34.
SAlu(u32, u8, Src, Src),
/// SOPC opcode 0-11: `s_cmp_{eq,lg,gt,ge,lt,le}_i32` then `_u32`.
@@ -208,6 +211,20 @@ fn run_wave(program: &[(usize, Insn)], wave: &[u32]) -> Result<Vec<u32>, String>
None
})
}
Insn::SUnary(opcode, dst, src) => {
if dst < 8 {
Err(format!("{offset:#x}: write to a resource SGPR"))
} else {
read_s(&sgpr, src).map(|value| {
let (result, flag) = scalar_unary(opcode, value);
sgpr[dst as usize] = Some(result);
if let Some(flag) = flag {
scc = Some(flag);
}
None
})
}
}
Insn::SMov(dst, src) => {
if dst < 8 {
Err(format!("{offset:#x}: write to a resource SGPR"))
@@ -377,6 +394,51 @@ fn scalar_compare(opcode: u32, a: u32, b: u32) -> bool {
}
}
/// RDNA 2 ISA guide 12.3, evaluated with arithmetic on the value (no host bit
/// intrinsics): D and, for NOT, BCNT0, BCNT1 and ABS, SCC = (D != 0).
fn scalar_unary(opcode: u32, value: u32) -> (u32, Option<bool>) {
// The bits from the LSB, as repeated halving produces them.
let mut bits = [false; 32];
let mut rest = u64::from(value);
for slot in &mut bits {
*slot = rest % 2 == 1;
rest /= 2;
}
let word = |bits: &dyn Fn(usize) -> bool| {
(0..32).rev().fold(0u64, |w, i| w * 2 + u64::from(bits(i))) as u32
};
let position = |found: Option<usize>| found.map_or(u32::MAX, |i| i as u32);
let ones = bits.iter().filter(|&&b| b).count() as u32;
let result = match opcode {
7 => 0xffff_ffff - value,
11 => word(&|i| bits[31 - i]),
13 => 32 - ones,
15 => ones,
17 => position(bits.iter().position(|&b| !b)),
19 => position(bits.iter().position(|&b| b)),
21 => position(bits.iter().rev().position(|&b| b)),
23 => position(
bits.iter()
.rev()
.skip(1)
.position(|&b| b != bits[31])
.map(|i| i + 1),
),
// signext of the low 8 or 16 bits: subtract 2^n when the value mod 2^n
// is at least 2^(n-1).
25 | 26 => {
let span = if opcode == 25 { 256 } else { 65_536 };
let low = i64::from(value) % span;
let signed = if low >= span / 2 { low - span } else { low };
signed.rem_euclid(1 << 32) as u32
}
// |S0| modulo 2^32, so INT_MIN stays 0x80000000.
_ => i64::from(value as i32).abs().rem_euclid(1 << 32) as u32,
};
let flag = matches!(opcode, 7 | 13 | 15 | 52).then_some(result != 0);
(result, flag)
}
fn read_vgpr(lane: &Lane, reg: u8, offset: usize) -> Result<u32, String> {
if reg == 0 {
return Err(format!("{offset:#x}: v0 is the resource index"));
@@ -824,6 +886,18 @@ fn decode_one(code: &[u8], offset: usize) -> Result<(Insn, usize), String> {
}
return Ok((Insn::VAddNc(first as u8, src0 as u8, src1 as u8), 8));
}
// SOP1: ENCODING 0b10_1111101 in bits 23-31, SDST 16-22, OP 8-15.
let sop1_op = (first >> 8) & 0xff;
if first >> 23 == 0b1_0111_1101
&& matches!(sop1_op, 7 | 11 | 13 | 15 | 17 | 19 | 21 | 23 | 25 | 26 | 52)
{
let dst = ((first >> 16) & 0x7f) as u8;
if dst > 105 {
return Err(fail("unsupported SOP1 destination"));
}
let (src, len) = scalar_source(code, offset, first as u8)?;
return Ok((Insn::SUnary(sop1_op, dst, src), len));
}
if first & 0xff80_ff00 == 0xbe80_0300 {
let dst = ((first >> 16) & 0x7f) as u8;
if dst > 107 && dst < 126 {