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