diff mbox

[i-g-t,3/8] lib: Define a common bit operations library

Message ID 20170209021828.22813-4-michel.thierry@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michel Thierry Feb. 9, 2017, 2:18 a.m. UTC
Bring the test/set/clear bit functions we have into a single place.

Signed-off-by: Michel Thierry <michel.thierry@intel.com>
---
 lib/igt.h        |  1 +
 lib/igt_bitops.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_primes.c | 25 +-------------------
 3 files changed, 71 insertions(+), 24 deletions(-)
 create mode 100644 lib/igt_bitops.h

Comments

Joonas Lahtinen Feb. 9, 2017, 7:34 a.m. UTC | #1
On ke, 2017-02-08 at 18:18 -0800, Michel Thierry wrote:
> Bring the test/set/clear bit functions we have into a single place.
> 
> Signed-off-by: Michel Thierry <michel.thierry@intel.com>

Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>

Regards, Joonas
Daniel Vetter Feb. 27, 2017, 9:35 a.m. UTC | #2
On Thu, Feb 09, 2017 at 09:34:49AM +0200, Joonas Lahtinen wrote:
> On ke, 2017-02-08 at 18:18 -0800, Michel Thierry wrote:
> > Bring the test/set/clear bit functions we have into a single place.
> > 
> > Signed-off-by: Michel Thierry <michel.thierry@intel.com>
> 
> Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>

Can we please have docs for this new library?

Thanks, Danel
Michel Thierry March 1, 2017, 2:19 p.m. UTC | #3
On 2/27/2017 1:35 AM, Daniel Vetter wrote:
> On Thu, Feb 09, 2017 at 09:34:49AM +0200, Joonas Lahtinen wrote:
>> On ke, 2017-02-08 at 18:18 -0800, Michel Thierry wrote:
>>> Bring the test/set/clear bit functions we have into a single place.
>>>
>>> Signed-off-by: Michel Thierry <michel.thierry@intel.com>
>>
>> Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
>
> Can we please have docs for this new library?
>

Sure; although they won't be static (or just a .h file) anymore.
diff mbox

Patch

diff --git a/lib/igt.h b/lib/igt.h
index 4f54698d..7a8942aa 100644
--- a/lib/igt.h
+++ b/lib/igt.h
@@ -28,6 +28,7 @@ 
 #include "i915_3d.h"
 #include "i915_pciids.h"
 #include "igt_aux.h"
+#include "igt_bitops.h"
 #include "igt_core.h"
 #include "igt_debugfs.h"
 #include "igt_draw.h"
diff --git a/lib/igt_bitops.h b/lib/igt_bitops.h
new file mode 100644
index 00000000..11e0d8b5
--- /dev/null
+++ b/lib/igt_bitops.h
@@ -0,0 +1,69 @@ 
+/*
+ * Copyright 2017 Intel Corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#ifndef _I915_BITOPS_H
+#define _I915_BITOPS_H
+
+#include <stdbool.h>
+
+#define BIT(nr)			(1UL << (nr))
+#define BIT_ULL(nr)		(1ULL << (nr))
+#define BITS_PER_CHAR		8
+#define BITS_PER_LONG		(sizeof(long)*BITS_PER_CHAR)
+
+static inline unsigned long __bit__(unsigned long nr)
+{
+	return 1UL << (nr % BITS_PER_LONG);
+}
+
+static inline void set_bit(unsigned long nr, unsigned long *addr)
+{
+	addr[nr / BITS_PER_LONG] |= __bit__(nr);
+}
+
+static inline void clear_bit(unsigned long nr, unsigned long *addr)
+{
+	addr[nr / BITS_PER_LONG] &= ~__bit__(nr);
+}
+
+static inline bool test_bit(unsigned long nr, const unsigned long *addr)
+{
+	return addr[nr / BITS_PER_LONG] & __bit__(nr);
+}
+
+static inline bool test_and_set_bit(unsigned long nr, unsigned long *addr)
+{
+	bool ret = test_bit(nr, addr);
+	set_bit(nr, addr);
+	return ret;
+}
+
+static inline bool test_and_clear_bit(unsigned long nr, unsigned long *addr)
+{
+	bool ret = test_bit(nr, addr);
+	clear_bit(nr, addr);
+	return ret;
+}
+
+#endif /* _I915_BITOPS_H */
diff --git a/lib/igt_primes.c b/lib/igt_primes.c
index d5232e54..30caafdb 100644
--- a/lib/igt_primes.c
+++ b/lib/igt_primes.c
@@ -21,16 +21,13 @@ 
  * IN THE SOFTWARE.
  */
 
+#include "igt_bitops.h"
 #include "igt_primes.h"
 
 #include <stdlib.h>
-#include <stdbool.h>
 #include <string.h>
 #include <math.h>
 
-#define BITS_PER_CHAR 8
-#define BITS_PER_LONG (sizeof(long)*BITS_PER_CHAR)
-
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
 
@@ -52,26 +49,6 @@ 
 	_max1 > _max2 ? _max1 : _max2;		\
 })
 
-static inline unsigned long __bit__(unsigned long nr)
-{
-	return 1UL << (nr % BITS_PER_LONG);
-}
-
-static inline void set_bit(unsigned long nr, unsigned long *addr)
-{
-	addr[nr / BITS_PER_LONG] |= __bit__(nr);
-}
-
-static inline void clear_bit(unsigned long nr, unsigned long *addr)
-{
-	addr[nr / BITS_PER_LONG] &= ~__bit__(nr);
-}
-
-static inline bool test_bit(unsigned long nr, const unsigned long *addr)
-{
-	return addr[nr / BITS_PER_LONG] & __bit__(nr);
-}
-
 static unsigned long
 __find_next_bit(const unsigned long *addr,
 		unsigned long nbits, unsigned long start,