blob: 969db1981868cc86e540098ed356014f1932db2e [file] [log] [blame]
Arnaldo Carvalho de Melo88bcea42014-12-16 10:53:27 -03001#ifndef _TOOLS_LINUX_BITOPS_H_
2#define _TOOLS_LINUX_BITOPS_H_
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02003
Arnaldo Carvalho de Melo25cd4802015-01-13 10:19:12 -03004#include <asm/types.h>
Arnaldo Carvalho de Meloa860a602011-01-22 19:07:36 -02005#include <linux/compiler.h>
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02006
Irina Tirdea3f34f6c2012-09-11 01:15:00 +03007#ifndef __WORDSIZE
8#define __WORDSIZE (__SIZEOF_LONG__ * 8)
9#endif
10
Ingo Molnar7b39caf2016-07-13 09:37:43 +020011#ifndef BITS_PER_LONG
12# define BITS_PER_LONG __WORDSIZE
13#endif
Arnaldo Carvalho de Melo93c49b32014-12-15 17:08:00 -030014
15#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
16#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
17#define BITS_PER_BYTE 8
18#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
19#define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
20#define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
21#define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
Paul Mackerrasee3d2502009-11-24 15:19:43 +110022
Arnaldo Carvalho de Melo25cd4802015-01-13 10:19:12 -030023extern unsigned int __sw_hweight8(unsigned int w);
24extern unsigned int __sw_hweight16(unsigned int w);
25extern unsigned int __sw_hweight32(unsigned int w);
26extern unsigned long __sw_hweight64(__u64 w);
27
Arnaldo Carvalho de Melo88bcea42014-12-16 10:53:27 -030028/*
29 * Include this here because some architectures need generic_ffs/fls in
30 * scope
31 *
32 * XXX: this needs to be asm/bitops.h, when we get to per arch optimizations
33 */
34#include <asm-generic/bitops.h>
35
Robert Richterb1e5a9b2011-12-07 10:02:57 +010036#define for_each_set_bit(bit, addr, size) \
37 for ((bit) = find_first_bit((addr), (size)); \
38 (bit) < (size); \
39 (bit) = find_next_bit((addr), (size), (bit) + 1))
40
Jiri Olsa02bc11d2016-10-10 09:26:33 +020041#define for_each_clear_bit(bit, addr, size) \
42 for ((bit) = find_first_zero_bit((addr), (size)); \
43 (bit) < (size); \
44 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
45
Robert Richterb1e5a9b2011-12-07 10:02:57 +010046/* same as for_each_set_bit() but use bit as value to start with */
Akinobu Mita307b1cd2012-03-23 15:02:03 -070047#define for_each_set_bit_from(bit, addr, size) \
Robert Richterb1e5a9b2011-12-07 10:02:57 +010048 for ((bit) = find_next_bit((addr), (size), (bit)); \
49 (bit) < (size); \
50 (bit) = find_next_bit((addr), (size), (bit) + 1))
51
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030052static inline unsigned long hweight_long(unsigned long w)
53{
54 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
55}
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020056
Arnaldo Carvalho de Meloafcd4f62014-12-16 11:26:35 -030057static inline unsigned fls_long(unsigned long l)
58{
59 if (sizeof(l) == 4)
60 return fls(l);
61 return fls64(l);
62}
63
Levin, Alexander (Sasha Levin)6c8e6482017-05-25 12:58:47 +000064/**
65 * rol32 - rotate a 32-bit value left
66 * @word: value to rotate
67 * @shift: bits to roll
68 */
69static inline __u32 rol32(__u32 word, unsigned int shift)
70{
71 return (word << shift) | (word >> ((-shift) & 31));
72}
73
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020074#endif