OSDN Git Service

drivers: leds: Import Xiaomi changes
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / include / linux / bitops.h
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 #include <asm/types.h>
4 #include <linux/bits.h>
5
6 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
7
8 extern unsigned int __sw_hweight8(unsigned int w);
9 extern unsigned int __sw_hweight16(unsigned int w);
10 extern unsigned int __sw_hweight32(unsigned int w);
11 extern unsigned long __sw_hweight64(__u64 w);
12
13 /*
14  * Include this here because some architectures need generic_ffs/fls in
15  * scope
16  */
17 #include <asm/bitops.h>
18
19 #define for_each_set_bit(bit, addr, size) \
20         for ((bit) = find_first_bit((addr), (size));            \
21              (bit) < (size);                                    \
22              (bit) = find_next_bit((addr), (size), (bit) + 1))
23
24 /* same as for_each_set_bit() but use bit as value to start with */
25 #define for_each_set_bit_from(bit, addr, size) \
26         for ((bit) = find_next_bit((addr), (size), (bit));      \
27              (bit) < (size);                                    \
28              (bit) = find_next_bit((addr), (size), (bit) + 1))
29
30 #define for_each_clear_bit(bit, addr, size) \
31         for ((bit) = find_first_zero_bit((addr), (size));       \
32              (bit) < (size);                                    \
33              (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
34
35 /* same as for_each_clear_bit() but use bit as value to start with */
36 #define for_each_clear_bit_from(bit, addr, size) \
37         for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
38              (bit) < (size);                                    \
39              (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
40
41 static inline int get_bitmask_order(unsigned int count)
42 {
43         int order;
44
45         order = fls(count);
46         return order;   /* We could be slightly more clever with -1 here... */
47 }
48
49 static inline int get_count_order(unsigned int count)
50 {
51         int order;
52
53         order = fls(count) - 1;
54         if (count & (count - 1))
55                 order++;
56         return order;
57 }
58
59 static __always_inline unsigned long hweight_long(unsigned long w)
60 {
61         return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
62 }
63
64 /**
65  * rol64 - rotate a 64-bit value left
66  * @word: value to rotate
67  * @shift: bits to roll
68  */
69 static inline __u64 rol64(__u64 word, unsigned int shift)
70 {
71         return (word << (shift & 63)) | (word >> ((-shift) & 63));
72 }
73
74 /**
75  * ror64 - rotate a 64-bit value right
76  * @word: value to rotate
77  * @shift: bits to roll
78  */
79 static inline __u64 ror64(__u64 word, unsigned int shift)
80 {
81         return (word >> (shift & 63)) | (word << ((-shift) & 63));
82 }
83
84 /**
85  * rol32 - rotate a 32-bit value left
86  * @word: value to rotate
87  * @shift: bits to roll
88  */
89 static inline __u32 rol32(__u32 word, unsigned int shift)
90 {
91         return (word << (shift & 31)) | (word >> ((-shift) & 31));
92 }
93
94 /**
95  * ror32 - rotate a 32-bit value right
96  * @word: value to rotate
97  * @shift: bits to roll
98  */
99 static inline __u32 ror32(__u32 word, unsigned int shift)
100 {
101         return (word >> (shift & 31)) | (word << ((-shift) & 31));
102 }
103
104 /**
105  * rol16 - rotate a 16-bit value left
106  * @word: value to rotate
107  * @shift: bits to roll
108  */
109 static inline __u16 rol16(__u16 word, unsigned int shift)
110 {
111         return (word << (shift & 15)) | (word >> ((-shift) & 15));
112 }
113
114 /**
115  * ror16 - rotate a 16-bit value right
116  * @word: value to rotate
117  * @shift: bits to roll
118  */
119 static inline __u16 ror16(__u16 word, unsigned int shift)
120 {
121         return (word >> (shift & 15)) | (word << ((-shift) & 15));
122 }
123
124 /**
125  * rol8 - rotate an 8-bit value left
126  * @word: value to rotate
127  * @shift: bits to roll
128  */
129 static inline __u8 rol8(__u8 word, unsigned int shift)
130 {
131         return (word << (shift & 7)) | (word >> ((-shift) & 7));
132 }
133
134 /**
135  * ror8 - rotate an 8-bit value right
136  * @word: value to rotate
137  * @shift: bits to roll
138  */
139 static inline __u8 ror8(__u8 word, unsigned int shift)
140 {
141         return (word >> (shift & 7)) | (word << ((-shift) & 7));
142 }
143
144 /**
145  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
146  * @value: value to sign extend
147  * @index: 0 based bit index (0<=index<32) to sign bit
148  *
149  * This is safe to use for 16- and 8-bit types as well.
150  */
151 static inline __s32 sign_extend32(__u32 value, int index)
152 {
153         __u8 shift = 31 - index;
154         return (__s32)(value << shift) >> shift;
155 }
156
157 /**
158  * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
159  * @value: value to sign extend
160  * @index: 0 based bit index (0<=index<64) to sign bit
161  */
162 static inline __s64 sign_extend64(__u64 value, int index)
163 {
164         __u8 shift = 63 - index;
165         return (__s64)(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 number of bits to search
216  *
217  * Returns the bit number of the last 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