Skip to content

Commit

Permalink
line: Avoid division when maximum_operations_per_instruction == 1
Browse files Browse the repository at this point in the history
Before:
test bench_executing_line_number_programs ... bench: 246,375 ns/iter (+/- 11,192)

After:
test bench_executing_line_number_programs ... bench: 173,505 ns/iter (+/- 4,188)
  • Loading branch information
philipc committed Dec 30, 2017
1 parent f67d15c commit 35c7750
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,16 @@ where
let maximum_operations_per_instruction =
self.header().maximum_operations_per_instruction as u64;

let op_index_with_advance = self.row.registers.op_index + operation_advance;

self.row.registers.address += minimum_instruction_length *
(op_index_with_advance / maximum_operations_per_instruction);

self.row.registers.op_index = op_index_with_advance % maximum_operations_per_instruction;
if maximum_operations_per_instruction == 1 {
self.row.registers.address += minimum_instruction_length * operation_advance;
self.row.registers.op_index = 0;
} else {
let op_index_with_advance = self.row.registers.op_index + operation_advance;
self.row.registers.address += minimum_instruction_length
* (op_index_with_advance / maximum_operations_per_instruction);
self.row.registers.op_index =
op_index_with_advance % maximum_operations_per_instruction;
}
}

fn adjust_opcode(&self, opcode: u8) -> u8 {
Expand Down

0 comments on commit 35c7750

Please sign in to comment.