[Intel-gfx] [PATCH i-g-t v3] lib: Define a common bit operations library
Michel Thierry
michel.thierry at intel.com
Thu Mar 2 19:49:34 UTC 2017
Bring the test/set/clear bit functions we have into a single place.
v2: Add gtk-doc comment blocks (Daniel)
v3: Restore inline keyword.
Cc: Daniel Vetter <daniel.vetter at ffwll.ch>
Reviewed-by: Joonas Lahtinen <joonas.lahtinen at linux.intel.com> (v1)
Signed-off-by: Michel Thierry <michel.thierry at intel.com>
---
.../intel-gpu-tools/intel-gpu-tools-docs.xml | 1 +
lib/Makefile.sources | 2 +
lib/igt.h | 1 +
lib/igt_bitops.c | 111 +++++++++++++++++++++
lib/igt_bitops.h | 46 +++++++++
lib/igt_primes.c | 25 +----
6 files changed, 162 insertions(+), 24 deletions(-)
create mode 100644 lib/igt_bitops.c
create mode 100644 lib/igt_bitops.h
diff --git a/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml b/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml
index 990bbb80..0fc51508 100644
--- a/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml
+++ b/docs/reference/intel-gpu-tools/intel-gpu-tools-docs.xml
@@ -25,6 +25,7 @@
<xi:include href="xml/igt_kmod.xml"/>
<xi:include href="xml/igt_fb.xml"/>
<xi:include href="xml/igt_aux.xml"/>
+ <xi:include href="xml/igt_bitops.xml"/>
<xi:include href="xml/igt_gt.xml"/>
<xi:include href="xml/igt_pm.xml"/>
<xi:include href="xml/ioctl_wrappers.xml"/>
diff --git a/lib/Makefile.sources b/lib/Makefile.sources
index 6348487f..801312c6 100644
--- a/lib/Makefile.sources
+++ b/lib/Makefile.sources
@@ -11,6 +11,8 @@ lib_source_list = \
igt_debugfs.h \
igt_aux.c \
igt_aux.h \
+ igt_bitops.c \
+ igt_bitops.h \
igt_edid_template.h \
igt_gt.c \
igt_gt.h \
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.c b/lib/igt_bitops.c
new file mode 100644
index 00000000..feacaf00
--- /dev/null
+++ b/lib/igt_bitops.c
@@ -0,0 +1,111 @@
+/*
+ * 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.
+ */
+
+#include <igt_bitops.h>
+
+/**
+ * SECTION:igt_bitops
+ * @short_description: Useful bit operations, based on kernel functions.
+ * @title: bitops
+ * @include: igt.h
+ *
+ * This library provides functions to set/clear/test bits in a given memory
+ * location.
+ */
+
+/**
+ * set_bit:
+ * @nr: The bit to set
+ * @addr: The address to start counting from
+ *
+ * Set a bit in memory.
+ *
+ */
+inline void set_bit(unsigned long nr, unsigned long *addr)
+{
+ addr[nr / BITS_PER_LONG] |= __bit__(nr);
+}
+
+/**
+ * clear_bit:
+ * @nr: The bit to clear
+ * @addr: The address to start counting from
+ *
+ * Clear a bit in memory.
+ *
+ */
+inline void clear_bit(unsigned long nr, unsigned long *addr)
+{
+ addr[nr / BITS_PER_LONG] &= ~__bit__(nr);
+}
+
+/**
+ * test_bit:
+ * @nr: The bit to test
+ * @addr: The address to start counting from
+ *
+ * Determine whether a bit is set.
+ *
+ * Returns:
+ * True if bit was set, false otherwise.
+ */
+inline bool test_bit(unsigned long nr, const unsigned long *addr)
+{
+ return addr[nr / BITS_PER_LONG] & __bit__(nr);
+}
+
+/**
+ * test_and_set_bit:
+ * @nr: The bit to set
+ * @addr: The address to start counting from
+ *
+ * Set a bit and return its old value.
+ *
+ * Returns:
+ * The bit previous value.
+ */
+inline bool test_and_set_bit(unsigned long nr, unsigned long *addr)
+{
+ bool ret = test_bit(nr, addr);
+ set_bit(nr, addr);
+ return ret;
+}
+
+/**
+ * test_and_clear_bit:
+ * @nr: The bit to clear
+ * @addr: The address to start counting from
+ *
+ * Clear a bit and return its old value.
+ *
+ * Returns:
+ * The bit previous value.
+ */
+inline bool test_and_clear_bit(unsigned long nr, unsigned long *addr)
+{
+ bool ret = test_bit(nr, addr);
+ clear_bit(nr, addr);
+ return ret;
+}
diff --git a/lib/igt_bitops.h b/lib/igt_bitops.h
new file mode 100644
index 00000000..236f20f0
--- /dev/null
+++ b/lib/igt_bitops.h
@@ -0,0 +1,46 @@
+/*
+ * 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);
+}
+
+void set_bit(unsigned long nr, unsigned long *addr);
+void clear_bit(unsigned long nr, unsigned long *addr);
+bool test_bit(unsigned long nr, const unsigned long *addr);
+bool test_and_set_bit(unsigned long nr, unsigned long *addr);
+bool test_and_clear_bit(unsigned long nr, unsigned long *addr);
+
+#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,
--
2.11.0
More information about the Intel-gfx
mailing list