diff mbox

[kvm-unit-tests,v2,2/6] lib: generic bitops.h

Message ID 1456871694-23042-3-git-send-email-pfeiner@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

Peter Feiner March 1, 2016, 10:34 p.m. UTC
Factored out common bitops stuff, just like Linux's
include/linux/bitops.h.

Signed-off-by: Peter Feiner <pfeiner@google.com>
---
 lib/arm/asm/bitops.h   |  8 ++++----
 lib/arm/asm/cpumask.h  |  2 +-
 lib/arm/bitops.c       |  2 +-
 lib/arm64/asm/bitops.h |  8 ++++----
 lib/bitops.h           | 35 +++++++++++++++++++++++++++++++++++
 lib/ppc64/asm/bitops.h | 10 ++++++++++
 lib/x86/asm/bitops.h   | 14 ++++++++++++++
 7 files changed, 69 insertions(+), 10 deletions(-)
 create mode 100644 lib/bitops.h
 create mode 100644 lib/ppc64/asm/bitops.h
 create mode 100644 lib/x86/asm/bitops.h
diff mbox

Patch

diff --git a/lib/arm/asm/bitops.h b/lib/arm/asm/bitops.h
index 8049634..d46cc5d 100644
--- a/lib/arm/asm/bitops.h
+++ b/lib/arm/asm/bitops.h
@@ -2,7 +2,6 @@ 
 #define _ASMARM_BITOPS_H_
 /*
  * Adapated from
- *   include/linux/bitops.h
  *   arch/arm/lib/bitops.h
  *
  * Copyright (C) 2015, Red Hat Inc, Andrew Jones <drjones@redhat.com>
@@ -10,10 +9,11 @@ 
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 
+#ifndef _BITOPS_H_
+#error only <bitops.h> can be included directly
+#endif
+
 #define BITS_PER_LONG	32
-#define BIT(nr)		(1UL << (nr))
-#define BIT_MASK(nr)	(1UL << ((nr) % BITS_PER_LONG))
-#define BIT_WORD(nr)	((nr) / BITS_PER_LONG)
 
 #define ATOMIC_BITOP(insn, mask, word)				\
 ({								\
diff --git a/lib/arm/asm/cpumask.h b/lib/arm/asm/cpumask.h
index 85b8e4b..6683bb6 100644
--- a/lib/arm/asm/cpumask.h
+++ b/lib/arm/asm/cpumask.h
@@ -8,7 +8,7 @@ 
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include <asm/setup.h>
-#include <asm/bitops.h>
+#include <bitops.h>
 
 #define CPUMASK_NR_LONGS ((NR_CPUS + BITS_PER_LONG - 1) / BITS_PER_LONG)
 
diff --git a/lib/arm/bitops.c b/lib/arm/bitops.c
index 9ad1121..1f1db93 100644
--- a/lib/arm/bitops.c
+++ b/lib/arm/bitops.c
@@ -6,7 +6,7 @@ 
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
-#include <asm/bitops.h>
+#include <bitops.h>
 #include <asm/barrier.h>
 #include <asm/mmu.h>
 
diff --git a/lib/arm64/asm/bitops.h b/lib/arm64/asm/bitops.h
index 3371c60..618468c 100644
--- a/lib/arm64/asm/bitops.h
+++ b/lib/arm64/asm/bitops.h
@@ -2,7 +2,6 @@ 
 #define _ASMARM64_BITOPS_H_
 /*
  * Adapated from
- *   include/linux/bitops.h
  *   arch/arm64/lib/bitops.S
  *
  * Copyright (C) 2015, Red Hat Inc, Andrew Jones <drjones@redhat.com>
@@ -10,10 +9,11 @@ 
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 
+#ifndef _BITOPS_H_
+#error only <bitops.h> can be included directly
+#endif
+
 #define BITS_PER_LONG	64
-#define BIT(nr)		(1UL << (nr))
-#define BIT_MASK(nr)	(1UL << ((nr) % BITS_PER_LONG))
-#define BIT_WORD(nr)	((nr) / BITS_PER_LONG)
 
 #define ATOMIC_BITOP(insn, mask, word)				\
 ({								\
diff --git a/lib/bitops.h b/lib/bitops.h
new file mode 100644
index 0000000..c550eba
--- /dev/null
+++ b/lib/bitops.h
@@ -0,0 +1,35 @@ 
+#ifndef _BITOPS_H_
+#define _BITOPS_H_
+
+/*
+ * Adapated from
+ *   include/linux/bitops.h
+ *
+ * Copyright (C) 2015, Red Hat Inc, Andrew Jones <drjones@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#define BIT(nr)			(1UL << (nr))
+#define BIT_ULL(nr)		(1ULL << (nr))
+#define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
+#define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
+#define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
+#define BITS_PER_BYTE		8
+#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
+
+#include <asm/bitops.h>
+
+/*
+ * Create a contiguous bitmask starting at bit position @l and ending at
+ * position @h. For example
+ * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
+ */
+#define GENMASK(h, l) \
+	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
+
+#define GENMASK_ULL(h, l) \
+	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
+
+#endif
diff --git a/lib/ppc64/asm/bitops.h b/lib/ppc64/asm/bitops.h
new file mode 100644
index 0000000..34624b4
--- /dev/null
+++ b/lib/ppc64/asm/bitops.h
@@ -0,0 +1,10 @@ 
+#ifndef _ASMPPC64_BITOPS_H_
+#define _ASMPPC64_BITOPS_H_
+
+#ifndef _BITOPS_H_
+#error only <bitops.h> can be included directly
+#endif
+
+#define BITS_PER_LONG	64
+
+#endif
diff --git a/lib/x86/asm/bitops.h b/lib/x86/asm/bitops.h
new file mode 100644
index 0000000..eb4aaa9
--- /dev/null
+++ b/lib/x86/asm/bitops.h
@@ -0,0 +1,14 @@ 
+#ifndef _ASMX86_BITOPS_H_
+#define _ASMX86_BITOPS_H_
+
+#ifndef _BITOPS_H_
+#error only <bitops.h> can be included directly
+#endif
+
+#ifdef __x86_64__
+#define BITS_PER_LONG	64
+#else
+#define BITS_PER_LONG	32
+#endif
+
+#endif