diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/pop_count | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/pop_count')
-rw-r--r-- | library/cpp/pop_count/benchmark/main.cpp | 94 | ||||
-rw-r--r-- | library/cpp/pop_count/benchmark/ya.make | 20 | ||||
-rw-r--r-- | library/cpp/pop_count/popcount.cpp | 52 | ||||
-rw-r--r-- | library/cpp/pop_count/popcount.h | 92 | ||||
-rw-r--r-- | library/cpp/pop_count/popcount_ut.cpp | 126 | ||||
-rw-r--r-- | library/cpp/pop_count/ut/ya.make | 14 | ||||
-rw-r--r-- | library/cpp/pop_count/ya.make | 16 |
7 files changed, 207 insertions, 207 deletions
diff --git a/library/cpp/pop_count/benchmark/main.cpp b/library/cpp/pop_count/benchmark/main.cpp index 41ea3c91cc5..13720beb18c 100644 --- a/library/cpp/pop_count/benchmark/main.cpp +++ b/library/cpp/pop_count/benchmark/main.cpp @@ -1,52 +1,52 @@ -#include <util/stream/output.h> -#include <util/datetime/cputimer.h> +#include <util/stream/output.h> +#include <util/datetime/cputimer.h> #include <util/system/type_name.h> - + #include <library/cpp/pop_count/popcount.h> #include <library/cpp/testing/benchmark/bench.h> - -template <class F, class I> -inline void DoRun(F&& f, I&& i) { - const ui64 n = i.Iterations(); - + +template <class F, class I> +inline void DoRun(F&& f, I&& i) { + const ui64 n = i.Iterations(); + for (ui64 j = 0; j < n; ++j) { Y_DO_NOT_OPTIMIZE_AWAY(f(j * (ui64)123456 + (ui64)1)); - } -} - -Y_CPU_BENCHMARK(PopCount_8, iface) { - DoRun([](ui8 x) { - return PopCount<ui8>(x); - }, - iface); -} - -Y_CPU_BENCHMARK(PopCount_16, iface) { - DoRun([](ui16 x) { - return PopCount<ui16>(x); - }, - iface); -} - -Y_CPU_BENCHMARK(PopCount_32, iface) { - DoRun([](ui32 x) { - return PopCount<ui32>(x); - }, - iface); -} - -Y_CPU_BENCHMARK(PopCount_64, iface) { - DoRun([](ui64 x) { - return PopCount<ui64>(x); - }, - iface); -} - -#if !defined(_MSC_VER) -Y_CPU_BENCHMARK(BUILTIN_64, iface) { - DoRun([](ui64 x) { - return __builtin_popcountll(x); - }, - iface); -} -#endif + } +} + +Y_CPU_BENCHMARK(PopCount_8, iface) { + DoRun([](ui8 x) { + return PopCount<ui8>(x); + }, + iface); +} + +Y_CPU_BENCHMARK(PopCount_16, iface) { + DoRun([](ui16 x) { + return PopCount<ui16>(x); + }, + iface); +} + +Y_CPU_BENCHMARK(PopCount_32, iface) { + DoRun([](ui32 x) { + return PopCount<ui32>(x); + }, + iface); +} + +Y_CPU_BENCHMARK(PopCount_64, iface) { + DoRun([](ui64 x) { + return PopCount<ui64>(x); + }, + iface); +} + +#if !defined(_MSC_VER) +Y_CPU_BENCHMARK(BUILTIN_64, iface) { + DoRun([](ui64 x) { + return __builtin_popcountll(x); + }, + iface); +} +#endif diff --git a/library/cpp/pop_count/benchmark/ya.make b/library/cpp/pop_count/benchmark/ya.make index 7fb54a519a2..02b61bdeca7 100644 --- a/library/cpp/pop_count/benchmark/ya.make +++ b/library/cpp/pop_count/benchmark/ya.make @@ -1,14 +1,14 @@ OWNER(g:util) Y_BENCHMARK() - -PEERDIR( - util/draft + +PEERDIR( + util/draft library/cpp/pop_count -) - -SRCS( - main.cpp -) - -END() +) + +SRCS( + main.cpp +) + +END() diff --git a/library/cpp/pop_count/popcount.cpp b/library/cpp/pop_count/popcount.cpp index 49276424be1..28743b4426e 100644 --- a/library/cpp/pop_count/popcount.cpp +++ b/library/cpp/pop_count/popcount.cpp @@ -1,30 +1,30 @@ -#include "popcount.h" - +#include "popcount.h" + #include <util/system/defaults.h> -#include <util/system/yassert.h> - -#include <string.h> - -static const ui8 PopCountLUT8Impl[1 << 8] = { -#define B2(n) n, n + 1, n + 1, n + 2 -#define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2) -#define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2) - B6(0), B6(1), B6(1), B6(2)}; - -ui8 const* PopCountLUT8 = PopCountLUT8Impl; +#include <util/system/yassert.h> -#if !defined(_MSC_VER) -//ICE here for msvc +#include <string.h> + +static const ui8 PopCountLUT8Impl[1 << 8] = { +#define B2(n) n, n + 1, n + 1, n + 2 +#define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2) +#define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2) + B6(0), B6(1), B6(1), B6(2)}; -static const ui8 PopCountLUT16Impl[1 << 16] = { -#define B2(n) n, n + 1, n + 1, n + 2 -#define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2) -#define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2) -#define B8(n) B6(n), B6(n + 1), B6(n + 1), B6(n + 2) -#define B10(n) B8(n), B8(n + 1), B8(n + 1), B8(n + 2) -#define B12(n) B10(n), B10(n + 1), B10(n + 1), B10(n + 2) -#define B14(n) B12(n), B12(n + 1), B12(n + 1), B12(n + 2) - B14(0), B14(1), B14(1), B14(2)}; +ui8 const* PopCountLUT8 = PopCountLUT8Impl; + +#if !defined(_MSC_VER) +//ICE here for msvc + +static const ui8 PopCountLUT16Impl[1 << 16] = { +#define B2(n) n, n + 1, n + 1, n + 2 +#define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2) +#define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2) +#define B8(n) B6(n), B6(n + 1), B6(n + 1), B6(n + 2) +#define B10(n) B8(n), B8(n + 1), B8(n + 1), B8(n + 2) +#define B12(n) B10(n), B10(n + 1), B10(n + 1), B10(n + 2) +#define B14(n) B12(n), B12(n + 1), B12(n + 1), B12(n + 2) + B14(0), B14(1), B14(1), B14(2)}; -ui8 const* PopCountLUT16 = PopCountLUT16Impl; -#endif +ui8 const* PopCountLUT16 = PopCountLUT16Impl; +#endif diff --git a/library/cpp/pop_count/popcount.h b/library/cpp/pop_count/popcount.h index 3d67737ed25..9833f039ff9 100644 --- a/library/cpp/pop_count/popcount.h +++ b/library/cpp/pop_count/popcount.h @@ -6,11 +6,11 @@ #include <util/system/hi_lo.h> #include <util/system/platform.h> -#if defined(_MSC_VER) -#include <intrin.h> +#if defined(_MSC_VER) +#include <intrin.h> #endif -static inline ui32 PopCountImpl(ui8 n) { +static inline ui32 PopCountImpl(ui8 n) { #if defined(_ppc64_) ui32 r; __asm__("popcntb %0, %1" @@ -19,35 +19,35 @@ static inline ui32 PopCountImpl(ui8 n) { :); return r; #else - extern ui8 const* PopCountLUT8; - return PopCountLUT8[n]; + extern ui8 const* PopCountLUT8; + return PopCountLUT8[n]; #endif -} +} -static inline ui32 PopCountImpl(ui16 n) { -#if defined(_MSC_VER) - return __popcnt16(n); +static inline ui32 PopCountImpl(ui16 n) { +#if defined(_MSC_VER) + return __popcnt16(n); #else - extern ui8 const* PopCountLUT16; - return PopCountLUT16[n]; + extern ui8 const* PopCountLUT16; + return PopCountLUT16[n]; #endif -} +} -static inline ui32 PopCountImpl(ui32 n) { -#if defined(_MSC_VER) - return __popcnt(n); +static inline ui32 PopCountImpl(ui32 n) { +#if defined(_MSC_VER) + return __popcnt(n); #else -#if defined(_x86_64_) +#if defined(_x86_64_) if (NX86::CachedHavePOPCNT()) { - ui32 r; - - __asm__("popcnt %1, %0;" - : "=r"(r) - : "r"(n) - :); - - return r; - } + ui32 r; + + __asm__("popcnt %1, %0;" + : "=r"(r) + : "r"(n) + :); + + return r; + } #else #if defined(_ppc64_) ui32 r; @@ -58,28 +58,28 @@ static inline ui32 PopCountImpl(ui32 n) { :); return r; +#endif #endif -#endif - + return PopCountImpl((ui16)Lo16(n)) + PopCountImpl((ui16)Hi16(n)); #endif -} +} -static inline ui32 PopCountImpl(ui64 n) { +static inline ui32 PopCountImpl(ui64 n) { #if defined(_MSC_VER) && !defined(_i386_) - return __popcnt64(n); + return __popcnt64(n); #else -#if defined(_x86_64_) +#if defined(_x86_64_) if (NX86::CachedHavePOPCNT()) { - ui64 r; - - __asm__("popcnt %1, %0;" - : "=r"(r) - : "r"(n) - :); - - return r; - } + ui64 r; + + __asm__("popcnt %1, %0;" + : "=r"(r) + : "r"(n) + :); + + return r; + } #else #if defined(_ppc64_) ui32 r; @@ -90,16 +90,16 @@ static inline ui32 PopCountImpl(ui64 n) { :); return r; +#endif #endif -#endif - + return PopCountImpl((ui32)Lo32(n)) + PopCountImpl((ui32)Hi32(n)); #endif -} +} -template <class T> -static inline ui32 PopCount(T n) { +template <class T> +static inline ui32 PopCount(T n) { using TCvt = TFixedWidthUnsignedInt<T>; - return PopCountImpl((TCvt)n); + return PopCountImpl((TCvt)n); } diff --git a/library/cpp/pop_count/popcount_ut.cpp b/library/cpp/pop_count/popcount_ut.cpp index 5cd6605411f..df2d246de2a 100644 --- a/library/cpp/pop_count/popcount_ut.cpp +++ b/library/cpp/pop_count/popcount_ut.cpp @@ -1,70 +1,70 @@ -#include "popcount.h" - +#include "popcount.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/random/random.h> - + +#include <util/random/random.h> + Y_UNIT_TEST_SUITE(TestPopCount) { - template <class T> - static inline ui32 SlowPopCount(T t) { - ui32 ret = 0; - - while (t) { - if (t & T(1)) { - ++ret; - } - - t = t >> 1; - } - - return ret; - } - - template <class T> - static inline void Test() { - for (size_t i = 0; i < 10000; ++i) { - const T rndv = RandomNumber<T>(); - - UNIT_ASSERT_VALUES_EQUAL(SlowPopCount(rndv), PopCount(rndv)); - } - } - + template <class T> + static inline ui32 SlowPopCount(T t) { + ui32 ret = 0; + + while (t) { + if (t & T(1)) { + ++ret; + } + + t = t >> 1; + } + + return ret; + } + + template <class T> + static inline void Test() { + for (size_t i = 0; i < 10000; ++i) { + const T rndv = RandomNumber<T>(); + + UNIT_ASSERT_VALUES_EQUAL(SlowPopCount(rndv), PopCount(rndv)); + } + } + Y_UNIT_TEST(Test8) { - Test<ui8>(); - } - + Test<ui8>(); + } + Y_UNIT_TEST(Test16) { - Test<ui16>(); - } - + Test<ui16>(); + } + Y_UNIT_TEST(Test32) { - Test<ui32>(); - } - + Test<ui32>(); + } + Y_UNIT_TEST(Test64) { - Test<ui64>(); - } - + Test<ui64>(); + } + Y_UNIT_TEST(TestPopCount) { - UNIT_ASSERT_VALUES_EQUAL(PopCount(0), 0); - UNIT_ASSERT_VALUES_EQUAL(PopCount(1), 1); - UNIT_ASSERT_VALUES_EQUAL(PopCount(1 << 10), 1); - UNIT_ASSERT_VALUES_EQUAL(PopCount((1 << 10) + 1), 2); - UNIT_ASSERT_VALUES_EQUAL(PopCount(0xFFFF), 16); - UNIT_ASSERT_VALUES_EQUAL(PopCount(0xFFFFFFFF), 32); - UNIT_ASSERT_VALUES_EQUAL(PopCount(0x55555555), 16); - - UNIT_ASSERT_VALUES_EQUAL(0, PopCount(0ULL)); - UNIT_ASSERT_VALUES_EQUAL(1, PopCount(1ULL)); - UNIT_ASSERT_VALUES_EQUAL(16, PopCount(0xAAAAAAAAULL)); - UNIT_ASSERT_VALUES_EQUAL(32, PopCount(0xFFFFFFFFULL)); - UNIT_ASSERT_VALUES_EQUAL(32, PopCount(0xAAAAAAAAAAAAAAAAULL)); - UNIT_ASSERT_VALUES_EQUAL(64, PopCount(0xFFFFFFFFFFFFFFFFULL)); - - ui64 v = 0; - - for (int i = 0; i < 64; v |= 1ULL << i, ++i) { - UNIT_ASSERT_VALUES_EQUAL(i, PopCount(v)); - } - } -} + UNIT_ASSERT_VALUES_EQUAL(PopCount(0), 0); + UNIT_ASSERT_VALUES_EQUAL(PopCount(1), 1); + UNIT_ASSERT_VALUES_EQUAL(PopCount(1 << 10), 1); + UNIT_ASSERT_VALUES_EQUAL(PopCount((1 << 10) + 1), 2); + UNIT_ASSERT_VALUES_EQUAL(PopCount(0xFFFF), 16); + UNIT_ASSERT_VALUES_EQUAL(PopCount(0xFFFFFFFF), 32); + UNIT_ASSERT_VALUES_EQUAL(PopCount(0x55555555), 16); + + UNIT_ASSERT_VALUES_EQUAL(0, PopCount(0ULL)); + UNIT_ASSERT_VALUES_EQUAL(1, PopCount(1ULL)); + UNIT_ASSERT_VALUES_EQUAL(16, PopCount(0xAAAAAAAAULL)); + UNIT_ASSERT_VALUES_EQUAL(32, PopCount(0xFFFFFFFFULL)); + UNIT_ASSERT_VALUES_EQUAL(32, PopCount(0xAAAAAAAAAAAAAAAAULL)); + UNIT_ASSERT_VALUES_EQUAL(64, PopCount(0xFFFFFFFFFFFFFFFFULL)); + + ui64 v = 0; + + for (int i = 0; i < 64; v |= 1ULL << i, ++i) { + UNIT_ASSERT_VALUES_EQUAL(i, PopCount(v)); + } + } +} diff --git a/library/cpp/pop_count/ut/ya.make b/library/cpp/pop_count/ut/ya.make index f0e6c014e59..a2be7f79514 100644 --- a/library/cpp/pop_count/ut/ya.make +++ b/library/cpp/pop_count/ut/ya.make @@ -1,9 +1,9 @@ UNITTEST_FOR(library/cpp/pop_count) - + OWNER(g:util) - -SRCS( - popcount_ut.cpp -) - -END() + +SRCS( + popcount_ut.cpp +) + +END() diff --git a/library/cpp/pop_count/ya.make b/library/cpp/pop_count/ya.make index 0dec238979b..6e86560c856 100644 --- a/library/cpp/pop_count/ya.make +++ b/library/cpp/pop_count/ya.make @@ -1,9 +1,9 @@ -LIBRARY() - +LIBRARY() + OWNER(g:util) - -SRCS( - popcount.cpp -) - -END() + +SRCS( + popcount.cpp +) + +END() |