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