-
Notifications
You must be signed in to change notification settings - Fork 98
alarm: create ms_to_ticks
helper function
#434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
f6d9cc8
to
35b77cd
Compare
libtock/services/alarm.c
Outdated
// Note the following code is mathematically equivalent to the following | ||
// more readable conversion. | ||
// ``` | ||
// uint32_t seconds = ms / 1000; | ||
// uint32_t leftover_millis = ms % 1000; | ||
// uint32_t millihertz = frequency / 1000; // ticks per millisecond | ||
// return (seconds * frequency) + (leftover_millis * millihertz); | ||
// ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that with the exception of this comment, the other lengthy comments/proof in this function should be moved to the PR description.
You can leave a pointer to this PR for those interested in the more in depth discussion you provide here. Having the lengthy description is helpful for future work, but I think more verbose than necessary in the code base. Leaving this for posterity in the PR that is pointed to from the comment seems to offer a good middle ground.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I've changed it, and I am pasting the content here temporarily and will move it to the PR description later.
Once you update the comment, rebase on master, and format to pass CI (run |
7fc6a79
to
749ce29
Compare
749ce29
to
229e7a9
Compare
This helper function has been thoroughly debugged in tock#395. Floating point imprecision was causing timer overflows to work incorrectly. `ms_to_ticks` now better corresponds with the behavior of the libtock-c 32-bit tick rollover and is precise to 1ms of error.
229e7a9
to
fea5e92
Compare
// This conversion has a max error of 1ms. | ||
// View the justification here https://github.com/tock/libtock-c/pull/434 | ||
uint32_t frequency; | ||
libtock_alarm_command_get_frequency(&frequency); | ||
|
||
uint32_t seconds = ms / 1000; | ||
uint32_t leftover_millis = ms % 1000; | ||
uint32_t milliseconds_per_second = 1000; | ||
|
||
uint64_t ticks = (uint64_t) seconds * frequency; | ||
ticks += ((uint64_t) leftover_millis * frequency) / milliseconds_per_second; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just catching up to speed, but given that we're in 64-bit arithmetic now, why is this still dealing with seconds and fractional seconds individually? Wouldn't this suffice now?
ms = (ticks * milliseconds_per_second) / freq
ticks = (ms * freq) / milliseconds_per_second
#395 has stalled as the PR has grown beyond original scope. This PR splits out the modifications to arithmetic logic and should get merged before #395.
This mega-size comment was originally in the code explaining the logic of the code change.