all_u32/
all-u32.rs

1use std::process::exit;
2
3use riscv_codec::{assembly::assemble_line, instruction::Instruction,};
4fn main() {
5    let mut valid: u32 = 0;
6    for x in 0..u32::MAX {
7        if x % 10000 == 0 {
8            println!("{:.4}%", 100.0 * (x as f64) / (u32::MAX as f64))
9        }
10        if let Ok(i) = Instruction::decode(x) {
11            valid += 1;
12            let e = Instruction::encode(&i);
13            if x != e {
14                println!("i: {i}\nx: {x:032b}\ne: {e:032b}");
15                exit(1);
16            }
17
18            let d = i.to_string();
19            let i2 = assemble_line(&d).unwrap().i();
20            if i != i2 {
21                println!("disassembled {i:#?} to get {d}. Assembled to get {i2:#?}!");
22                exit(1);
23            }
24        }
25    }
26    println!("Done.");
27    println!("proportion of encoding space used: {}%", 100.0 * (valid as f64) / (u32::MAX as f64))
28}