[PATCH v5 3/7] rust: time: add Delta::as_millis_ceil()
FUJITA Tomonori <[email protected]> Thu, 6 Aug 2026 16:32:37 +0900
| Newsgroups | org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
From: FUJITA Tomonori <[email protected]> Add a ceiling variant, mirroring the existing as_micros_ceil() since the existing as_millis() truncates towards zero. Reviewed-by: Gary Guo <[email protected]> Signed-off-by: FUJITA Tomonori <[email protected]> --- rust/kernel/time.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 71f6dafc79e5..9d65a6c1992a 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -525,6 +525,32 @@ pub fn as_millis(self) -> i64 { } } + /// Return the smallest number of milliseconds greater than or equal + /// to the value in the [`Delta`]. + #[inline] + pub fn as_millis_ceil(self) -> i64 { + // Only positive values need to be rounded up: truncating division already + // rounds towards zero, i.e. up, for negative values. + // + // The usual `(nanos + d - 1) / d` is not used because the addition overflows + // once `nanos` exceeds `i64::MAX - (d - 1)`; saturating the addition instead + // would drop the rounding bias and return a result one unit too small. + let n = self.as_nanos(); + + let (n, add) = if n > 0 { (n - 1, 1) } else { (n, 0) }; + + #[cfg(CONFIG_64BIT)] + { + n / NSEC_PER_MSEC + add + } + + #[cfg(not(CONFIG_64BIT))] + // SAFETY: It is always safe to call `ktime_to_ms()` with any value. + unsafe { + bindings::ktime_to_ms(n) + add + } + } + /// Return `self % dividend` where `dividend` is in nanoseconds. /// /// The kernel doesn't have any emulation for `s64 % s64` on 32 bit platforms, so this is -- 2.43.0