Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssa refactor): ACIR gen NOT integer #1749

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,14 @@ impl AcirContext {
}

/// Adds a new variable that is constrained to be the logical NOT of `x`.
///
/// `x` must be a 1-bit integer (i.e. a boolean)
pub(crate) fn not_var(&mut self, x: AcirVar) -> AcirVar {
// Since `x` can only be 0 or 1, we can derive NOT as 1 - x
self.add_data(AcirVarData::Expr(&Expression::one() - self.vars[x].to_expression().as_ref()))
pub(crate) fn not_var(&mut self, x: AcirVar, typ: AcirType) -> Result<AcirVar, AcirGenError> {
if typ.is_signed() {
todo!("implement NOT for signed integers");
}
let bit_size = typ.bit_size();
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
// Subtracting from max flips the bits
let max = self.add_constant(FieldElement::from((1_u128 << bit_size) - 1));
self.sub_var(max, x)
}

/// Returns an `AcirVar` that is constrained to be `lhs << rhs`.
Expand Down
10 changes: 8 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,14 @@ impl Context {
}
}
Instruction::Not(value_id) => {
let boolean_var = self.convert_numeric_value(*value_id, dfg);
let result_acir_var = self.acir_context.not_var(boolean_var);
let (acir_var, typ) = match self.convert_value(*value_id, dfg) {
AcirValue::Var(acir_var, typ) => (acir_var, typ),
_ => unreachable!("NOT is only applied to numerics"),
};
let result_acir_var = self
.acir_context
.not_var(acir_var, typ)
.expect("add Result types to all methods so errors bubble up");
self.define_result_var(dfg, instruction_id, result_acir_var);
}
Instruction::Truncate { value, bit_size, max_bit_size } => {
Expand Down