git: d4820dc1e6cb - main - linuxkpi: Add `int_sqrt()`

From: Jean-Sébastien Pédron <dumbbell_at_FreeBSD.org>
Date: Thu, 10 Sep 2026 19:50:20 UTC
The branch main has been updated by dumbbell:

URL: https://cgit.FreeBSD.org/src/commit/?id=d4820dc1e6cb9ec9d8d3ef030f143c69f92228cd

commit d4820dc1e6cb9ec9d8d3ef030f143c69f92228cd
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2026-08-15 18:30:35 +0000
Commit:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
CommitDate: 2026-09-10 19:39:32 +0000

    linuxkpi: Add `int_sqrt()`
    
    The implementation comes from `isqrt64()` in `sys/cam/cam_iosched.c`. It
    is modified to take an `unsigned long` and return an `unsigned int`.
    
    The i915 DRM driver started to use it in Linux 6.15.
    
    Reviewed by:    bz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D58251
---
 sys/compat/linuxkpi/common/include/linux/math.h |  3 ++
 sys/compat/linuxkpi/common/src/linux_math.c     | 43 +++++++++++++++++++++++++
 sys/conf/files                                  |  2 ++
 sys/modules/linuxkpi/Makefile                   |  1 +
 4 files changed, 49 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/math.h b/sys/compat/linuxkpi/common/include/linux/math.h
index 1d50e011f66d..777d989c041f 100644
--- a/sys/compat/linuxkpi/common/include/linux/math.h
+++ b/sys/compat/linuxkpi/common/include/linux/math.h
@@ -64,6 +64,9 @@
 })
 #endif
 
+unsigned long linuxkpi_int_sqrt(unsigned long val);
+#define	int_sqrt(x)	linuxkpi_int_sqrt(x)
+
 static inline uintmax_t
 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
 {
diff --git a/sys/compat/linuxkpi/common/src/linux_math.c b/sys/compat/linuxkpi/common/src/linux_math.c
new file mode 100644
index 000000000000..29e538c5d9c9
--- /dev/null
+++ b/sys/compat/linuxkpi/common/src/linux_math.c
@@ -0,0 +1,43 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2015 Netflix, Inc.
+ */
+
+#include <linux/math.h>
+
+/*
+ * Based on the implementation of `isqrt64()` from `sys/cam/cam_iosched.c` as
+ * of FreeBSD commit 153446ecd740702af00cf9b09d8cd39f6d397dd3. The return value
+ * was changed from `uint64_t` to `unsigned long`.
+ */
+unsigned long
+linuxkpi_int_sqrt(unsigned long val)
+{
+	unsigned long res = 0;
+	unsigned long bit = 1ULL << (sizeof(unsigned long) * NBBY - 2);
+
+	/*
+	 * Find the largest power of 4 smaller than val.
+	 */
+	while (bit > val)
+		bit >>= 2;
+
+	/*
+	 * Accumulate the answer, one bit at a time (we keep moving
+	 * them over since 2 is the square root of 4 and we test
+	 * powers of 4). We accumulate where we find the bit, but
+	 * the successive shifts land the bit in the right place
+	 * by the end.
+	 */
+	while (bit != 0) {
+		if (val >= res + bit) {
+			val -= res + bit;
+			res = (res >> 1) + bit;
+		} else
+			res >>= 1;
+		bit >>= 2;
+	}
+
+	return res;
+}
diff --git a/sys/conf/files b/sys/conf/files
index 8dd5a6a6873b..c6655459187f 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -4740,6 +4740,8 @@ compat/linuxkpi/common/src/linux_kthread.c	optional compat_linuxkpi \
 	compile-with "${LINUXKPI_C}"
 compat/linuxkpi/common/src/linux_lock.c		optional compat_linuxkpi \
 	compile-with "${LINUXKPI_C}"
+compat/linuxkpi/common/src/linux_math.c		optional compat_linuxkpi \
+	compile-with "${LINUXKPI_C}"
 compat/linuxkpi/common/src/linux_mhi.c		optional compat_linuxkpi wlan \
 	compile-with "${LINUXKPI_C}"
 compat/linuxkpi/common/src/linux_netdev.c	optional compat_linuxkpi \
diff --git a/sys/modules/linuxkpi/Makefile b/sys/modules/linuxkpi/Makefile
index 35dc19f93185..97319194946e 100644
--- a/sys/modules/linuxkpi/Makefile
+++ b/sys/modules/linuxkpi/Makefile
@@ -19,6 +19,7 @@ SRCS=	linux_compat.c \
 	linux_kobject.c \
 	linux_kthread.c \
 	linux_lock.c \
+	linux_math.c \
 	linux_netdev.c \
 	linux_page.c \
 	linuxkpi_page_pool.c \