shader: add s_add_i32 and s_sub_i32 with signed-overflow SCC

#185 left both forms unsupported because the pinned XML says only
"carry-out" for them. The AMD RDNA 2 ISA reference guide (GPUOpen,
section 12.1 SOP2 Instructions, p. 101-102, and section 5.3 Scalar
Condition Code, p. 33) defines SCC for both as signed overflow:
add overflows when the operands share a sign that the result lacks;
subtract when the operands' signs differ and the result's sign differs
from S0's. The guide also confirms the unsigned rules used in #185.

Adds both forms to the generated opcodes, the decoder, the reference
(exact signed result must fit in i32), the independent interpreter (the
guide's sign-bit rule over its ripple adder) and the translator, plus
400 boundary programs, eight hand-worked overflow and no-overflow cases
(-1 + 1 and 0 - 1 carry or borrow but do not overflow) and two signed
overflows in the scalar ALU fixture. Inverting the overflow rule in any
one implementation fails three tests. s_min_i32 and the other SOP2
forms stay unsupported.

Refs #185

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
This commit is contained in:
elforeign committed 2026-11-24 01:48:52 -06:00
1 parent 1212280ab8
commit 425327a0f2
18 files changed
+248 -63

No files matched your search

+17 -2
View File
@@ -85,6 +85,9 @@ pub enum ScalarAlu {
Subtract,
AddWithCarry,
SubtractWithBorrow,
/// SCC = signed overflow (RDNA 2 ISA guide 12.1).
AddSigned,
SubtractSigned,
And,
Or,
Xor,
@@ -231,15 +234,17 @@ pub fn decode(code: &[u8]) -> Result<Vec<DecodedInstruction>, ReferenceError> {
left,
right,
low,
high @ (0x80 | 0x82 | 0x87 | 0x88 | 0x89),
high @ (0x80..=0x82 | 0x87 | 0x88 | 0x89),
tail @ ..,
] if (*left, *right) != (255, 255)
&& low & 0x7f <= 105
&& (low & 0x80 == 0 || matches!(high, 0x80 | 0x82)) =>
&& (low & 0x80 == 0 || matches!(high, 0x80..=0x82)) =>
{
let kind = match (high, low >> 7) {
(0x80, 0) => ScalarAlu::Add,
(0x80, _) => ScalarAlu::Subtract,
(0x81, 0) => ScalarAlu::AddSigned,
(0x81, _) => ScalarAlu::SubtractSigned,
(0x82, 0) => ScalarAlu::AddWithCarry,
(0x82, _) => ScalarAlu::SubtractWithBorrow,
(0x87, _) => ScalarAlu::And,
@@ -546,6 +551,16 @@ fn execute_lane(
let difference = lhs - rhs - carry;
(difference, difference < 0)
}
// The exact signed result must fit in i32, else SCC = overflow.
ScalarAlu::AddSigned | ScalarAlu::SubtractSigned => {
let signed = |value: i64| i64::from(value as u32 as i32);
let exact = if kind == ScalarAlu::AddSigned {
signed(lhs) + signed(rhs)
} else {
signed(lhs) - signed(rhs)
};
(exact, i32::try_from(exact).is_err())
}
ScalarAlu::And | ScalarAlu::Or | ScalarAlu::Xor => {
let bits = match kind {
ScalarAlu::And => lhs & rhs,