[ARITH] Tight bound for floormod#6771
Conversation
| /* let a / b = x + y, where x is integer, y \in [0, 1) | ||
| * floormod(a, b) = a - floordiv(a, b) * b | ||
| * floordiv(a, b) = x | ||
| * floormod(a, b) = a - floordiv(a, b) * b | ||
| * = a - x * b | ||
| * = a - (a / b - y) * b | ||
| * = a - a + y * b | ||
| * = y * b | ||
| * note that 0 <= y < 1 | ||
| * when b > 0, 0 <= b * y < b | ||
| * 0 <= b * y <= b - 1 | ||
| * when b < 0, b < b * y <= 0 | ||
| * b + 1 <= b * y <= 0 | ||
| * In all cases, min(0, b + 1) <= b * y <= max(0, b - 1) | ||
| * min(0, b_min + 1) <= b * y <= max(0, b_max - 1) | ||
| * That is, min(0, b_min + 1) <= floormod(a, b) <= max(0, b_max - 1) | ||
| */ | ||
| int64_t b_min_cap = InfAwareAdd(b.min_value, 1); |
There was a problem hiding this comment.
Just a suggestion: why don't you move this (very well done) explanation at the beginning of the function? It seems to cover more than just the else branch
There was a problem hiding this comment.
Sure. Thanks for the suggestion.
|
@tqchen The ir (before narrowing) is like Let In a simplified form, I guess |
|
It sort of make sense, because at the narrowing pass, we still don't know I added the above example as a test. The FPS improves to 400 with INDEX_DEFAULT_I64=ON |
|
I see, in this particular case, perhaps it makes sense to optimize such pattern and make sure It would also be useful to find out why cast(value, i32) is inserted since we preferred i64 in most cases. We can do that in another PR. This PR's improvement is certainly useful |
Estimate the range of
floormod(a, b)whenb < 0. Fix #6691