1 // Copyright 2022 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <algorithm>
7 #include <cmath>
8 #include <functional>
9 #include <random>
10 #include <vector>
11
12 #include <benchmark/benchmark.h>
13 #include "bench/utils.h"
14
15 #include <xnnpack.h>
16 #include <xnnpack/aligned-allocator.h>
17 #include <xnnpack/common.h>
18 #include <xnnpack/microfnptr.h>
19 #include <xnnpack/microparams-init.h>
20 #include <xnnpack/vunary.h>
21
22
u64_u32_vsqrtshift(benchmark::State & state,xnn_u64_u32_vsqrtshift_ukernel_function vsqrtshift,benchmark::utils::IsaCheckFunction isa_check=nullptr)23 static void u64_u32_vsqrtshift(
24 benchmark::State& state,
25 xnn_u64_u32_vsqrtshift_ukernel_function vsqrtshift,
26 benchmark::utils::IsaCheckFunction isa_check = nullptr)
27 {
28 if (isa_check && !isa_check(state)) {
29 return;
30 }
31
32 const size_t num_elements = state.range(0);
33
34 std::random_device random_device;
35 auto rng = std::mt19937(random_device());
36 auto u64rng = std::bind(std::uniform_int_distribution<uint64_t>(), std::ref(rng));
37
38 std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> x(num_elements + XNN_EXTRA_BYTES / sizeof(uint64_t));
39 std::vector<uint32_t, AlignedAllocator<uint32_t, 64>> y(num_elements);
40 std::generate(x.begin(), x.end(), std::ref(u64rng));
41 std::fill(y.begin(), y.end(), UINT32_C(0xDEADBEEF));
42
43 for (auto _ : state) {
44 vsqrtshift(num_elements * sizeof(uint64_t), x.data(), y.data(), 1 /* shift */);
45 }
46
47 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
48 if (cpu_frequency != 0) {
49 state.counters["cpufreq"] = cpu_frequency;
50 }
51
52 const size_t elements_per_iteration = num_elements;
53 state.counters["elements"] =
54 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
55
56 const size_t bytes_per_iteration = num_elements * (sizeof(uint64_t) + sizeof(uint32_t));
57 state.counters["bytes"] =
58 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
59 }
60
61 BENCHMARK_CAPTURE(u64_u32_vsqrtshift, scalar_cvtu32_sqrt_cvtu32f64_x1,
62 xnn_u64_u32_vsqrtshift_ukernel__scalar_cvtu32_sqrt_cvtu32f64_x1)
63 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint64_t, uint32_t>)
64 ->UseRealTime();
65
66 #ifndef XNNPACK_BENCHMARK_NO_MAIN
67 BENCHMARK_MAIN();
68 #endif
69