Repository navigation
Conversation
Clock dividers, prescalers and tick counts need a quotient that rounds up, and every caller wrote its own. hal::divide_round_up() is that, and can't overflow the way adding the denominator before dividing can. hal::rounding_division() was wrong for some inputs: 7 / 3 gave 3, 10 / 7 gave 2, 3 / 4 and 1 / 2 gave 0, and a zero denominator gave 0. It now calls the new hal::divide_round_nearest(), which rounds to the nearest integer and a half away from zero, for any integral type.
This branch has not been deployed
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.
Adds
hal::divide_round_up()andhal::divide_round_nearest()to v6, and fixeshal::rounding_division()by having it call the second. Closes #127.Changes
hal::divide_round_up(numerator, denominator)takes unsigned operands and returns the quotient, plus 1 if there is a remainder:(a / b) + (a % b != 0). It can't overflow, which(a + b - 1) / bcan for a numerator near the type's largest value. With Clang 23 at-Os, a 32-bit call on a Cortex-M3 isudiv,mls, a compare and a conditional add. On a Cortex-M0+ it is one call to__aeabi_uidiv, and a 64-bit call is one call to__aeabi_uldivmod.hal::divide_round_nearest(numerator, denominator)takes any integral type and rounds to the nearest integer, with a half rounding away from zero (5 / 2 is 3, -5 / 2 is -3). It works on the magnitudes in the unsigned type, so the smallest signed value doesn't overflow.hal::rounding_division()keeps itshal::division<T>signature and callsdivide_round_nearest(). The old version gave wrong results:/Choices to review
These are my answers to the questions in #127, and each is easy to change:
/.{ quotient, remainder }. A caller that wants the rate it actually reaches can work it out from the quotient.rounding_division()keeps the struct because it already exists, and nothing in the org calls it./. It is a compile error in a constant expression.Not in this PR
The hand-written round-ups listed in #127 (
cycles_per, the arm-mcu SPI prescaler, ADC wait, I2C timing and SysTick counts) still use their own code. They can switch todivide_round_up()once this is released.Testing
tests/math.test.cppcovers exact and inexact division, a half in each sign combination, the inputs from the table above, and the largest and smallest values of 32-bit and 64-bit types. All 17 test suites pass with Clang 23, ASan and clang-tidy as warnings-as-errors.