From patchwork Wed Sep 26 15:56:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [v2,34/45] host-utils: add ffsl Date: Wed, 26 Sep 2012 05:56:40 -0000 From: Paolo Bonzini X-Patchwork-Id: 187124 Message-Id: <1348675011-8794-35-git-send-email-pbonzini@redhat.com> To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, jcody@redhat.com We can provide fast versions based on the other functions defined by host-utils.h. Some care is required on glibc, which provides ffsl already. Signed-off-by: Paolo Bonzini --- host-utils.h | 26 ++++++++++++++++++++++++++ 1 file modificato, 26 inserzioni(+) diff --git a/host-utils.h b/host-utils.h index 821db93..2724be0 100644 --- a/host-utils.h +++ b/host-utils.h @@ -24,6 +24,7 @@ */ #include "compiler.h" /* QEMU_GNUC_PREREQ */ +#include /* ffsl */ #if defined(__x86_64__) #define __HAVE_FAST_MULU64__ @@ -234,3 +235,28 @@ static inline int ctpop64(uint64_t val) return val; #endif } + +/* glibc does not provide an inline version of ffsl, so always define + * ours. We need to give it a different name, however. + */ +#ifdef __GLIBC__ +#define ffsl qemu_ffsl +#endif +static inline int ffsl(long val) +{ + if (!val) { + return 0; + } + +#if QEMU_GNUC_PREREQ(3, 4) + return __builtin_ctzl(val) + 1; +#else + if (sizeof(long) == 4) { + return ctz32(val) + 1; + } else if (sizeof(long) == 8) { + return ctz64(val) + 1; + } else { + abort(); + } +#endif +}