-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify how variants are handled (#877)
- Loading branch information
Showing
55 changed files
with
1,760 additions
and
2,481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
enum Op { | ||
Inc(v), | ||
Move(v), | ||
Loop(ops), | ||
Input, | ||
Print, | ||
} | ||
|
||
struct Tape { | ||
pos, | ||
tape, | ||
} | ||
|
||
impl Tape { | ||
fn new() { | ||
Tape { pos: 0, tape: [0] } | ||
} | ||
|
||
fn get(self) { | ||
self.tape[self.pos] | ||
} | ||
|
||
fn inc(self, x) { | ||
self.tape[self.pos] = (self.tape[self.pos] + x) % 256; | ||
|
||
if self.tape[self.pos] < 0 { | ||
self.tape[self.pos] = self.tape[self.pos] + 256; | ||
} | ||
} | ||
|
||
fn mov(self, x) { | ||
self.pos += x; | ||
|
||
while self.pos >= self.tape.len() { | ||
self.tape.push(0); | ||
} | ||
} | ||
|
||
fn set(self, v) { | ||
self.tape[self.pos] = v; | ||
} | ||
} | ||
|
||
fn run(program, tape, inputs) { | ||
for op in program { | ||
match op { | ||
Op::Inc(x) => tape.inc(x), | ||
Op::Move(x) => tape.mov(x), | ||
Op::Loop(program) => while tape.get() != 0 { | ||
run(program, tape, inputs); | ||
}, | ||
Op::Print => { | ||
let c = char::from_i64(tape.get()).expect("A valid char"); | ||
print!("{c}"); | ||
} | ||
Op::Input => { | ||
tape.set(0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn parse(it) { | ||
let buf = Vec::new(); | ||
|
||
while let Some(c) = it.next() { | ||
let op = match c { | ||
'+' => Op::Inc(1), | ||
'-' => Op::Inc(-1), | ||
'>' => Op::Move(1), | ||
'<' => Op::Move(-1), | ||
'.' => Op::Print, | ||
'[' => Op::Loop(parse(it)), | ||
',' => Op::Input, | ||
']' => break, | ||
_ => continue, | ||
}; | ||
|
||
buf.push(op); | ||
} | ||
|
||
buf | ||
} | ||
|
||
struct Program { | ||
ops, | ||
inputs, | ||
} | ||
|
||
impl Program { | ||
fn new(code, inputs) { | ||
Program { ops: parse(code), inputs } | ||
} | ||
|
||
fn run(self) { | ||
let tape = Tape::new(); | ||
run(self.ops, tape, self.inputs); | ||
} | ||
} | ||
|
||
fn bf(s, i) { | ||
let program = Program::new(s.chars(), i); | ||
program.run(); | ||
} | ||
|
||
#[bench] | ||
pub fn bf_hello_world(b) { | ||
b.iter( | ||
|| { | ||
bf( | ||
"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.", | ||
0, | ||
) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
fn fib(n) { | ||
if n <= 1 { | ||
n | ||
} else { | ||
fib(n - 2) + fib(n - 1) | ||
} | ||
} | ||
|
||
#[bench] | ||
pub fn fib15(b) { | ||
b.iter(|| fib(15)); | ||
} | ||
|
||
#[bench] | ||
pub fn fib20(b) { | ||
b.iter(|| fib(20)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const MAX_NUMBER_TO_CHECK = 10_000; | ||
|
||
#[bench] | ||
pub fn primes(b) { | ||
b.iter( | ||
|| { | ||
let prime_mask = []; | ||
|
||
prime_mask.resize(MAX_NUMBER_TO_CHECK, true); | ||
|
||
prime_mask[0] = false; | ||
prime_mask[1] = false; | ||
|
||
let total_primes_found = 0; | ||
|
||
for p in 2..MAX_NUMBER_TO_CHECK { | ||
if prime_mask[p] { | ||
total_primes_found += 1; | ||
let i = 2 * p; | ||
|
||
while i < MAX_NUMBER_TO_CHECK { | ||
prime_mask[i] = false; | ||
i += p; | ||
} | ||
} | ||
} | ||
|
||
total_primes_found | ||
}, | ||
); | ||
} |
Oops, something went wrong.