blob: 259df43fb00f0320be668aa685d3aa8d7e1fe2a9 [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>
wdenkb15cbc02000-08-21 15:05:47 +00007
Masahiro Yamada14fecf92017-11-22 02:38:11 +09008#ifdef __KERNEL__
Jagan Teki79d4d5f2015-10-21 17:30:34 +05309#define BIT(nr) (1UL << (nr))
Masahiro Yamada14fecf92017-11-22 02:38:11 +090010#define BIT_ULL(nr) (1ULL << (nr))
Jagan Teki79d4d5f2015-10-21 17:30:34 +053011#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
12#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Masahiro Yamada14fecf92017-11-22 02:38:11 +090013#define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
14#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
15#define BITS_PER_BYTE 8
16#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
17#endif
Heiko Schocher69bab552015-09-07 13:43:52 +020018
wdenkb15cbc02000-08-21 15:05:47 +000019/*
Jagan Teki8aceb8f2015-10-21 16:46:51 +053020 * Create a contiguous bitmask starting at bit position @l and ending at
21 * position @h. For example
22 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
23 */
Vignesh R1b084b22019-02-05 11:29:11 +053024#ifdef CONFIG_SANDBOX
25#define GENMASK(h, l) \
26 (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h))))
27#else
Jagan Teki8aceb8f2015-10-21 16:46:51 +053028#define GENMASK(h, l) \
29 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
Vignesh R1b084b22019-02-05 11:29:11 +053030#endif
Jagan Teki8aceb8f2015-10-21 16:46:51 +053031
32#define GENMASK_ULL(h, l) \
33 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
34
35/*
wdenkb15cbc02000-08-21 15:05:47 +000036 * ffs: find first bit set. This is defined the same way as
37 * the libc and compiler builtin ffs routines, therefore
38 * differs in spirit from the above ffz (man ffs).
39 */
40
41static inline int generic_ffs(int x)
42{
43 int r = 1;
44
45 if (!x)
46 return 0;
47 if (!(x & 0xffff)) {
48 x >>= 16;
49 r += 16;
50 }
51 if (!(x & 0xff)) {
52 x >>= 8;
53 r += 8;
54 }
55 if (!(x & 0xf)) {
56 x >>= 4;
57 r += 4;
58 }
59 if (!(x & 3)) {
60 x >>= 2;
61 r += 2;
62 }
63 if (!(x & 1)) {
64 x >>= 1;
65 r += 1;
66 }
67 return r;
68}
69
Simon Kagstrom2258cec2009-08-24 09:10:12 +020070/**
71 * fls - find last (most-significant) bit set
72 * @x: the word to search
73 *
74 * This is defined the same way as ffs.
75 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
76 */
77static inline int generic_fls(int x)
78{
79 int r = 32;
80
81 if (!x)
82 return 0;
83 if (!(x & 0xffff0000u)) {
84 x <<= 16;
85 r -= 16;
86 }
87 if (!(x & 0xff000000u)) {
88 x <<= 8;
89 r -= 8;
90 }
91 if (!(x & 0xf0000000u)) {
92 x <<= 4;
93 r -= 4;
94 }
95 if (!(x & 0xc0000000u)) {
96 x <<= 2;
97 r -= 2;
98 }
99 if (!(x & 0x80000000u)) {
100 x <<= 1;
101 r -= 1;
102 }
103 return r;
104}
105
106
wdenkb15cbc02000-08-21 15:05:47 +0000107/*
108 * hweightN: returns the hamming weight (i.e. the number
109 * of bits set) of a N-bit word
110 */
111
112static inline unsigned int generic_hweight32(unsigned int w)
113{
wdenk57b2d802003-06-27 21:31:46 +0000114 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
115 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
116 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
117 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
118 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +0000119}
120
121static inline unsigned int generic_hweight16(unsigned int w)
122{
wdenk57b2d802003-06-27 21:31:46 +0000123 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
124 res = (res & 0x3333) + ((res >> 2) & 0x3333);
125 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
126 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +0000127}
128
129static inline unsigned int generic_hweight8(unsigned int w)
130{
wdenk57b2d802003-06-27 21:31:46 +0000131 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
132 res = (res & 0x33) + ((res >> 2) & 0x33);
133 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000134}
135
136#include <asm/bitops.h>
137
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200138/* linux/include/asm-generic/bitops/non-atomic.h */
139
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200140#ifndef PLATFORM__SET_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200141# define __set_bit generic_set_bit
142#endif
143
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200144#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200145# define __clear_bit generic_clear_bit
146#endif
147
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200148#ifndef PLATFORM_FFS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200149# define ffs generic_ffs
150#endif
151
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200152#ifndef PLATFORM_FLS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200153# define fls generic_fls
154#endif
155
Fabio Estevam5e39d082015-11-05 12:43:38 -0200156static inline unsigned fls_long(unsigned long l)
157{
158 if (sizeof(l) == 4)
159 return fls(l);
160 return fls64(l);
161}
162
163/**
164 * __ffs64 - find first set bit in a 64 bit word
165 * @word: The 64 bit word
166 *
167 * On 64 bit arches this is a synomyn for __ffs
168 * The result is not defined if no bits are set, so check that @word
169 * is non-zero before calling this.
170 */
171static inline unsigned long __ffs64(u64 word)
172{
173#if BITS_PER_LONG == 32
174 if (((u32)word) == 0UL)
175 return __ffs((u32)(word >> 32)) + 32;
176#elif BITS_PER_LONG != 64
177#error BITS_PER_LONG not 32 or 64
178#endif
179 return __ffs((unsigned long)word);
180}
181
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200182/**
183 * __set_bit - Set a bit in memory
184 * @nr: the bit to set
185 * @addr: the address to start counting from
186 *
187 * Unlike set_bit(), this function is non-atomic and may be reordered.
188 * If it's called on the same region of memory simultaneously, the effect
189 * may be that only one operation succeeds.
190 */
191static inline void generic_set_bit(int nr, volatile unsigned long *addr)
192{
193 unsigned long mask = BIT_MASK(nr);
194 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
195
196 *p |= mask;
197}
198
199static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
200{
201 unsigned long mask = BIT_MASK(nr);
202 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
203
204 *p &= ~mask;
205}
wdenkb15cbc02000-08-21 15:05:47 +0000206
207#endif