diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/pop_count/popcount.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/pop_count/popcount.h')
-rw-r--r-- | library/cpp/pop_count/popcount.h | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/library/cpp/pop_count/popcount.h b/library/cpp/pop_count/popcount.h new file mode 100644 index 00000000000..3d67737ed25 --- /dev/null +++ b/library/cpp/pop_count/popcount.h @@ -0,0 +1,105 @@ +#pragma once + +#include <util/generic/typelist.h> +#include <util/system/cpu_id.h> +#include <util/system/defaults.h> +#include <util/system/hi_lo.h> +#include <util/system/platform.h> + +#if defined(_MSC_VER) +#include <intrin.h> +#endif + +static inline ui32 PopCountImpl(ui8 n) { +#if defined(_ppc64_) + ui32 r; + __asm__("popcntb %0, %1" + : "=r"(r) + : "r"(n) + :); + return r; +#else + extern ui8 const* PopCountLUT8; + return PopCountLUT8[n]; +#endif +} + +static inline ui32 PopCountImpl(ui16 n) { +#if defined(_MSC_VER) + return __popcnt16(n); +#else + extern ui8 const* PopCountLUT16; + return PopCountLUT16[n]; +#endif +} + +static inline ui32 PopCountImpl(ui32 n) { +#if defined(_MSC_VER) + return __popcnt(n); +#else +#if defined(_x86_64_) + if (NX86::CachedHavePOPCNT()) { + ui32 r; + + __asm__("popcnt %1, %0;" + : "=r"(r) + : "r"(n) + :); + + return r; + } +#else +#if defined(_ppc64_) + ui32 r; + + __asm__("popcntw %0, %1" + : "=r"(r) + : "r"(n) + :); + + return r; +#endif +#endif + + return PopCountImpl((ui16)Lo16(n)) + PopCountImpl((ui16)Hi16(n)); +#endif +} + +static inline ui32 PopCountImpl(ui64 n) { +#if defined(_MSC_VER) && !defined(_i386_) + return __popcnt64(n); +#else +#if defined(_x86_64_) + if (NX86::CachedHavePOPCNT()) { + ui64 r; + + __asm__("popcnt %1, %0;" + : "=r"(r) + : "r"(n) + :); + + return r; + } +#else +#if defined(_ppc64_) + ui32 r; + + __asm__("popcntd %0, %1" + : "=r"(r) + : "r"(n) + :); + + return r; +#endif +#endif + + return PopCountImpl((ui32)Lo32(n)) + PopCountImpl((ui32)Hi32(n)); +#endif +} + +template <class T> +static inline ui32 PopCount(T n) { + using TCvt = TFixedWidthUnsignedInt<T>; + + return PopCountImpl((TCvt)n); +} |