blob: 714eecc42685e34707fd2a02ed6a709b48cf823b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05007#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.comf910b362011-06-06 15:16:31 +000012
tfarinaf168b862014-06-19 12:32:29 -070013class ScalarBench : public Benchmark {
tomhudson@google.comf910b362011-06-06 15:16:31 +000014 SkString fName;
tomhudson@google.comf910b362011-06-06 15:16:31 +000015public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000016 ScalarBench(const char name[]) {
tomhudson@google.comf910b362011-06-06 15:16:31 +000017 fName.printf("scalar_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018 }
19
mtklein36352bf2015-03-25 18:17:31 -070020 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000021 return backend == kNonRendering_Backend;
tomhudson@google.comf910b362011-06-06 15:16:31 +000022 }
23
24 virtual void performTest() = 0;
25
26protected:
27 virtual int mulLoopCount() const { return 1; }
28
mtklein36352bf2015-03-25 18:17:31 -070029 const char* onGetName() override {
tomhudson@google.comf910b362011-06-06 15:16:31 +000030 return fName.c_str();
31 }
32
mtkleina1ebeb22015-10-01 09:43:39 -070033 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000034 for (int i = 0; i < loops; i++) {
tomhudson@google.comf910b362011-06-06 15:16:31 +000035 this->performTest();
36 }
37 }
38
39private:
tfarinaf168b862014-06-19 12:32:29 -070040 typedef Benchmark INHERITED;
tomhudson@google.comf910b362011-06-06 15:16:31 +000041};
42
tomhudson@google.comf910b362011-06-06 15:16:31 +000043// 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.
46template <typename T> void init9(T array[9]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000047 SkRandom rand;
tomhudson@google.comf910b362011-06-06 15:16:31 +000048 for (int i = 0; i < 9; i++) {
49 array[i] = rand.nextSScalar1();
50 }
51}
52
53class FloatComparisonBench : public ScalarBench {
54public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000055 FloatComparisonBench() : INHERITED("compare_float") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000056 init9(fArray);
57 }
58protected:
59 virtual int mulLoopCount() const { return 4; }
60 virtual void performTest() {
mtklein80f709a2014-06-02 12:41:07 -070061 // 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.comf910b362011-06-06 15:16:31 +000065 }
66private:
67 float fArray[9];
68 typedef ScalarBench INHERITED;
69};
70
71class ForcedIntComparisonBench : public ScalarBench {
72public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000073 ForcedIntComparisonBench()
74 : INHERITED("compare_forced_int") {
tomhudson@google.comf910b362011-06-06 15:16:31 +000075 init9(fArray);
76 }
77protected:
78 virtual int mulLoopCount() const { return 4; }
79 virtual void performTest() {
mtklein80f709a2014-06-02 12:41:07 -070080 // 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.comf910b362011-06-06 15:16:31 +000087 }
88private:
89 static const int32_t kPersp1Int = 0x3f800000;
tomhudson@google.com25583a32011-06-06 17:55:11 +000090 SkScalar fArray[9];
tomhudson@google.comf910b362011-06-06 15:16:31 +000091 typedef ScalarBench INHERITED;
92};
93
reed@google.com357818c2012-06-13 12:30:35 +000094class IsFiniteScalarBench : public ScalarBench {
95public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000096 IsFiniteScalarBench() : INHERITED("isfinite") {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000097 SkRandom rand;
reed@google.com357818c2012-06-13 12:30:35 +000098 for (size_t i = 0; i < ARRAY_N; ++i) {
99 fArray[i] = rand.nextSScalar1();
100 }
101 }
102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 int mulLoopCount() const override { return 1; }
104 void performTest() override {
reed@google.com357818c2012-06-13 12:30:35 +0000105 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) {}
116private:
117 enum {
118 ARRAY_N = 64
119 };
120 SkScalar fArray[ARRAY_N];
121
122 typedef ScalarBench INHERITED;
123};
124
reed@google.com63c57612012-05-15 14:14:04 +0000125///////////////////////////////////////////////////////////////////////////////
126
tfarinaf168b862014-06-19 12:32:29 -0700127class RectBoundsBench : public Benchmark {
reed@google.com63c57612012-05-15 14:14:04 +0000128 enum {
129 PTS = 100,
reed@google.com63c57612012-05-15 14:14:04 +0000130 };
131 SkPoint fPts[PTS];
132
133public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000134 RectBoundsBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000135 SkRandom rand;
reed@google.com63c57612012-05-15 14:14:04 +0000136 for (int i = 0; i < PTS; ++i) {
137 fPts[i].fX = rand.nextSScalar1();
138 fPts[i].fY = rand.nextSScalar1();
139 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000143 return backend == kNonRendering_Backend;
reed@google.com63c57612012-05-15 14:14:04 +0000144 }
145
146protected:
mtklein36352bf2015-03-25 18:17:31 -0700147 const char* onGetName() override {
reed@google.com63c57612012-05-15 14:14:04 +0000148 return "rect_bounds";
149 }
150
mtkleina1ebeb22015-10-01 09:43:39 -0700151 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com63c57612012-05-15 14:14:04 +0000152 SkRect r;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000153 for (int i = 0; i < loops; ++i) {
reedb25a83b2015-03-25 14:30:49 -0700154 for (int i = 0; i < 1000; ++i) {
Mike Reed92b33352019-08-24 19:39:13 -0400155 r.setBounds(fPts, PTS);
reedb25a83b2015-03-25 14:30:49 -0700156 }
reed@google.com63c57612012-05-15 14:14:04 +0000157 }
158 }
159
160private:
tfarinaf168b862014-06-19 12:32:29 -0700161 typedef Benchmark INHERITED;
reed@google.com63c57612012-05-15 14:14:04 +0000162};
163
164///////////////////////////////////////////////////////////////////////////////
165
mtklein@google.com410e6e82013-09-13 19:52:27 +0000166DEF_BENCH( return new FloatComparisonBench(); )
167DEF_BENCH( return new ForcedIntComparisonBench(); )
168DEF_BENCH( return new RectBoundsBench(); )
169DEF_BENCH( return new IsFiniteScalarBench(); )