Add a function to do signed 64-bit division with remainder. This is
implemented using div64_u64_rem in the same way that div_s64_rem is
implemented using div_u64_rem.
Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
---
include/linux/math64.h | 18 ++++++++++++++++++
lib/math/div64.c | 20 ++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/include/linux/math64.h b/include/linux/math64.h
index 6aaccc1626ab..0a414446af89 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -57,6 +57,20 @@ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
return dividend / divisor;
}
+/**
+ * div64_s64_rem - signed 64bit divide with 64bit divisor and remainder
+ * @dividend: signed 64bit dividend
+ * @divisor: signed 64bit divisor
+ * @remainder: pointer to signed 64bit remainder
+ *
+ * Return: sets ``*remainder``, then returns dividend / divisor
+ */
+static inline s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder)
+{
+ *remainder = dividend % divisor;
+ return dividend / divisor;
+}
+
/**
* div64_u64 - unsigned 64bit divide with 64bit divisor
* @dividend: unsigned 64bit dividend
@@ -102,6 +116,10 @@ extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
#endif
+#ifndef div64_s64_rem
+s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder);
+#endif
+
#ifndef div64_u64
extern u64 div64_u64(u64 dividend, u64 divisor);
#endif
diff --git a/lib/math/div64.c b/lib/math/div64.c
index 5faa29208bdb..ccef0db85681 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -124,6 +124,26 @@ u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
EXPORT_SYMBOL(div64_u64_rem);
#endif
+#ifndef div_s64_rem
+s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder)
+{
+ u64 quotient;
+
+ if (dividend < 0) {
+ quotient = div64_u64_rem(-dividend, abs(divisor), (u64 *)remainder);
+ *remainder = -*remainder;
+ if (divisor > 0)
+ quotient = -quotient;
+ } else {
+ quotient = div64_u64_rem(dividend, abs(divisor), (u64 *)remainder);
+ if (divisor < 0)
+ quotient = -quotient;
+ }
+ return quotient;
+}
+EXPORT_SYMBOL(div64_s64_rem);
+#endif
+
/*
* div64_u64 - unsigned 64bit divide with 64bit divisor
* @dividend: 64bit dividend
--
2.35.1.1320.gc452695387.dirty
On Mon, Jul 14, 2025 at 09:20:17PM -0400, Sean Anderson wrote: > Add a function to do signed 64-bit division with remainder. This is > implemented using div64_u64_rem in the same way that div_s64_rem is > implemented using div_u64_rem. LGTM, but one important Q. Can we (start to) add the test cases, please? -- With Best Regards, Andy Shevchenko
On 7/15/25 04:03, Andy Shevchenko wrote:
> On Mon, Jul 14, 2025 at 09:20:17PM -0400, Sean Anderson wrote:
>> Add a function to do signed 64-bit division with remainder. This is
>> implemented using div64_u64_rem in the same way that div_s64_rem is
>> implemented using div_u64_rem.
>
> LGTM, but one important Q. Can we (start to) add the test cases, please?
>
Well, this just calls div64_u64_rem. So I am inclined to make the test something
like
#define test(n, d, q, r) ({ \
u64 _q, _r; \
_q = div64_u64_rem(n, d, &r); \
assert(_q == q); \
assert(_r == r); \
})
test( 3, 2, 1, 1);
test( 3, -2, -1, 1);
test(-3, 2, -1, -1);
test(-3, -2, 1, -1);
--Sean
On Tue, Jul 15, 2025 at 01:36:33PM -0400, Sean Anderson wrote:
> On 7/15/25 04:03, Andy Shevchenko wrote:
> > On Mon, Jul 14, 2025 at 09:20:17PM -0400, Sean Anderson wrote:
> >> Add a function to do signed 64-bit division with remainder. This is
> >> implemented using div64_u64_rem in the same way that div_s64_rem is
> >> implemented using div_u64_rem.
> >
> > LGTM, but one important Q. Can we (start to) add the test cases, please?
>
> Well, this just calls div64_u64_rem. So I am inclined to make the test something
> like
>
> #define test(n, d, q, r) ({ \
> u64 _q, _r; \
> _q = div64_u64_rem(n, d, &r); \
> assert(_q == q); \
> assert(_r == r); \
> })
>
> test( 3, 2, 1, 1);
> test( 3, -2, -1, 1);
> test(-3, 2, -1, -1);
> test(-3, -2, 1, -1);
Perhaps, but it should be done somewhere in lib/tests/...
--
With Best Regards,
Andy Shevchenko
© 2016 - 2026 Red Hat, Inc.