Skip to content

Commit

Permalink
Use rc.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Dec 10, 2024
1 parent d21c061 commit 6bbaf54
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions executor/src/witgen/jit/symbolic_expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{self, Display, Formatter},
ops::{Add, BitAnd, BitOr, Mul, Neg},
rc::Rc,
};

use powdr_number::FieldElement;
Expand All @@ -17,12 +18,12 @@ pub enum SymbolicExpression<T: FieldElement, V> {
/// A symbolic value known at run-time, referencing either a cell or a local variable.
Variable(V, Option<RangeConstraint<T>>),
BinaryOperation(
Box<Self>,
Rc<Self>,
BinaryOperator,
Box<Self>,
Rc<Self>,
Option<RangeConstraint<T>>,
),
UnaryOperation(UnaryOperator, Box<Self>, Option<RangeConstraint<T>>),
UnaryOperation(UnaryOperator, Rc<Self>, Option<RangeConstraint<T>>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -141,9 +142,9 @@ impl<T: FieldElement, V: Clone> Add for &SymbolicExpression<T, V> {
SymbolicExpression::Concrete(*a + *b)
}
_ => SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::Add,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
self.range_constraint()
.zip(rhs.range_constraint())
.map(|(a, b)| a.combine_sum(&b)),
Expand All @@ -165,10 +166,12 @@ impl<T: FieldElement, V: Clone> Neg for &SymbolicExpression<T, V> {
fn neg(self) -> Self::Output {
match self {
SymbolicExpression::Concrete(n) => SymbolicExpression::Concrete(-*n),
SymbolicExpression::UnaryOperation(UnaryOperator::Neg, expr, _) => *expr.clone(),
SymbolicExpression::UnaryOperation(UnaryOperator::Neg, expr, _) => {
expr.as_ref().clone()
}
_ => SymbolicExpression::UnaryOperation(
UnaryOperator::Neg,
Box::new(self.clone()),
Rc::new(self.clone()),
self.range_constraint().map(|rc| rc.multiple(-T::from(1))),
),
}
Expand Down Expand Up @@ -200,9 +203,9 @@ impl<T: FieldElement, V: Clone> Mul for &SymbolicExpression<T, V> {
-self
} else {
SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::Mul,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
None,
)
}
Expand Down Expand Up @@ -232,9 +235,9 @@ impl<T: FieldElement, V: Clone> SymbolicExpression<T, V> {
} else {
// TODO other simplifications like `-x / -y => x / y`, `-x / concrete => x / -concrete`, etc.
SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::Div,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
None,
)
}
Expand All @@ -246,9 +249,9 @@ impl<T: FieldElement, V: Clone> SymbolicExpression<T, V> {
self.clone()
} else {
SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::IntegerDiv,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
None,
)
}
Expand All @@ -265,9 +268,9 @@ impl<T: FieldElement, V: Clone> BitAnd for &SymbolicExpression<T, V> {
SymbolicExpression::Concrete(T::from(0))
} else {
SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::BitAnd,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
self.range_constraint()
.zip(rhs.range_constraint())
.map(|(a, b)| a.conjunction(&b)),
Expand All @@ -294,9 +297,9 @@ impl<T: FieldElement, V: Clone> BitOr for &SymbolicExpression<T, V> {
SymbolicExpression::Concrete(T::from(v))
} else {
SymbolicExpression::BinaryOperation(
Box::new(self.clone()),
Rc::new(self.clone()),
BinaryOperator::BitOr,
Box::new(rhs.clone()),
Rc::new(rhs.clone()),
None,
)
}
Expand Down

0 comments on commit 6bbaf54

Please sign in to comment.