[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250724185700.557505-3-lyude@redhat.com>
Date: Thu, 24 Jul 2025 14:54:07 -0400
From: Lyude Paul <lyude@...hat.com>
To: rust-for-linux@...r.kernel.org,
Thomas Gleixner <tglx@...utronix.de>,
Boqun Feng <boqun.feng@...il.com>,
linux-kernel@...r.kernel.org,
Andreas Hindborg <a.hindborg@...nel.org>,
FUJITA Tomonori <fujita.tomonori@...il.com>
Cc: Frederic Weisbecker <frederic@...nel.org>,
Anna-Maria Behnsen <anna-maria@...utronix.de>,
John Stultz <jstultz@...gle.com>,
Stephen Boyd <sboyd@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH 2/2] rust: time: Implement basic arithmetic operations for Delta
While rvkms is only going to be using a few of these, since Deltas are
basically the same as i64 it's easy enough to just implement all of the
basic arithmetic operations for Delta types.
Note that for division and remainders, we currently limit these operations
to CONFIG_64BIT as u64 / u64 and u64 % u64 is not supported on all 32 bit
platforms natively. The correct solution we want to aim for here in the
future is to use the kernel's math library for performing these operations
so they're emulated on 32 bit platforms.
Signed-off-by: Lyude Paul <lyude@...hat.com>
---
rust/kernel/time.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index ac5cab62070c6..8ece5a5d5a11b 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -251,6 +251,92 @@ pub struct Delta {
nanos: i64,
}
+impl ops::Add for Delta {
+ type Output = Self;
+
+ fn add(self, rhs: Self) -> Self {
+ Self {
+ nanos: self.nanos + rhs.nanos,
+ }
+ }
+}
+
+impl ops::AddAssign for Delta {
+ fn add_assign(&mut self, rhs: Self) {
+ self.nanos += rhs.nanos;
+ }
+}
+
+impl ops::Sub for Delta {
+ type Output = Self;
+
+ fn sub(self, rhs: Self) -> Self::Output {
+ Self {
+ nanos: self.nanos - rhs.nanos,
+ }
+ }
+}
+
+impl ops::SubAssign for Delta {
+ fn sub_assign(&mut self, rhs: Self) {
+ self.nanos -= rhs.nanos;
+ }
+}
+
+impl ops::Mul for Delta {
+ type Output = Self;
+
+ fn mul(self, rhs: Self) -> Self::Output {
+ Self {
+ nanos: self.nanos * rhs.nanos,
+ }
+ }
+}
+
+impl ops::MulAssign for Delta {
+ fn mul_assign(&mut self, rhs: Self) {
+ self.nanos *= rhs.nanos;
+ }
+}
+
+// TODO: When we get support for u64/u64 division and remainders helpers remove this, until then
+// these ops only work on 64bit platforms.
+#[cfg(CONFIG_64BIT)]
+impl ops::Div for Delta {
+ type Output = Self;
+
+ fn div(self, rhs: Self) -> Self::Output {
+ Self {
+ nanos: self.nanos / rhs.nanos,
+ }
+ }
+}
+
+#[cfg(CONFIG_64BIT)]
+impl ops::DivAssign for Delta {
+ fn div_assign(&mut self, rhs: Self) {
+ self.nanos /= rhs.nanos;
+ }
+}
+
+#[cfg(CONFIG_64BIT)]
+impl ops::Rem for Delta {
+ type Output = Self;
+
+ fn rem(self, rhs: Self) -> Self::Output {
+ Self {
+ nanos: self.nanos % rhs.nanos,
+ }
+ }
+}
+
+#[cfg(CONFIG_64BIT)]
+impl ops::RemAssign for Delta {
+ fn rem_assign(&mut self, rhs: Self) {
+ self.nanos %= rhs.nanos;
+ }
+}
+
impl Delta {
/// A span of time equal to zero.
pub const ZERO: Self = Self { nanos: 0 };
--
2.50.0
Powered by blists - more mailing lists