blob: 536af28340991b02715dc12bdba2885c5d3c1749 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.comf910b362011-06-06 15:16:31 +00008#include "SkBenchmark.h"
tomhudson@google.com25583a32011-06-06 17:55:11 +00009#include "SkFloatBits.h"
tomhudson@google.comf910b362011-06-06 15:16:31 +000010#include "SkRandom.h"
reed@google.com63c57612012-05-15 14:14:04 +000011#include "SkRect.h"
tomhudson@google.comf910b362011-06-06 15:16:31 +000012#include "SkString.h"
13
14class ScalarBench : public SkBenchmark {
15 SkString fName;
tomhudson@google.comf910b362011-06-06 15:16:31 +000016public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000017 ScalarBench(const char name[]) {
tomhudson@google.comf910b362011-06-06 15:16:31 +000018 fName.printf("scalar_%s", name);
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000019 fIsRendering = false;
tomhudson@google.comf910b362011-06-06 15:16:31 +000020 }
21
22 virtual void performTest() = 0;
23
24protected:
25 virtual int mulLoopCount() const { return 1; }
26
reed@google.com357818c2012-06-13 12:30:35 +000027 virtual const char* onGetName() SK_OVERRIDE {
tomhudson@google.comf910b362011-06-06 15:16:31 +000028 return fName.c_str();
29 }
30
31 virtual void onDraw(SkCanvas* canvas) {
mtklein@google.comc2897432013-09-10 19:23:38 +000032 for (int i = 0; i < this->getLoops(); i++) {
tomhudson@google.comf910b362011-06-06 15:16:31 +000033 this->performTest();
34 }
35 }
36
37private:
38 typedef SkBenchmark INHERITED;
39};
40
41// we want to stop the compiler from eliminating code that it thinks is a no-op
42// so we have a non-static global we increment, hoping that will convince the
43// compiler to execute everything
44int gScalarBench_NonStaticGlobal;
45
46#define always_do(pred) \
47 do { \
48 if (pred) { \
49 ++gScalarBench_NonStaticGlobal; \
50 } \
51 } while (0)
52
53// having unknown values in our arrays can throw off the timing a lot, perhaps
54// handling NaN values is a lot slower. Anyway, this guy is just meant to put
55// reasonable values in our arrays.
56template <typename T> void init9(T array[9]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000057 SkRandom rand;
tomhudson@google.comf910b362011-06-06 15:16:31 +000058 for (int i = 0; i < 9; i++) {
59 array[i] = rand.nextSScalar1();
60 }
61}
62
63class FloatComparisonBench : public ScalarBench {
64public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000065 FloatComparisonBench() : INHERITED("compare_float") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000066 init9(fArray);
67 }
68protected:
69 virtual int mulLoopCount() const { return 4; }
70 virtual void performTest() {
71 always_do(fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
72 always_do(fArray[2] != 0.0f || fArray[5] != 0.0f);
73 }
74private:
75 float fArray[9];
76 typedef ScalarBench INHERITED;
77};
78
79class ForcedIntComparisonBench : public ScalarBench {
80public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000081 ForcedIntComparisonBench()
82 : INHERITED("compare_forced_int") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000083 init9(fArray);
84 }
85protected:
86 virtual int mulLoopCount() const { return 4; }
87 virtual void performTest() {
88 always_do(SkScalarAs2sCompliment(fArray[6]) |
89 SkScalarAs2sCompliment(fArray[7]) |
90 (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
91 always_do(SkScalarAs2sCompliment(fArray[2]) |
92 SkScalarAs2sCompliment(fArray[5]));
93 }
94private:
95 static const int32_t kPersp1Int = 0x3f800000;
tomhudson@google.com25583a32011-06-06 17:55:11 +000096 SkScalar fArray[9];
tomhudson@google.comf910b362011-06-06 15:16:31 +000097 typedef ScalarBench INHERITED;
98};
99
reed@google.com357818c2012-06-13 12:30:35 +0000100class IsFiniteScalarBench : public ScalarBench {
101public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000102 IsFiniteScalarBench() : INHERITED("isfinite") {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000103 SkRandom rand;
reed@google.com357818c2012-06-13 12:30:35 +0000104 for (size_t i = 0; i < ARRAY_N; ++i) {
105 fArray[i] = rand.nextSScalar1();
106 }
107 }
108protected:
109 virtual int mulLoopCount() const { return 1; }
110 virtual void performTest() SK_OVERRIDE {
111 int sum = 0;
112 for (size_t i = 0; i < ARRAY_N; ++i) {
113 // We pass -fArray[i], so the compiler can't cheat and treat the
114 // value as an int (even though we tell it that it is a float)
115 sum += SkScalarIsFinite(-fArray[i]);
116 }
117 // we do this so the compiler won't optimize our loop away...
118 this->doSomething(fArray, sum);
119 }
120
121 virtual void doSomething(SkScalar array[], int sum) {}
122private:
123 enum {
124 ARRAY_N = 64
125 };
126 SkScalar fArray[ARRAY_N];
127
128 typedef ScalarBench INHERITED;
129};
130
reed@google.com63c57612012-05-15 14:14:04 +0000131///////////////////////////////////////////////////////////////////////////////
132
133class RectBoundsBench : public SkBenchmark {
134 enum {
135 PTS = 100,
reed@google.com63c57612012-05-15 14:14:04 +0000136 };
137 SkPoint fPts[PTS];
138
139public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000140 RectBoundsBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000141 SkRandom rand;
reed@google.com63c57612012-05-15 14:14:04 +0000142 for (int i = 0; i < PTS; ++i) {
143 fPts[i].fX = rand.nextSScalar1();
144 fPts[i].fY = rand.nextSScalar1();
145 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +0000146 fIsRendering = false;
reed@google.com63c57612012-05-15 14:14:04 +0000147 }
148
149protected:
150 virtual const char* onGetName() SK_OVERRIDE {
151 return "rect_bounds";
152 }
153
154 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
155 SkRect r;
mtklein@google.comc2897432013-09-10 19:23:38 +0000156 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.com63c57612012-05-15 14:14:04 +0000157 r.set(fPts, PTS);
158 }
159 }
160
161private:
162 typedef SkBenchmark INHERITED;
163};
164
165///////////////////////////////////////////////////////////////////////////////
166
mtklein@google.com410e6e82013-09-13 19:52:27 +0000167DEF_BENCH( return new FloatComparisonBench(); )
168DEF_BENCH( return new ForcedIntComparisonBench(); )
169DEF_BENCH( return new RectBoundsBench(); )
170DEF_BENCH( return new IsFiniteScalarBench(); )