1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <array>
18 #include <climits>
19 #include <cstdlib>
20 #include <random>
21 #include <vector>
22
23 #include <benchmark/benchmark.h>
24
25 #include <audio_utils/intrinsic_utils.h>
26 #include <audio_utils/format.h>
27
BM_Intrinsic(benchmark::State & state)28 static void BM_Intrinsic(benchmark::State& state) {
29 using D = float;
30 using namespace android::audio_utils::intrinsics;
31 constexpr size_t SIMD_LENGTH = 4;
32
33 // Possible testing types:
34 using vec = android::audio_utils::intrinsics::internal_array_t<D, SIMD_LENGTH>;
35 //using vec = float32x4_t;
36 //using vec = float32x4x4_t;
37
38 constexpr size_t DATA_SIZE = 1024;
39 D a[DATA_SIZE];
40 D b[DATA_SIZE];
41 D c[DATA_SIZE];
42 D d[DATA_SIZE];
43
44 constexpr std::minstd_rand::result_type SEED = 42; // arbitrary choice.
45 std::minstd_rand gen(SEED);
46 const D amplitude = 1.0f;
47 std::uniform_real_distribution<> dis(-amplitude, amplitude);
48 for (size_t i = 0; i < DATA_SIZE; ++i) {
49 a[i] = dis(gen);
50 b[i] = dis(gen);
51 c[i] = dis(gen);
52 }
53
54 while (state.KeepRunning()) {
55 for (size_t i = 0; i < DATA_SIZE; i += sizeof(vec) / sizeof(D)) {
56 const vec av = vld1<vec>(a + i);
57 const vec bv = vld1<vec>(b + i);
58 const vec cv = vld1<vec>(c + i);
59 const vec dv = vmla(cv, av, bv);
60 vst1(d + i, dv);
61 }
62 benchmark::DoNotOptimize(d[0]);
63 benchmark::ClobberMemory();
64 }
65 //fprintf(stderr, "%f: %f %f\n %f", d[0], c[0], a[0], b[0]);
66 state.SetComplexityN(state.range(0));
67 }
68
69 // A simple test using the VMLA intrinsic.
70 // One can alter either the intrinsic code or the compilation flags to see the benefit.
71 // Recommend using objdump to view the assembly.
BM_IntrinsicArgs(benchmark::internal::Benchmark * b)72 static void BM_IntrinsicArgs(benchmark::internal::Benchmark* b) {
73 for (int k = 0; k < 2; k++) // 0 for normal random data, 1 for subnormal random data
74 b->Args({k});
75 }
76
77 BENCHMARK(BM_Intrinsic)->Apply(BM_IntrinsicArgs);
78
79 BENCHMARK_MAIN();
80