blob: 92608123ee2c887b08f7221ef457a8bc2d37c4ab [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.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
tfarinaf168b862014-06-19 12:32:29 -070014class ScalarBench : public Benchmark {
tomhudson@google.comf910b362011-06-06 15:16:31 +000015 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);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000019 }
20
21 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
22 return backend == kNonRendering_Backend;
tomhudson@google.comf910b362011-06-06 15:16:31 +000023 }
24
25 virtual void performTest() = 0;
26
27protected:
28 virtual int mulLoopCount() const { return 1; }
29
reed@google.com357818c2012-06-13 12:30:35 +000030 virtual const char* onGetName() SK_OVERRIDE {
tomhudson@google.comf910b362011-06-06 15:16:31 +000031 return fName.c_str();
32 }
33
tfarinaaa458fb2015-01-05 17:18:51 -080034 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000035 for (int i = 0; i < loops; i++) {
tomhudson@google.comf910b362011-06-06 15:16:31 +000036 this->performTest();
37 }
38 }
39
40private:
tfarinaf168b862014-06-19 12:32:29 -070041 typedef Benchmark INHERITED;
tomhudson@google.comf910b362011-06-06 15:16:31 +000042};
43
tomhudson@google.comf910b362011-06-06 15:16:31 +000044// 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.
47template <typename T> void init9(T array[9]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000048 SkRandom rand;
tomhudson@google.comf910b362011-06-06 15:16:31 +000049 for (int i = 0; i < 9; i++) {
50 array[i] = rand.nextSScalar1();
51 }
52}
53
54class FloatComparisonBench : public ScalarBench {
55public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000056 FloatComparisonBench() : INHERITED("compare_float") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000057 init9(fArray);
58 }
59protected:
60 virtual int mulLoopCount() const { return 4; }
61 virtual void performTest() {
mtklein80f709a2014-06-02 12:41:07 -070062 // 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.comf910b362011-06-06 15:16:31 +000066 }
67private:
68 float fArray[9];
69 typedef ScalarBench INHERITED;
70};
71
72class ForcedIntComparisonBench : public ScalarBench {
73public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000074 ForcedIntComparisonBench()
75 : INHERITED("compare_forced_int") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000076 init9(fArray);
77 }
78protected:
79 virtual int mulLoopCount() const { return 4; }
80 virtual void performTest() {
mtklein80f709a2014-06-02 12:41:07 -070081 // 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.comf910b362011-06-06 15:16:31 +000088 }
89private:
90 static const int32_t kPersp1Int = 0x3f800000;
tomhudson@google.com25583a32011-06-06 17:55:11 +000091 SkScalar fArray[9];
tomhudson@google.comf910b362011-06-06 15:16:31 +000092 typedef ScalarBench INHERITED;
93};
94
reed@google.com357818c2012-06-13 12:30:35 +000095class IsFiniteScalarBench : public ScalarBench {
96public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000097 IsFiniteScalarBench() : INHERITED("isfinite") {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000098 SkRandom rand;
reed@google.com357818c2012-06-13 12:30:35 +000099 for (size_t i = 0; i < ARRAY_N; ++i) {
100 fArray[i] = rand.nextSScalar1();
101 }
102 }
103protected:
tfarinaaa458fb2015-01-05 17:18:51 -0800104 virtual int mulLoopCount() const SK_OVERRIDE { return 1; }
reed@google.com357818c2012-06-13 12:30:35 +0000105 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) {}
117private:
118 enum {
119 ARRAY_N = 64
120 };
121 SkScalar fArray[ARRAY_N];
122
123 typedef ScalarBench INHERITED;
124};
125
reed@google.com63c57612012-05-15 14:14:04 +0000126///////////////////////////////////////////////////////////////////////////////
127
tfarinaf168b862014-06-19 12:32:29 -0700128class RectBoundsBench : public Benchmark {
reed@google.com63c57612012-05-15 14:14:04 +0000129 enum {
130 PTS = 100,
reed@google.com63c57612012-05-15 14:14:04 +0000131 };
132 SkPoint fPts[PTS];
133
134public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000135 RectBoundsBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000136 SkRandom rand;
reed@google.com63c57612012-05-15 14:14:04 +0000137 for (int i = 0; i < PTS; ++i) {
138 fPts[i].fX = rand.nextSScalar1();
139 fPts[i].fY = rand.nextSScalar1();
140 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141 }
142
143 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
144 return backend == kNonRendering_Backend;
reed@google.com63c57612012-05-15 14:14:04 +0000145 }
146
147protected:
148 virtual const char* onGetName() SK_OVERRIDE {
149 return "rect_bounds";
150 }
151
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000152 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.com63c57612012-05-15 14:14:04 +0000153 SkRect r;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000154 for (int i = 0; i < loops; ++i) {
reed@google.com63c57612012-05-15 14:14:04 +0000155 r.set(fPts, PTS);
156 }
157 }
158
159private:
tfarinaf168b862014-06-19 12:32:29 -0700160 typedef Benchmark INHERITED;
reed@google.com63c57612012-05-15 14:14:04 +0000161};
162
163///////////////////////////////////////////////////////////////////////////////
164
mtklein@google.com410e6e82013-09-13 19:52:27 +0000165DEF_BENCH( return new FloatComparisonBench(); )
166DEF_BENCH( return new ForcedIntComparisonBench(); )
167DEF_BENCH( return new RectBoundsBench(); )
168DEF_BENCH( return new IsFiniteScalarBench(); )