From 2e3050c98994a4f4792839a3a1e1f294b24ba363 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sun, 15 Apr 2018 17:45:47 -0400 Subject: Use GCC builtins for clz operation --- src/lib/utils/bit_ops.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/lib/utils') diff --git a/src/lib/utils/bit_ops.h b/src/lib/utils/bit_ops.h index aa41db391..c7e401492 100644 --- a/src/lib/utils/bit_ops.h +++ b/src/lib/utils/bit_ops.h @@ -107,11 +107,36 @@ inline size_t ctz(T n) template<> inline size_t ctz(uint32_t n) { + if(n == 0) + return 32; return __builtin_ctz(n); } -#endif +template<> +inline size_t ctz(uint64_t n) + { + if(n == 0) + return 64; + return __builtin_ctzll(n); + } +template<> +inline size_t high_bit(uint32_t x) + { + if(x == 0) + return 0; + return (32 - __builtin_clz(x)); + } + +template<> +inline size_t high_bit(uint64_t x) + { + if(x == 0) + return 0; + return (64 - __builtin_clzll(x)); + } + +#endif template size_t ceil_log2(T x) -- cgit v1.2.3