Skip to content

Commit 64ff9ba

Browse files
lord-nestappersg
authored andcommitted
Divide numerator and denominator by their GCD at compile time
1 parent 0909757 commit 64ff9ba

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lib.rs

Lines changed: 15 additions & 2 deletions
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,6 +41,16 @@ 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
}
50+
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+
}

0 commit comments

Comments
 (0)