git: 7e4a2b381dcb - main - LinuxKPI: improve hweight<n> if complie time constant

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Sun, 25 Jan 2026 12:35:54 UTC
The branch main has been updated by bz:

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

commit 7e4a2b381dcb5b2bcaf528e010096fd2d14868b1
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2026-01-20 22:41:12 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2026-01-25 12:35:22 +0000

    LinuxKPI: improve hweight<n> if complie time constant
    
    rtw89(4) uses a static_assert() with hweight<n> calls.  In order to
    avoid compile time errors, deal with the case when the arguments to
    hweight<n> are complie time constant.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 days
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D54806
---
 sys/compat/linuxkpi/common/include/linux/bitops.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/bitops.h b/sys/compat/linuxkpi/common/include/linux/bitops.h
index ebe9aa120094..125081ab5b74 100644
--- a/sys/compat/linuxkpi/common/include/linux/bitops.h
+++ b/sys/compat/linuxkpi/common/include/linux/bitops.h
@@ -51,12 +51,6 @@
 #define	BITS_PER_TYPE(t)	(sizeof(t) * BITS_PER_BYTE)
 #define	BITS_TO_BYTES(n)	howmany((n), BITS_PER_BYTE)
 
-#define	hweight8(x)	bitcount((uint8_t)(x))
-#define	hweight16(x)	bitcount16(x)
-#define	hweight32(x)	bitcount32(x)
-#define	hweight64(x)	bitcount64(x)
-#define	hweight_long(x)	bitcountl(x)
-
 #if __has_builtin(__builtin_popcountg)
 #define	HWEIGHT8(x)	(__builtin_popcountg((uint8_t)(x)))
 #define	HWEIGHT16(x)	(__builtin_popcountg((uint16_t)(x)))
@@ -70,6 +64,12 @@
 #define	HWEIGHT64(x)	(__const_bitcount64((uint64_t)(x)))
 #endif
 
+#define	hweight8(x)	(__builtin_constant_p(x) ? HWEIGHT8(x)  : bitcount((uint8_t)(x)))
+#define	hweight16(x)	(__builtin_constant_p(x) ? HWEIGHT16(x) : bitcount16(x))
+#define	hweight32(x)	(__builtin_constant_p(x) ? HWEIGHT32(x) : bitcount32(x))
+#define	hweight64(x)	(__builtin_constant_p(x) ? HWEIGHT64(x) : bitcount64(x))
+#define	hweight_long(x)	bitcountl(x)
+
 static inline int
 __ffs(int mask)
 {