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 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 68 | BENCHMARK_WITH_ARG(BM_math_isinf, double)->AT_COMMON_VALS; |
| 69 | void BM_math_isinf::Run(int iters, double value) { |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 70 | StartBenchmarkTiming(); |
| 71 | |
| 72 | d = 0.0; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 73 | v = value; |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 74 | for (int i = 0; i < iters; ++i) { |
| 75 | d += (isinf)(v); |
| 76 | } |
| 77 | |
| 78 | StopBenchmarkTiming(); |
| 79 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 80 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 81 | BENCHMARK_NO_ARG(BM_math_sin_fast); |
| 82 | void BM_math_sin_fast::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 83 | StartBenchmarkTiming(); |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 84 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 85 | d = 1.0; |
| 86 | for (int i = 0; i < iters; ++i) { |
| 87 | d += sin(d); |
| 88 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 89 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 90 | StopBenchmarkTiming(); |
| 91 | } |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 92 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 93 | BENCHMARK_NO_ARG(BM_math_sin_feupdateenv); |
| 94 | void BM_math_sin_feupdateenv::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 95 | StartBenchmarkTiming(); |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 96 | |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 97 | 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 Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 108 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 109 | BENCHMARK_NO_ARG(BM_math_sin_fesetenv); |
| 110 | void BM_math_sin_fesetenv::Run(int iters) { |
Serban Constantinescu | a147a1d | 2014-06-08 16:55:22 +0100 | [diff] [blame] | 111 | 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 Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 124 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 125 | BENCHMARK_WITH_ARG(BM_math_fpclassify, double)->AT_COMMON_VALS; |
| 126 | void BM_math_fpclassify::Run(int iters, double value) { |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 127 | StartBenchmarkTiming(); |
| 128 | |
| 129 | d = 0.0; |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 130 | v = value; |
Elliott Hughes | 02c78a3 | 2014-04-11 17:02:20 -0700 | [diff] [blame] | 131 | for (int i = 0; i < iters; ++i) { |
| 132 | d += fpclassify(v); |
| 133 | } |
| 134 | |
| 135 | StopBenchmarkTiming(); |
| 136 | } |