aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/benchmark/rotate_bits/main.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/benchmark/rotate_bits/main.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/benchmark/rotate_bits/main.cpp')
-rw-r--r--util/generic/benchmark/rotate_bits/main.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/util/generic/benchmark/rotate_bits/main.cpp b/util/generic/benchmark/rotate_bits/main.cpp
new file mode 100644
index 0000000000..057edbe864
--- /dev/null
+++ b/util/generic/benchmark/rotate_bits/main.cpp
@@ -0,0 +1,66 @@
+#include <library/cpp/testing/benchmark/bench.h>
+
+#include <util/generic/vector.h>
+#include <util/generic/xrange.h>
+#include <util/generic/singleton.h>
+
+#include <util/random/fast.h>
+
+namespace {
+ template <typename T>
+ struct TExample {
+ T Value;
+ ui8 Shift;
+ };
+
+ template <typename T, size_t N>
+ struct TExamplesHolder {
+ TExamplesHolder()
+ : Examples(N)
+ {
+ TFastRng<ui64> prng{42u * sizeof(T) * N};
+ for (auto& e : Examples) {
+ e.Value = prng();
+ e.Shift = prng() % (8 * sizeof(T));
+ }
+ }
+
+ TVector<TExample<T>> Examples;
+ };
+}
+
+#define DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(type, count) \
+ Y_CPU_BENCHMARK(LeftRotate_##type##_##count, iface) { \
+ const auto& examples = Default<TExamplesHolder<type, count>>().Examples; \
+ for (const auto i : xrange(iface.Iterations())) { \
+ Y_UNUSED(i); \
+ for (const auto e : examples) { \
+ Y_DO_NOT_OPTIMIZE_AWAY(RotateBitsLeft(e.Value, e.Shift)); \
+ } \
+ } \
+ } \
+ \
+ Y_CPU_BENCHMARK(RightRotate_##type##_##count, iface) { \
+ const auto& examples = Default<TExamplesHolder<type, count>>().Examples; \
+ for (const auto i : xrange(iface.Iterations())) { \
+ Y_UNUSED(i); \
+ for (const auto e : examples) { \
+ Y_DO_NOT_OPTIMIZE_AWAY(RotateBitsRight(e.Value, e.Shift)); \
+ } \
+ } \
+ }
+
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui8, 1)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui8, 10)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui8, 100)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui16, 1)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui16, 10)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui16, 100)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui32, 1)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui32, 10)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui32, 100)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui64, 1)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui64, 10)
+DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES(ui64, 100)
+
+#undef DEFINE_BENCHMARKS_FOR_UNSIGNED_TYPES