Skip to content

Commit

Permalink
switch to extra brillig op
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Nov 1, 2024
1 parent df6e1fe commit 4397055
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
3 changes: 3 additions & 0 deletions acvm-repo/brillig/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ pub enum BinaryIntOp {
Add,
Sub,
Mul,
CheckedAdd,
CheckedSub,
CheckedMul,
Div,
/// (==) equal
Equals,
Expand Down
12 changes: 9 additions & 3 deletions acvm-repo/brillig_vm/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ fn evaluate_binary_int_op_128(
lhs.wrapping_shr(rhs as u32)
}
}
BinaryIntOp::CheckedAdd => lhs.wrapping_add(rhs),
BinaryIntOp::CheckedSub => lhs.wrapping_sub(rhs),
BinaryIntOp::CheckedMul => lhs.wrapping_mul(rhs),
};
Ok(result)
}
Expand All @@ -163,20 +166,23 @@ fn evaluate_binary_int_op_generic(
let bit_modulo = 1 << bit_size;
let result = match op {
// Perform addition, subtraction, and multiplication, applying a modulo operation to keep the result within the bit size.
BinaryIntOp::Add => {
BinaryIntOp::Add => (lhs + rhs) % bit_modulo,
BinaryIntOp::Sub => (bit_modulo + lhs - rhs) % bit_modulo,
BinaryIntOp::Mul => (lhs * rhs) % bit_modulo,
BinaryIntOp::CheckedAdd => {
let result = (lhs + rhs) % bit_modulo;
if lhs > result {
return Err(BrilligArithmeticError::OverflowAdd);
}
result
}
BinaryIntOp::Sub => {
BinaryIntOp::CheckedSub => {
if rhs > lhs {
return Err(BrilligArithmeticError::OverflowSub);
}
(bit_modulo + lhs - rhs) % bit_modulo
}
BinaryIntOp::Mul => {
BinaryIntOp::CheckedMul => {
let result = (lhs * rhs) % bit_modulo;
if rhs != 0 && result / rhs != lhs {
return Err(BrilligArithmeticError::OverflowMul);
Expand Down
24 changes: 21 additions & 3 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,9 +1218,27 @@ impl<'block> BrilligBlock<'block> {
BrilligBinaryOp::Modulo
}
}
BinaryOp::Add => BrilligBinaryOp::Add,
BinaryOp::Sub => BrilligBinaryOp::Sub,
BinaryOp::Mul => BrilligBinaryOp::Mul,
BinaryOp::Add => {
if is_signed {
BrilligBinaryOp::Add
} else {
BrilligBinaryOp::CheckedAdd
}
}
BinaryOp::Sub => {
if is_signed {
BrilligBinaryOp::Sub
} else {
BrilligBinaryOp::CheckedSub
}
}
BinaryOp::Mul => {
if is_signed {
BrilligBinaryOp::Mul
} else {
BrilligBinaryOp::CheckedMul
}
}
BinaryOp::Eq => BrilligBinaryOp::Equals,
BinaryOp::Lt => {
if is_signed {
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ impl DebugToString for BrilligBinaryOp {
BrilligBinaryOp::Add => "+".into(),
BrilligBinaryOp::Sub => "-".into(),
BrilligBinaryOp::Mul => "*".into(),
BrilligBinaryOp::CheckedAdd => "+".into(),
BrilligBinaryOp::CheckedSub => "-".into(),
BrilligBinaryOp::CheckedMul => "*".into(),
BrilligBinaryOp::Equals => "==".into(),
BrilligBinaryOp::FieldDiv => "f/".into(),
BrilligBinaryOp::UnsignedDiv => "/".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ pub(crate) enum BrilligBinaryOp {
Add,
Sub,
Mul,
CheckedAdd,
CheckedSub,
CheckedMul,
FieldDiv,
UnsignedDiv,
Equals,
Expand All @@ -458,6 +461,9 @@ impl From<BrilligBinaryOp> for BinaryFieldOp {
BrilligBinaryOp::Add => BinaryFieldOp::Add,
BrilligBinaryOp::Sub => BinaryFieldOp::Sub,
BrilligBinaryOp::Mul => BinaryFieldOp::Mul,
BrilligBinaryOp::CheckedAdd => BinaryFieldOp::Add,
BrilligBinaryOp::CheckedSub => BinaryFieldOp::Sub,
BrilligBinaryOp::CheckedMul => BinaryFieldOp::Mul,
BrilligBinaryOp::FieldDiv => BinaryFieldOp::Div,
BrilligBinaryOp::UnsignedDiv => BinaryFieldOp::IntegerDiv,
BrilligBinaryOp::Equals => BinaryFieldOp::Equals,
Expand All @@ -474,6 +480,9 @@ impl From<BrilligBinaryOp> for BinaryIntOp {
BrilligBinaryOp::Add => BinaryIntOp::Add,
BrilligBinaryOp::Sub => BinaryIntOp::Sub,
BrilligBinaryOp::Mul => BinaryIntOp::Mul,
BrilligBinaryOp::CheckedAdd => BinaryIntOp::CheckedAdd,
BrilligBinaryOp::CheckedSub => BinaryIntOp::CheckedSub,
BrilligBinaryOp::CheckedMul => BinaryIntOp::CheckedMul,
BrilligBinaryOp::UnsignedDiv => BinaryIntOp::Div,
BrilligBinaryOp::Equals => BinaryIntOp::Equals,
BrilligBinaryOp::LessThan => BinaryIntOp::LessThan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,34 @@ pub(super) fn compile_prepare_vector_insert_procedure<F: AcirField + DebugToStri
);

// Compute the number of elements to the right of the insertion index
let element_count_to_the_right = brillig_context.allocate_register();
let element_count_to_the_right: MemoryAddress = brillig_context.allocate_register();
brillig_context.memory_op_instruction(
source_size.address,
index.address,
element_count_to_the_right,
BrilligBinaryOp::Sub,
);

// Copy the elements to the right of the index
brillig_context.codegen_mem_copy_from_the_end(
source_pointer_at_index,
target_pointer_after_index,
SingleAddrVariable::new_usize(element_count_to_the_right),
let num_elements_variable: SingleAddrVariable =
SingleAddrVariable::new_usize(element_count_to_the_right);
// brillig_context.codegen_branch(condition, f);
let is_num_items_zero = brillig_context.allocate_register();
brillig_context.codegen_usize_op(
num_elements_variable.address,
is_num_items_zero,
BrilligBinaryOp::Equals,
0,
);

brillig_context.codegen_if_not(is_num_items_zero, |ctx| {
// Copy the elements to the right of the index
ctx.codegen_mem_copy_from_the_end(
source_pointer_at_index,
target_pointer_after_index,
num_elements_variable,
);
});

brillig_context.deallocate_single_addr(source_rc);
brillig_context.deallocate_single_addr(source_size);
brillig_context.deallocate_single_addr(source_capacity);
Expand Down
3 changes: 3 additions & 0 deletions tooling/profiler/src/opcode_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ fn format_binary_int(op: &BinaryIntOp) -> String {
BinaryIntOp::Xor => "xor".to_string(),
BinaryIntOp::Shl => "shl".to_string(),
BinaryIntOp::Shr => "shr".to_string(),
BinaryIntOp::CheckedAdd => "c_add".to_string(),
BinaryIntOp::CheckedSub => "c_sub".to_string(),
BinaryIntOp::CheckedMul => "c_mul".to_string(),
}
}

Expand Down

0 comments on commit 4397055

Please sign in to comment.