Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 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 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 17 | #include <fenv.h> |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 18 | #include <math.h> |
| 19 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 20 | #include <benchmark/Benchmark.h> |
| 21 | |
| 22 | #define AT_COMMON_VALS \ |
| 23 | Arg(1234.0)->Arg(nan(""))->Arg(HUGE_VAL)->Arg(0.0) |
| 24 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 25 | // Avoid optimization. |
Dan Albert | 055a59c | 2014-09-25 15:43:48 -0700 | [diff] [blame] | 26 | volatile double d; |
| 27 | volatile double v; |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 28 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 29 | BENCHMARK_NO_ARG(BM_math_sqrt); |
| 30 | void BM_math_sqrt::Run(int iters) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 31 | 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 Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 41 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 42 | BENCHMARK_NO_ARG(BM_math_log10); |
| 43 | void BM_math_log10::Run(int iters) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 44 | 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 Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 54 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 55 | BENCHMARK_NO_ARG(BM_math_logb); |
| 56 | void BM_math_logb::Run(int iters) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 57 | 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 Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | b662280 | 2015-08-14 14:04:30 -0700 | [diff] [blame] | 68 | BENCHMARK_WITH_ARG(BM_math_isfinite_macro, double)->AT_COMMON_VALS; |
| 69 | void BM_math_isfinite_macro::Run(int iters, double value) { |
| 70 | StartBenchmarkTiming(); |
| 71 | |
| 72 | d = 0.0; |
| 73 | v = value; |
| 74 | for (int i = 0; i < iters; ++i) { |
| 75 | d += isfinite(v); |
| 76 | } |
| 77 | |
| 78 | StopBenchmarkTiming(); |
| 79 | } |
| 80 | |
| 81 | #if defined(__BIONIC__) |
| 82 | #define test_isfinite __isfinite |
| 83 | #else |
| 84 | #define test_isfinite __finite |
| 85 | #endif |
| 86 | BENCHMARK_WITH_ARG(BM_math_isfinite, double)->AT_COMMON_VALS; |
| 87 | void BM_math_isfinite::Run(int iters, double value) { |
| 88 | StartBenchmarkTiming(); |
| 89 | |
| 90 | d = 0.0; |
| 91 | v = value; |
| 92 | for (int i = 0; i < iters; ++i) { |
| 93 | d += test_isfinite(v); |
| 94 | } |
| 95 | |
| 96 | StopBenchmarkTiming(); |
| 97 | } |
| 98 | |
| 99 | BENCHMARK_WITH_ARG(BM_math_isinf_macro, double)->AT_COMMON_VALS; |
| 100 | void BM_math_isinf_macro::Run(int iters, double value) { |
| 101 | StartBenchmarkTiming(); |
| 102 | |
| 103 | d = 0.0; |
| 104 | v = value; |
| 105 | for (int i = 0; i < iters; ++i) { |
| 106 | d += isinf(v); |
| 107 | } |
| 108 | |
| 109 | StopBenchmarkTiming(); |
| 110 | } |
| 111 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 112 | BENCHMARK_WITH_ARG(BM_math_isinf, double)->AT_COMMON_VALS; |
| 113 | void BM_math_isinf::Run(int iters, double value) { |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 114 | StartBenchmarkTiming(); |
| 115 | |
| 116 | d = 0.0; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 117 | v = value; |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 118 | for (int i = 0; i < iters; ++i) { |
| 119 | d += (isinf)(v); |
| 120 | } |
| 121 | |
| 122 | StopBenchmarkTiming(); |
| 123 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 124 | |
Elliott Hughes | b662280 | 2015-08-14 14:04:30 -0700 | [diff] [blame] | 125 | BENCHMARK_WITH_ARG(BM_math_isnan_macro, double)->AT_COMMON_VALS; |
| 126 | void BM_math_isnan_macro::Run(int iters, double value) { |
| 127 | StartBenchmarkTiming(); |
| 128 | |
| 129 | d = 0.0; |
| 130 | v = value; |
| 131 | for (int i = 0; i < iters; ++i) { |
| 132 | d += isnan(v); |
| 133 | } |
| 134 | |
| 135 | StopBenchmarkTiming(); |
| 136 | } |
| 137 | |
| 138 | BENCHMARK_WITH_ARG(BM_math_isnan, double)->AT_COMMON_VALS; |
| 139 | void BM_math_isnan::Run(int iters, double value) { |
| 140 | StartBenchmarkTiming(); |
| 141 | |
| 142 | d = 0.0; |
| 143 | v = value; |
| 144 | for (int i = 0; i < iters; ++i) { |
| 145 | d += (isnan)(v); |
| 146 | } |
| 147 | |
| 148 | StopBenchmarkTiming(); |
| 149 | } |
| 150 | |
| 151 | BENCHMARK_WITH_ARG(BM_math_isnormal_macro, double)->AT_COMMON_VALS; |
| 152 | void BM_math_isnormal_macro::Run(int iters, double value) { |
| 153 | StartBenchmarkTiming(); |
| 154 | |
| 155 | d = 0.0; |
| 156 | v = value; |
| 157 | for (int i = 0; i < iters; ++i) { |
| 158 | d += isnormal(v); |
| 159 | } |
| 160 | |
| 161 | StopBenchmarkTiming(); |
| 162 | } |
| 163 | |
| 164 | #if defined(__BIONIC__) |
| 165 | BENCHMARK_WITH_ARG(BM_math_isnormal, double)->AT_COMMON_VALS; |
| 166 | void BM_math_isnormal::Run(int iters, double value) { |
| 167 | StartBenchmarkTiming(); |
| 168 | |
| 169 | d = 0.0; |
| 170 | v = value; |
| 171 | for (int i = 0; i < iters; ++i) { |
| 172 | d += (__isnormal)(v); |
| 173 | } |
| 174 | |
| 175 | StopBenchmarkTiming(); |
| 176 | } |
| 177 | #endif |
| 178 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 179 | BENCHMARK_NO_ARG(BM_math_sin_fast); |
| 180 | void BM_math_sin_fast::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 181 | StartBenchmarkTiming(); |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 182 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 183 | d = 1.0; |
| 184 | for (int i = 0; i < iters; ++i) { |
| 185 | d += sin(d); |
| 186 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 187 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 188 | StopBenchmarkTiming(); |
| 189 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 190 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 191 | BENCHMARK_NO_ARG(BM_math_sin_feupdateenv); |
| 192 | void BM_math_sin_feupdateenv::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 193 | StartBenchmarkTiming(); |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 194 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 195 | d = 1.0; |
| 196 | for (int i = 0; i < iters; ++i) { |
| 197 | fenv_t __libc_save_rm; |
| 198 | feholdexcept(&__libc_save_rm); |
| 199 | fesetround(FE_TONEAREST); |
| 200 | d += sin(d); |
| 201 | feupdateenv(&__libc_save_rm); |
| 202 | } |
| 203 | |
| 204 | StopBenchmarkTiming(); |
| 205 | } |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 206 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 207 | BENCHMARK_NO_ARG(BM_math_sin_fesetenv); |
| 208 | void BM_math_sin_fesetenv::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 209 | StartBenchmarkTiming(); |
| 210 | |
| 211 | d = 1.0; |
| 212 | for (int i = 0; i < iters; ++i) { |
| 213 | fenv_t __libc_save_rm; |
| 214 | feholdexcept(&__libc_save_rm); |
| 215 | fesetround(FE_TONEAREST); |
| 216 | d += sin(d); |
| 217 | fesetenv(&__libc_save_rm); |
| 218 | } |
| 219 | |
| 220 | StopBenchmarkTiming(); |
| 221 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 222 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 223 | BENCHMARK_WITH_ARG(BM_math_fpclassify, double)->AT_COMMON_VALS; |
| 224 | void BM_math_fpclassify::Run(int iters, double value) { |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 225 | StartBenchmarkTiming(); |
| 226 | |
| 227 | d = 0.0; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 228 | v = value; |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 229 | for (int i = 0; i < iters; ++i) { |
| 230 | d += fpclassify(v); |
| 231 | } |
| 232 | |
| 233 | StopBenchmarkTiming(); |
| 234 | } |
Elliott Hughes | b662280 | 2015-08-14 14:04:30 -0700 | [diff] [blame] | 235 | |
| 236 | BENCHMARK_WITH_ARG(BM_math_signbit_macro, double)->AT_COMMON_VALS; |
| 237 | void BM_math_signbit_macro::Run(int iters, double value) { |
| 238 | StartBenchmarkTiming(); |
| 239 | |
| 240 | d = 0.0; |
| 241 | v = value; |
| 242 | for (int i = 0; i < iters; ++i) { |
| 243 | d += signbit(v); |
| 244 | } |
| 245 | |
| 246 | StopBenchmarkTiming(); |
| 247 | } |
| 248 | |
| 249 | BENCHMARK_WITH_ARG(BM_math_signbit, double)->AT_COMMON_VALS; |
| 250 | void BM_math_signbit::Run(int iters, double value) { |
| 251 | StartBenchmarkTiming(); |
| 252 | |
| 253 | d = 0.0; |
| 254 | v = value; |
| 255 | for (int i = 0; i < iters; ++i) { |
| 256 | d += (__signbit)(v); |
| 257 | } |
| 258 | |
| 259 | StopBenchmarkTiming(); |
| 260 | } |
Elliott Hughes | f9f4a43 | 2015-08-24 22:57:08 +0000 | [diff] [blame] | 261 | |
| 262 | BENCHMARK_WITH_ARG(BM_math_fabs_macro, double)->AT_COMMON_VALS; |
| 263 | void BM_math_fabs_macro::Run(int iters, double value) { |
| 264 | StartBenchmarkTiming(); |
| 265 | |
| 266 | d = 0.0; |
| 267 | v = value; |
| 268 | for (int i = 0; i < iters; ++i) { |
| 269 | d += fabs(v); |
| 270 | } |
| 271 | |
| 272 | StopBenchmarkTiming(); |
| 273 | } |
| 274 | |
| 275 | BENCHMARK_WITH_ARG(BM_math_fabs, double)->AT_COMMON_VALS; |
| 276 | void BM_math_fabs::Run(int iters, double value) { |
| 277 | StartBenchmarkTiming(); |
| 278 | |
| 279 | d = 0.0; |
| 280 | v = value; |
| 281 | for (int i = 0; i < iters; ++i) { |
| 282 | d += (fabs)(v); |
| 283 | } |
| 284 | |
| 285 | StopBenchmarkTiming(); |
| 286 | } |