From 9ee28672473d65d5f0054ded91ab1137aa3722f9 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Mon, 4 Feb 2019 17:00:38 +0300 Subject: [PATCH] Improve count leading/trailing zeros implementation Use x86 intrinsics if compiled with appropriate instruction support --- Utilities/asm.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Utilities/asm.h b/Utilities/asm.h index f1c81e37bb..61999060ba 100644 --- a/Utilities/asm.h +++ b/Utilities/asm.h @@ -9,6 +9,8 @@ namespace utils #ifdef _MSC_VER ulong res; return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32; +#elif __LZCNT__ + return _lzcnt_u32(arg); #else return arg || nonzero ? __builtin_clz(arg) : 32; #endif @@ -19,6 +21,8 @@ namespace utils #ifdef _MSC_VER ulong res; return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64; +#elif __LZCNT__ + return _lzcnt_u64(arg); #else return arg || nonzero ? __builtin_clzll(arg) : 64; #endif @@ -29,6 +33,8 @@ namespace utils #ifdef _MSC_VER ulong res; return _BitScanForward(&res, arg) || nonzero ? res : 32; +#elif __BMI__ + return _tzcnt_u32(arg); #else return arg || nonzero ? __builtin_ctz(arg) : 32; #endif @@ -39,6 +45,8 @@ namespace utils #ifdef _MSC_VER ulong res; return _BitScanForward64(&res, arg) || nonzero ? res : 64; +#elif __BMI__ + return _tzcnt_u64(arg); #else return arg || nonzero ? __builtin_ctzll(arg) : 64; #endif