From 522957f9f302b9521507a365da5871849a03594d Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 15 Mar 2019 22:50:57 -0400 Subject: Implement a MultiLevelQueue --- src/common/bit_util.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/common/bit_util.h') diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 1eea17ba1..14e53c273 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -58,4 +58,23 @@ inline u64 CountLeadingZeroes64(u64 value) { return __builtin_clzll(value); } #endif + +inline u32 CountTrailingZeroes32(u32 value) { + u32 count = 0; + while (((value >> count) & 0xf) == 0 && count < 32) + count += 4; + while (((value >> count) & 1) == 0 && count < 32) + count++; + return count; +} + +inline u64 CountTrailingZeroes64(u64 value) { + u64 count = 0; + while (((value >> count) & 0xf) == 0 && count < 64) + count += 4; + while (((value >> count) & 1) == 0 && count < 64) + count++; + return count; +} + } // namespace Common -- cgit v1.2.3 From 3bc815a5dc18a646334ba933c74ce7ce44099625 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 15 Mar 2019 23:18:11 -0400 Subject: Implement intrinsics CountTrailingZeroes and test it. --- src/common/bit_util.h | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/common/bit_util.h') diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 14e53c273..70e728a5e 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -59,22 +59,43 @@ inline u64 CountLeadingZeroes64(u64 value) { } #endif + +#ifdef _MSC_VER inline u32 CountTrailingZeroes32(u32 value) { - u32 count = 0; - while (((value >> count) & 0xf) == 0 && count < 32) - count += 4; - while (((value >> count) & 1) == 0 && count < 32) - count++; - return count; + unsigned long trailing_zero = 0; + + if (_BitScanForward(&trailing_zero, value) != 0) { + return trailing_zero; + } + + return 32; } inline u64 CountTrailingZeroes64(u64 value) { - u64 count = 0; - while (((value >> count) & 0xf) == 0 && count < 64) - count += 4; - while (((value >> count) & 1) == 0 && count < 64) - count++; - return count; + unsigned long trailing_zero = 0; + + if (_BitScanForward64(&trailing_zero, value) != 0) { + return trailing_zero; + } + + return 64; } +#else +inline u32 CountTrailingZeroes32(u32 value) { + if (value == 0) { + return 32; + } + + return __builtin_ctz(value); +} + +inline u64 CountTrailingZeroes64(u64 value) { + if (value == 0) { + return 64; + } + + return __builtin_ctzll(value); +} +#endif } // namespace Common -- cgit v1.2.3 From db42bcb306323d6221e7f893d39558c3db579bf3 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Tue, 19 Mar 2019 22:20:15 -0400 Subject: Fixes and corrections on formatting. --- src/common/bit_util.h | 1 - 1 file changed, 1 deletion(-) (limited to 'src/common/bit_util.h') diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 70e728a5e..a4f9ed4aa 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -59,7 +59,6 @@ inline u64 CountLeadingZeroes64(u64 value) { } #endif - #ifdef _MSC_VER inline u32 CountTrailingZeroes32(u32 value) { unsigned long trailing_zero = 0; -- cgit v1.2.3