1 /*
2  * Copyright 2024 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 <audio_utils/power.h>
18 
19 #include <random>
20 #include <vector>
21 
22 #include <audio_utils/format.h>
23 #include <benchmark/benchmark.h>
24 #include <log/log.h>
25 
26 /*
27 Pixel 7 (USE_NEON code) 1024 frames
28 ------------------------------------------------------------------------------------
29 Benchmark                       Time                      CPU             Iteration
30 ------------------------------------------------------------------------------------
31 audio_power_benchmark:
32   #BM_Power_PCM16/0     180.92289679585028 ns    180.15104399245882 ns      3885325
33   #BM_Power_PCM16/1     181.16126874965656 ns    180.28731034748992 ns      3885725
34   #BM_Power_PCM16/2      366.9960881450727 ns    365.39403294327775 ns      1916992
35   #BM_Power_PCM16/3      548.5112717240582 ns     546.0744994668512 ns      1124451
36   #BM_Power_PCM16/4      729.0495355718209 ns     725.8359358395215 ns       964519
37   #BM_Power_PCM16/5       730.510620314316 ns     726.6058324908629 ns       964425
38   #BM_Power_PCM16/6      728.7902308009492 ns     725.5839959346857 ns       964255
39   #BM_Power_PCM16/7      728.9437536542266 ns     725.7622692705022 ns       964116
40   #BM_Power_PCM16/8      909.5644815487676 ns     905.2594277274385 ns       773198
41   #BM_Power_PCM16/9      909.8487590417226 ns      905.541972865661 ns       773190
42   #BM_Power_PCM16/10      909.183445722452 ns     905.3521771357088 ns       773057
43   #BM_Power_PCM16/11      1090.09789477985 ns    1085.3346029826685 ns       645060
44   #BM_Power_PCM16/12    1090.3917184788897 ns    1085.1526904891118 ns       645050
45   #BM_Power_PCM16/13    1090.6139456707829 ns    1085.3129791109552 ns       645075
46   #BM_Power_PCM16/14    1270.3910705694595 ns    1264.6607241907009 ns       553473
47   #BM_Power_PCM16/15    1451.2089754139336 ns    1444.6507448881837 ns       484368
48   #BM_Power_PCM16/16    1450.9700749658366 ns    1444.8052807460408 ns       484477
49   #BM_Power_PCM16/17     1812.303903144669 ns     1803.980427679052 ns       388048
50   #BM_Power_PCM16/18    1811.9722496408933 ns     1804.060838412867 ns       387995
51   #BM_Power_PCM16/19    2172.9285815784997 ns     2163.549761392581 ns       323544
52   #BM_Power_PCM16/20     2895.717365719501 ns    2882.4285590756745 ns       242858
53   #BM_Power_PCM16/21    2362.8065810160383 ns     2343.214059957551 ns       298678
54   #BM_Power_PCM16/22     4375.607432531217 ns    4349.7750805466185 ns       162018
55   #BM_Power_PCM24/0      729.5894710558571 ns     726.1363670792448 ns       963781
56   #BM_Power_PCM24/1      729.3882670878899 ns     726.1209068088475 ns       964040
57   #BM_Power_PCM24/2     1458.6373129782264 ns    1451.9195964682701 ns       482143
58   #BM_Power_PCM24/3     2187.9754265826004 ns    2177.8859773833788 ns       321445
59   #BM_Power_PCM24/4      2916.471421225014 ns    2903.4799741173238 ns       241088
60   #BM_Power_PCM24/5     2916.8920647399905 ns     2903.440625661062 ns       241089
61   #BM_Power_PCM24/6     2917.0361012229073 ns     2903.434031522186 ns       241100
62   #BM_Power_PCM24/7      2917.120625640503 ns    2903.6405370541643 ns       241093
63   #BM_Power_PCM24/8      3646.476550238203 ns     3629.390335960183 ns       192880
64   #BM_Power_PCM24/9      3645.840301799253 ns    3629.2467050344903 ns       192870
65   #BM_Power_PCM24/10     3644.269759390758 ns    3629.3227967213143 ns       192883
66   #BM_Power_PCM24/11     4380.128164445656 ns     4354.962614554768 ns       160731
67   #BM_Power_PCM24/12     4388.973117427308 ns     4354.930644037431 ns       160736
68   #BM_Power_PCM24/13     4374.525575418697 ns     4354.927302592676 ns       160721
69   #BM_Power_PCM24/14     5118.992190017827 ns     5087.803814971004 ns       137773
70   #BM_Power_PCM24/15      5834.08466279494 ns     5806.663514107713 ns       120537
71   #BM_Power_PCM24/16      5833.92617600098 ns     5806.763406521383 ns       120557
72   #BM_Power_PCM24/17     7292.561040415548 ns     7258.959351364824 ns        96387
73   #BM_Power_PCM24/18      7290.05913892853 ns     7257.935738432544 ns        96434
74   #BM_Power_PCM24/19      8748.81338349567 ns      8710.19083466056 ns        80368
75   #BM_Power_PCM24/20     11670.86173811743 ns      11617.8891747444 ns        60248
76   #BM_Power_PCM24/21     9483.894109345772 ns     9440.172604919655 ns        74152
77   #BM_Power_PCM24/22       17519.172322663 ns    17440.687347386174 ns        40134
78   #BM_Power_PCM32/0      180.0209277417017 ns    179.20765234810716 ns      3905677
79   #BM_Power_PCM32/1      179.9928913935374 ns     179.1684100688871 ns      3906251
80   #BM_Power_PCM32/2       365.261156361417 ns    363.63974493506385 ns      1925000
81   #BM_Power_PCM32/3      547.3235281262711 ns     544.8913868960904 ns      1284624
82   #BM_Power_PCM32/4      729.4047246495911 ns     726.1992527464306 ns       963796
83   #BM_Power_PCM32/5      729.4026048853057 ns     726.2301922807576 ns       963955
84   #BM_Power_PCM32/6      729.4836424896052 ns     726.2090315177677 ns       963869
85   #BM_Power_PCM32/7      729.4162009832139 ns     726.1991190455375 ns       959868
86   #BM_Power_PCM32/8      912.2165759413726 ns     908.1020182643767 ns       771009
87   #BM_Power_PCM32/9      912.4562880732158 ns     908.1058311552687 ns       770945
88   #BM_Power_PCM32/10     913.6662892148578 ns     909.0608019154854 ns       770979
89   #BM_Power_PCM32/11     1098.264694586373 ns    1092.9951908063122 ns       640440
90   #BM_Power_PCM32/12    1097.8838885144464 ns    1092.9489029436984 ns       640350
91   #BM_Power_PCM32/13    1098.1035265039986 ns     1092.978751342772 ns       640464
92   #BM_Power_PCM32/14    1281.5146387499506 ns     1274.678222409978 ns       549193
93   #BM_Power_PCM32/15     1462.932101769714 ns    1456.0376080037322 ns       477877
94   #BM_Power_PCM32/16    1462.1173842383478 ns    1455.9795936297865 ns       478037
95   #BM_Power_PCM32/17    1826.1988109374897 ns    1818.7870198104167 ns       384848
96   #BM_Power_PCM32/18    1826.7986165280556 ns    1819.0798595772844 ns       384838
97   #BM_Power_PCM32/19    2191.3019592411697 ns     2181.655935394193 ns       320838
98   #BM_Power_PCM32/20    2931.8883516177143 ns     2917.725262082898 ns       239905
99   #BM_Power_PCM32/21    2379.3091425177577 ns     2368.454290770179 ns       295553
100   #BM_Power_PCM32/22     4422.450541711959 ns     4403.615219171803 ns       159943
101   #BM_Power_FLOAT/0      93.18406291611159 ns     92.72058949295965 ns      7553678
102   #BM_Power_FLOAT/1      92.96117761951658 ns     92.53548505608882 ns      7556054
103   #BM_Power_FLOAT/2     188.52435343170848 ns     187.6555618949056 ns      3728037
104   #BM_Power_FLOAT/3     280.48933614094835 ns    279.55113138431284 ns      2504675
105   #BM_Power_FLOAT/4      368.2923103024517 ns    366.59376988731435 ns      1893481
106   #BM_Power_FLOAT/5      368.1838437118831 ns     366.5048535862692 ns      1910340
107   #BM_Power_FLOAT/6     369.36474434357825 ns    367.32105894972386 ns      1909288
108   #BM_Power_FLOAT/7     368.20110381565314 ns     366.5717728132608 ns      1909372
109   #BM_Power_FLOAT/8      458.8126480295947 ns    456.74935048246607 ns      1533061
110   #BM_Power_FLOAT/9     458.29885591662384 ns    456.42679064123894 ns      1533110
111   #BM_Power_FLOAT/10     458.4213634993639 ns     456.3022994462401 ns      1534065
112   #BM_Power_FLOAT/11     548.6835275729175 ns     546.1359314624694 ns      1281050
113   #BM_Power_FLOAT/12     548.5658948648728 ns     546.1222584767767 ns      1281678
114   #BM_Power_FLOAT/13     549.7411047192542 ns     546.3104058269406 ns      1280398
115   #BM_Power_FLOAT/14     639.2796692591446 ns     636.3039215508022 ns      1100432
116   #BM_Power_FLOAT/15     732.6126268814043 ns     728.9874047781268 ns       960126
117   #BM_Power_FLOAT/16     732.3223255154735 ns     729.0677081868715 ns       960135
118   #BM_Power_FLOAT/17      912.688931847384 ns     908.6882020903489 ns       770111
119   #BM_Power_FLOAT/18       912.94823797903 ns      908.767039789369 ns       770256
120   #BM_Power_FLOAT/19    1093.0075590498282 ns    1088.1831744600092 ns       643070
121   #BM_Power_FLOAT/20     1463.591460755948 ns    1457.1715864853975 ns       480370
122   #BM_Power_FLOAT/21    1186.9261155706474 ns    1181.6336976486523 ns       592344
123   #BM_Power_FLOAT/22     2219.151555923471 ns    2208.8095264975427 ns       318879
124 
125 Pixel 7 (generic intrinsics)
126 audio_power_benchmark:
127   #BM_Power_PCM16/0     141.39894261206203 ns    140.74631807527237 ns      4973961
128   #BM_Power_PCM16/1     141.30068134838092 ns      140.673563398614 ns      4974675
129   #BM_Power_PCM16/2      278.2230465394509 ns    276.91650670645566 ns      2526670
130   #BM_Power_PCM16/3     416.70229281643674 ns      413.696774806424 ns      1694813
131   #BM_Power_PCM16/4      551.6619269166048 ns     549.2264481641901 ns      1270039
132   #BM_Power_PCM16/5      550.4119551187774 ns     548.1631002490425 ns      1276491
133   #BM_Power_PCM16/6      549.8963120792694 ns     547.3601694915252 ns      1273456
134   #BM_Power_PCM16/7      551.9152668960979 ns     549.5658038666706 ns      1274378
135   #BM_Power_PCM16/8      691.2875636182188 ns      687.659654354893 ns      1025387
136   #BM_Power_PCM16/9      689.7719348428035 ns     683.1671495932702 ns      1017753
137   #BM_Power_PCM16/10      688.385108618128 ns     685.2313404997149 ns      1021343
138   #BM_Power_PCM16/11     824.7405061324669 ns     821.3459965179951 ns       851234
139   #BM_Power_PCM16/12     824.8310788746044 ns     821.1664236259036 ns       854452
140   #BM_Power_PCM16/13     822.9901445586745 ns     819.2438917777235 ns       857901
141   #BM_Power_PCM16/14     961.7364426681155 ns     957.7359233226135 ns       731689
142   #BM_Power_PCM16/15     1060.764876366047 ns    1056.6351267548569 ns       641790
143   #BM_Power_PCM16/16    1097.4902606840346 ns    1093.1660882847204 ns       640292
144   #BM_Power_PCM16/17    1371.8822829713993 ns    1366.1015167052424 ns       511965
145   #BM_Power_PCM16/18     1379.527502464746 ns    1369.2386592623832 ns       512136
146   #BM_Power_PCM16/19    1642.4514828653857 ns     1635.289426374952 ns       428869
147   #BM_Power_PCM16/20    2181.5514388801307 ns     2171.463230349669 ns       321216
148   #BM_Power_PCM16/21    1772.0538271784708 ns    1764.8680693837691 ns       396231
149   #BM_Power_PCM16/22    3253.2907084319877 ns    3238.3741398011207 ns       219426
150   #BM_Power_PCM24/0      728.6418691492001 ns     725.3989273888943 ns       964935
151   #BM_Power_PCM24/1      728.3793964971507 ns     725.3592953992454 ns       964972
152   #BM_Power_PCM24/2     1457.2141301147049 ns    1450.6804473488357 ns       482487
153   #BM_Power_PCM24/3      2186.456712541353 ns     2176.054527369373 ns       321710
154   #BM_Power_PCM24/4      2930.220041861956 ns    2913.4611367827733 ns       241295
155   #BM_Power_PCM24/5     2914.6065552550235 ns     2901.222598977792 ns       241241
156   #BM_Power_PCM24/6     2914.4422212238487 ns    2901.1865175244834 ns       241291
157   #BM_Power_PCM24/7     2914.0364494153173 ns     2901.173080031326 ns       241293
158   #BM_Power_PCM24/8      3641.242930020149 ns     3626.409360996713 ns       193035
159   #BM_Power_PCM24/9     3654.4684532440847 ns     3632.299661712377 ns       193031
160   #BM_Power_PCM24/10    3642.4799922930624 ns    3626.4395211007954 ns       193026
161   #BM_Power_PCM24/11     4370.161811319268 ns     4351.627426113113 ns       160854
162   #BM_Power_PCM24/12     4371.614458426819 ns    4351.7940097723185 ns       160862
163   #BM_Power_PCM24/13     4387.422872492355 ns     4363.170747931368 ns       160857
164   #BM_Power_PCM24/14     5101.130549812997 ns     5077.108034788132 ns       137863
165   #BM_Power_PCM24/15     5826.311256536362 ns     5802.145559755925 ns       120624
166   #BM_Power_PCM24/16     5829.212266562846 ns     5802.334386087034 ns       120636
167   #BM_Power_PCM24/17     7284.666217547489 ns     7252.658321762346 ns        96506
168   #BM_Power_PCM24/18     7283.647952825528 ns     7252.640452115548 ns        96524
169   #BM_Power_PCM24/19     8741.574695279003 ns     8703.302113901984 ns        80420
170   #BM_Power_PCM24/20     11660.35250035382 ns    11608.754092379155 ns        60295
171   #BM_Power_PCM24/21     9498.126111783777 ns     9429.283448331573 ns        74204
172   #BM_Power_PCM24/22    17500.249881617296 ns    17427.527024672003 ns        40167
173   #BM_Power_PCM32/0      482.2639310164815 ns    480.16807832291283 ns      1457862
174   #BM_Power_PCM32/1      482.5041888036376 ns    480.17729091453714 ns      1457813
175   #BM_Power_PCM32/2      970.7658094980881 ns      966.432565916653 ns       724248
176   #BM_Power_PCM32/3     1470.8525316656937 ns    1453.8102836400858 ns       481561
177   #BM_Power_PCM32/4     1945.6320264943743 ns    1937.5130371239736 ns       361276
178   #BM_Power_PCM32/5     1957.2762784111894 ns    1941.2989846816433 ns       361266
179   #BM_Power_PCM32/6      1947.606134398178 ns    1937.6027027251473 ns       361339
180   #BM_Power_PCM32/7      1945.564237181621 ns    1937.4354472085072 ns       361185
181   #BM_Power_PCM32/8      2449.222944697634 ns    2430.0832236340734 ns       288668
182   #BM_Power_PCM32/9     2447.1957648212733 ns     2429.213497391568 ns       288678
183   #BM_Power_PCM32/10    2451.5493899925777 ns    2433.2397932673425 ns       288682
184   #BM_Power_PCM32/11    2918.8149689454144 ns    2907.2442323189375 ns       240738
185   #BM_Power_PCM32/12     2922.758025449901 ns    2907.4440844806995 ns       240765
186   #BM_Power_PCM32/13     2920.348076535013 ns     2907.431136456142 ns       240766
187   #BM_Power_PCM32/14    3409.4233573973634 ns     3392.591268822002 ns       206341
188   #BM_Power_PCM32/15    3892.2040848068073 ns    3875.2047879260954 ns       180621
189   #BM_Power_PCM32/16     3892.308491260789 ns     3875.119794723041 ns       180634
190   #BM_Power_PCM32/17     4888.503959312049 ns     4860.592739616924 ns       144345
191   #BM_Power_PCM32/18     4873.615265528875 ns      4849.78500253456 ns       144011
192   #BM_Power_PCM32/19     5845.323708631462 ns     5818.852960983018 ns       120281
193   #BM_Power_PCM32/20     7797.718549579573 ns      7766.08286177126 ns        90126
194   #BM_Power_PCM32/21     6337.479234114833 ns     6314.164677589355 ns       111023
195   #BM_Power_PCM32/22    11808.601733014153 ns    11756.832074866097 ns        59199
196   #BM_Power_FLOAT/0      94.11062191883384 ns      93.5736954638585 ns      7472407
197   #BM_Power_FLOAT/1      94.06728569924215 ns     93.63184288806207 ns      7483938
198   #BM_Power_FLOAT/2      191.0388817303329 ns    190.21414162539318 ns      3681755
199   #BM_Power_FLOAT/3     276.28209107363284 ns    275.12531375653344 ns      2531023
200   #BM_Power_FLOAT/4      370.5403080819118 ns     367.7074869536902 ns      1885974
201   #BM_Power_FLOAT/5      368.4663243042483 ns     366.8448226390385 ns      1909834
202   #BM_Power_FLOAT/6      368.1926881965903 ns    366.49226689956845 ns      1909842
203   #BM_Power_FLOAT/7      368.2706570163833 ns     366.7828121257029 ns      1907717
204   #BM_Power_FLOAT/8     458.03934294857197 ns    456.15671441471744 ns      1534862
205   #BM_Power_FLOAT/9     458.27552421764045 ns    456.13744029161853 ns      1533495
206   #BM_Power_FLOAT/10      458.581440769096 ns     456.3724914204393 ns      1533587
207   #BM_Power_FLOAT/11     551.6756052533791 ns     549.2340707415847 ns      1274102
208   #BM_Power_FLOAT/12     551.9493186375631 ns     549.5109769458269 ns      1274216
209   #BM_Power_FLOAT/13     554.0572718262348 ns       550.19352884349 ns      1274239
210   #BM_Power_FLOAT/14     642.1574617184507 ns     639.3852457219499 ns      1094774
211   #BM_Power_FLOAT/15     735.7774064797563 ns     732.5321681956469 ns       960001
212   #BM_Power_FLOAT/16     737.5902914983094 ns     734.2400489412615 ns       950527
213   #BM_Power_FLOAT/17     911.3091743315161 ns     907.1963370929745 ns       773211
214   #BM_Power_FLOAT/18     911.1150959691154 ns     907.0670443387347 ns       772981
215   #BM_Power_FLOAT/19     1090.110913165201 ns    1085.3036540513142 ns       644928
216   #BM_Power_FLOAT/20     1471.985775109006 ns     1465.513079056131 ns       477825
217   #BM_Power_FLOAT/21    1195.7143711208662 ns     1190.262823056866 ns       587087
218   #BM_Power_FLOAT/22    2220.2886873989446 ns    2209.9696500844384 ns       317991
219 
220  */
221 
222 static constexpr audio_channel_mask_t kChannelPositionMasks[] = {
223     AUDIO_CHANNEL_OUT_FRONT_LEFT,
224     AUDIO_CHANNEL_OUT_FRONT_CENTER,
225     AUDIO_CHANNEL_OUT_STEREO,
226     AUDIO_CHANNEL_OUT_2POINT1,
227     AUDIO_CHANNEL_OUT_2POINT0POINT2,
228     AUDIO_CHANNEL_OUT_QUAD, // AUDIO_CHANNEL_OUT_QUAD_BACK
229     AUDIO_CHANNEL_OUT_QUAD_SIDE,
230     AUDIO_CHANNEL_OUT_SURROUND,
231     AUDIO_CHANNEL_OUT_2POINT1POINT2,
232     AUDIO_CHANNEL_OUT_3POINT0POINT2,
233     AUDIO_CHANNEL_OUT_PENTA,
234     AUDIO_CHANNEL_OUT_3POINT1POINT2,
235     AUDIO_CHANNEL_OUT_5POINT1, // AUDIO_CHANNEL_OUT_5POINT1_BACK
236     AUDIO_CHANNEL_OUT_5POINT1_SIDE,
237     AUDIO_CHANNEL_OUT_6POINT1,
238     AUDIO_CHANNEL_OUT_5POINT1POINT2,
239     AUDIO_CHANNEL_OUT_7POINT1,
240     AUDIO_CHANNEL_OUT_5POINT1POINT4,
241     AUDIO_CHANNEL_OUT_7POINT1POINT2,
242     AUDIO_CHANNEL_OUT_7POINT1POINT4,
243     AUDIO_CHANNEL_OUT_9POINT1POINT6,
244     AUDIO_CHANNEL_OUT_13POINT_360RA,
245     AUDIO_CHANNEL_OUT_22POINT2,
246 };
247 
248 template<audio_format_t FORMAT>
BenchmarkPower(benchmark::State & state)249 static void BenchmarkPower(benchmark::State& state) {
250     const audio_channel_mask_t channelMask = kChannelPositionMasks[state.range(0)];
251     const size_t channels = audio_channel_count_from_out_mask(channelMask);
252 
253     // set up random generator.
254     constexpr float amplitude = 0.01f;
255     std::minstd_rand gen(channelMask);
256     std::uniform_real_distribution<> dis(-amplitude, amplitude);
257 
258     // get random audio data.
259     constexpr size_t frameCount = 1024;
260     std::vector<float> input(channels * frameCount);
261     for (auto& in : input) {
262         in = dis(gen);
263     }
264 
265     // convert to proper PCM format.
266     std::vector<uint8_t> buffer(channels * frameCount *  audio_bytes_per_sample(FORMAT));
267     memcpy_by_audio_format(buffer.data(), FORMAT, input.data(),
268                            AUDIO_FORMAT_PCM_FLOAT, input.size());
269 
270     // run the test
271     for (auto _ : state) {
272         benchmark::DoNotOptimize(buffer);
273         audio_utils_compute_energy_mono(buffer.data(), FORMAT, input.size());
274         benchmark::ClobberMemory();
275     }
276 
277     state.SetComplexityN(channels);
278     state.SetLabel(audio_channel_out_mask_to_string(channelMask));
279 }
280 
ChannelArgs(benchmark::internal::Benchmark * b)281 static void ChannelArgs(benchmark::internal::Benchmark* b) {
282     for (int i = 0; i < (int)std::size(kChannelPositionMasks); i++) {
283         b->Args({i});
284     }
285 }
286 
BM_Power_PCM16(benchmark::State & state)287 static void BM_Power_PCM16(benchmark::State& state) {
288     BenchmarkPower<AUDIO_FORMAT_PCM_16_BIT>(state);
289 }
290 
BM_Power_PCM24(benchmark::State & state)291 static void BM_Power_PCM24(benchmark::State& state) {
292     BenchmarkPower<AUDIO_FORMAT_PCM_24_BIT_PACKED>(state);
293 }
294 
BM_Power_PCM32(benchmark::State & state)295 static void BM_Power_PCM32(benchmark::State& state) {
296     BenchmarkPower<AUDIO_FORMAT_PCM_32_BIT>(state);
297 }
298 
BM_Power_FLOAT(benchmark::State & state)299 static void BM_Power_FLOAT(benchmark::State& state) {
300     BenchmarkPower<AUDIO_FORMAT_PCM_FLOAT>(state);
301 }
302 
303 BENCHMARK(BM_Power_PCM16)->Apply(ChannelArgs);
304 BENCHMARK(BM_Power_PCM24)->Apply(ChannelArgs);
305 BENCHMARK(BM_Power_PCM32)->Apply(ChannelArgs);
306 BENCHMARK(BM_Power_FLOAT)->Apply(ChannelArgs);
307 
308 BENCHMARK_MAIN();
309