blob: ccb35557d23bfed6eadc25df862e8b8dbe88cd08 [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
Elliott Hughes281e06b2016-02-17 10:23:52 -080020#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070021#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080022
Elliott Hughes281e06b2016-02-17 10:23:52 -080023static const double values[] = { 1234.0, nan(""), HUGE_VAL, 0.0 };
24static const char* names[] = { "1234.0", "nan", "HUGE_VAL", "0.0" };
25
Elliott Hughes281e06b2016-02-17 10:23:52 -080026
27static void SetLabel(benchmark::State& state) {
Martijn Coenenbe763d82016-11-14 14:16:08 +010028 state.SetLabel(names[state.range(0)]);
Elliott Hughes281e06b2016-02-17 10:23:52 -080029}
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080030
Elliott Hughes9edb3e02013-02-06 15:47:09 -080031// Avoid optimization.
Dan Albert055a59c2014-09-25 15:43:48 -070032volatile double d;
33volatile double v;
Elliott Hughes9edb3e02013-02-06 15:47:09 -080034
Elliott Hughes281e06b2016-02-17 10:23:52 -080035static void BM_math_sqrt(benchmark::State& state) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080036 d = 0.0;
37 v = 2.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -080038 while (state.KeepRunning()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080039 d += sqrt(v);
40 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -080041}
Anders Lewisa7b0f882017-07-24 20:01:13 -070042BIONIC_BENCHMARK(BM_math_sqrt);
Elliott Hughes9edb3e02013-02-06 15:47:09 -080043
Elliott Hughes281e06b2016-02-17 10:23:52 -080044static void BM_math_log10(benchmark::State& state) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080045 d = 0.0;
46 v = 1234.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -080047 while (state.KeepRunning()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080048 d += log10(v);
49 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -080050}
Anders Lewisa7b0f882017-07-24 20:01:13 -070051BIONIC_BENCHMARK(BM_math_log10);
Elliott Hughes9edb3e02013-02-06 15:47:09 -080052
Elliott Hughes281e06b2016-02-17 10:23:52 -080053static void BM_math_logb(benchmark::State& state) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080054 d = 0.0;
55 v = 1234.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -080056 while (state.KeepRunning()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -080057 d += logb(v);
58 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -080059}
Anders Lewisa7b0f882017-07-24 20:01:13 -070060BIONIC_BENCHMARK(BM_math_logb);
Elliott Hughes02c78a32014-04-11 17:02:20 -070061
Elliott Hughes281e06b2016-02-17 10:23:52 -080062static void BM_math_isfinite_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -070063 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010064 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080065 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -070066 d += isfinite(v);
67 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080068 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -070069}
Christopher Ferris858e3362017-11-30 08:53:15 -080070BIONIC_BENCHMARK_WITH_ARG(BM_math_isfinite_macro, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -070071
Elliott Hughes281e06b2016-02-17 10:23:52 -080072static void BM_math_isfinite(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -070073 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010074 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080075 while (state.KeepRunning()) {
Elliott Hughes5c6a7bf2017-10-19 13:56:28 -070076 d += isfinite(v);
Elliott Hughesb6622802015-08-14 14:04:30 -070077 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080078 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -070079}
Christopher Ferris858e3362017-11-30 08:53:15 -080080BIONIC_BENCHMARK_WITH_ARG(BM_math_isfinite, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -070081
Elliott Hughes281e06b2016-02-17 10:23:52 -080082static void BM_math_isinf_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -070083 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010084 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080085 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -070086 d += isinf(v);
87 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080088 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -070089}
Christopher Ferris858e3362017-11-30 08:53:15 -080090BIONIC_BENCHMARK_WITH_ARG(BM_math_isinf_macro, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -070091
Elliott Hughes281e06b2016-02-17 10:23:52 -080092static void BM_math_isinf(benchmark::State& state) {
Elliott Hughes02c78a32014-04-11 17:02:20 -070093 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010094 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080095 while (state.KeepRunning()) {
Elliott Hughes02c78a32014-04-11 17:02:20 -070096 d += (isinf)(v);
97 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080098 SetLabel(state);
Elliott Hughes02c78a32014-04-11 17:02:20 -070099}
Christopher Ferris858e3362017-11-30 08:53:15 -0800100BIONIC_BENCHMARK_WITH_ARG(BM_math_isinf, "MATH_COMMON");
Elliott Hughes02c78a32014-04-11 17:02:20 -0700101
Elliott Hughes281e06b2016-02-17 10:23:52 -0800102static void BM_math_isnan_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700103 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100104 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800105 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700106 d += isnan(v);
107 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800108 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700109}
Christopher Ferris858e3362017-11-30 08:53:15 -0800110BIONIC_BENCHMARK_WITH_ARG(BM_math_isnan_macro, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700111
Elliott Hughes281e06b2016-02-17 10:23:52 -0800112static void BM_math_isnan(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700113 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100114 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800115 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700116 d += (isnan)(v);
117 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800118 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700119}
Christopher Ferris858e3362017-11-30 08:53:15 -0800120BIONIC_BENCHMARK_WITH_ARG(BM_math_isnan, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700121
Elliott Hughes281e06b2016-02-17 10:23:52 -0800122static void BM_math_isnormal_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700123 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100124 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800125 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700126 d += isnormal(v);
127 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800128 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700129}
Christopher Ferris858e3362017-11-30 08:53:15 -0800130BIONIC_BENCHMARK_WITH_ARG(BM_math_isnormal_macro, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700131
Elliott Hughes281e06b2016-02-17 10:23:52 -0800132static void BM_math_isnormal(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700133 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100134 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800135 while (state.KeepRunning()) {
Elliott Hughes5c6a7bf2017-10-19 13:56:28 -0700136 d += isnormal(v);
Elliott Hughesb6622802015-08-14 14:04:30 -0700137 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800138 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700139}
Christopher Ferris858e3362017-11-30 08:53:15 -0800140BIONIC_BENCHMARK_WITH_ARG(BM_math_isnormal, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700141
Elliott Hughes281e06b2016-02-17 10:23:52 -0800142static void BM_math_sin_fast(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100143 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800144 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100145 d += sin(d);
146 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100147}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700148BIONIC_BENCHMARK(BM_math_sin_fast);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700149
Elliott Hughes281e06b2016-02-17 10:23:52 -0800150static void BM_math_sin_feupdateenv(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100151 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800152 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100153 fenv_t __libc_save_rm;
154 feholdexcept(&__libc_save_rm);
155 fesetround(FE_TONEAREST);
156 d += sin(d);
157 feupdateenv(&__libc_save_rm);
158 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100159}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700160BIONIC_BENCHMARK(BM_math_sin_feupdateenv);
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100161
Elliott Hughes281e06b2016-02-17 10:23:52 -0800162static void BM_math_sin_fesetenv(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100163 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800164 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100165 fenv_t __libc_save_rm;
166 feholdexcept(&__libc_save_rm);
167 fesetround(FE_TONEAREST);
168 d += sin(d);
169 fesetenv(&__libc_save_rm);
170 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100171}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700172BIONIC_BENCHMARK(BM_math_sin_fesetenv);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700173
Elliott Hughes281e06b2016-02-17 10:23:52 -0800174static void BM_math_fpclassify(benchmark::State& state) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700175 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100176 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800177 while (state.KeepRunning()) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700178 d += fpclassify(v);
179 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800180 SetLabel(state);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700181}
Christopher Ferris858e3362017-11-30 08:53:15 -0800182BIONIC_BENCHMARK_WITH_ARG(BM_math_fpclassify, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700183
Elliott Hughes281e06b2016-02-17 10:23:52 -0800184static void BM_math_signbit_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700185 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100186 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800187 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700188 d += signbit(v);
189 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800190 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700191}
Christopher Ferris858e3362017-11-30 08:53:15 -0800192BIONIC_BENCHMARK_WITH_ARG(BM_math_signbit_macro, "MATH_COMMON");
Elliott Hughesb6622802015-08-14 14:04:30 -0700193
Elliott Hughes281e06b2016-02-17 10:23:52 -0800194static void BM_math_signbit(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700195 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100196 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800197 while (state.KeepRunning()) {
Elliott Hughes5c6a7bf2017-10-19 13:56:28 -0700198 d += signbit(v);
Elliott Hughesb6622802015-08-14 14:04:30 -0700199 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800200 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700201}
Christopher Ferris858e3362017-11-30 08:53:15 -0800202BIONIC_BENCHMARK_WITH_ARG(BM_math_signbit, "MATH_COMMON");
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000203
Elliott Hughes281e06b2016-02-17 10:23:52 -0800204static void BM_math_fabs_macro(benchmark::State& state) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000205 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100206 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800207 while (state.KeepRunning()) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000208 d += fabs(v);
209 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800210 SetLabel(state);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000211}
Christopher Ferris858e3362017-11-30 08:53:15 -0800212BIONIC_BENCHMARK_WITH_ARG(BM_math_fabs_macro, "MATH_COMMON");
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000213
Elliott Hughes281e06b2016-02-17 10:23:52 -0800214static void BM_math_fabs(benchmark::State& state) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000215 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100216 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800217 while (state.KeepRunning()) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000218 d += (fabs)(v);
219 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800220 SetLabel(state);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000221}
Christopher Ferris858e3362017-11-30 08:53:15 -0800222BIONIC_BENCHMARK_WITH_ARG(BM_math_fabs, "MATH_COMMON");
Elliott Hughese332f652018-05-08 15:07:43 -0700223
224static void BM_math_sincos(benchmark::State& state) {
225 d = 1.0;
226 while (state.KeepRunning()) {
227 double s, c;
228 sincos(d, &s, &c);
229 d += s + c;
230 }
231}
232BIONIC_BENCHMARK(BM_math_sincos);