Re: [PATCH v3 3/3] rust: math: add signed division and remainder helpers

From: FUJITA Tomonori

Date: Wed Oct 21 2026 - 05:40:49 EST


On Sat, 17 Oct 2026 19:11:34 +0800
chenhan <chenhan0017.work@xxxxxxxxx> wrote:

> Delta's division and remainder implementations operate on integers and
> do not depend on time units. Move them into a math module to provide a
> common home for arithmetic helpers, with Delta as the initial user.
>
> Expose div64_s64() and rem_s64() so driver crates can use these operations
> without constructing time values. The division helper takes two i64
> operands; the remainder helper takes an i64 dividend and an i32 divisor
> and returns an i64. Both keep Rust's signed operator semantics, including
> the unconditional panic checks, and use the existing C helpers on 32-bit.
>
> Keep the Delta API and its panic documentation unchanged. This is separate
> from the preceding behavior fix so that introducing the math module is
> not required for a backport.
>
> Assisted-by: LLM
> Signed-off-by: chenhan <chenhan0017.work@xxxxxxxxx>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/math.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++
> rust/kernel/time.rs | 49 ++++------------------------------
> 3 files changed, 70 insertions(+), 44 deletions(-)
> create mode 100644 rust/kernel/math.rs
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 4d5c96ddc49c..60c2af705b47 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -98,6 +98,7 @@
> pub mod kunit;
> pub mod list;
> pub mod maple_tree;
> +pub mod math;
> pub mod miscdevice;
> pub mod mm;
> pub mod module;
> diff --git a/rust/kernel/math.rs b/rust/kernel/math.rs
> new file mode 100644
> index 000000000000..34ca097caac9
> --- /dev/null
> +++ b/rust/kernel/math.rs
> @@ -0,0 +1,64 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Integer arithmetic helpers.

//! C header: [`include/linux/math64.h`](srctree/include/linux/math64.h).

?

> +/// Returns the remainder of an [`i64`] dividend divided by an [`i32`] divisor.
> +///
> +/// A non-zero remainder has the sign of `dividend`.
> +///
> +/// # Panics
> +///
> +/// Panics if `divisor` is zero, or if `dividend` is [`i64::MIN`] and `divisor` is `-1`.
> +#[inline]
> +pub fn rem_s64(dividend: i64, divisor: i32) -> i64 {

How about matching the C function? Something like:

pub fn div_s64_rem(dividend: i64, divisor: i32) -> (i64, i32)