[PATCH v2 17/21] rust: num: Add an upward alignment helper for usize
From: Alexandre Courbot
Date: Thu May 01 2025 - 09:04:53 EST
From: Joel Fernandes <joelagnelf@xxxxxxxxxx>
This will be used in the nova-core driver where we need to upward-align
the image size to get to the next image in the VBIOS ROM.
[acourbot@xxxxxxxxxx: handled conflicts due to removal of patch creating
num.rs]
Signed-off-by: Joel Fernandes <joelagnelf@xxxxxxxxxx>
Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
---
rust/kernel/lib.rs | 1 +
rust/kernel/num.rs | 21 +++++++++++++++++++++
2 files changed, 22 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ab0286857061d2de1be0279cbd2cd3490e5a48c3..be75b196aa7a29cf3eed7c902ed8fb98689bbb50 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -67,6 +67,7 @@
pub mod miscdevice;
#[cfg(CONFIG_NET)]
pub mod net;
+pub mod num;
pub mod of;
pub mod page;
#[cfg(CONFIG_PCI)]
diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
new file mode 100644
index 0000000000000000000000000000000000000000..953c6ab012601efb3c9387b4b299e22233670c4b
--- /dev/null
+++ b/rust/kernel/num.rs
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Numerical and binary utilities for primitive types.
+
+/// A trait providing alignment operations for `usize`.
+pub trait UsizeAlign {
+ /// Aligns `self` upwards to the nearest multiple of `align`.
+ fn align_up(self, align: usize) -> usize;
+}
+
+impl UsizeAlign for usize {
+ fn align_up(mut self, align: usize) -> usize {
+ self = (self + align - 1) & !(align - 1);
+ self
+ }
+}
+
+/// Aligns `val` upwards to the nearest multiple of `align`.
+pub const fn usize_align_up(val: usize, align: usize) -> usize {
+ (val + align - 1) & !(align - 1)
+}
--
2.49.0