Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 25 Jan 2026 12:35:54 +0000
From:      Bjoern A. Zeeb <bz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 7e4a2b381dcb - main - LinuxKPI: improve hweight<n> if complie time constant
Message-ID:  <69760e2a.4605b.3f960e22@gitrepo.freebsd.org>

index | next in thread | raw e-mail

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)
 {


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69760e2a.4605b.3f960e22>