add rules for mod(mul(X, Y), A) & mod(add(X, Y), A)

This commit is contained in:
Daniel Lupu 2022-05-02 00:10:37 +03:00 committed by wechman
parent 75300c32a8
commit 8c5f5c7db0
3 changed files with 45 additions and 1 deletions

View File

@ -279,7 +279,7 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
Pattern B, Pattern B,
Pattern, Pattern,
Pattern X, Pattern X,
Pattern Pattern Y
) )
{ {
using Word = typename Pattern::Word; using Word = typename Pattern::Word;
@ -287,6 +287,24 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
std::vector<SimplificationRule<Pattern>> rules; std::vector<SimplificationRule<Pattern>> rules;
// Replace MOD(MUL(X, Y), A) with MULMOD(X, Y, A) iff A=2**N
rules.push_back({
Builtins::MOD(Builtins::MUL(X, Y), A),
[=]() -> Pattern { return Builtins::MULMOD(X, Y, A); },
[=] {
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
}
});
// Replace MOD(ADD(X, Y), A) with ADDMOD(X, Y, A) iff A=2**N
rules.push_back({
Builtins::MOD(Builtins::ADD(X, Y), A),
[=]() -> Pattern { return Builtins::ADDMOD(X, Y, A); },
[=] {
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
}
});
// Replace MOD X, <power-of-two> with AND X, <power-of-two> - 1 // Replace MOD X, <power-of-two> with AND X, <power-of-two> - 1
for (size_t i = 0; i < Pattern::WordSize; ++i) for (size_t i = 0; i < Pattern::WordSize; ++i)
{ {

View File

@ -0,0 +1,13 @@
{
mstore(0, mod(add(mload(0), mload(1)), 32))
}
// ----
// step: expressionSimplifier
//
// {
// {
// let _3 := mload(1)
// let _4 := 0
// mstore(_4, addmod(mload(_4), _3, 32))
// }
// }

View File

@ -0,0 +1,13 @@
{
mstore(0, mod(mul(mload(0), mload(1)), 32))
}
// ----
// step: expressionSimplifier
//
// {
// {
// let _3 := mload(1)
// let _4 := 0
// mstore(_4, mulmod(mload(_4), _3, 32))
// }
// }