blob: 25b8b2f33ae947c0aedc9ab59c0ce88326da024c [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
Shannon Nelson3e037452007-10-16 01:27:40 -070019#define for_each_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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static __inline__ int get_bitmask_order(unsigned int count)
26{
27 int order;
Peter Zijlstra9f416992010-01-22 15:59:29 +010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 order = fls(count);
30 return order; /* We could be slightly more clever with -1 here... */
31}
32
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010033static __inline__ int get_count_order(unsigned int count)
34{
35 int order;
Peter Zijlstra9f416992010-01-22 15:59:29 +010036
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010037 order = fls(count) - 1;
38 if (count & (count - 1))
39 order++;
40 return order;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static inline unsigned long hweight_long(unsigned long w)
44{
Akinobu Mitae9bebd62006-03-26 01:39:55 -080045 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Peter Zijlstrafce877e2010-01-29 13:25:12 +010048/*
49 * Clearly slow versions of the hweightN() functions, their benefit is
50 * of course compile time evaluation of constant arguments.
51 */
52#define HWEIGHT8(w) \
53 ( BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + \
54 (!!((w) & (1ULL << 0))) + \
55 (!!((w) & (1ULL << 1))) + \
56 (!!((w) & (1ULL << 2))) + \
57 (!!((w) & (1ULL << 3))) + \
58 (!!((w) & (1ULL << 4))) + \
59 (!!((w) & (1ULL << 5))) + \
60 (!!((w) & (1ULL << 6))) + \
Peter Zijlstra9f416992010-01-22 15:59:29 +010061 (!!((w) & (1ULL << 7))) )
62
Peter Zijlstrafce877e2010-01-29 13:25:12 +010063#define HWEIGHT16(w) (HWEIGHT8(w) + HWEIGHT8((w) >> 8))
64#define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
65#define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
66
67/*
68 * Type invariant version that simply casts things to the
69 * largest type.
70 */
71#define HWEIGHT(w) HWEIGHT64((u64)(w))
Peter Zijlstra9f416992010-01-22 15:59:29 +010072
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080073/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * rol32 - rotate a 32-bit value left
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * @word: value to rotate
76 * @shift: bits to roll
77 */
78static inline __u32 rol32(__u32 word, unsigned int shift)
79{
80 return (word << shift) | (word >> (32 - shift));
81}
82
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080083/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * ror32 - rotate a 32-bit value right
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * @word: value to rotate
86 * @shift: bits to roll
87 */
88static inline __u32 ror32(__u32 word, unsigned int shift)
89{
90 return (word >> shift) | (word << (32 - shift));
91}
92
Harvey Harrison3afe3922008-03-28 14:16:01 -070093/**
94 * rol16 - rotate a 16-bit value left
95 * @word: value to rotate
96 * @shift: bits to roll
97 */
98static inline __u16 rol16(__u16 word, unsigned int shift)
99{
100 return (word << shift) | (word >> (16 - shift));
101}
102
103/**
104 * ror16 - rotate a 16-bit value right
105 * @word: value to rotate
106 * @shift: bits to roll
107 */
108static inline __u16 ror16(__u16 word, unsigned int shift)
109{
110 return (word >> shift) | (word << (16 - shift));
111}
112
113/**
114 * rol8 - rotate an 8-bit value left
115 * @word: value to rotate
116 * @shift: bits to roll
117 */
118static inline __u8 rol8(__u8 word, unsigned int shift)
119{
120 return (word << shift) | (word >> (8 - shift));
121}
122
123/**
124 * ror8 - rotate an 8-bit value right
125 * @word: value to rotate
126 * @shift: bits to roll
127 */
128static inline __u8 ror8(__u8 word, unsigned int shift)
129{
130 return (word >> shift) | (word << (8 - shift));
131}
132
Andrew Morton962749a2006-03-25 03:08:01 -0800133static inline unsigned fls_long(unsigned long l)
134{
135 if (sizeof(l) == 4)
136 return fls(l);
137 return fls64(l);
138}
139
Steven Whitehouse952043a2009-04-23 08:48:15 +0100140/**
141 * __ffs64 - find first set bit in a 64 bit word
142 * @word: The 64 bit word
143 *
144 * On 64 bit arches this is a synomyn for __ffs
145 * The result is not defined if no bits are set, so check that @word
146 * is non-zero before calling this.
147 */
148static inline unsigned long __ffs64(u64 word)
149{
150#if BITS_PER_LONG == 32
151 if (((u32)word) == 0UL)
152 return __ffs((u32)(word >> 32)) + 32;
153#elif BITS_PER_LONG != 64
154#error BITS_PER_LONG not 32 or 64
155#endif
156 return __ffs((unsigned long)word);
157}
158
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100159#ifdef __KERNEL__
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200160#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200161
162/**
163 * find_first_bit - find the first set bit in a memory region
164 * @addr: The address to start the search at
165 * @size: The maximum size to search
166 *
167 * Returns the bit number of the first set bit.
168 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200169extern unsigned long find_first_bit(const unsigned long *addr,
170 unsigned long size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200171
172/**
173 * find_first_zero_bit - find the first cleared bit in a memory region
174 * @addr: The address to start the search at
175 * @size: The maximum size to search
176 *
177 * Returns the bit number of the first cleared bit.
178 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200179extern unsigned long find_first_zero_bit(const unsigned long *addr,
180 unsigned long size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200181#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
182
Rusty Russellab53d472009-01-01 10:12:19 +1030183#ifdef CONFIG_GENERIC_FIND_LAST_BIT
184/**
185 * find_last_bit - find the last set bit in a memory region
186 * @addr: The address to start the search at
187 * @size: The maximum size to search
188 *
189 * Returns the bit number of the first set bit, or size.
190 */
191extern unsigned long find_last_bit(const unsigned long *addr,
192 unsigned long size);
193#endif /* CONFIG_GENERIC_FIND_LAST_BIT */
194
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100195#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100196
197/**
198 * find_next_bit - find the next set bit in a memory region
199 * @addr: The address to base the search on
200 * @offset: The bitnumber to start searching at
201 * @size: The bitmap size in bits
202 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200203extern unsigned long find_next_bit(const unsigned long *addr,
204 unsigned long size, unsigned long offset);
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100205
206/**
207 * find_next_zero_bit - find the next cleared bit in a memory region
208 * @addr: The address to base the search on
209 * @offset: The bitnumber to start searching at
210 * @size: The bitmap size in bits
211 */
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100212
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200213extern unsigned long find_next_zero_bit(const unsigned long *addr,
214 unsigned long size,
215 unsigned long offset);
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100216
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100217#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
218#endif /* __KERNEL__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif