time: use bit manipulation instead of modulo (#4480)

time: resolve TODO

## Motivation

Existing `TODO` comment in `src/time/driver/wheel/level.rs`.

## Solution

`level_range()` always return a strictly positive power of 2. If `b` is a
strictly positive power of 2, `a - (a % b)` is equal to `a & !(b - 1)`.
This commit is contained in:
Samuel Tardieu
2022-02-15 09:26:07 -08:00
committed by GitHub
parent 0826f763e0
commit 28b983c4bc
+3 -2
View File
@@ -143,8 +143,9 @@ impl Level {
let level_range = level_range(self.level);
let slot_range = slot_range(self.level);
// TODO: This can probably be simplified w/ power of 2 math
let level_start = now - (now % level_range);
// Compute the start date of the current level by masking the low bits
// of `now` (`level_range` is a power of 2).
let level_start = now & !(level_range - 1);
let mut deadline = level_start + slot as u64 * slot_range;
if deadline <= now {