Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/external/bsd/drm2/include/linux Implement Linux bitmap u...



details:   https://anonhg.NetBSD.org/src/rev/041b39fc5212
branches:  trunk
changeset: 992604:041b39fc5212
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Aug 27 07:13:45 2018 +0000

description:
Implement Linux bitmap utilities.

diffstat:

 sys/external/bsd/drm2/include/linux/bitmap.h |  142 ++++++++++++++++++++++++++-
 1 files changed, 140 insertions(+), 2 deletions(-)

diffs (156 lines):

diff -r 0d55d49d09f6 -r 041b39fc5212 sys/external/bsd/drm2/include/linux/bitmap.h
--- a/sys/external/bsd/drm2/include/linux/bitmap.h      Mon Aug 27 07:10:39 2018 +0000
+++ b/sys/external/bsd/drm2/include/linux/bitmap.h      Mon Aug 27 07:13:45 2018 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: bitmap.h,v 1.2 2014/03/18 18:20:43 riastradh Exp $     */
+/*     $NetBSD: bitmap.h,v 1.3 2018/08/27 07:13:45 riastradh Exp $     */
 
 /*-
- * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -32,4 +32,142 @@
 #ifndef _LINUX_BITMAP_H_
 #define _LINUX_BITMAP_H_
 
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/systm.h>
+
+/*
+ * bitmap_zero(bitmap, nbits)
+ *
+ *     Zero a bitmap that was allocated to have nbits bits.  Yes, this
+ *     zeros bits past nbits.
+ */
+static inline void
+bitmap_zero(unsigned long *bitmap, size_t nbits)
+{
+       const size_t bpl = NBBY * sizeof(*bitmap);
+
+       memset(bitmap, 0, howmany(nbits, bpl) * sizeof(*bitmap));
+}
+
+/*
+ * bitmap_empty(bitmap, nbits)
+ *
+ *     Return true if all bits at 0, 1, 2, ..., nbits-2, nbits-1 are
+ *     0, or false if any of them is 1.
+ */
+static inline bool
+bitmap_empty(const unsigned long *bitmap, size_t nbits)
+{
+       const size_t bpl = NBBY * sizeof(*bitmap);
+
+       for (; bpl <= nbits; nbits -= bpl) {
+               if (*bitmap++)
+                       return false;
+       }
+
+       if (nbits) {
+               if (*bitmap & ~(~0UL << nbits))
+                       return false;
+       }
+
+       return true;
+}
+
+/*
+ * bitmap_weight(bitmap, nbits)
+ *
+ *     Compute the number of 1 bits at 0, 1, 2, ..., nbits-2, nbits-1.
+ */
+static inline int
+bitmap_weight(const unsigned long *bitmap, size_t nbits)
+{
+       const size_t bpl = NBBY * sizeof(*bitmap);
+       int weight = 0;
+
+       for (; bpl <= nbits; nbits -= bpl)
+               weight += popcountl(*bitmap++);
+       if (nbits)
+               weight += popcountl(*bitmap & ~(~0UL << nbits));
+
+       return weight;
+}
+
+/*
+ * bitmap_set(bitmap, startbit, nbits)
+ *
+ *     Set bits at startbit, startbit+1, ..., nbits-2, nbits-1 to 1.
+ */
+static inline void
+bitmap_set(unsigned long *bitmap, size_t startbit, size_t nbits)
+{
+       const size_t bpl = NBBY * sizeof(*bitmap);
+       unsigned long *p;
+       unsigned long mask;
+
+       for (p = bitmap + startbit/bpl, mask = ~(~0UL << (startbit%bpl));
+            nbits >= bpl;
+            p++, nbits -= bpl, mask = ~0UL)
+               *p |= mask;
+
+       if (nbits)
+               *p |= mask & (~0UL << nbits);
+}
+
+/*
+ * bitmap_set(bitmap, startbit, nbits)
+ *
+ *     Clear bits at startbit, startbit+1, ..., nbits-2, nbits-1,
+ *     replacing them by 0.
+ */
+static inline void
+bitmap_clear(unsigned long *bitmap, size_t startbit, size_t nbits)
+{
+       const size_t bpl = NBBY * sizeof(*bitmap);
+       unsigned long *p;
+       unsigned long mask;
+
+       for (p = bitmap + startbit/bpl, mask = ~0UL << (startbit%bpl);
+            nbits >= bpl;
+            p++, nbits -= bpl, mask = 0UL)
+               *p &= mask;
+
+       if (nbits)
+               *p &= mask | ~(~0UL << nbits);
+}
+
+/*
+ * bitmap_and(dst, src1, src2, nbits)
+ *
+ *     Set dst to be the bitwise AND of src1 and src2, all bitmaps
+ *     allocated to have nbits bits.  Yes, this modifies bits past
+ *     nbits.
+ */
+static inline void
+bitmap_and(unsigned long *dst, const unsigned long *src1,
+    const unsigned long *src2, size_t nbits)
+{
+       size_t n = howmany(nbits, NBBY * sizeof(unsigned long));
+
+       while (n --> 0)
+               *dst++ = *src1++ & *src2++;
+}
+
+/*
+ * bitmap_and(dst, src1, src2, nbits)
+ *
+ *     Set dst to be the bitwise inclusive-OR of src1 and src2, all
+ *     bitmaps allocated to have nbits bits.  Yes, this modifies bits
+ *     past nbits.
+ */
+static inline void
+bitmap_or(unsigned long *dst, const unsigned long *src1,
+    const unsigned long *src2, size_t nbits)
+{
+       size_t n = howmany(nbits, NBBY * sizeof(unsigned long));
+
+       while (n --> 0)
+               *dst++ = *src1++ | *src2++;
+}
+
 #endif  /* _LINUX_BITMAP_H_ */



Home | Main Index | Thread Index | Old Index