]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
tools bitmap: Implement bitmap_equal() operation at bitmap API
authorAlexey Budankov <alexey.budankov@linux.intel.com>
Tue, 3 Dec 2019 11:43:33 +0000 (14:43 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 6 Jan 2020 14:46:04 +0000 (11:46 -0300)
Extend tools bitmap API with bitmap_equal() implementation.

The implementation has been derived from the kernel.

Extend tools bitmap API with bitmap_free() implementation for symmetry
with bitmap_alloc() function.

Signed-off-by: Alexey Budankov <alexey.budankov@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/43757993-0b28-d8af-a6c7-ede12e3a6877@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/include/linux/bitmap.h
tools/lib/bitmap.c

index 05dca5c203f39ae4a2e0c5217169de0a5a1dc7cf..477a1cae513f2f98ddf49d4bd5062e18603eed46 100644 (file)
@@ -15,6 +15,8 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
                 const unsigned long *bitmap2, int bits);
 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
                 const unsigned long *bitmap2, unsigned int bits);
+int __bitmap_equal(const unsigned long *bitmap1,
+                  const unsigned long *bitmap2, unsigned int bits);
 void bitmap_clear(unsigned long *map, unsigned int start, int len);
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
@@ -123,6 +125,15 @@ static inline unsigned long *bitmap_alloc(int nbits)
        return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
 }
 
+/*
+ * bitmap_free - Free bitmap
+ * @bitmap: pointer to bitmap
+ */
+static inline void bitmap_free(unsigned long *bitmap)
+{
+       free(bitmap);
+}
+
 /*
  * bitmap_scnprintf - print bitmap list into buffer
  * @bitmap: bitmap
@@ -148,4 +159,23 @@ static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
        return __bitmap_and(dst, src1, src2, nbits);
 }
 
+#ifdef __LITTLE_ENDIAN
+#define BITMAP_MEM_ALIGNMENT 8
+#else
+#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
+#endif
+#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
+#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
+
+static inline int bitmap_equal(const unsigned long *src1,
+                       const unsigned long *src2, unsigned int nbits)
+{
+       if (small_const_nbits(nbits))
+               return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
+       if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
+           IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
+               return !memcmp(src1, src2, nbits / 8);
+       return __bitmap_equal(src1, src2, nbits);
+}
+
 #endif /* _PERF_BITOPS_H */
index 38494782be06e386d3df83d4ed262faf0c9c958f..5043747ef6c5f9f2d662f26add8736ddc275b461 100644 (file)
@@ -71,3 +71,18 @@ int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
                           BITMAP_LAST_WORD_MASK(bits));
        return result != 0;
 }
+
+int __bitmap_equal(const unsigned long *bitmap1,
+               const unsigned long *bitmap2, unsigned int bits)
+{
+       unsigned int k, lim = bits/BITS_PER_LONG;
+       for (k = 0; k < lim; ++k)
+               if (bitmap1[k] != bitmap2[k])
+                       return 0;
+
+       if (bits % BITS_PER_LONG)
+               if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
+                       return 0;
+
+       return 1;
+}