core/macros: rewrite DIV_ROUND add DIV_ROUND_UINT #18856
Closed
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.
with #18853 i had hope to remove
SIGNOF
fromDIV_ROUND
but tests showed that types and negative numbers had some false results, so this makesDIV_ROUND
deliver what it promisses (math rounded division for integers that don't reach to high into the rage)There are some variants to implement such thing at https://godbolt.org/z/TsKcnPoYx sadly most of them either fail in a specific range or if divisor or dividend is negative or unsigned are typical, so i choose the one that worked across the test range (DIV_ROUNDb) please feel free to find a better one.
Condition:
Working for either
i
orj
or both beinguint
and for negative an positive values ofi
andj
Contribution description
This
DIV_ROUND
works for:a
orb
or both beinguint
anda
andb
as expectedcurrent upstream tells me -15 / -5 = rounded to nearest is 2 (that is wrong)
with this PR i get 3 (see the godbolt)
Oh and i shall not forget i added
DIV_ROUND_UINT
for the most likely usecasesthis also circumvents the
-Wtype-limits
warning by having the same result for positive and 0 but 2 checks (thatat ar optimized away) an alternative would be to have 3 result 1,0,-1 orTesting procedure
read, run tests, look at that linked compiler explorer
Issues/PRs references
this fixes the issue of #18849
#18853 was my first atempt on this
https://godbolt.org/z/TsKcnPoYx