From bb7888b2817bb70644f4e843a88dc7a722e3fb89 Mon Sep 17 00:00:00 2001 From: chenyu Date: Tue, 10 Mar 2026 05:21:32 -0400 Subject: [PATCH] cleanup (x%(k*c))//c and (x%(k*c))%c (#15206) these two are in the same family --- tinygrad/uop/divandmod.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tinygrad/uop/divandmod.py b/tinygrad/uop/divandmod.py index f41cdf959a..75f0a65cc3 100644 --- a/tinygrad/uop/divandmod.py +++ b/tinygrad/uop/divandmod.py @@ -22,12 +22,9 @@ def fold_divmod_general(d: UOp, correct_divmod_folding: bool) -> UOp|None: # ** Constant Denominator Rules ** # these rules strictly require y to be a scalar constant > 0 if y.op is Ops.CONST and (c := y.arg) > 0: - # canonicalize_mod_div: (x%(d*k))//d -> (x//d)%k, puts nested div/mod in div-first canonical form for recombine - if d.op is Ops.IDIV and x.op is Ops.MOD and x.src[1].op is Ops.CONST and x.vmin >= 0 and x.src[1].arg % c == 0: - return x.src[0] // y % x.ufix(x.src[1].arg // c) - - # remove_nested_mod: (x%(k*c))%c -> x%c - if d.op is Ops.MOD and x.op is Ops.MOD and x.src[1].divides(c) is not None: return x.src[0] % y + # nested_div_mod: (x%(k*c))//c -> (x//c)%k, and (x%(k*c))%c -> x%c + if x.op is Ops.MOD and (k := x.src[1].divides(c)) is not None: + return x.src[0] // y % k if d.op is Ops.IDIV else x.src[0] % y # remove_nested_mod in sum: (a%4 + b)%2 -> (a+b)%2, requires non-negative sums if d.op is Ops.MOD and x.vmin >= 0: