Merge tag 'lsk-v3.10-android-15.01'
[firefly-linux-kernel-4.4.55.git] / include / linux / bitops.h
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 #include <asm/types.h>
4
5 #ifdef  __KERNEL__
6 #define BIT(nr)                 (1UL << (nr))
7 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
8 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
9 #define BITS_PER_BYTE           8
10 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
11 #endif
12
13 /*
14  * Create a contiguous bitmask starting at bit position @l and ending at
15  * position @h. For example
16  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
17  */
18 #define GENMASK(h, l)           (((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
19 #define GENMASK_ULL(h, l)       (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
20
21 extern unsigned int __sw_hweight8(unsigned int w);
22 extern unsigned int __sw_hweight16(unsigned int w);
23 extern unsigned int __sw_hweight32(unsigned int w);
24 extern unsigned long __sw_hweight64(__u64 w);
25
26 /*
27  * Include this here because some architectures need generic_ffs/fls in
28  * scope
29  */
30 #include <asm/bitops.h>
31
32 #define for_each_set_bit(bit, addr, size) \
33         for ((bit) = find_first_bit((addr), (size));            \
34              (bit) < (size);                                    \
35              (bit) = find_next_bit((addr), (size), (bit) + 1))
36
37 /* same as for_each_set_bit() but use bit as value to start with */
38 #define for_each_set_bit_from(bit, addr, size) \
39         for ((bit) = find_next_bit((addr), (size), (bit));      \
40              (bit) < (size);                                    \
41              (bit) = find_next_bit((addr), (size), (bit) + 1))
42
43 #define for_each_clear_bit(bit, addr, size) \
44         for ((bit) = find_first_zero_bit((addr), (size));       \
45              (bit) < (size);                                    \
46              (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
47
48 /* same as for_each_clear_bit() but use bit as value to start with */
49 #define for_each_clear_bit_from(bit, addr, size) \
50         for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
51              (bit) < (size);                                    \
52              (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
53
54 static __inline__ int get_bitmask_order(unsigned int count)
55 {
56         int order;
57
58         order = fls(count);
59         return order;   /* We could be slightly more clever with -1 here... */
60 }
61
62 static __inline__ int get_count_order(unsigned int count)
63 {
64         int order;
65
66         order = fls(count) - 1;
67         if (count & (count - 1))
68                 order++;
69         return order;
70 }
71
72 static inline unsigned long hweight_long(unsigned long w)
73 {
74         return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
75 }
76
77 /**
78  * rol64 - rotate a 64-bit value left
79  * @word: value to rotate
80  * @shift: bits to roll
81  */
82 static inline __u64 rol64(__u64 word, unsigned int shift)
83 {
84         return (word << shift) | (word >> (64 - shift));
85 }
86
87 /**
88  * ror64 - rotate a 64-bit value right
89  * @word: value to rotate
90  * @shift: bits to roll
91  */
92 static inline __u64 ror64(__u64 word, unsigned int shift)
93 {
94         return (word >> shift) | (word << (64 - shift));
95 }
96
97 /**
98  * rol32 - rotate a 32-bit value left
99  * @word: value to rotate
100  * @shift: bits to roll
101  */
102 static inline __u32 rol32(__u32 word, unsigned int shift)
103 {
104         return (word << shift) | (word >> (32 - shift));
105 }
106
107 /**
108  * ror32 - rotate a 32-bit value right
109  * @word: value to rotate
110  * @shift: bits to roll
111  */
112 static inline __u32 ror32(__u32 word, unsigned int shift)
113 {
114         return (word >> shift) | (word << (32 - shift));
115 }
116
117 /**
118  * rol16 - rotate a 16-bit value left
119  * @word: value to rotate
120  * @shift: bits to roll
121  */
122 static inline __u16 rol16(__u16 word, unsigned int shift)
123 {
124         return (word << shift) | (word >> (16 - shift));
125 }
126
127 /**
128  * ror16 - rotate a 16-bit value right
129  * @word: value to rotate
130  * @shift: bits to roll
131  */
132 static inline __u16 ror16(__u16 word, unsigned int shift)
133 {
134         return (word >> shift) | (word << (16 - shift));
135 }
136
137 /**
138  * rol8 - rotate an 8-bit value left
139  * @word: value to rotate
140  * @shift: bits to roll
141  */
142 static inline __u8 rol8(__u8 word, unsigned int shift)
143 {
144         return (word << shift) | (word >> (8 - shift));
145 }
146
147 /**
148  * ror8 - rotate an 8-bit value right
149  * @word: value to rotate
150  * @shift: bits to roll
151  */
152 static inline __u8 ror8(__u8 word, unsigned int shift)
153 {
154         return (word >> shift) | (word << (8 - shift));
155 }
156
157 /**
158  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
159  * @value: value to sign extend
160  * @index: 0 based bit index (0<=index<32) to sign bit
161  */
162 static inline __s32 sign_extend32(__u32 value, int index)
163 {
164         __u8 shift = 31 - index;
165         return (__s32)(value << shift) >> shift;
166 }
167
168 static 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  */
183 static 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
194 #ifdef __KERNEL__
195
196 #ifndef set_mask_bits
197 #define set_mask_bits(ptr, _mask, _bits)        \
198 ({                                                              \
199         const typeof(*ptr) mask = (_mask), bits = (_bits);      \
200         typeof(*ptr) old, new;                                  \
201                                                                 \
202         do {                                                    \
203                 old = ACCESS_ONCE(*ptr);                        \
204                 new = (old & ~mask) | bits;                     \
205         } while (cmpxchg(ptr, old, new) != old);                \
206                                                                 \
207         new;                                                    \
208 })
209 #endif
210
211 #ifndef find_last_bit
212 /**
213  * find_last_bit - find the last set bit in a memory region
214  * @addr: The address to start the search at
215  * @size: The maximum size to search
216  *
217  * Returns the bit number of the first set bit, or size.
218  */
219 extern unsigned long find_last_bit(const unsigned long *addr,
220                                    unsigned long size);
221 #endif
222
223 #endif /* __KERNEL__ */
224 #endif