Skip to content

Commit b1963f7

Browse files
lord-nestappersg
authored andcommitted
Divide numerator and denominator by their GCD at compile time
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.
1 parent 124751f commit b1963f7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lib.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ pub fn delay(count: u32) {
2828
/// compile time, otherwise the delay may be much longer than specified.
2929
#[inline(always)]
3030
pub fn delay_ms(ms: u32) {
31-
let ticks: u64 = (u64::from(avr_config::CPU_FREQUENCY_HZ) * u64::from(ms)) / 4_000;
31+
const GCD: u32 = gcd(avr_config::CPU_FREQUENCY_HZ, 4_000);
32+
const NUMERATOR: u32 = avr_config::CPU_FREQUENCY_HZ / GCD;
33+
const DENOMINATOR: u32 = 4_000 / GCD;
34+
let ticks: u64 = (u64::from(ms) * u64::from(NUMERATOR)) / u64::from(DENOMINATOR);
3235
delay_impl::delay_count_48(ticks);
3336
}
3437

@@ -38,10 +41,20 @@ pub fn delay_ms(ms: u32) {
3841
/// compile time, otherwise the delay may be much longer than specified.
3942
#[inline(always)]
4043
pub fn delay_us(us: u32) {
41-
let ticks: u64 = (u64::from(avr_config::CPU_FREQUENCY_HZ) * u64::from(us)) / 4_000_000;
44+
const GCD: u32 = gcd(avr_config::CPU_FREQUENCY_HZ, 4_000_000);
45+
const NUMERATOR: u32 = avr_config::CPU_FREQUENCY_HZ / GCD;
46+
const DENOMINATOR: u32 = 4_000_000 / GCD;
47+
let ticks: u64 = (u64::from(us) * u64::from(NUMERATOR)) / u64::from(DENOMINATOR);
4248
delay_impl::delay_count_48(ticks);
4349
}
4450

51+
const fn gcd(mut a: u32, mut b: u32) -> u32 {
52+
while b != 0 {
53+
(a, b) = (b, a % b);
54+
}
55+
return a;
56+
}
57+
4558
#[cfg(test)]
4659
mod tests {
4760
#[test]

0 commit comments

Comments
 (0)