Skip to content

Commit b5ede40

Browse files
committed
support bitwise operations in formulas
1 parent 6343fcb commit b5ede40

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To install or update LODA, please follow the [installation instructions](https:/
66

77
* Improve negativity check in formula generation
88
* Simplify formulas generated from `nrt` operations
9+
* Limited support for bit-wise operations in formulas
910

1011
## v24.12.8
1112

src/form/formula_gen.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ Expression FormulaGenerator::divToFraction(
6868
return wrapper;
6969
}
7070

71+
bool FormulaGenerator::bitfunc(const std::string& name, const Expression& a,
72+
const Expression& b, Expression& res) const {
73+
if (ExpressionUtil::canBeNegative(a, offset) ||
74+
ExpressionUtil::canBeNegative(b, offset)) {
75+
// TODO: remove this limitation
76+
return false;
77+
}
78+
res = Expression(Expression::Type::FUNCTION, name, {a, b});
79+
return true;
80+
}
81+
7182
bool FormulaGenerator::update(const Operation& op) {
7283
auto source = operandToExpression(op.source);
7384
auto target = operandToExpression(op.target);
@@ -162,6 +173,18 @@ bool FormulaGenerator::update(const Operation& op) {
162173
res = Expression(Expression::Type::FUNCTION, "max", {prevTarget, source});
163174
break;
164175
}
176+
case Operation::Type::BAN: {
177+
okay = bitfunc("bitand", prevTarget, source, res);
178+
break;
179+
}
180+
case Operation::Type::BOR: {
181+
okay = bitfunc("bitor", prevTarget, source, res);
182+
break;
183+
}
184+
case Operation::Type::BXO: {
185+
okay = bitfunc("bitxor", prevTarget, source, res);
186+
break;
187+
}
165188
case Operation::Type::SEQ: {
166189
res = Expression(Expression::Type::FUNCTION,
167190
ProgramUtil::idStr(source.value.asInt()), {prevTarget});

src/form/formula_gen.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class FormulaGenerator {
3737
Expression divToFraction(const Expression& numerator,
3838
const Expression& denominator) const;
3939

40+
bool bitfunc(const std::string& name, const Expression& a,
41+
const Expression& b, Expression& res) const;
42+
4043
bool update(const Operation& op);
4144

4245
bool update(const Program& p);

0 commit comments

Comments
 (0)