blob: 4de28d1b3d6fd39e69daf765080038c33251d3cb [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
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080068BENCHMARK_WITH_ARG(BM_math_isinf, double)->AT_COMMON_VALS;
69void BM_math_isinf::Run(int iters, double value) {
Elliott Hughes02c78a32014-04-11 17:02:20 -070070 StartBenchmarkTiming();
71
72 d = 0.0;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080073 v = value;
Elliott Hughes02c78a32014-04-11 17:02:20 -070074 for (int i = 0; i < iters; ++i) {
75 d += (isinf)(v);
76 }
77
78 StopBenchmarkTiming();
79}
Elliott Hughes02c78a32014-04-11 17:02:20 -070080
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080081BENCHMARK_NO_ARG(BM_math_sin_fast);
82void BM_math_sin_fast::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +010083 StartBenchmarkTiming();
Elliott Hughes02c78a32014-04-11 17:02:20 -070084
Serban Constantinescua147a1d2014-06-08 16:55:22 +010085 d = 1.0;
86 for (int i = 0; i < iters; ++i) {
87 d += sin(d);
88 }
Elliott Hughes02c78a32014-04-11 17:02:20 -070089
Serban Constantinescua147a1d2014-06-08 16:55:22 +010090 StopBenchmarkTiming();
91}
Elliott Hughes02c78a32014-04-11 17:02:20 -070092
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080093BENCHMARK_NO_ARG(BM_math_sin_feupdateenv);
94void BM_math_sin_feupdateenv::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +010095 StartBenchmarkTiming();
Elliott Hughes02c78a32014-04-11 17:02:20 -070096
Serban Constantinescua147a1d2014-06-08 16:55:22 +010097 d = 1.0;
98 for (int i = 0; i < iters; ++i) {
99 fenv_t __libc_save_rm;
100 feholdexcept(&__libc_save_rm);
101 fesetround(FE_TONEAREST);
102 d += sin(d);
103 feupdateenv(&__libc_save_rm);
104 }
105
106 StopBenchmarkTiming();
107}
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100108
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800109BENCHMARK_NO_ARG(BM_math_sin_fesetenv);
110void BM_math_sin_fesetenv::Run(int iters) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100111 StartBenchmarkTiming();
112
113 d = 1.0;
114 for (int i = 0; i < iters; ++i) {
115 fenv_t __libc_save_rm;
116 feholdexcept(&__libc_save_rm);
117 fesetround(FE_TONEAREST);
118 d += sin(d);
119 fesetenv(&__libc_save_rm);
120 }
121
122 StopBenchmarkTiming();
123}
Elliott Hughes02c78a32014-04-11 17:02:20 -0700124
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800125BENCHMARK_WITH_ARG(BM_math_fpclassify, double)->AT_COMMON_VALS;
126void BM_math_fpclassify::Run(int iters, double value) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700127 StartBenchmarkTiming();
128
129 d = 0.0;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800130 v = value;
Elliott Hughes02c78a32014-04-11 17:02:20 -0700131 for (int i = 0; i < iters; ++i) {
132 d += fpclassify(v);
133 }
134
135 StopBenchmarkTiming();
136}