Fix fast_integer_divide for signed numerator with denominator == 1 - #9271
Merged
Conversation
Member
Author
|
This was found and fixed by Opus while working on something else. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9271 +/- ##
==========================================
+ Coverage 70.18% 70.26% +0.07%
==========================================
Files 257 257
Lines 79105 79109 +4
Branches 18954 18954
==========================================
+ Hits 55518 55583 +65
+ Misses 17908 17866 -42
+ Partials 5679 5660 -19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
Please fix test/const_division.cpp to include denominators of 1 (and zero) rather than adding a new test. |
The signed round-toward-negative-infinity branch rewrites `numerator` in place (numerator = xsign ^ numerator) to fold in the sign, but the denominator == 1 special case then returned that bit-flipped value instead of the original. So fast_integer_divide(n, 1) returned ~n for negative signed n (e.g. int8 -128 -> 127, -5 -> 4, 0 -> -1). Capture the original numerator before the branch and use it in the denominator == 1 select. The unsigned and round-to-zero branches never reassigned numerator, so they are unaffected. Add test/correctness/fast_integer_divide.cpp, which compares fast_integer_divide / fast_integer_modulo against floor division over u8/u16/u32/s8/s16/s32; it exercises the signed denominator == 1 case that previously went uncovered. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Previously fast_integer_divide (and fast_integer_divide_round_to_zero) treated a zero denominator as a divide by 256, and fast_integer_modulo inherited that via numerator - ratio*denominator (using the raw 0, not 256, so it just returned the numerator unchanged). Make both match Halide's ordinary / and %, which are total functions defined to return 0 for a zero denominator. Replace test/correctness/fast_integer_divide.cpp, added in c7ec49b to cover the denominator == 1 bug, with an exhaustive check in test/performance/const_division.cpp that compares fast_integer_divide/ fast_integer_divide_round_to_zero/fast_integer_modulo against a reference across the full uint8 denominator space, 0 to 255. This couldn't be done by simply widening the existing f/g/h sweep, since `f` unrolls the denominator into a compile-time constant and Halide disallows a literal-zero constant divisor. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
alexreinking
force-pushed
the
alexreinking/fast-integer-divide-d1-fix
branch
from
August 4, 2026 20:13
5efb299 to
1b41862
Compare
Member
Author
|
Adding |
abadams
approved these changes
Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The signed round-toward-negative-infinity branch of
fast_integer_dividerewritesnumeratorin place (numerator = xsign ^ numerator) to fold in the sign, but thedenominator == 1special case then returned that bit-flipped value instead of the original. Sofast_integer_divide(n, 1)returned~nfor negative signedn(e.g. int8-128->127,-5->4,0->-1).Capture the original numerator before the branch and use it in the
denominator == 1select. The unsigned and round-to-zero branches never reassignednumerator, so they are unaffected.Adds
test/correctness/fast_integer_divide.cpp, which comparesfast_integer_divide/fast_integer_moduloagainst floor division over u8/u16/u32/s8/s16/s32; it exercises the signed denominator == 1 case that previously went uncovered.Breaking changes
fast_integer_dividenow returns0with a zero denominator.fast_integer_modulonow returns0with a0modulus.Checklist