blob: 7b9a2832119e8a5e59fd743b2e6ddc3b225f1be7 [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}
Anders Lewisa7b0f882017-07-24 20:01:13 -070070BIONIC_BENCHMARK(BM_math_isfinite_macro);
Elliott Hughesb6622802015-08-14 14:04:30 -070071
72#if defined(__BIONIC__)
73#define test_isfinite __isfinite
74#else
75#define test_isfinite __finite
76#endif
Elliott Hughes281e06b2016-02-17 10:23:52 -080077static void BM_math_isfinite(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -070078 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010079 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080080 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -070081 d += test_isfinite(v);
82 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080083 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -070084}
Anders Lewisa7b0f882017-07-24 20:01:13 -070085BIONIC_BENCHMARK(BM_math_isfinite);
Elliott Hughesb6622802015-08-14 14:04:30 -070086
Elliott Hughes281e06b2016-02-17 10:23:52 -080087static void BM_math_isinf_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -070088 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010089 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -080090 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -070091 d += isinf(v);
92 }
Elliott Hughes281e06b2016-02-17 10:23:52 -080093 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -070094}
Anders Lewisa7b0f882017-07-24 20:01:13 -070095BIONIC_BENCHMARK(BM_math_isinf_macro);
Elliott Hughesb6622802015-08-14 14:04:30 -070096
Elliott Hughes281e06b2016-02-17 10:23:52 -080097static void BM_math_isinf(benchmark::State& state) {
Elliott Hughes02c78a32014-04-11 17:02:20 -070098 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +010099 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800100 while (state.KeepRunning()) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700101 d += (isinf)(v);
102 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800103 SetLabel(state);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700104}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700105BIONIC_BENCHMARK(BM_math_isinf);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700106
Elliott Hughes281e06b2016-02-17 10:23:52 -0800107static void BM_math_isnan_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700108 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100109 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800110 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700111 d += isnan(v);
112 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800113 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700114}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700115BIONIC_BENCHMARK(BM_math_isnan_macro);
Elliott Hughesb6622802015-08-14 14:04:30 -0700116
Elliott Hughes281e06b2016-02-17 10:23:52 -0800117static void BM_math_isnan(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700118 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100119 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800120 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700121 d += (isnan)(v);
122 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800123 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700124}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700125BIONIC_BENCHMARK(BM_math_isnan);
Elliott Hughesb6622802015-08-14 14:04:30 -0700126
Elliott Hughes281e06b2016-02-17 10:23:52 -0800127static void BM_math_isnormal_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700128 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100129 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800130 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700131 d += isnormal(v);
132 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800133 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700134}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700135BIONIC_BENCHMARK(BM_math_isnormal_macro);
Elliott Hughesb6622802015-08-14 14:04:30 -0700136
137#if defined(__BIONIC__)
Elliott Hughes281e06b2016-02-17 10:23:52 -0800138static void BM_math_isnormal(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700139 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100140 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800141 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700142 d += (__isnormal)(v);
143 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800144 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700145}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700146BIONIC_BENCHMARK(BM_math_isnormal);
Elliott Hughesb6622802015-08-14 14:04:30 -0700147#endif
148
Elliott Hughes281e06b2016-02-17 10:23:52 -0800149static void BM_math_sin_fast(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100150 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800151 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100152 d += sin(d);
153 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100154}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700155BIONIC_BENCHMARK(BM_math_sin_fast);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700156
Elliott Hughes281e06b2016-02-17 10:23:52 -0800157static void BM_math_sin_feupdateenv(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100158 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800159 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100160 fenv_t __libc_save_rm;
161 feholdexcept(&__libc_save_rm);
162 fesetround(FE_TONEAREST);
163 d += sin(d);
164 feupdateenv(&__libc_save_rm);
165 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100166}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700167BIONIC_BENCHMARK(BM_math_sin_feupdateenv);
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100168
Elliott Hughes281e06b2016-02-17 10:23:52 -0800169static void BM_math_sin_fesetenv(benchmark::State& state) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100170 d = 1.0;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800171 while (state.KeepRunning()) {
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100172 fenv_t __libc_save_rm;
173 feholdexcept(&__libc_save_rm);
174 fesetround(FE_TONEAREST);
175 d += sin(d);
176 fesetenv(&__libc_save_rm);
177 }
Serban Constantinescua147a1d2014-06-08 16:55:22 +0100178}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700179BIONIC_BENCHMARK(BM_math_sin_fesetenv);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700180
Elliott Hughes281e06b2016-02-17 10:23:52 -0800181static void BM_math_fpclassify(benchmark::State& state) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700182 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100183 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800184 while (state.KeepRunning()) {
Elliott Hughes02c78a32014-04-11 17:02:20 -0700185 d += fpclassify(v);
186 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800187 SetLabel(state);
Elliott Hughes02c78a32014-04-11 17:02:20 -0700188}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700189BIONIC_BENCHMARK(BM_math_fpclassify);
Elliott Hughesb6622802015-08-14 14:04:30 -0700190
Elliott Hughes281e06b2016-02-17 10:23:52 -0800191static void BM_math_signbit_macro(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700192 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100193 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800194 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700195 d += signbit(v);
196 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800197 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700198}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700199BIONIC_BENCHMARK(BM_math_signbit_macro);
Elliott Hughesb6622802015-08-14 14:04:30 -0700200
Elliott Hughes281e06b2016-02-17 10:23:52 -0800201static void BM_math_signbit(benchmark::State& state) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700202 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100203 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800204 while (state.KeepRunning()) {
Elliott Hughesb6622802015-08-14 14:04:30 -0700205 d += (__signbit)(v);
206 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800207 SetLabel(state);
Elliott Hughesb6622802015-08-14 14:04:30 -0700208}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700209BIONIC_BENCHMARK(BM_math_signbit);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000210
Elliott Hughes281e06b2016-02-17 10:23:52 -0800211static void BM_math_fabs_macro(benchmark::State& state) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000212 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100213 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800214 while (state.KeepRunning()) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000215 d += fabs(v);
216 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800217 SetLabel(state);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000218}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700219BIONIC_BENCHMARK(BM_math_fabs_macro);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000220
Elliott Hughes281e06b2016-02-17 10:23:52 -0800221static void BM_math_fabs(benchmark::State& state) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000222 d = 0.0;
Martijn Coenenbe763d82016-11-14 14:16:08 +0100223 v = values[state.range(0)];
Elliott Hughes281e06b2016-02-17 10:23:52 -0800224 while (state.KeepRunning()) {
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000225 d += (fabs)(v);
226 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800227 SetLabel(state);
Elliott Hughesf9f4a432015-08-24 22:57:08 +0000228}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700229BIONIC_BENCHMARK(BM_math_fabs);