Skip to content

Commit

Permalink
Merge branch 'ab/unchecked-math' of github.com:noir-lang/noir into ab…
Browse files Browse the repository at this point in the history
…/unchecked-math
  • Loading branch information
asterite committed Jan 10, 2025
2 parents 7840ced + dfad378 commit c10520c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions compiler/noirc_evaluator/src/ssa/opt/remove_bit_shifts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Context<'_> {
let pow = self.pow(base, rhs);
let pow = self.insert_cast(pow, typ);

// Unchecked mul because predicate is 0 or 1
// Unchecked mul because `predicate` will be 1 or 0
(
FieldElement::max_num_bits(),
self.insert_binary(predicate, BinaryOp::Mul { unchecked: true }, pow),
Expand Down Expand Up @@ -218,8 +218,8 @@ impl Context<'_> {
let rhs_bits = rhs_bits[0];
let one = self.field_constant(FieldElement::one());
let mut r = one;
// All operations are unchecked as we're acting on Field types (which are always unchecked)
for i in 1..bit_size + 1 {
// All of these operations are unchecked because they deal with fields
let r_squared = self.insert_binary(r, BinaryOp::Mul { unchecked: true }, r);
let a = self.insert_binary(r_squared, BinaryOp::Mul { unchecked: true }, lhs);
let idx = self.field_constant(FieldElement::from((bit_size - i) as i128));
Expand Down
5 changes: 3 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl<'a> FunctionContext<'a> {
BinaryOp::Mul { unchecked: true },
two_complement,
);
// Unchecked addition because either `positive_predicate` or `negative_predicate` will be 0
self.builder.insert_binary(
positive_predicate,
BinaryOp::Add { unchecked: true },
Expand Down Expand Up @@ -488,7 +489,7 @@ impl<'a> FunctionContext<'a> {
//Check the result has the same sign as its inputs
let result_sign = self.builder.insert_binary(result, BinaryOp::Lt, half_width);
let sign_diff = self.builder.insert_binary(result_sign, BinaryOp::Eq, lhs_sign);
// Unchecked mul because sign_diff is a boolean
// Unchecked multiplication because boolean inputs
let sign_diff_with_predicate = self.builder.insert_binary(
sign_diff,
BinaryOp::Mul { unchecked: true },
Expand Down Expand Up @@ -522,7 +523,7 @@ impl<'a> FunctionContext<'a> {
let not_same = self.builder.insert_not(same_sign);
let not_same_sign_field =
self.insert_safe_cast(not_same, NumericType::unsigned(bit_size), location);
// This shouldn't overflow so it's unchecked
// TODO: should this be unchecked?
let positive_maximum_with_offset = self.builder.insert_binary(
half_width,
BinaryOp::Add { unchecked: true },
Expand Down

0 comments on commit c10520c

Please sign in to comment.