Module Name:    src
Committed By:   riastradh
Date:           Mon Aug 27 07:13:45 UTC 2018

Modified Files:
        src/sys/external/bsd/drm2/include/linux: bitmap.h

Log Message:
Implement Linux bitmap utilities.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/external/bsd/drm2/include/linux/bitmap.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/external/bsd/drm2/include/linux/bitmap.h
diff -u src/sys/external/bsd/drm2/include/linux/bitmap.h:1.2 src/sys/external/bsd/drm2/include/linux/bitmap.h:1.3
--- src/sys/external/bsd/drm2/include/linux/bitmap.h:1.2	Tue Mar 18 18:20:43 2014
+++ src/sys/external/bsd/drm2/include/linux/bitmap.h	Mon Aug 27 07:13:45 2018
@@ -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_ */

Reply via email to