Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Feb 2026 02:29:28 +0000
From:      Bjoern A. Zeeb <bz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 5585c7c36518 - stable/15 - LinuxKPI: improve hweight<n> if complie time constant
Message-ID:  <69a10188.21a1b.22132e06@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/15 has been updated by bz:

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

commit 5585c7c365180aecb9ffd82723604ee4cde6ffc9
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2026-01-20 22:41:12 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2026-02-26 22:59:59 +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
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D54806
    
    (cherry picked from commit 7e4a2b381dcb5b2bcaf528e010096fd2d14868b1)
---
 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 8fac80820f30..e34ba391421a 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?69a10188.21a1b.22132e06>