Skip to content

Rewrite: Prevent overflow from public functions #30

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

Merged
merged 3 commits into from
Jun 13, 2022

Conversation

lord-ne
Copy link

@lord-ne lord-ne commented Jun 12, 2022

This is the rewrite discussed in #17. Its main purpose is to ensure that public functions cannot overflow, by having them delegate to private functions that accept larger parameters. Some work was also put in to mitigating the effects of the delays not being inlined at compile time.

lord-ne added 3 commits June 12, 2022 22:39
Move delay loop into internal functions
.
Refactor the assembly loop into its own function, to allow easier
reuse in the below functions.
.
Add internal delay functions calling the assembly loop function.
One function uses a 32 bit counter, and the other uses a 48 bit counter.
These functions will be used to reactor delay_ms and delay_us to prevent
overflow.
.
Refactor delay to use the internal delay functions.
Refactor delay_ms and delay_us to prevent overflow
.
Change delay_ms and delay_us to call the internal delay function with
a 48 bit counter. This prevents delay_ms and delay_us from overflowing
when passed a large argument.
.
Move the internal delay functions to their own module. Remove the
unused internal delay_loop_3_cycles function.
Calculating the number of cycles to delay for in delay_ms and delay_us
requires multiplying by a constant and dividing by another constant. Divide
both constants by their GCD at compile time, to allow the compiler to
better optimize the multiplication and division. This reduces the overhead in
the case where the compiler does not know the length of the delay at
compile time, or otherwise fails to inline the function.
@stappersg stappersg merged commit f1eef15 into avr-rust:rerewrite Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants