Repository navigation
♻️ (minor) v6: Make hal::duration_from_cycles the reverse of cycles_per - #128
Merged
Merged
Conversation
duration_from_cycles() computed in float, so long counts lost precision, took 32-bit cycles, and rounded down, so a sleep until a deadline could end early. It now works in integers: 64-bit cycles, rounded up like cycles_per(), so the duration is never shorter than the cycles take. It returns the duration instead of an optional: hal::time_duration::max() from its last second on, and at 0 Hz, which never completes a cycle. Nothing outside libhal-util calls it.
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.
Makes
hal::duration_from_cycles()the reverse ofhal::cycles_per(): the time a number of cycles of a frequency takes, in whole integers and rounded up. It's for code that counts in ticks of a clock and needs the time left as a duration, such as sleeping on a waiter until a steady-clock deadline. libhal/libhal-arm-mcu#242'sdma_i2cis the first caller, in place of its own hand-written conversion.Changes
std::optional<hal::time_duration> duration_from_cycles(hertz, std::uint32_t)constexpr hal::time_duration duration_from_cycles(hertz, hal::u64)float, so long counts lose precisioncycles_per(): the duration is never shorter than the cycles takestd::nullopthal::time_duration::max()(about 292 years), from its last whole second onstd::nullopthal::time_duration::max(), since such a clock never completes a cycle. 0 cycles still take 0.Changing the return type breaks callers that call
.value(), but nothing outside libhal-util calls it.Size
The function is
constexprand inlines into its caller. On the stm32f103c8 (LLVM 23, MinSizeRel), using it indma_i2c's DMA wait in place of the hand-written version growswait_for_transfer()by 46 B (392 to 438 B). The difference is the 0 Hz case and the saturation atmax(), which the hand-written version handled by forcing the frequency to at least 1 Hz and capping each sleep at 2^32 seconds. Both versions make the same two 64-bit divisions.Testing
All 17 host test suites pass, with clang-tidy on. The
duration_from_cyclestests now cover:cycles_per()of the result giving back the cycles, or one more from rounding both ways;The release this PR cuts (
(minor)) is what libhal/libhal-arm-mcu#242 needs as its minimum libhal-util version.