Fixing power-of-two calculation

master
Ryan Summers 2020-12-08 16:38:07 +01:00
parent 645a1cd832
commit 2e0681ebcc
1 changed files with 10 additions and 1 deletions

View File

@ -325,7 +325,16 @@ const APP: () = {
SAMPLE_BUFFER_SIZE as u64 * ADC_SAMPLE_TICKS as u64;
let batches_per_overflow: u64 =
(1u64 + u32::MAX as u64) / batch_duration;
let period: u64 = batch_duration * batches_per_overflow - 1u64;
// Calculate the largest power-of-two that is less than `batches_per_overflow`.
// This is completed by eliminating the least significant bits of the value until
// only the msb remains, which is always a power of two.
let mut j = batches_per_overflow;
while (j & (j - 1)) != 0 {
j = j & (j - 1);
}
let period: u64 = batch_duration * j - 1u64;
period.try_into().unwrap()
};