blob: ca160270fdfafb43149752da73ce0e8560d61990 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -03002#ifndef _PERF_BITOPS_H
3#define _PERF_BITOPS_H
4
5#include <string.h>
6#include <linux/bitops.h>
Jiri Olsa98c03292016-08-02 13:33:02 +02007#include <stdlib.h>
Matthew Wilcoxc68a2aa2016-12-16 11:52:43 -05008#include <linux/kernel.h>
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -03009
Borislav Petkovd944c4e2014-04-25 21:31:02 +020010#define DECLARE_BITMAP(name,bits) \
11 unsigned long name[BITS_TO_LONGS(bits)]
12
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030013int __bitmap_weight(const unsigned long *bitmap, int bits);
Jiri Olsa850f8122012-01-27 15:34:23 +010014void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
15 const unsigned long *bitmap2, int bits);
Jiri Olsa741c74f2016-08-01 20:02:31 +020016int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
17 const unsigned long *bitmap2, unsigned int bits);
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030018
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030019#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
20
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030021#define BITMAP_LAST_WORD_MASK(nbits) \
22( \
23 ((nbits) % BITS_PER_LONG) ? \
24 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
25)
26
27#define small_const_nbits(nbits) \
28 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
29
30static inline void bitmap_zero(unsigned long *dst, int nbits)
31{
32 if (small_const_nbits(nbits))
33 *dst = 0UL;
34 else {
35 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
36 memset(dst, 0, len);
37 }
38}
39
Matthew Wilcoxb328daf2016-12-14 15:08:26 -080040static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
41{
42 unsigned int nlongs = BITS_TO_LONGS(nbits);
43 if (!small_const_nbits(nbits)) {
44 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
45 memset(dst, 0xff, len);
46 }
47 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
48}
49
50static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
51{
52 if (small_const_nbits(nbits))
53 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
54
55 return find_first_bit(src, nbits) == nbits;
56}
57
58static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
59{
60 if (small_const_nbits(nbits))
61 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
62
63 return find_first_zero_bit(src, nbits) == nbits;
64}
65
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030066static inline int bitmap_weight(const unsigned long *src, int nbits)
67{
68 if (small_const_nbits(nbits))
69 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
70 return __bitmap_weight(src, nbits);
71}
72
Jiri Olsa850f8122012-01-27 15:34:23 +010073static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
74 const unsigned long *src2, int nbits)
75{
76 if (small_const_nbits(nbits))
77 *dst = *src1 | *src2;
78 else
79 __bitmap_or(dst, src1, src2, nbits);
80}
81
Jiri Olsa416c4192014-10-26 23:44:03 +010082/**
83 * test_and_set_bit - Set a bit and return its old value
84 * @nr: Bit to set
85 * @addr: Address to count from
86 */
87static inline int test_and_set_bit(int nr, unsigned long *addr)
88{
89 unsigned long mask = BIT_MASK(nr);
90 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
91 unsigned long old;
92
93 old = *p;
94 *p = old | mask;
95
96 return (old & mask) != 0;
97}
98
Jiri Olsa98c03292016-08-02 13:33:02 +020099/**
100 * bitmap_alloc - Allocate bitmap
101 * @nr: Bit to set
102 */
103static inline unsigned long *bitmap_alloc(int nbits)
104{
105 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
106}
107
Jiri Olsa820d12b2016-08-01 20:02:30 +0200108/*
109 * bitmap_scnprintf - print bitmap list into buffer
110 * @bitmap: bitmap
111 * @nbits: size of bitmap
112 * @buf: buffer to store output
113 * @size: size of @buf
114 */
115size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
116 char *buf, size_t size);
117
Jiri Olsa741c74f2016-08-01 20:02:31 +0200118/**
119 * bitmap_and - Do logical and on bitmaps
120 * @dst: resulting bitmap
121 * @src1: operand 1
122 * @src2: operand 2
123 * @nbits: size of bitmap
124 */
125static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
126 const unsigned long *src2, unsigned int nbits)
127{
128 if (small_const_nbits(nbits))
129 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
130 return __bitmap_and(dst, src1, src2, nbits);
131}
132
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -0300133#endif /* _PERF_BITOPS_H */