Skip to content

Commit 741c74f

Browse files
olsajiriacmel
authored andcommitted
tools lib: Add bitmap_and function
Add support to perform logical and on bitmaps. Code taken from kernel's include/linux/bitmap.h. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1470074555-24889-4-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 820d12b commit 741c74f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎tools/include/linux/bitmap.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
int __bitmap_weight(const unsigned long *bitmap, int bits);
1212
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
1313
const unsigned long *bitmap2, int bits);
14+
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
15+
const unsigned long *bitmap2, unsigned int bits);
1416

1517
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
1618

@@ -85,4 +87,19 @@ static inline unsigned long *bitmap_alloc(int nbits)
8587
size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
8688
char *buf, size_t size);
8789

90+
/**
91+
* bitmap_and - Do logical and on bitmaps
92+
* @dst: resulting bitmap
93+
* @src1: operand 1
94+
* @src2: operand 2
95+
* @nbits: size of bitmap
96+
*/
97+
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
98+
const unsigned long *src2, unsigned int nbits)
99+
{
100+
if (small_const_nbits(nbits))
101+
return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
102+
return __bitmap_and(dst, src1, src2, nbits);
103+
}
104+
88105
#endif /* _PERF_BITOPS_H */

‎tools/lib/bitmap.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,18 @@ size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
5858
}
5959
return ret;
6060
}
61+
62+
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
63+
const unsigned long *bitmap2, unsigned int bits)
64+
{
65+
unsigned int k;
66+
unsigned int lim = bits/BITS_PER_LONG;
67+
unsigned long result = 0;
68+
69+
for (k = 0; k < lim; k++)
70+
result |= (dst[k] = bitmap1[k] & bitmap2[k]);
71+
if (bits % BITS_PER_LONG)
72+
result |= (dst[k] = bitmap1[k] & bitmap2[k] &
73+
BITMAP_LAST_WORD_MASK(bits));
74+
return result != 0;
75+
}

0 commit comments

Comments
 (0)