Repository navigation
Fix automatic result type of % when the divisor excludes zero - #153
Open
vera-rubin wants to merge 1 commit into
Open
vera-rubin wants to merge 1 commit into
vera-rubin wants to merge 1 commit into
Conversation
With automatic promotion, modulus_result chose the result type from t_interval / u_interval when the divisor range excludes zero. The operation is done in that type, so a dividend or divisor which doesn't fit was truncated. For example 999 % 1001 with safe_unsigned_range<0, 1000, automatic> and safe_unsigned_literal<1001, automatic> used 8 bit storage and returned 231. Keep the quotient interval and add both operand intervals to it. a % b is undefined if a / b doesn't fit, so the quotient still has to fit. The remainder lies between zero and the dividend, so it fits as well.
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.
modulus_resultusedt_interval / u_intervalstorage underautomaticfor zero-excluding divisor ranges, truncating unfit operands during%:999 % 1001withsafe_unsigned_range<0, 1000, automatic>andsafe_unsigned_literal<1001, automatic>returned 231 using 8-bit storage.Only storage selection changes:
(t_interval / u_interval) | t_interval | u_intervalfits operands, preserving supported widening for signed minima modulo -1 because C++ requires representable quotients.Bounds still use interval corners in
safe_base_operations.hpp(#132):safe<std::uint32_t, automatic>modulo literal 10 declares [0,5]. Zero-containing divisor ranges andsafe<std::int64_t, automatic>minimum modulo literal -1 undefined behavior stay unchanged.test3()(test/test_range.cpp): fails base headers, passes fix. Directtest/Jamfile.v2matches base: 90/90 debug, 91/91 release. b2/CI, MSVC, other standards untested.Version: fix
42ae4c11ad4d06b325e495ccdbfa3e2160ea2b1f, basebe8f6473d3bbb5c546b0a031a9e78f9ba91579c9, GCC 13.3, Clang 18, C++14, Boost 1.83 headers, Linux x86_64.Fixes #152
— ChipLab