Skip to content

Instantly share code, notes, and snippets.

@peterdelevoryas
Created December 14, 2017 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterdelevoryas/d28d5169595bd82b3cee0ca9add98ec7 to your computer and use it in GitHub Desktop.
Save peterdelevoryas/d28d5169595bd82b3cee0ca9add98ec7 to your computer and use it in GitHub Desktop.
#![feature(conservative_impl_trait)]
type Reg = usize;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct Cpu {
reg: [isize; 3]
}
impl Cpu {
fn mov(&mut self, src: Reg, dst: Reg) {
self.reg[dst] = self.reg[src];
}
fn sub(&mut self, src: Reg, dst: Reg) {
self.reg[dst] -= self.reg[src];
}
}
fn main() {
let cpu = Cpu {
reg: [-32_819, 595, 222_212]
};
// a = b - c
//
// a = b
// a -= c
for (a, b, c) in alloc_permutations() {
let expected = cpu.reg[b] - cpu.reg[c];
let mut inst = cpu.clone();
inst.mov(b, a);
inst.sub(c, a);
if inst.reg[a] != expected {
println!("expected {:>width$} = {:>width$} - {:>width$}, got {:>width$}, a={} b={} c={} cpu={:?}",
expected, cpu.reg[b], cpu.reg[c],
inst.reg[a], a, b, c, inst, width=8);
println!("mov {}, {}", b, a);
println!("sub {}, {}", c, a);
println!();
}
}
}
fn alloc_permutations() -> impl Iterator<Item=(Reg, Reg, Reg)> {
(0..3).into_iter()
.flat_map(|a| {
(0..3).into_iter().map(move |b| (a, b))
})
.flat_map(|(a, b)| {
(0..3).into_iter().map(move |c| (a, b, c))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment