aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pop_count
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:17 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:17 +0300
commitd3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch)
treedd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/pop_count
parent72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff)
downloadydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/pop_count')
-rw-r--r--library/cpp/pop_count/benchmark/main.cpp94
-rw-r--r--library/cpp/pop_count/benchmark/ya.make20
-rw-r--r--library/cpp/pop_count/popcount.cpp52
-rw-r--r--library/cpp/pop_count/popcount.h92
-rw-r--r--library/cpp/pop_count/popcount_ut.cpp126
-rw-r--r--library/cpp/pop_count/ut/ya.make14
-rw-r--r--library/cpp/pop_count/ya.make16
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 13720beb18c..41ea3c91cc5 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 02b61bdeca7..7fb54a519a2 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 28743b4426e..49276424be1 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 <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 <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)};
+#if !defined(_MSC_VER)
+//ICE here for msvc
-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)};
+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 9833f039ff9..3d67737ed25 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 df2d246de2a..5cd6605411f 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 a2be7f79514..f0e6c014e59 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 6e86560c856..0dec238979b 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()