Repository navigation
✨ (minor) v6: Add hal::divide() with floor, ceil and round - #131
Merged
Merged
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.
kammce
commented
Nov 16, 2026
kammce
left a comment
Member
Author
There was a problem hiding this comment.
Looks good but I'm think it might be better to have a single divide function that accepts a enum as a template parameter to decide the rounding type. Like div::ceil, round, floor.
hal::divide<Mode, T>() replaces hal::divide_round_up(),
hal::divide_round_nearest() and hal::rounding_division(). The mode is
rounding::floor, ceil or round, each rounding the exact quotient like
the <cmath> function of the same name. Truncation stays plain /.
The operands are always named, { .num = ..., .den = ... }, so a call
can't swap them, and giving T keeps a narrowing operand a compile error.
A denominator of 0 divides by 1, except in a constant expression, where
it fails to compile.
rounding::round now takes an exact half to the even neighbor, as std::chrono::round does, so 5 / 2 is 2 and 7 / 2 is 4. With floor and ceil already rounding like std::chrono::floor and ceil, all three modes match the chrono functions of the same name. A zero denominator in a constant expression now fails by dividing by zero, so the compiler reports a division by zero inside hal::divide(), instead of a call to a helper function.
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<Mode, T>(), an integer division that rounds its quotient down, up or to the nearest integer, and removeshal::rounding_division(). Closes #127.Changes
hal::roundinghas three modes, which round the waystd::chrono::floor,std::chrono::ceilandstd::chrono::roundround a duration:floorceilround(a half to even)/, for comparisonTruncation stays plain
/, asstd::chrono::duration_casttruncates, so it isn't a mode. For unsigned types,flooris the same as/.hal::division<T>names the operandsnumandden, asstd::ratiodoes. Every call names them, so they can't be swapped.hal::divide<Mode, T>(division<T>)works on any integralT. SinceTis given, the braced operands convert to it, and an operand that would narrow intoTis a compile error.ceilcan't overflow, unlike(num + den - 1) / den. The magnitudes are worked in the unsigned type, so the smallest signed value doesn't overflow either. The one exception is that value divided by -1, which doesn't fit inT, the same as for/.hal::divide().hal::rounding_division()is removed. It had no callers in the org and gave wrong results: 7 / 3 gave 3, 3 / 4 and 1 / 2 gave 0, and 1 / 0 gave 0.Not in this PR
The hand-written round-ups listed in #127 still use their own code:
cycles_per, and in libhal-arm-mcu the SPI prescaler, the ADC wait, the I2C timing and the SysTick counts. They can switch todivide<rounding::ceil>once this is released. v5'srounding_division()is unchanged.Testing
tests/math.test.cppcovers each mode in every sign combination, exact division, halves rounding to even, the largest and smallest values of 8-, 32- and 64-bit types, a 64-bit numerator over a 32-bit denominator, a run-time zero denominator, and calls in constant expressions. All 17 test suites pass with Clang 23, ASan and clang-tidy as warnings-as-errors. I also checked by hand that a constant zero denominator in astatic_assertfails to compile.