1 /*
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
17 #include <fenv.h>
18 #include <math.h>
19
20 #include <benchmark/benchmark.h>
21 #include "util.h"
22
23 static const double values[] = { 1234.0, nan(""), HUGE_VAL, 0.0 };
24 static const char* names[] = { "1234.0", "nan", "HUGE_VAL", "0.0" };
25
26
SetLabel(benchmark::State & state)27 static void SetLabel(benchmark::State& state) {
28 state.SetLabel(names[state.range(0)]);
29 }
30
31 // Avoid optimization.
32 volatile double d;
33 volatile double v;
34
BM_math_sqrt(benchmark::State & state)35 static void BM_math_sqrt(benchmark::State& state) {
36 d = 0.0;
37 v = 2.0;
38 while (state.KeepRunning()) {
39 d += sqrt(v);
40 }
41 }
42 BIONIC_BENCHMARK(BM_math_sqrt);
43
BM_math_log10(benchmark::State & state)44 static void BM_math_log10(benchmark::State& state) {
45 d = 0.0;
46 v = 1234.0;
47 while (state.KeepRunning()) {
48 d += log10(v);
49 }
50 }
51 BIONIC_BENCHMARK(BM_math_log10);
52
BM_math_logb(benchmark::State & state)53 static void BM_math_logb(benchmark::State& state) {
54 d = 0.0;
55 v = 1234.0;
56 while (state.KeepRunning()) {
57 d += logb(v);
58 }
59 }
60 BIONIC_BENCHMARK(BM_math_logb);
61
BM_math_isfinite_macro(benchmark::State & state)62 static void BM_math_isfinite_macro(benchmark::State& state) {
63 d = 0.0;
64 v = values[state.range(0)];
65 while (state.KeepRunning()) {
66 d += isfinite(v);
67 }
68 SetLabel(state);
69 }
70 BIONIC_BENCHMARK_WITH_ARG(BM_math_isfinite_macro, "MATH_COMMON");
71
BM_math_isfinite(benchmark::State & state)72 static void BM_math_isfinite(benchmark::State& state) {
73 d = 0.0;
74 v = values[state.range(0)];
75 while (state.KeepRunning()) {
76 d += isfinite(v);
77 }
78 SetLabel(state);
79 }
80 BIONIC_BENCHMARK_WITH_ARG(BM_math_isfinite, "MATH_COMMON");
81
BM_math_isinf_macro(benchmark::State & state)82 static void BM_math_isinf_macro(benchmark::State& state) {
83 d = 0.0;
84 v = values[state.range(0)];
85 while (state.KeepRunning()) {
86 d += isinf(v);
87 }
88 SetLabel(state);
89 }
90 BIONIC_BENCHMARK_WITH_ARG(BM_math_isinf_macro, "MATH_COMMON");
91
BM_math_isinf(benchmark::State & state)92 static void BM_math_isinf(benchmark::State& state) {
93 d = 0.0;
94 v = values[state.range(0)];
95 while (state.KeepRunning()) {
96 d += (isinf)(v);
97 }
98 SetLabel(state);
99 }
100 BIONIC_BENCHMARK_WITH_ARG(BM_math_isinf, "MATH_COMMON");
101
BM_math_isnan_macro(benchmark::State & state)102 static void BM_math_isnan_macro(benchmark::State& state) {
103 d = 0.0;
104 v = values[state.range(0)];
105 while (state.KeepRunning()) {
106 d += isnan(v);
107 }
108 SetLabel(state);
109 }
110 BIONIC_BENCHMARK_WITH_ARG(BM_math_isnan_macro, "MATH_COMMON");
111
BM_math_isnan(benchmark::State & state)112 static void BM_math_isnan(benchmark::State& state) {
113 d = 0.0;
114 v = values[state.range(0)];
115 while (state.KeepRunning()) {
116 d += (isnan)(v);
117 }
118 SetLabel(state);
119 }
120 BIONIC_BENCHMARK_WITH_ARG(BM_math_isnan, "MATH_COMMON");
121
BM_math_isnormal_macro(benchmark::State & state)122 static void BM_math_isnormal_macro(benchmark::State& state) {
123 d = 0.0;
124 v = values[state.range(0)];
125 while (state.KeepRunning()) {
126 d += isnormal(v);
127 }
128 SetLabel(state);
129 }
130 BIONIC_BENCHMARK_WITH_ARG(BM_math_isnormal_macro, "MATH_COMMON");
131
BM_math_isnormal(benchmark::State & state)132 static void BM_math_isnormal(benchmark::State& state) {
133 d = 0.0;
134 v = values[state.range(0)];
135 while (state.KeepRunning()) {
136 d += isnormal(v);
137 }
138 SetLabel(state);
139 }
140 BIONIC_BENCHMARK_WITH_ARG(BM_math_isnormal, "MATH_COMMON");
141
BM_math_sin_fast(benchmark::State & state)142 static void BM_math_sin_fast(benchmark::State& state) {
143 d = 1.0;
144 while (state.KeepRunning()) {
145 d += sin(d);
146 }
147 }
148 BIONIC_BENCHMARK(BM_math_sin_fast);
149
BM_math_sin_feupdateenv(benchmark::State & state)150 static void BM_math_sin_feupdateenv(benchmark::State& state) {
151 d = 1.0;
152 while (state.KeepRunning()) {
153 fenv_t __libc_save_rm;
154 feholdexcept(&__libc_save_rm);
155 fesetround(FE_TONEAREST);
156 d += sin(d);
157 feupdateenv(&__libc_save_rm);
158 }
159 }
160 BIONIC_BENCHMARK(BM_math_sin_feupdateenv);
161
BM_math_sin_fesetenv(benchmark::State & state)162 static void BM_math_sin_fesetenv(benchmark::State& state) {
163 d = 1.0;
164 while (state.KeepRunning()) {
165 fenv_t __libc_save_rm;
166 feholdexcept(&__libc_save_rm);
167 fesetround(FE_TONEAREST);
168 d += sin(d);
169 fesetenv(&__libc_save_rm);
170 }
171 }
172 BIONIC_BENCHMARK(BM_math_sin_fesetenv);
173
BM_math_fpclassify(benchmark::State & state)174 static void BM_math_fpclassify(benchmark::State& state) {
175 d = 0.0;
176 v = values[state.range(0)];
177 while (state.KeepRunning()) {
178 d += fpclassify(v);
179 }
180 SetLabel(state);
181 }
182 BIONIC_BENCHMARK_WITH_ARG(BM_math_fpclassify, "MATH_COMMON");
183
BM_math_signbit_macro(benchmark::State & state)184 static void BM_math_signbit_macro(benchmark::State& state) {
185 d = 0.0;
186 v = values[state.range(0)];
187 while (state.KeepRunning()) {
188 d += signbit(v);
189 }
190 SetLabel(state);
191 }
192 BIONIC_BENCHMARK_WITH_ARG(BM_math_signbit_macro, "MATH_COMMON");
193
BM_math_signbit(benchmark::State & state)194 static void BM_math_signbit(benchmark::State& state) {
195 d = 0.0;
196 v = values[state.range(0)];
197 while (state.KeepRunning()) {
198 d += signbit(v);
199 }
200 SetLabel(state);
201 }
202 BIONIC_BENCHMARK_WITH_ARG(BM_math_signbit, "MATH_COMMON");
203
BM_math_fabs_macro(benchmark::State & state)204 static void BM_math_fabs_macro(benchmark::State& state) {
205 d = 0.0;
206 v = values[state.range(0)];
207 while (state.KeepRunning()) {
208 d += fabs(v);
209 }
210 SetLabel(state);
211 }
212 BIONIC_BENCHMARK_WITH_ARG(BM_math_fabs_macro, "MATH_COMMON");
213
BM_math_fabs(benchmark::State & state)214 static void BM_math_fabs(benchmark::State& state) {
215 d = 0.0;
216 v = values[state.range(0)];
217 while (state.KeepRunning()) {
218 d += (fabs)(v);
219 }
220 SetLabel(state);
221 }
222 BIONIC_BENCHMARK_WITH_ARG(BM_math_fabs, "MATH_COMMON");
223