Skip to content

Commit c32fa99

Browse files
nvpwingomolnar
authored andcommitted
bitops: Fix signedness of compile-time hweight implementations
Enabling '-Wsign-compare' compiler warnings on code that includes include/linux/bitops.h can generate the following warning: In file included from include/linux/kernel.h:10:0, from <random filename>:48: include/linux/bitops.h: In function 'hweight_long': include/linux/bitops.h:77:26: error: signed and unsigned type in conditional expression [-Werror=sign-compare] (converted to an error with -Werror) This is due to the use of the logical negation operator '!' in the __const_hweight8 macro in include/asm-generic/bitops/const_hweight.h. The use of that operator here results in a signed value. Fix by explicitly casting the __const_hweight8 macro expansion to 'unsigned int'. While here, clean up several checkpatch.pl warnings. Signed-off-by: Paul Walmsley <pwalmsley@nvidia.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/alpine.DEB.2.02.1312180459580.30198@tamien Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4944790 commit c32fa99

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

‎include/asm-generic/bitops/const_hweight.h‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* Compile time versions of __arch_hweightN()
66
*/
77
#define __const_hweight8(w) \
8-
( (!!((w) & (1ULL << 0))) + \
9-
(!!((w) & (1ULL << 1))) + \
10-
(!!((w) & (1ULL << 2))) + \
11-
(!!((w) & (1ULL << 3))) + \
12-
(!!((w) & (1ULL << 4))) + \
13-
(!!((w) & (1ULL << 5))) + \
14-
(!!((w) & (1ULL << 6))) + \
15-
(!!((w) & (1ULL << 7))) )
8+
((unsigned int) \
9+
((!!((w) & (1ULL << 0))) + \
10+
(!!((w) & (1ULL << 1))) + \
11+
(!!((w) & (1ULL << 2))) + \
12+
(!!((w) & (1ULL << 3))) + \
13+
(!!((w) & (1ULL << 4))) + \
14+
(!!((w) & (1ULL << 5))) + \
15+
(!!((w) & (1ULL << 6))) + \
16+
(!!((w) & (1ULL << 7)))))
1617

1718
#define __const_hweight16(w) (__const_hweight8(w) + __const_hweight8((w) >> 8 ))
1819
#define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))

0 commit comments

Comments
 (0)