From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, rkrcmar@redhat.com, cdall@linaro.org,
david@redhat.com, lvivier@redhat.com, thuth@redhat.com
Subject: [PATCH kvm-unit-tests v2 08/12] bitops: add fls
Date: Wed, 17 Jan 2018 11:40:01 +0100 [thread overview]
Message-ID: <20180117104005.29211-9-drjones@redhat.com> (raw)
In-Reply-To: <20180117104005.29211-1-drjones@redhat.com>
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
lib/arm/asm/bitops.h | 2 ++
lib/arm64/asm/bitops.h | 2 ++
lib/bitops.h | 38 ++++++++++++++++++++++++++++++++++++++
lib/ppc64/asm/bitops.h | 2 ++
lib/s390x/asm/bitops.h | 10 ++++++++++
lib/x86/asm/bitops.h | 2 ++
6 files changed, 56 insertions(+)
create mode 100644 lib/s390x/asm/bitops.h
diff --git a/lib/arm/asm/bitops.h b/lib/arm/asm/bitops.h
index 1d9148d2ba15..79cf4ec65a29 100644
--- a/lib/arm/asm/bitops.h
+++ b/lib/arm/asm/bitops.h
@@ -15,6 +15,8 @@
#define BITS_PER_LONG 32
+#define HAVE_BUILTIN_FLS 1
+
#define ATOMIC_BITOP(insn, mask, word) \
({ \
unsigned long tmp1, tmp2; \
diff --git a/lib/arm64/asm/bitops.h b/lib/arm64/asm/bitops.h
index 1b076b6a8f03..91b4bd94372f 100644
--- a/lib/arm64/asm/bitops.h
+++ b/lib/arm64/asm/bitops.h
@@ -15,6 +15,8 @@
#define BITS_PER_LONG 64
+#define HAVE_BUILTIN_FLS 1
+
#define ATOMIC_BITOP(insn, mask, word) \
({ \
unsigned long tmp1, tmp2; \
diff --git a/lib/bitops.h b/lib/bitops.h
index 185c5d361fea..636064c0fa85 100644
--- a/lib/bitops.h
+++ b/lib/bitops.h
@@ -33,4 +33,42 @@
#define GENMASK_ULL(h, l) \
(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
+#ifndef HAVE_BUILTIN_FLS
+static inline unsigned long fls(unsigned long word)
+{
+ int num = BITS_PER_LONG - 1;
+
+#if BITS_PER_LONG == 64
+ if (!(word & (~0ul << 32))) {
+ num -= 32;
+ word <<= 32;
+ }
+#endif
+ if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
+ num -= 16;
+ word <<= 16;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
+ num -= 8;
+ word <<= 8;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
+ num -= 4;
+ word <<= 4;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
+ num -= 2;
+ word <<= 2;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-1))))
+ num -= 1;
+ return num;
+}
+#else
+static inline unsigned long fls(unsigned long word)
+{
+ return BITS_PER_LONG - __builtin_clzl(word) - 1;
+}
+#endif
+
#endif
diff --git a/lib/ppc64/asm/bitops.h b/lib/ppc64/asm/bitops.h
index 34624b4bfca3..c93d64bb9005 100644
--- a/lib/ppc64/asm/bitops.h
+++ b/lib/ppc64/asm/bitops.h
@@ -7,4 +7,6 @@
#define BITS_PER_LONG 64
+#define HAVE_BUILTIN_FLS 1
+
#endif
diff --git a/lib/s390x/asm/bitops.h b/lib/s390x/asm/bitops.h
new file mode 100644
index 000000000000..e7cdda920d68
--- /dev/null
+++ b/lib/s390x/asm/bitops.h
@@ -0,0 +1,10 @@
+#ifndef _ASMS390X_BITOPS_H_
+#define _ASMS390X_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
index eb4aaa9fb29a..13a25ec9853d 100644
--- a/lib/x86/asm/bitops.h
+++ b/lib/x86/asm/bitops.h
@@ -11,4 +11,6 @@
#define BITS_PER_LONG 32
#endif
+#define HAVE_BUILTIN_FLS 1
+
#endif
--
2.13.6
next prev parent reply other threads:[~2018-01-17 10:40 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-17 10:39 [PATCH kvm-unit-tests v2 00/12] arm/arm64: mmu fixes and feature Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 01/12] arm/arm64: cleanup alloc.h includes Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 02/12] arm/arm64: add pgtable to thread_info Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 03/12] arm/arm64: fix virt_to_phys Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 04/12] arm/arm64: flush page table cache when installing entries Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 05/12] arm/arm64: setup: don't allow gaps in phys range Andrew Jones
2018-01-17 10:39 ` [PATCH kvm-unit-tests v2 06/12] phys_alloc: ensure we account all allocations Andrew Jones
2018-01-17 12:15 ` David Hildenbrand
2018-01-17 10:40 ` [PATCH kvm-unit-tests v2 07/12] page_alloc: allow initialization before setup_vm call Andrew Jones
2018-01-17 10:40 ` Andrew Jones [this message]
2018-01-17 10:40 ` [PATCH kvm-unit-tests v2 09/12] page_alloc: add yet another memalign Andrew Jones
2018-01-17 10:40 ` [PATCH kvm-unit-tests v2 10/12] lib/auxinfo: add flags field Andrew Jones
2018-01-17 10:40 ` [PATCH kvm-unit-tests v2 11/12] arm/arm64: allow setup_vm to be skipped Andrew Jones
2018-01-17 10:40 ` [PATCH kvm-unit-tests v2 12/12] arm/arm64: sieve should start with the mmu off Andrew Jones
2018-01-17 11:17 ` [PATCH kvm-unit-tests v2 00/12] arm/arm64: mmu fixes and feature David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180117104005.29211-9-drjones@redhat.com \
--to=drjones@redhat.com \
--cc=cdall@linaro.org \
--cc=david@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=rkrcmar@redhat.com \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox