blob: a07c70fd485804c8544097d6ba52dbef395e96e5 [file] [log] [blame]
wdenkb15cbc02000-08-21 15:05:47 +00001#ifndef _LINUX_BITOPS_H
2#define _LINUX_BITOPS_H
3
Simon Kagstrom13edaf32009-08-24 09:09:50 +02004#include <asm/types.h>
Stefan Roese5e9aade2017-02-20 16:50:26 +01005#include <asm-generic/bitsperlong.h>
Fabio Estevam5e39d082015-11-05 12:43:38 -02006#include <linux/compiler.h>
Vignesh Raghavendra18fd00b2019-12-09 10:25:31 +05307#include <linux/kernel.h>
wdenkb15cbc02000-08-21 15:05:47 +00008
Masahiro Yamada14fecf92017-11-22 02:38:11 +09009#ifdef __KERNEL__
Jagan Teki79d4d5f2015-10-21 17:30:34 +053010#define BIT(nr) (1UL << (nr))
Masahiro Yamada14fecf92017-11-22 02:38:11 +090011#define BIT_ULL(nr) (1ULL << (nr))
Jagan Teki79d4d5f2015-10-21 17:30:34 +053012#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
13#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Masahiro Yamada14fecf92017-11-22 02:38:11 +090014#define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
15#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
16#define BITS_PER_BYTE 8
17#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
18#endif
Heiko Schocher69bab552015-09-07 13:43:52 +020019
wdenkb15cbc02000-08-21 15:05:47 +000020/*
Jagan Teki8aceb8f2015-10-21 16:46:51 +053021 * Create a contiguous bitmask starting at bit position @l and ending at
22 * position @h. For example
23 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
24 */
Vignesh R1b084b22019-02-05 11:29:11 +053025#ifdef CONFIG_SANDBOX
26#define GENMASK(h, l) \
27 (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h))))
28#else
Jagan Teki8aceb8f2015-10-21 16:46:51 +053029#define GENMASK(h, l) \
30 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
Vignesh R1b084b22019-02-05 11:29:11 +053031#endif
Jagan Teki8aceb8f2015-10-21 16:46:51 +053032
33#define GENMASK_ULL(h, l) \
34 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
35
36/*
wdenkb15cbc02000-08-21 15:05:47 +000037 * ffs: find first bit set. This is defined the same way as
38 * the libc and compiler builtin ffs routines, therefore
39 * differs in spirit from the above ffz (man ffs).
40 */
41
42static inline int generic_ffs(int x)
43{
44 int r = 1;
45
46 if (!x)
47 return 0;
48 if (!(x & 0xffff)) {
49 x >>= 16;
50 r += 16;
51 }
52 if (!(x & 0xff)) {
53 x >>= 8;
54 r += 8;
55 }
56 if (!(x & 0xf)) {
57 x >>= 4;
58 r += 4;
59 }
60 if (!(x & 3)) {
61 x >>= 2;
62 r += 2;
63 }
64 if (!(x & 1)) {
65 x >>= 1;
66 r += 1;
67 }
68 return r;
69}
70
Simon Kagstrom2258cec2009-08-24 09:10:12 +020071/**
72 * fls - find last (most-significant) bit set
73 * @x: the word to search
74 *
75 * This is defined the same way as ffs.
76 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
77 */
78static inline int generic_fls(int x)
79{
80 int r = 32;
81
82 if (!x)
83 return 0;
84 if (!(x & 0xffff0000u)) {
85 x <<= 16;
86 r -= 16;
87 }
88 if (!(x & 0xff000000u)) {
89 x <<= 8;
90 r -= 8;
91 }
92 if (!(x & 0xf0000000u)) {
93 x <<= 4;
94 r -= 4;
95 }
96 if (!(x & 0xc0000000u)) {
97 x <<= 2;
98 r -= 2;
99 }
100 if (!(x & 0x80000000u)) {
101 x <<= 1;
102 r -= 1;
103 }
104 return r;
105}
106
107
wdenkb15cbc02000-08-21 15:05:47 +0000108/*
109 * hweightN: returns the hamming weight (i.e. the number
110 * of bits set) of a N-bit word
111 */
112
113static inline unsigned int generic_hweight32(unsigned int w)
114{
wdenk57b2d802003-06-27 21:31:46 +0000115 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
116 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
117 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
118 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
119 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +0000120}
121
122static inline unsigned int generic_hweight16(unsigned int w)
123{
wdenk57b2d802003-06-27 21:31:46 +0000124 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
125 res = (res & 0x3333) + ((res >> 2) & 0x3333);
126 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
127 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +0000128}
129
130static inline unsigned int generic_hweight8(unsigned int w)
131{
wdenk57b2d802003-06-27 21:31:46 +0000132 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
133 res = (res & 0x33) + ((res >> 2) & 0x33);
134 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000135}
136
Vignesh Raghavendra18fd00b2019-12-09 10:25:31 +0530137static inline unsigned long generic_hweight64(__u64 w)
138{
139 return generic_hweight32((unsigned int)(w >> 32)) +
140 generic_hweight32((unsigned int)w);
141}
142
143static inline unsigned long hweight_long(unsigned long w)
144{
145 return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
146}
147
wdenkb15cbc02000-08-21 15:05:47 +0000148#include <asm/bitops.h>
149
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200150/* linux/include/asm-generic/bitops/non-atomic.h */
151
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200152#ifndef PLATFORM__SET_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200153# define __set_bit generic_set_bit
154#endif
155
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200156#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200157# define __clear_bit generic_clear_bit
158#endif
159
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200160#ifndef PLATFORM_FFS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200161# define ffs generic_ffs
162#endif
163
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200164#ifndef PLATFORM_FLS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200165# define fls generic_fls
166#endif
167
Fabio Estevam5e39d082015-11-05 12:43:38 -0200168static inline unsigned fls_long(unsigned long l)
169{
170 if (sizeof(l) == 4)
171 return fls(l);
172 return fls64(l);
173}
174
175/**
176 * __ffs64 - find first set bit in a 64 bit word
177 * @word: The 64 bit word
178 *
179 * On 64 bit arches this is a synomyn for __ffs
180 * The result is not defined if no bits are set, so check that @word
181 * is non-zero before calling this.
182 */
183static inline unsigned long __ffs64(u64 word)
184{
185#if BITS_PER_LONG == 32
186 if (((u32)word) == 0UL)
187 return __ffs((u32)(word >> 32)) + 32;
188#elif BITS_PER_LONG != 64
189#error BITS_PER_LONG not 32 or 64
190#endif
191 return __ffs((unsigned long)word);
192}
193
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200194/**
195 * __set_bit - Set a bit in memory
196 * @nr: the bit to set
197 * @addr: the address to start counting from
198 *
199 * Unlike set_bit(), this function is non-atomic and may be reordered.
200 * If it's called on the same region of memory simultaneously, the effect
201 * may be that only one operation succeeds.
202 */
203static inline void generic_set_bit(int nr, volatile unsigned long *addr)
204{
205 unsigned long mask = BIT_MASK(nr);
206 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
207
208 *p |= mask;
209}
210
211static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
212{
213 unsigned long mask = BIT_MASK(nr);
214 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
215
216 *p &= ~mask;
217}
wdenkb15cbc02000-08-21 15:05:47 +0000218
219#endif