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 "benchmark.h"
18
19#include <fenv.h>
20#include <math.h>
21
22// Avoid optimization.
23double d;
24double v;
25
26static void BM_math_sqrt(int iters) {
27  StartBenchmarkTiming();
28
29  d = 0.0;
30  v = 2.0;
31  for (int i = 0; i < iters; ++i) {
32    d += sqrt(v);
33  }
34
35  StopBenchmarkTiming();
36}
37BENCHMARK(BM_math_sqrt);
38
39static void BM_math_log10(int iters) {
40  StartBenchmarkTiming();
41
42  d = 0.0;
43  v = 1234.0;
44  for (int i = 0; i < iters; ++i) {
45    d += log10(v);
46  }
47
48  StopBenchmarkTiming();
49}
50BENCHMARK(BM_math_log10);
51
52static void BM_math_logb(int iters) {
53  StartBenchmarkTiming();
54
55  d = 0.0;
56  v = 1234.0;
57  for (int i = 0; i < iters; ++i) {
58    d += logb(v);
59  }
60
61  StopBenchmarkTiming();
62}
63BENCHMARK(BM_math_logb);
64
65static void BM_math_isinf_NORMAL(int iters) {
66  StartBenchmarkTiming();
67
68  d = 0.0;
69  v = 1234.0; // FP_NORMAL
70  for (int i = 0; i < iters; ++i) {
71    d += (isinf)(v);
72  }
73
74  StopBenchmarkTiming();
75}
76BENCHMARK(BM_math_isinf_NORMAL);
77
78static void BM_math_isinf_NAN(int iters) {
79  StartBenchmarkTiming();
80
81  d = 0.0;
82  v = nan(""); // FP_NAN
83  for (int i = 0; i < iters; ++i) {
84    d += (isinf)(v);
85  }
86
87  StopBenchmarkTiming();
88}
89BENCHMARK(BM_math_isinf_NAN);
90
91static void BM_math_isinf_INFINITE(int iters) {
92  StartBenchmarkTiming();
93
94  d = 0.0;
95  v = HUGE_VAL; // FP_INFINITE
96  for (int i = 0; i < iters; ++i) {
97    d += (isinf)(v);
98  }
99
100  StopBenchmarkTiming();
101}
102BENCHMARK(BM_math_isinf_INFINITE);
103
104static void BM_math_isinf_ZERO(int iters) {
105  StartBenchmarkTiming();
106
107  d = 0.0;
108  v = 0.0; // FP_ZERO
109  for (int i = 0; i < iters; ++i) {
110    d += (isinf)(v);
111  }
112
113  StopBenchmarkTiming();
114}
115BENCHMARK(BM_math_isinf_ZERO);
116
117static void BM_math_sin_fast(int iters) {
118  StartBenchmarkTiming();
119
120  d = 1.0;
121  for (int i = 0; i < iters; ++i) {
122    d += sin(d);
123  }
124
125  StopBenchmarkTiming();
126}
127BENCHMARK(BM_math_sin_fast);
128
129static void BM_math_sin_feupdateenv(int iters) {
130  StartBenchmarkTiming();
131
132  d = 1.0;
133  for (int i = 0; i < iters; ++i) {
134    fenv_t __libc_save_rm;
135    feholdexcept(&__libc_save_rm);
136    fesetround(FE_TONEAREST);
137    d += sin(d);
138    feupdateenv(&__libc_save_rm);
139  }
140
141  StopBenchmarkTiming();
142}
143BENCHMARK(BM_math_sin_feupdateenv);
144
145static void BM_math_sin_fesetenv(int iters) {
146  StartBenchmarkTiming();
147
148  d = 1.0;
149  for (int i = 0; i < iters; ++i) {
150    fenv_t __libc_save_rm;
151    feholdexcept(&__libc_save_rm);
152    fesetround(FE_TONEAREST);
153    d += sin(d);
154    fesetenv(&__libc_save_rm);
155  }
156
157  StopBenchmarkTiming();
158}
159BENCHMARK(BM_math_sin_fesetenv);
160
161static void BM_math_fpclassify_NORMAL(int iters) {
162  StartBenchmarkTiming();
163
164  d = 0.0;
165  v = 1234.0; // FP_NORMAL
166  for (int i = 0; i < iters; ++i) {
167    d += fpclassify(v);
168  }
169
170  StopBenchmarkTiming();
171}
172BENCHMARK(BM_math_fpclassify_NORMAL);
173
174static void BM_math_fpclassify_NAN(int iters) {
175  StartBenchmarkTiming();
176
177  d = 0.0;
178  v = nan(""); // FP_NAN
179  for (int i = 0; i < iters; ++i) {
180    d += fpclassify(v);
181  }
182
183  StopBenchmarkTiming();
184}
185BENCHMARK(BM_math_fpclassify_NAN);
186
187static void BM_math_fpclassify_INFINITE(int iters) {
188  StartBenchmarkTiming();
189
190  d = 0.0;
191  v = HUGE_VAL; // FP_INFINITE
192  for (int i = 0; i < iters; ++i) {
193    d += fpclassify(v);
194  }
195
196  StopBenchmarkTiming();
197}
198BENCHMARK(BM_math_fpclassify_INFINITE);
199
200static void BM_math_fpclassify_ZERO(int iters) {
201  StartBenchmarkTiming();
202
203  d = 0.0;
204  v = 0.0; // FP_ZERO
205  for (int i = 0; i < iters; ++i) {
206    d += fpclassify(v);
207  }
208
209  StopBenchmarkTiming();
210}
211BENCHMARK(BM_math_fpclassify_ZERO);
212