blob: 576b15dc53c33381eccd7fe02efd9bd73615adc3 [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
Jagan Teki79d4d5f2015-10-21 17:30:34 +05308#define BIT(nr) (1UL << (nr))
9#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
10#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Heiko Schocher69bab552015-09-07 13:43:52 +020011
wdenkb15cbc02000-08-21 15:05:47 +000012/*
Jagan Teki8aceb8f2015-10-21 16:46:51 +053013 * Create a contiguous bitmask starting at bit position @l and ending at
14 * position @h. For example
15 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
16 */
17#define GENMASK(h, l) \
18 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
19
20#define GENMASK_ULL(h, l) \
21 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
22
23/*
wdenkb15cbc02000-08-21 15:05:47 +000024 * ffs: find first bit set. This is defined the same way as
25 * the libc and compiler builtin ffs routines, therefore
26 * differs in spirit from the above ffz (man ffs).
27 */
28
29static inline int generic_ffs(int x)
30{
31 int r = 1;
32
33 if (!x)
34 return 0;
35 if (!(x & 0xffff)) {
36 x >>= 16;
37 r += 16;
38 }
39 if (!(x & 0xff)) {
40 x >>= 8;
41 r += 8;
42 }
43 if (!(x & 0xf)) {
44 x >>= 4;
45 r += 4;
46 }
47 if (!(x & 3)) {
48 x >>= 2;
49 r += 2;
50 }
51 if (!(x & 1)) {
52 x >>= 1;
53 r += 1;
54 }
55 return r;
56}
57
Simon Kagstrom2258cec2009-08-24 09:10:12 +020058/**
59 * fls - find last (most-significant) bit set
60 * @x: the word to search
61 *
62 * This is defined the same way as ffs.
63 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
64 */
65static inline int generic_fls(int x)
66{
67 int r = 32;
68
69 if (!x)
70 return 0;
71 if (!(x & 0xffff0000u)) {
72 x <<= 16;
73 r -= 16;
74 }
75 if (!(x & 0xff000000u)) {
76 x <<= 8;
77 r -= 8;
78 }
79 if (!(x & 0xf0000000u)) {
80 x <<= 4;
81 r -= 4;
82 }
83 if (!(x & 0xc0000000u)) {
84 x <<= 2;
85 r -= 2;
86 }
87 if (!(x & 0x80000000u)) {
88 x <<= 1;
89 r -= 1;
90 }
91 return r;
92}
93
94
wdenkb15cbc02000-08-21 15:05:47 +000095/*
96 * hweightN: returns the hamming weight (i.e. the number
97 * of bits set) of a N-bit word
98 */
99
100static inline unsigned int generic_hweight32(unsigned int w)
101{
wdenk57b2d802003-06-27 21:31:46 +0000102 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
103 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
104 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
105 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
106 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +0000107}
108
109static inline unsigned int generic_hweight16(unsigned int w)
110{
wdenk57b2d802003-06-27 21:31:46 +0000111 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
112 res = (res & 0x3333) + ((res >> 2) & 0x3333);
113 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
114 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +0000115}
116
117static inline unsigned int generic_hweight8(unsigned int w)
118{
wdenk57b2d802003-06-27 21:31:46 +0000119 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
120 res = (res & 0x33) + ((res >> 2) & 0x33);
121 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000122}
123
124#include <asm/bitops.h>
125
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200126/* linux/include/asm-generic/bitops/non-atomic.h */
127
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200128#ifndef PLATFORM__SET_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200129# define __set_bit generic_set_bit
130#endif
131
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200132#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200133# define __clear_bit generic_clear_bit
134#endif
135
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200136#ifndef PLATFORM_FFS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200137# define ffs generic_ffs
138#endif
139
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200140#ifndef PLATFORM_FLS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200141# define fls generic_fls
142#endif
143
Fabio Estevam5e39d082015-11-05 12:43:38 -0200144static inline unsigned fls_long(unsigned long l)
145{
146 if (sizeof(l) == 4)
147 return fls(l);
148 return fls64(l);
149}
150
151/**
152 * __ffs64 - find first set bit in a 64 bit word
153 * @word: The 64 bit word
154 *
155 * On 64 bit arches this is a synomyn for __ffs
156 * The result is not defined if no bits are set, so check that @word
157 * is non-zero before calling this.
158 */
159static inline unsigned long __ffs64(u64 word)
160{
161#if BITS_PER_LONG == 32
162 if (((u32)word) == 0UL)
163 return __ffs((u32)(word >> 32)) + 32;
164#elif BITS_PER_LONG != 64
165#error BITS_PER_LONG not 32 or 64
166#endif
167 return __ffs((unsigned long)word);
168}
169
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200170/**
171 * __set_bit - Set a bit in memory
172 * @nr: the bit to set
173 * @addr: the address to start counting from
174 *
175 * Unlike set_bit(), this function is non-atomic and may be reordered.
176 * If it's called on the same region of memory simultaneously, the effect
177 * may be that only one operation succeeds.
178 */
179static inline void generic_set_bit(int nr, volatile unsigned long *addr)
180{
181 unsigned long mask = BIT_MASK(nr);
182 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
183
184 *p |= mask;
185}
186
187static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
188{
189 unsigned long mask = BIT_MASK(nr);
190 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
191
192 *p &= ~mask;
193}
wdenkb15cbc02000-08-21 15:05:47 +0000194
195#endif