From: FUJITA Tomonori <tomo@flapping.org>
To: a.hindborg@kernel.org, ojeda@kernel.org
Cc: acourbot@nvidia.com, aliceryhl@google.com,
anna-maria@linutronix.de, bjorn3_gh@protonmail.com,
boqun@kernel.org, dakr@kernel.org, daniel.almeida@collabora.com,
frederic@kernel.org, gary@garyguo.net, jstultz@google.com,
lossin@kernel.org, lyude@redhat.com, sboyd@kernel.org,
tamird@kernel.org, tglx@kernel.org, tmgross@umich.edu,
work@onurozkan.dev, rust-for-linux@vger.kernel.org,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v1] rust: time: share the code of as_micros_ceil() and as_millis_ceil()
Date: Mon, 30 Nov 2026 19:57:50 +0900 [thread overview]
Message-ID: <20261130105750.465220-1-tomo@flapping.org> (raw)
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
as_micros_ceil() and as_millis_ceil() are the same apart from the
divisor and the C helper used on 32-bit.
Move the rounding up into a private ceil_with() method. It takes a
method that converts a Delta and rounds towards zero: as_millis(), or
a new private as_micros().
No functional change.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/time.rs | 79 ++++++++++++++++++++++-----------------------
1 file changed, 39 insertions(+), 40 deletions(-)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 0782fdb06bfc..261acd4b6ce1 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -636,6 +636,43 @@ pub const fn as_nanos(self) -> i64 {
self.value
}
+ /// Return the number of microseconds in the [`Delta`].
+ #[inline]
+ fn as_micros(self) -> i64 {
+ #[cfg(CONFIG_64BIT)]
+ {
+ self.as_nanos() / NSEC_PER_USEC
+ }
+
+ #[cfg(not(CONFIG_64BIT))]
+ // SAFETY: It is always safe to call `ktime_to_us()` with any value.
+ unsafe {
+ bindings::ktime_to_us(self.as_nanos())
+ }
+ }
+
+ /// Convert the [`Delta`] with `div`, rounding towards positive infinity.
+ ///
+ /// `div` must convert to a unit larger than a nanosecond and round towards zero.
+ #[inline]
+ fn ceil_with(self, div: impl FnOnce(Self) -> i64) -> 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 (delta, add) = if n > 0 {
+ (Self::from_nanos(n - 1), 1)
+ } else {
+ (self, 0)
+ };
+
+ div(delta) + add
+ }
+
/// Return the smallest number of microseconds greater than or equal
/// to the value in the [`Delta`].
///
@@ -652,26 +689,7 @@ pub const fn as_nanos(self) -> i64 {
/// ```
#[inline]
pub fn as_micros_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_USEC + add
- }
-
- #[cfg(not(CONFIG_64BIT))]
- // SAFETY: It is always safe to call `ktime_to_us()` with any value.
- unsafe {
- bindings::ktime_to_us(n) + add
- }
+ self.ceil_with(Self::as_micros)
}
/// Return the number of milliseconds in the [`Delta`].
@@ -717,26 +735,7 @@ pub fn as_millis(self) -> i64 {
/// ```
#[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
- }
+ self.ceil_with(Self::as_millis)
}
/// Convert this span to a [`Delta<Jiffy>`] suitable for use as a timeout.
base-commit: 6d614de4288dd9738557b5bbc5be90851bf27701
--
2.43.0
reply other threads:[~2026-11-30 10:58 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130105750.465220-1-tomo@flapping.org \
--to=tomo@flapping.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=jstultz@google.com \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tamird@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox