Skip to content

Commit

Permalink
feat: replace modulo operations with truncations where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 12, 2024
1 parent fc32703 commit 32a40f3
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ impl Binary {
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
if operand_type.is_unsigned() {
// lhs % 2**bit_size is equivalent to truncating `lhs` to `bit_size` bits.
// We then convert to a truncation for consistency, allowing more optimizations.
if let Some(modulus) = rhs {
let modulus = modulus.to_u128();
if modulus.is_power_of_two() {
let bit_size = modulus.ilog2();

Check warning on line 150 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ilog)
return SimplifyResult::SimplifiedToInstruction(
Instruction::Truncate {
value: self.lhs,
bit_size,
max_bit_size: operand_type.bit_size(),
},
);
}
}
}
}
BinaryOp::Eq => {
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) {
Expand Down

0 comments on commit 32a40f3

Please sign in to comment.