blob: b796eab5ca75325bcfb9ff1c56c9527aee42feff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_BITOPS_H
2#define _LINUX_BITOPS_H
3#include <asm/types.h>
4
Jiri Slabyd05be132007-10-18 23:40:31 -07005#ifdef __KERNEL__
Jiri Slaby93043ec2007-10-18 23:40:35 -07006#define BIT(nr) (1UL << (nr))
Jiri Slabyd05be132007-10-18 23:40:31 -07007#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Jiri Slabyd05be132007-10-18 23:40:31 -07009#define BITS_PER_BYTE 8
Eric Dumazetede9c692008-04-29 00:58:35 -070010#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
Jiri Slabyd05be132007-10-18 23:40:31 -070011#endif
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Include this here because some architectures need generic_ffs/fls in
15 * scope
16 */
17#include <asm/bitops.h>
18
Akinobu Mita984b3f52010-03-05 13:41:37 -080019#define for_each_set_bit(bit, addr, size) \
Shannon Nelson3e037452007-10-16 01:27:40 -070020 for ((bit) = find_first_bit((addr), (size)); \
21 (bit) < (size); \
22 (bit) = find_next_bit((addr), (size), (bit) + 1))
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static __inline__ int get_bitmask_order(unsigned int count)
25{
26 int order;
Peter Zijlstra9f416992010-01-22 15:59:29 +010027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 order = fls(count);
29 return order; /* We could be slightly more clever with -1 here... */
30}
31
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010032static __inline__ int get_count_order(unsigned int count)
33{
34 int order;
Peter Zijlstra9f416992010-01-22 15:59:29 +010035
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010036 order = fls(count) - 1;
37 if (count & (count - 1))
38 order++;
39 return order;
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static inline unsigned long hweight_long(unsigned long w)
43{
Akinobu Mitae9bebd62006-03-26 01:39:55 -080044 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Peter Zijlstrafce877e2010-01-29 13:25:12 +010047/*
48 * Clearly slow versions of the hweightN() functions, their benefit is
49 * of course compile time evaluation of constant arguments.
50 */
51#define HWEIGHT8(w) \
52 ( BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + \
53 (!!((w) & (1ULL << 0))) + \
54 (!!((w) & (1ULL << 1))) + \
55 (!!((w) & (1ULL << 2))) + \
56 (!!((w) & (1ULL << 3))) + \
57 (!!((w) & (1ULL << 4))) + \
58 (!!((w) & (1ULL << 5))) + \
59 (!!((w) & (1ULL << 6))) + \
Peter Zijlstra9f416992010-01-22 15:59:29 +010060 (!!((w) & (1ULL << 7))) )
61
Peter Zijlstrafce877e2010-01-29 13:25:12 +010062#define HWEIGHT16(w) (HWEIGHT8(w) + HWEIGHT8((w) >> 8))
63#define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
64#define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
65
66/*
67 * Type invariant version that simply casts things to the
68 * largest type.
69 */
70#define HWEIGHT(w) HWEIGHT64((u64)(w))
Peter Zijlstra9f416992010-01-22 15:59:29 +010071
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080072/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * rol32 - rotate a 32-bit value left
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * @word: value to rotate
75 * @shift: bits to roll
76 */
77static inline __u32 rol32(__u32 word, unsigned int shift)
78{
79 return (word << shift) | (word >> (32 - shift));
80}
81
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080082/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * ror32 - rotate a 32-bit value right
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * @word: value to rotate
85 * @shift: bits to roll
86 */
87static inline __u32 ror32(__u32 word, unsigned int shift)
88{
89 return (word >> shift) | (word << (32 - shift));
90}
91
Harvey Harrison3afe3922008-03-28 14:16:01 -070092/**
93 * rol16 - rotate a 16-bit value left
94 * @word: value to rotate
95 * @shift: bits to roll
96 */
97static inline __u16 rol16(__u16 word, unsigned int shift)
98{
99 return (word << shift) | (word >> (16 - shift));
100}
101
102/**
103 * ror16 - rotate a 16-bit value right
104 * @word: value to rotate
105 * @shift: bits to roll
106 */
107static inline __u16 ror16(__u16 word, unsigned int shift)
108{
109 return (word >> shift) | (word << (16 - shift));
110}
111
112/**
113 * rol8 - rotate an 8-bit value left
114 * @word: value to rotate
115 * @shift: bits to roll
116 */
117static inline __u8 rol8(__u8 word, unsigned int shift)
118{
119 return (word << shift) | (word >> (8 - shift));
120}
121
122/**
123 * ror8 - rotate an 8-bit value right
124 * @word: value to rotate
125 * @shift: bits to roll
126 */
127static inline __u8 ror8(__u8 word, unsigned int shift)
128{
129 return (word >> shift) | (word << (8 - shift));
130}
131
Andrew Morton962749a2006-03-25 03:08:01 -0800132static inline unsigned fls_long(unsigned long l)
133{
134 if (sizeof(l) == 4)
135 return fls(l);
136 return fls64(l);
137}
138
Steven Whitehouse952043a2009-04-23 08:48:15 +0100139/**
140 * __ffs64 - find first set bit in a 64 bit word
141 * @word: The 64 bit word
142 *
143 * On 64 bit arches this is a synomyn for __ffs
144 * The result is not defined if no bits are set, so check that @word
145 * is non-zero before calling this.
146 */
147static inline unsigned long __ffs64(u64 word)
148{
149#if BITS_PER_LONG == 32
150 if (((u32)word) == 0UL)
151 return __ffs((u32)(word >> 32)) + 32;
152#elif BITS_PER_LONG != 64
153#error BITS_PER_LONG not 32 or 64
154#endif
155 return __ffs((unsigned long)word);
156}
157
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100158#ifdef __KERNEL__
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200159#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200160
161/**
162 * find_first_bit - find the first set bit in a memory region
163 * @addr: The address to start the search at
164 * @size: The maximum size to search
165 *
166 * Returns the bit number of the first set bit.
167 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200168extern unsigned long find_first_bit(const unsigned long *addr,
169 unsigned long size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200170
171/**
172 * find_first_zero_bit - find the first cleared bit in a memory region
173 * @addr: The address to start the search at
174 * @size: The maximum size to search
175 *
176 * Returns the bit number of the first cleared bit.
177 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200178extern unsigned long find_first_zero_bit(const unsigned long *addr,
179 unsigned long size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200180#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
181
Rusty Russellab53d472009-01-01 10:12:19 +1030182#ifdef CONFIG_GENERIC_FIND_LAST_BIT
183/**
184 * find_last_bit - find the last set bit in a memory region
185 * @addr: The address to start the search at
186 * @size: The maximum size to search
187 *
188 * Returns the bit number of the first set bit, or size.
189 */
190extern unsigned long find_last_bit(const unsigned long *addr,
191 unsigned long size);
192#endif /* CONFIG_GENERIC_FIND_LAST_BIT */
193
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100194#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100195
196/**
197 * find_next_bit - find the next set bit in a memory region
198 * @addr: The address to base the search on
199 * @offset: The bitnumber to start searching at
200 * @size: The bitmap size in bits
201 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200202extern unsigned long find_next_bit(const unsigned long *addr,
203 unsigned long size, unsigned long offset);
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100204
205/**
206 * find_next_zero_bit - find the next cleared bit in a memory region
207 * @addr: The address to base the search on
208 * @offset: The bitnumber to start searching at
209 * @size: The bitmap size in bits
210 */
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100211
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200212extern unsigned long find_next_zero_bit(const unsigned long *addr,
213 unsigned long size,
214 unsigned long offset);
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100215
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100216#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
217#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif