add more test values around i32 and i64 overflow
[power-instruction-analyzer.git] / src / main.rs
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // See Notices.txt for copyright information
3
4 use power_instruction_analyzer::{
5 CarryFlags, Instr, InstructionInput, InstructionInputRegister, MissingInstructionInput,
6 OverflowFlags, TestCase, WholeTest,
7 };
8
9 const TEST_VALUES: &[u64] = &[
10 0x0,
11 0x1,
12 0x2,
13 0xFFFF_FFFF_FFFF_FFFF,
14 0xFFFF_FFFF_FFFF_FFFE,
15 0x7FFF_FFFF_FFFF_FFFE,
16 0x7FFF_FFFF_FFFF_FFFF,
17 0x8000_0000_0000_0000,
18 0x8000_0000_0000_0001,
19 0x1234_5678_0000_0000,
20 0x1234_5678_7FFF_FFFE,
21 0x1234_5678_7FFF_FFFF,
22 0x1234_5678_8000_0000,
23 0x1234_5678_8000_0001,
24 0x1234_5678_FFFF_FFFF,
25 ];
26
27 const BOOL_VALUES: &[bool] = &[false, true];
28
29 fn call_with_inputs(
30 mut inputs: InstructionInput,
31 input_registers: &[InstructionInputRegister],
32 f: &mut impl FnMut(InstructionInput) -> Result<(), MissingInstructionInput>,
33 ) -> Result<(), MissingInstructionInput> {
34 if let Some((&input_register, input_registers)) = input_registers.split_first() {
35 match input_register {
36 InstructionInputRegister::Ra => {
37 for &i in TEST_VALUES {
38 inputs.ra = Some(i);
39 call_with_inputs(inputs, input_registers, f)?;
40 }
41 }
42 InstructionInputRegister::Rb => {
43 for &i in TEST_VALUES {
44 inputs.rb = Some(i);
45 call_with_inputs(inputs, input_registers, f)?;
46 }
47 }
48 InstructionInputRegister::Rc => {
49 for &i in TEST_VALUES {
50 inputs.rc = Some(i);
51 call_with_inputs(inputs, input_registers, f)?;
52 }
53 }
54 InstructionInputRegister::Carry => {
55 for &ca in BOOL_VALUES {
56 for &ca32 in BOOL_VALUES {
57 inputs.carry = Some(CarryFlags { ca, ca32 });
58 call_with_inputs(inputs, input_registers, f)?;
59 }
60 }
61 }
62 InstructionInputRegister::Overflow => {
63 for &so in BOOL_VALUES {
64 for &ov in BOOL_VALUES {
65 for &ov32 in BOOL_VALUES {
66 inputs.overflow = Some(OverflowFlags { so, ov, ov32 });
67 call_with_inputs(inputs, input_registers, f)?;
68 }
69 }
70 }
71 }
72 }
73 } else {
74 f(inputs)?;
75 }
76 Ok(())
77 }
78
79 fn main() -> Result<(), String> {
80 let mut test_cases = Vec::new();
81 let mut any_model_mismatch = false;
82 for &instr in Instr::VALUES {
83 call_with_inputs(
84 InstructionInput::default(),
85 instr.get_used_input_registers(),
86 &mut |inputs| -> Result<(), _> {
87 let model_outputs = instr.get_model_fn()(inputs)?;
88 #[cfg(feature = "native_instrs")]
89 let native_outputs = Some(instr.get_native_fn()(inputs)?);
90 #[cfg(not(feature = "native_instrs"))]
91 let native_outputs = None;
92 let model_mismatch = match native_outputs {
93 Some(native_outputs) if native_outputs != model_outputs => true,
94 _ => false,
95 };
96 any_model_mismatch |= model_mismatch;
97 test_cases.push(TestCase {
98 instr,
99 inputs,
100 native_outputs,
101 model_outputs,
102 model_mismatch,
103 });
104 Ok(())
105 },
106 )
107 .map_err(|err| format!("instruction {}: {}", instr.name(), err))?;
108 }
109 let whole_test = WholeTest {
110 test_cases,
111 any_model_mismatch,
112 };
113 serde_json::to_writer_pretty(std::io::stdout().lock(), &whole_test).unwrap();
114 println!();
115 Ok(())
116 }