xref: /freebsd/sys/sys/bitcount.h (revision 27aa23cee81088b0ffa974eec9f03c654c36438e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef _SYS_BITCOUNT_H_
38 #define	_SYS_BITCOUNT_H_
39 
40 #include <sys/_types.h>
41 
42 #define	__const_bitcount8(x) ( \
43     !!((x) & (1 << 0)) + \
44     !!((x) & (1 << 1)) + \
45     !!((x) & (1 << 2)) + \
46     !!((x) & (1 << 3)) + \
47     !!((x) & (1 << 4)) + \
48     !!((x) & (1 << 5)) + \
49     !!((x) & (1 << 6)) + \
50     !!((x) & (1 << 7)))
51 
52 #define	__const_bitcount16(x) ( \
53     __const_bitcount8(x) + \
54     __const_bitcount8((x) >> 8))
55 
56 #define	__const_bitcount32(x) ( \
57     __const_bitcount16(x) + \
58     __const_bitcount16((x) >> 16))
59 
60 #define	__const_bitcount64(x) ( \
61     __const_bitcount32(x) + \
62     __const_bitcount32((x) >> 32))
63 
64 #ifdef __POPCNT__
65 #define	__bitcount64(x)	__builtin_popcountll((__uint64_t)(x))
66 #define	__bitcount32(x)	__builtin_popcount((__uint32_t)(x))
67 #define	__bitcount16(x)	__builtin_popcount((__uint16_t)(x))
68 #define	__bitcountl(x)	__builtin_popcountl((unsigned long)(x))
69 #define	__bitcount(x)	__builtin_popcount((unsigned int)(x))
70 #else
71 /*
72  * Population count algorithm using SWAR approach
73  * - "SIMD Within A Register".
74  */
75 static __inline __uint16_t
__bitcount16(__uint16_t _x)76 __bitcount16(__uint16_t _x)
77 {
78 
79 	_x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1);
80 	_x = (_x & 0x3333) + ((_x & 0xcccc) >> 2);
81 	_x = (_x + (_x >> 4)) & 0x0f0f;
82 	_x = (_x + (_x >> 8)) & 0x00ff;
83 	return (_x);
84 }
85 
86 static __inline __uint32_t
__bitcount32(__uint32_t _x)87 __bitcount32(__uint32_t _x)
88 {
89 
90 	_x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1);
91 	_x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2);
92 	_x = (_x + (_x >> 4)) & 0x0f0f0f0f;
93 	_x = (_x + (_x >> 8));
94 	_x = (_x + (_x >> 16)) & 0x000000ff;
95 	return (_x);
96 }
97 
98 #ifdef __LP64__
99 static __inline __uint64_t
__bitcount64(__uint64_t _x)100 __bitcount64(__uint64_t _x)
101 {
102 
103 	_x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1);
104 	_x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2);
105 	_x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f;
106 	_x = (_x + (_x >> 8));
107 	_x = (_x + (_x >> 16));
108 	_x = (_x + (_x >> 32)) & 0x000000ff;
109 	return (_x);
110 }
111 
112 #define	__bitcountl(x)	__bitcount64((unsigned long)(x))
113 #else
114 static __inline __uint64_t
__bitcount64(__uint64_t _x)115 __bitcount64(__uint64_t _x)
116 {
117 
118 	return (__bitcount32(_x >> 32) + __bitcount32(_x));
119 }
120 
121 #define	__bitcountl(x)	__bitcount32((unsigned long)(x))
122 #endif
123 #define	__bitcount(x)	__bitcount32((unsigned int)(x))
124 #endif
125 
126 #endif /* !_SYS_BITCOUNT_H_ */
127