-
Notifications
You must be signed in to change notification settings - Fork 11
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
Statically assert that delays are less than 25,769,803,784 cycles #36
Conversation
The maximum delay that the Delayer efficiently supports is 25,769,803,784 cycles. Beyond this, previously the implementation would support slightly longer delays (about 0.3% longer) but use many more instructions to do so, and for even longer delays would overflow silently. This commit adds a static check that the requested delay is less than 25,769,803,784 cycles. If the delay is longer than that, the compilation panics with an error message stating that the delay is too long.
The error message that results from having too large of a delay is as follows:
Ideally the error message would show the lines where you called |
Is blocked by #35, "Which branch of the library to release?". |
I think it isn't? This is a PR into the |
A commit message is a letter to your co-worker and your future self. And a letter is more as a single line. |
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 had completely missed that panic was supported in const expression. That's pretty sweet.
- Remove obsolete comment. - Remove compute_total_cycles function which is only called once, and instead use an expression block. - Use assert! instead of if and panic!. - Use <= instead of < when checking number of cycles.
On Tue, Jun 28, 2022 at 11:23:11PM -0700, lord-ne wrote:
@lord-ne commented on this pull request.
+ const MAX_SUPPORTED_CYCLES: u64 = 25_769_803_784;
At worst it's like four 64-bit multiplies, or sixteen 32-bit multiplies,
and some additions. Compared to the cost of running the rust compiler
in the first place, this is nothing.
Please document what is the reason that MAX_SUPPORTED_CYCLES = 25_769_803_784.
|
@stappersg It's in the doc comments for the Delayer: Lines 39 to 59 in 31dbd31
|
I'm done with merge request., there is too much doubt that it is an improvement. |
I'm done with this merge request., there is too much doubt that it is an improvement. |
What? That isn't the impression that I got from the discussion at all. Me and @bombela were just discussing a fairly minor detail (the u128 multiply). @stappersg, are you saying that based on the previous discussion there is too much doubt it is an improvement, OR are you saying that you personally doubt that it is an improvement? Because if it is based on the previous discussion, I don't think you are correct |
ahah woot. I think this PR is just fine and should be merged. |
Cycle accuracy makes sense for small delays. I'm really done with this merge request. |
This merge request ensures that there can never be any overflow in case somebody types out an overly long delay. Right now the overflow is silent. It would be annoying, difficult, and a waste of time to debug. I do agree with you that delay_cycles is probably best reserved for small delays. Not minutes. Nonetheless, like everything in software ; and life in general; there is a physical limit. As far as I know, it is always better to inform the user when the limit is reached. Rather than surprising them with a seemingly random behavior. The fastest AVR is 50Mhz (FPGA stuff). The fastest AVR MCU is 32Mhz. I personally have some ATTiny at 20Mhz. So for one second of delay, you need to be able to count to at least 50_000_000. Here are the limits per counter as a reminder:
I have personally used delays of up to a minute at @20Mhz when debugging hardware and code together. So I see an appeal to the 32 bits. And avr-gcc does the same thing, so why not follow. It is zero-cost anyways (ie: cost only if used). Anyways, no matter the biggest counter width we ultimately support, there is always a limit. And we should produce an error when attempting to overflow this limit. This is what this PR is adding. An assertion to prevent a silent overflow. |
The maximum delay that the Delayer efficiently supports is 25,769,803,784 cycles. Beyond this, previously the implementation would support slightly longer delays (about 0.3% longer) but use many more instructions to do so, and for even longer delays would overflow silently.
This PR adds a static check that the requested delay is less than 25,769,803,784 cycles. If the delay is longer than that, the compilation panics with an error message stating that the delay is too long.