xref: /aosp_15_r20/external/XNNPACK/bench/s16-rmaxabs.cc (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 <numeric>
10 #include <vector>
11 
12 #include "bench/utils.h"
13 #include <benchmark/benchmark.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/rmaxabs.h>
20 
21 
rmaxabs(benchmark::State & state,xnn_s16_rmaxabs_ukernel_function rmaxabs,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void rmaxabs(
23     benchmark::State& state,
24     xnn_s16_rmaxabs_ukernel_function rmaxabs,
25     benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27   if (isa_check && !isa_check(state)) {
28     return;
29   }
30   const size_t channels = state.range(0);
31 
32   std::vector<int16_t, AlignedAllocator<int16_t, 64>> input(
33       (channels) + XNN_EXTRA_BYTES / sizeof(int16_t));
34   std::iota(input.begin(), input.end(), 0);
35 
36   uint16_t output = 0u;
37   for (auto _ : state) {
38     rmaxabs(channels, input.data(), &output);
39   }
40 
41   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
42   if (cpu_frequency != 0) {
43     state.counters["cpufreq"] = cpu_frequency;
44   }
45 }
46 
BenchmarkKernelSize(benchmark::internal::Benchmark * b)47 static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
48 {
49   b->ArgNames({"rows", "channels"});
50   b->Args({32});
51   b->Args({64});
52   b->Args({216});
53   b->Args({400});
54   b->Args({1000});
55   b->Args({10000});
56   b->Args({100000});
57 }
58 
59 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
60 BENCHMARK_CAPTURE(rmaxabs, s16_neon_x8, xnn_s16_rmaxabs_ukernel__neon_x8, benchmark::utils::CheckNEON)
61     ->Apply(BenchmarkKernelSize)->UseRealTime();
62 BENCHMARK_CAPTURE(rmaxabs, s16_neon_x16, xnn_s16_rmaxabs_ukernel__neon_x16, benchmark::utils::CheckNEON)
63     ->Apply(BenchmarkKernelSize)->UseRealTime();
64 BENCHMARK_CAPTURE(rmaxabs, s16_neon_x24, xnn_s16_rmaxabs_ukernel__neon_x24, benchmark::utils::CheckNEON)
65     ->Apply(BenchmarkKernelSize)->UseRealTime();
66 BENCHMARK_CAPTURE(rmaxabs, s16_neon_x32, xnn_s16_rmaxabs_ukernel__neon_x32, benchmark::utils::CheckNEON)
67     ->Apply(BenchmarkKernelSize)->UseRealTime();
68 #endif  // XNN_ARCH_ARM || XNN_ARCH_ARM64
69 
70 BENCHMARK_CAPTURE(rmaxabs, s16_scalar_x1, xnn_s16_rmaxabs_ukernel__scalar_x1)
71     ->Apply(BenchmarkKernelSize)->UseRealTime();
72 BENCHMARK_CAPTURE(rmaxabs, s16_scalar_x2, xnn_s16_rmaxabs_ukernel__scalar_x2)
73     ->Apply(BenchmarkKernelSize)->UseRealTime();
74 BENCHMARK_CAPTURE(rmaxabs, s16_scalar_x3, xnn_s16_rmaxabs_ukernel__scalar_x3)
75     ->Apply(BenchmarkKernelSize)->UseRealTime();
76 BENCHMARK_CAPTURE(rmaxabs, s16_scalar_x4, xnn_s16_rmaxabs_ukernel__scalar_x4)
77     ->Apply(BenchmarkKernelSize)->UseRealTime();
78 
79 #ifndef XNNPACK_BENCHMARK_NO_MAIN
80 BENCHMARK_MAIN();
81 #endif
82