[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250715012023.2050178-2-sean.anderson@linux.dev>
Date: Mon, 14 Jul 2025 21:20:17 -0400
From: Sean Anderson <sean.anderson@...ux.dev>
To: Jonathan Cameron <jic23@...nel.org>,
Jean Delvare <jdelvare@...e.com>,
Guenter Roeck <linux@...ck-us.net>,
linux-iio@...r.kernel.org,
linux-hwmon@...r.kernel.org
Cc: Andy Shevchenko <andy@...nel.org>,
Nuno Sá <nuno.sa@...log.com>,
linux-kernel@...r.kernel.org,
David Lechner <dlechner@...libre.com>,
Sean Anderson <sean.anderson@...ux.dev>
Subject: [PATCH 1/7] math64: Add div64_s64_rem
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@...ux.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
Powered by blists - more mailing lists