Skip to content

Commit 21c8a5b

Browse files
ljskernelakpm00
authored andcommitted
tools: bitmap: add missing bitmap_[subset(), andnot()]
The bitmap_subset() and bitmap_andnot() functions are not present in the tools version of include/linux/bitmap.h, so add them as subsequent patches implement test code that requires them. We also add the missing __bitmap_subset() to tools/lib/bitmap.c. Link: https://lkml.kernel.org/r/0fd0d4ec868297f522003cb4b5898b53b498805b.1769097829.git.lorenzo.stoakes@oracle.com Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Acked-by: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Barry Song <baohua@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Zi Yan <ziy@nvidia.com> Cc: Damien Le Moal <dlemoal@kernel.org> Cc: "Darrick J. Wong" <djwong@kernel.org> Cc: Jarkko Sakkinen <jarkko@kernel.org> Cc: Yury Norov <ynorov@nvidia.com> Cc: Chris Mason <clm@fb.com> Cc: Pedro Falcato <pfalcato@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 1c62800 commit 21c8a5b

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

tools/include/linux/bitmap.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void __bitmap_set(unsigned long *map, unsigned int start, int len);
2424
void __bitmap_clear(unsigned long *map, unsigned int start, int len);
2525
bool __bitmap_intersects(const unsigned long *bitmap1,
2626
const unsigned long *bitmap2, unsigned int bits);
27+
bool __bitmap_subset(const unsigned long *bitmap1,
28+
const unsigned long *bitmap2, unsigned int nbits);
29+
bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
30+
const unsigned long *bitmap2, unsigned int nbits);
2731

2832
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
2933
#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
@@ -81,6 +85,15 @@ static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
8185
__bitmap_or(dst, src1, src2, nbits);
8286
}
8387

88+
static __always_inline
89+
bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
90+
const unsigned long *src2, unsigned int nbits)
91+
{
92+
if (small_const_nbits(nbits))
93+
return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
94+
return __bitmap_andnot(dst, src1, src2, nbits);
95+
}
96+
8497
static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags __maybe_unused)
8598
{
8699
return malloc(bitmap_size(nbits));
@@ -157,6 +170,15 @@ static inline bool bitmap_intersects(const unsigned long *src1,
157170
return __bitmap_intersects(src1, src2, nbits);
158171
}
159172

173+
static __always_inline
174+
bool bitmap_subset(const unsigned long *src1, const unsigned long *src2, unsigned int nbits)
175+
{
176+
if (small_const_nbits(nbits))
177+
return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
178+
else
179+
return __bitmap_subset(src1, src2, nbits);
180+
}
181+
160182
static inline void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits)
161183
{
162184
if (__builtin_constant_p(nbits) && nbits == 1)

tools/lib/bitmap.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,32 @@ void __bitmap_clear(unsigned long *map, unsigned int start, int len)
140140
*p &= ~mask_to_clear;
141141
}
142142
}
143+
144+
bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
145+
const unsigned long *bitmap2, unsigned int bits)
146+
{
147+
unsigned int k;
148+
unsigned int lim = bits/BITS_PER_LONG;
149+
unsigned long result = 0;
150+
151+
for (k = 0; k < lim; k++)
152+
result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
153+
if (bits % BITS_PER_LONG)
154+
result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
155+
BITMAP_LAST_WORD_MASK(bits));
156+
return result != 0;
157+
}
158+
159+
bool __bitmap_subset(const unsigned long *bitmap1,
160+
const unsigned long *bitmap2, unsigned int bits)
161+
{
162+
unsigned int k, lim = bits/BITS_PER_LONG;
163+
for (k = 0; k < lim; ++k)
164+
if (bitmap1[k] & ~bitmap2[k])
165+
return false;
166+
167+
if (bits % BITS_PER_LONG)
168+
if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
169+
return false;
170+
return true;
171+
}

0 commit comments

Comments
 (0)