Module Name:    src
Committed By:   riastradh
Date:           Mon Aug 27 14:50:24 UTC 2018

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

Log Message:
Try setting and clearing the bits we meant, not other bits.

Fix comments to match intent while we're fixing reality too.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 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.3 src/sys/external/bsd/drm2/include/linux/bitmap.h:1.4
--- src/sys/external/bsd/drm2/include/linux/bitmap.h:1.3	Mon Aug 27 07:13:45 2018
+++ src/sys/external/bsd/drm2/include/linux/bitmap.h	Mon Aug 27 14:50:24 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: bitmap.h,v 1.3 2018/08/27 07:13:45 riastradh Exp $	*/
+/*	$NetBSD: bitmap.h,v 1.4 2018/08/27 14:50:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -96,44 +96,47 @@ bitmap_weight(const unsigned long *bitma
 /*
  * bitmap_set(bitmap, startbit, nbits)
  *
- *	Set bits at startbit, startbit+1, ..., nbits-2, nbits-1 to 1.
+ *	Set bits at startbit, startbit+1, ..., startbit+nbits-2,
+ *	startbit+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 *p = bitmap + startbit/bpl;
 	unsigned long mask;
+	unsigned sz;
 
-	for (p = bitmap + startbit/bpl, mask = ~(~0UL << (startbit%bpl));
-	     nbits >= bpl;
-	     p++, nbits -= bpl, mask = ~0UL)
-		*p |= mask;
+	for (sz = bpl - (startbit%bpl), mask = ~0UL << (startbit%bpl);
+	     nbits >= sz;
+	     nbits -= sz, sz = bpl, mask = ~0UL)
+		*p++ |= mask;
 
 	if (nbits)
-		*p |= mask & (~0UL << nbits);
+		*p |= mask & ~(~0UL << (nbits + bpl - sz));
 }
 
 /*
- * bitmap_set(bitmap, startbit, nbits)
+ * bitmap_clear(bitmap, startbit, nbits)
  *
- *	Clear bits at startbit, startbit+1, ..., nbits-2, nbits-1,
- *	replacing them by 0.
+ *	Clear bits at startbit, startbit+1, ..., startbit+nbits-2,
+ *	startbit+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 *p = bitmap + startbit/bpl;
 	unsigned long mask;
+	unsigned sz;
 
-	for (p = bitmap + startbit/bpl, mask = ~0UL << (startbit%bpl);
-	     nbits >= bpl;
-	     p++, nbits -= bpl, mask = 0UL)
-		*p &= mask;
+	for (sz = bpl - (startbit%bpl), mask = ~(~0UL << (startbit%bpl));
+	     nbits >= sz;
+	     nbits -= sz, sz = bpl, mask = 0UL)
+		*p++ &= mask;
 
 	if (nbits)
-		*p &= mask | ~(~0UL << nbits);
+		*p &= mask | (~0UL << (nbits + bpl - sz));
 }
 
 /*

Reply via email to