don't simplify if div folding resulted in negative numerator (#10064)

* don't simplify if div folding resulted in negative numerator

* test
This commit is contained in:
chenyu
2025-04-26 17:01:18 -04:00
committed by GitHub
parent 1805403821
commit 4c1ce1a299
2 changed files with 5 additions and 2 deletions

View File

@@ -332,6 +332,9 @@ class TestSymbolic(unittest.TestCase):
self.helper_test_variable((Variable("a", 0, 5)+4)//4, 1, 2, "((a//4)+1)")
self.helper_test_variable((Variable("a", 0, 5)+5)//4, 1, 2, "(((a+1)//4)+1)")
def test_div_neg_rem(self):
self.helper_test_variable((-Variable("a", 0, 255)+256)//2, 0, 128, "(((a*-1)+256)//2)")
def test_mul_div_factor_mul(self):
self.helper_test_variable((Variable("a", 0, 10)*8)//4, 0, 20, "(a*2)")

View File

@@ -170,8 +170,8 @@ def div_and_mod_folding(x: UOp, y: UOp, which: Literal[Ops.MOD, Ops.IDIV], split
rem += r//gcd * v
quo += q * v
# if numerator is negative, and it has remainder, don't simplify because C divmod is different from python divmod.
if x.vmin < 0 and remainders: return None
# if numerator before/after is negative, and it has remainder, don't simplify because C divmod is different from python divmod.
if (x.vmin < 0 or rem.vmin < 0) and remainders: return None
if which is Ops.MOD: return gcd*(rem % (c//gcd)) + const%gcd
return rem//(c//gcd)+quo