summaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-06-04 14:01:02 -0700
committerMichael Forney <mforney@mforney.org>2019-06-10 11:15:55 -0700
commit9d58bbd4ffdce319128215443f7b8859f35da6b7 (patch)
treea7dd731e5d826786e12f2051fb669cd02cc96a94 /src/utils.h
parentdb7e79e78df07723dd07c8d00283244092dd8816 (diff)
downloadxorg-lib-libxkbcommon-9d58bbd4ffdce319128215443f7b8859f35da6b7.tar.gz
Use bitwise test instead of popcount to check if one bit is set
We don't need to determine the total number of bits set to determine if exactly one is set. Additionally, on x86_64 without any -march=* flag, __builtin_popcount will get compiled to a function call to the compiler runtime (on gcc), or a long sequence of bit operations (on clang). Signed-off-by: Michael Forney <mforney@mforney.org>
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h12
1 files changed, 2 insertions, 10 deletions
diff --git a/src/utils.h b/src/utils.h
index cb98e8e..d823e74 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -186,18 +186,10 @@ msb_pos(uint32_t mask)
return pos;
}
-// Avoid conflict with other popcount()s.
static inline int
-my_popcount(uint32_t x)
+one_bit_set(uint32_t x)
{
- int count;
-#if defined(HAVE___BUILTIN_POPCOUNT)
- count = __builtin_popcount(x);
-#else
- for (count = 0; x; count++)
- x &= x - 1;
-#endif
- return count;
+ return x && (x & (x - 1)) == 0;
}
bool