Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/delay_cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ struct Selection {
remainder: u64,
}

// Compute the intended number of cycles to delay, and panic if it is greater than
// the maximum supported amount of cycles
const fn compute_total_cycles(cycles: u64, mul: u64, div: u64) -> u64 {
const MAX_SUPPORTED_CYCLES: u64 = 25_769_803_784;

// Multiply first to avoid precision loss, and expand to u128 to avoid overflow
lord-ne marked this conversation as resolved.
Show resolved Hide resolved
let result: u128 = (cycles as u128) * (mul as u128) / (div as u128);
if result > (MAX_SUPPORTED_CYCLES as u128) {
panic!("Error: Tried to delay for too many cycles. The maximum supported delay is 25_769_803_784 cycles");
}
return result as u64;
}

const fn cycles(counter_mask: u64, cycles_per_run: u64, cycles_per_iter: u64) -> Cycles {
Cycles {
counter_mask,
Expand Down Expand Up @@ -103,11 +116,7 @@ const fn select(info: Cycles, cycles: u64, above: u64) -> Selection {
}

impl<const CYCLES: u64, const MUL: u64, const DIV: u64> Delayer<CYCLES, MUL, DIV> {
// Multiply first to avoid precision loss.
// With a u64 there is no overflow when MUL is lower than:
// (2^64-1)/25_769_803_784 == 715_827_882.
// Since MUL is usually CPU_FREQUENCY_HZ, this allows up to 715.83 MHz.
const TOTAL_CYCLES: u64 = CYCLES * MUL / DIV;
const TOTAL_CYCLES: u64 = compute_total_cycles(CYCLES, MUL, DIV);
lord-ne marked this conversation as resolved.
Show resolved Hide resolved

// With `feature(generic_const_exprs) it becomes possible to construct a static assertion.
lord-ne marked this conversation as resolved.
Show resolved Hide resolved
//const _: [(); 0 - ((Self::TOTAL_CYCLES > 25_769_803_784) as usize)] = [];
Expand Down