blob: ed5b56cc42712eeecd925b462d59b21ae3050700 [file] [log] [blame]
Elliott Hughes9edb3e02013-02-06 15:47:09 -08001/*
2 * Copyright (C) 2013 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
Serban Constantinescua147a1d2014-06-08 16:55:22 +010017#include <fenv.h>
Elliott Hughes9edb3e02013-02-06 15:47:09 -080018#include <math.h>
19
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080020#include <benchmark/Benchmark.h>
21
22#define AT_COMMON_VALS \
23 Arg(1234.0)->Arg(nan(""))->Arg(HUGE_VAL)->Arg(0.0)
24
Elliott Hughes9edb3e02013-02-06 15:47:09 -080025// Avoid optimization.
Dan Albert055a59c2014-09-25 15:43:48 -070026volatile double d;
27volatile double v;
Elliott Hughes9edb3e02013-02-06 15:47:09 -080028
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080029BENCHMARK_NO_ARG(BM_math_sqrt);
30void BM_math_sqrt::Run(int iters) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080031 StartBenchmarkTiming();
32
33 d = 0.0;
34 v = 2.0;
35 for (int i = 0; i < iters; ++i) {
36 d += sqrt(v);
37 }
38
39 StopBenchmarkTiming();
40}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080041
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080042BENCHMARK_NO_ARG(BM_math_log10);
43void BM_math_log10::Run(int iters) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080044 StartBenchmarkTiming();
45
46 d = 0.0;
47 v = 1234.0;
48 for (int i = 0; i < iters; ++i) {
49 d += log10(v);
50 }
51
52 StopBenchmarkTiming();
53}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080054
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080055BENCHMARK_NO_ARG(BM_math_logb);
56void BM_math_logb::Run(int iters) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080057 StartBenchmarkTiming();
58
59 d = 0.0;
60 v = 1234.0;
61 for (int i = 0; i < iters; ++i) {
62 d += logb(v);
63 }
64
65 StopBenchmarkTiming();
66}
Elliott Hughes02c78a32014-04-11 17:02:20 -070067
Elliott Hughesb6622802015-08-14 14:04:30 -070068BENCHMARK_WITH_ARG(BM_math_isfinite_macro, double)->AT_COMMON_VALS;
69void BM_math_isfinite_macro::Run(int iters, double value) {
70 StartBenchmarkTiming();
71
72 d = 0.0;
73 v = value;
74 for (int i = 0; i < iters; ++i) {
75 d += isfinite(v);
76 }
77
78 StopBenchmarkTiming();
79}
80
81#if defined(__BIONIC__)
82#define test_isfinite __isfinite
83#else
84#define test_isfinite __finite
85#endif
86BENCHMARK_WITH_ARG(BM_math_isfinite, double)->AT_COMMON_VALS;
87void BM_math_isfinite::Run(int iters, double value) {
88 StartBenchmarkTiming();
89
90 d = 0.0;
91 v = value;
92 for (int i = 0; i < iters; ++i) {
93 d += test_isfinite(v);
94 }
95
96 StopBenchmarkTiming();
97}
98
99BENCHMARK_WITH_ARG(BM_math_isinf_macro, double)->AT_COMMON_VALS;
100void BM_math_isinf_macro::Run(int iters, double value) {
101 StartBenchmarkTiming();
102
103 d = 0.0;
104 v = value;
105 for (int i = 0; i < iters; ++i) {
106 d += isinf(v);
107 }
108
109 StopBenchmarkTiming();
110}
111
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800112BENCHMARK_WITH_ARG(BM_math_isinf, double)->AT_COMMON_VALS;
113void BM_math_isinf::Run(int iters, double value) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700114 StartBenchmarkTiming();
115
116 d = 0.0;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800117 v = value;
Elliott Hughes02c78a32014-04-11 17:02:20 -0700118 for (int i = 0; i < iters; ++i) {
119 d += (isinf)(v);
120 }
121
122 StopBenchmarkTiming();
123}
Elliott Hughes02c78a32014-04-11 17:02:20 -0700124
Elliott Hughesb6622802015-08-14 14:04:30 -0700125BENCHMARK_WITH_ARG(BM_math_isnan_macro, double)->AT_COMMON_VALS;
126void BM_math_isnan_macro::Run(int iters, double value) {
127 StartBenchmarkTiming();
128
129 d = 0.0;
130 v = value;
131 for (int i = 0; i < iters; ++i) {
132 d += isnan(v);
133 }
134
135 StopBenchmarkTiming();
136}
137
138BENCHMARK_WITH_ARG(BM_math_isnan, double)->AT_COMMON_VALS;
139void BM_math_isnan::Run(int iters, double value) {
140 StartBenchmarkTiming();
141
142 d = 0.0;
143 v = value;
144 for (int i = 0; i < iters; ++i) {
145 d += (isnan)(v);
146 }
147
148 StopBenchmarkTiming();
149}
150
151BENCHMARK_WITH_ARG(BM_math_isnormal_macro, double)->AT_COMMON_VALS;
152void BM_math_isnormal_macro::Run(int iters, double value) {
153 StartBenchmarkTiming();
154
155 d = 0.0;
156 v = value;
157 for (int i = 0; i < iters; ++i) {
158 d += isnormal(v);
159 }
160
161 StopBenchmarkTiming();
162}
163
164#if defined(__BIONIC__)
165BENCHMARK_WITH_ARG(BM_math_isnormal, double)->AT_COMMON_VALS;
166void BM_math_isnormal::Run(int iters, double value) {
167 StartBenchmarkTiming();
168
169 d = 0.0;
170 v = value;
171 for (int i = 0; i < iters; ++i) {
172 d += (__isnormal)(v);
173 }
174
175 StopBenchmarkTiming();
176}
177#endif
178
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800179BENCHMARK_NO_ARG(BM_math_sin_fast);
180void BM_math_sin_fast::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100181 StartBenchmarkTiming();
Elliott Hughes02c78a32014-04-11 17:02:20 -0700182
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100183 d = 1.0;
184 for (int i = 0; i < iters; ++i) {
185 d += sin(d);
186 }
Elliott Hughes02c78a32014-04-11 17:02:20 -0700187
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100188 StopBenchmarkTiming();
189}
Elliott Hughes02c78a32014-04-11 17:02:20 -0700190
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800191BENCHMARK_NO_ARG(BM_math_sin_feupdateenv);
192void BM_math_sin_feupdateenv::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100193 StartBenchmarkTiming();
Elliott Hughes02c78a32014-04-11 17:02:20 -0700194
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100195 d = 1.0;
196 for (int i = 0; i < iters; ++i) {
197 fenv_t __libc_save_rm;
198 feholdexcept(&__libc_save_rm);
199 fesetround(FE_TONEAREST);
200 d += sin(d);
201 feupdateenv(&__libc_save_rm);
202 }
203
204 StopBenchmarkTiming();
205}
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100206
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800207BENCHMARK_NO_ARG(BM_math_sin_fesetenv);
208void BM_math_sin_fesetenv::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100209 StartBenchmarkTiming();
210
211 d = 1.0;
212 for (int i = 0; i < iters; ++i) {
213 fenv_t __libc_save_rm;
214 feholdexcept(&__libc_save_rm);
215 fesetround(FE_TONEAREST);
216 d += sin(d);
217 fesetenv(&__libc_save_rm);
218 }
219
220 StopBenchmarkTiming();
221}
Elliott Hughes02c78a32014-04-11 17:02:20 -0700222
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800223BENCHMARK_WITH_ARG(BM_math_fpclassify, double)->AT_COMMON_VALS;
224void BM_math_fpclassify::Run(int iters, double value) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700225 StartBenchmarkTiming();
226
227 d = 0.0;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800228 v = value;
Elliott Hughes02c78a32014-04-11 17:02:20 -0700229 for (int i = 0; i < iters; ++i) {
230 d += fpclassify(v);
231 }
232
233 StopBenchmarkTiming();
234}
Elliott Hughesb6622802015-08-14 14:04:30 -0700235
236BENCHMARK_WITH_ARG(BM_math_signbit_macro, double)->AT_COMMON_VALS;
237void BM_math_signbit_macro::Run(int iters, double value) {
238 StartBenchmarkTiming();
239
240 d = 0.0;
241 v = value;
242 for (int i = 0; i < iters; ++i) {
243 d += signbit(v);
244 }
245
246 StopBenchmarkTiming();
247}
248
249BENCHMARK_WITH_ARG(BM_math_signbit, double)->AT_COMMON_VALS;
250void BM_math_signbit::Run(int iters, double value) {
251 StartBenchmarkTiming();
252
253 d = 0.0;
254 v = value;
255 for (int i = 0; i < iters; ++i) {
256 d += (__signbit)(v);
257 }
258
259 StopBenchmarkTiming();
260}
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000261
262BENCHMARK_WITH_ARG(BM_math_fabs_macro, double)->AT_COMMON_VALS;
263void BM_math_fabs_macro::Run(int iters, double value) {
264 StartBenchmarkTiming();
265
266 d = 0.0;
267 v = value;
268 for (int i = 0; i < iters; ++i) {
269 d += fabs(v);
270 }
271
272 StopBenchmarkTiming();
273}
274
275BENCHMARK_WITH_ARG(BM_math_fabs, double)->AT_COMMON_VALS;
276void BM_math_fabs::Run(int iters, double value) {
277 StartBenchmarkTiming();
278
279 d = 0.0;
280 v = value;
281 for (int i = 0; i < iters; ++i) {
282 d += (fabs)(v);
283 }
284
285 StopBenchmarkTiming();
286}