epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "bench/Benchmark.h" |
| 8 | #include "include/core/SkRect.h" |
| 9 | #include "include/core/SkString.h" |
| 10 | #include "include/private/SkFloatBits.h" |
| 11 | #include "include/utils/SkRandom.h" |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 12 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | class ScalarBench : public Benchmark { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 14 | SkString fName; |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 15 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 16 | ScalarBench(const char name[]) { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 17 | fName.printf("scalar_%s", name); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 18 | } |
| 19 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 20 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 21 | return backend == kNonRendering_Backend; |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | virtual void performTest() = 0; |
| 25 | |
| 26 | protected: |
| 27 | virtual int mulLoopCount() const { return 1; } |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | const char* onGetName() override { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 30 | return fName.c_str(); |
| 31 | } |
| 32 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 33 | void onDraw(int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 34 | for (int i = 0; i < loops; i++) { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 35 | this->performTest(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 40 | typedef Benchmark INHERITED; |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 43 | // having unknown values in our arrays can throw off the timing a lot, perhaps |
| 44 | // handling NaN values is a lot slower. Anyway, this guy is just meant to put |
| 45 | // reasonable values in our arrays. |
| 46 | template <typename T> void init9(T array[9]) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 47 | SkRandom rand; |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 48 | for (int i = 0; i < 9; i++) { |
| 49 | array[i] = rand.nextSScalar1(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | class FloatComparisonBench : public ScalarBench { |
| 54 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 55 | FloatComparisonBench() : INHERITED("compare_float") { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 56 | init9(fArray); |
| 57 | } |
| 58 | protected: |
| 59 | virtual int mulLoopCount() const { return 4; } |
| 60 | virtual void performTest() { |
mtklein | 80f709a | 2014-06-02 12:41:07 -0700 | [diff] [blame] | 61 | // xoring into a volatile prevents the compiler from optimizing these checks away. |
| 62 | volatile bool junk = false; |
| 63 | junk ^= (fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f); |
| 64 | junk ^= (fArray[2] != 0.0f || fArray[5] != 0.0f); |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 65 | } |
| 66 | private: |
| 67 | float fArray[9]; |
| 68 | typedef ScalarBench INHERITED; |
| 69 | }; |
| 70 | |
| 71 | class ForcedIntComparisonBench : public ScalarBench { |
| 72 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 73 | ForcedIntComparisonBench() |
| 74 | : INHERITED("compare_forced_int") { |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 75 | init9(fArray); |
| 76 | } |
| 77 | protected: |
| 78 | virtual int mulLoopCount() const { return 4; } |
| 79 | virtual void performTest() { |
mtklein | 80f709a | 2014-06-02 12:41:07 -0700 | [diff] [blame] | 80 | // xoring into a volatile prevents the compiler from optimizing these checks away. |
| 81 | volatile int32_t junk = 0; |
| 82 | junk ^= (SkScalarAs2sCompliment(fArray[6]) | |
| 83 | SkScalarAs2sCompliment(fArray[7]) | |
| 84 | (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int)); |
| 85 | junk ^= (SkScalarAs2sCompliment(fArray[2]) | |
| 86 | SkScalarAs2sCompliment(fArray[5])); |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 87 | } |
| 88 | private: |
| 89 | static const int32_t kPersp1Int = 0x3f800000; |
tomhudson@google.com | 25583a3 | 2011-06-06 17:55:11 +0000 | [diff] [blame] | 90 | SkScalar fArray[9]; |
tomhudson@google.com | f910b36 | 2011-06-06 15:16:31 +0000 | [diff] [blame] | 91 | typedef ScalarBench INHERITED; |
| 92 | }; |
| 93 | |
reed@google.com | 357818c | 2012-06-13 12:30:35 +0000 | [diff] [blame] | 94 | class IsFiniteScalarBench : public ScalarBench { |
| 95 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 96 | IsFiniteScalarBench() : INHERITED("isfinite") { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 97 | SkRandom rand; |
reed@google.com | 357818c | 2012-06-13 12:30:35 +0000 | [diff] [blame] | 98 | for (size_t i = 0; i < ARRAY_N; ++i) { |
| 99 | fArray[i] = rand.nextSScalar1(); |
| 100 | } |
| 101 | } |
| 102 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 103 | int mulLoopCount() const override { return 1; } |
| 104 | void performTest() override { |
reed@google.com | 357818c | 2012-06-13 12:30:35 +0000 | [diff] [blame] | 105 | int sum = 0; |
| 106 | for (size_t i = 0; i < ARRAY_N; ++i) { |
| 107 | // We pass -fArray[i], so the compiler can't cheat and treat the |
| 108 | // value as an int (even though we tell it that it is a float) |
| 109 | sum += SkScalarIsFinite(-fArray[i]); |
| 110 | } |
| 111 | // we do this so the compiler won't optimize our loop away... |
| 112 | this->doSomething(fArray, sum); |
| 113 | } |
| 114 | |
| 115 | virtual void doSomething(SkScalar array[], int sum) {} |
| 116 | private: |
| 117 | enum { |
| 118 | ARRAY_N = 64 |
| 119 | }; |
| 120 | SkScalar fArray[ARRAY_N]; |
| 121 | |
| 122 | typedef ScalarBench INHERITED; |
| 123 | }; |
| 124 | |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 127 | class RectBoundsBench : public Benchmark { |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 128 | enum { |
| 129 | PTS = 100, |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 130 | }; |
| 131 | SkPoint fPts[PTS]; |
| 132 | |
| 133 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 134 | RectBoundsBench() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 135 | SkRandom rand; |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 136 | for (int i = 0; i < PTS; ++i) { |
| 137 | fPts[i].fX = rand.nextSScalar1(); |
| 138 | fPts[i].fY = rand.nextSScalar1(); |
| 139 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 140 | } |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 143 | return backend == kNonRendering_Backend; |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | const char* onGetName() override { |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 148 | return "rect_bounds"; |
| 149 | } |
| 150 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 151 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 152 | SkRect r; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 153 | for (int i = 0; i < loops; ++i) { |
reed | b25a83b | 2015-03-25 14:30:49 -0700 | [diff] [blame] | 154 | for (int i = 0; i < 1000; ++i) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 155 | r.setBounds(fPts, PTS); |
reed | b25a83b | 2015-03-25 14:30:49 -0700 | [diff] [blame] | 156 | } |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 161 | typedef Benchmark INHERITED; |
reed@google.com | 63c5761 | 2012-05-15 14:14:04 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 166 | DEF_BENCH( return new FloatComparisonBench(); ) |
| 167 | DEF_BENCH( return new ForcedIntComparisonBench(); ) |
| 168 | DEF_BENCH( return new RectBoundsBench(); ) |
| 169 | DEF_BENCH( return new IsFiniteScalarBench(); ) |