1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2025-01-31 20:41:45 +01:00

Improve count leading/trailing zeros implementation

Use x86 intrinsics if compiled with appropriate instruction support
This commit is contained in:
Nekotekina 2019-02-04 17:00:38 +03:00
parent 7b344b7654
commit 9ee2867247

View File

@ -9,6 +9,8 @@ namespace utils
#ifdef _MSC_VER #ifdef _MSC_VER
ulong res; ulong res;
return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32; return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32;
#elif __LZCNT__
return _lzcnt_u32(arg);
#else #else
return arg || nonzero ? __builtin_clz(arg) : 32; return arg || nonzero ? __builtin_clz(arg) : 32;
#endif #endif
@ -19,6 +21,8 @@ namespace utils
#ifdef _MSC_VER #ifdef _MSC_VER
ulong res; ulong res;
return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64; return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64;
#elif __LZCNT__
return _lzcnt_u64(arg);
#else #else
return arg || nonzero ? __builtin_clzll(arg) : 64; return arg || nonzero ? __builtin_clzll(arg) : 64;
#endif #endif
@ -29,6 +33,8 @@ namespace utils
#ifdef _MSC_VER #ifdef _MSC_VER
ulong res; ulong res;
return _BitScanForward(&res, arg) || nonzero ? res : 32; return _BitScanForward(&res, arg) || nonzero ? res : 32;
#elif __BMI__
return _tzcnt_u32(arg);
#else #else
return arg || nonzero ? __builtin_ctz(arg) : 32; return arg || nonzero ? __builtin_ctz(arg) : 32;
#endif #endif
@ -39,6 +45,8 @@ namespace utils
#ifdef _MSC_VER #ifdef _MSC_VER
ulong res; ulong res;
return _BitScanForward64(&res, arg) || nonzero ? res : 64; return _BitScanForward64(&res, arg) || nonzero ? res : 64;
#elif __BMI__
return _tzcnt_u64(arg);
#else #else
return arg || nonzero ? __builtin_ctzll(arg) : 64; return arg || nonzero ? __builtin_ctzll(arg) : 64;
#endif #endif