Files
betterxen/crates/decoder/tests/graphics.rs
T

86 lines
2.6 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
use xenora_decoder::{
Architecture, DecodeError, DecodeErrorKind, DecodedInstruction, ExportTarget, Instruction,
InterpolationPhase, decode,
};
#[test]
fn decodes_export_registers_and_interpolation_fields_in_byte_order() {
// Derived with llvm-mc -triple=amdgcn -mcpu=gfx1030 -show-encoding:
// exp param0, v255, v128, v15, v1
// v_interp_p1_f32 v255, v254, attr31.z
// v_interp_p2_f32 v128, v255, attr1.w
let bytes = [
0x0f, 0x02, 0x00, 0xf8, 0xff, 0x80, 0x0f, 0x01, 0xfe, 0x7e, 0xfc, 0xcb, 0xff, 0x07, 0x01,
0xca,
];
assert_eq!(
decode(Architecture::Gfx1030, &bytes).unwrap(),
[
DecodedInstruction {
offset: 0,
byte_len: 8,
instruction: Instruction::Export {
target: ExportTarget::Parameter0,
sources: [255, 128, 15, 1],
},
},
DecodedInstruction {
offset: 8,
byte_len: 4,
instruction: Instruction::Interpolate {
phase: InterpolationPhase::First,
dst: 255,
coordinate: 254,
attribute: 31,
component: 2,
},
},
DecodedInstruction {
offset: 12,
byte_len: 4,
instruction: Instruction::Interpolate {
phase: InterpolationPhase::Second,
dst: 128,
coordinate: 255,
attribute: 1,
component: 3,
},
},
],
);
}
#[test]
fn rejects_truncated_export_and_each_invalid_operand_field() {
let valid = 0xf800_180fu32;
let bytes = valid.to_le_bytes();
assert_eq!(
decode(Architecture::Gfx1030, &bytes).unwrap_err(),
DecodeError {
offset: 0,
kind: DecodeErrorKind::Truncated {
needed: 8,
available: 4
},
},
);
for bit in 0..26 {
let mut bytes = [0; 8];
bytes[..4].copy_from_slice(&(valid ^ (1 << bit)).to_le_bytes());
assert!(
matches!(
decode(Architecture::Gfx1030, &bytes),
Err(DecodeError {
offset: 0,
kind: DecodeErrorKind::UnsupportedForm {
mnemonic: "exp",
..
},
}),
),
"export bit {bit}"
);
}
}