blob: 74b89a3204577ebda4153da2693c2780b0c160c4 [file] [log] [blame]
mtkleinf0599002015-07-13 06:18:39 -07001/*
2 * Copyright 2015 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 */
7
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
halcanary4dbbd042016-06-07 17:21:10 -070011#include "SkMathPriv.h"
reed@google.comddc518b2011-08-29 17:49:23 +000012#include "SkMatrix.h"
tfarinaf168b862014-06-19 12:32:29 -070013#include "SkPaint.h"
reed@google.comddc518b2011-08-29 17:49:23 +000014#include "SkRandom.h"
15#include "SkString.h"
16
reed@google.com7f192412012-05-30 12:26:52 +000017static float sk_fsel(float pred, float result_ge, float result_lt) {
18 return pred >= 0 ? result_ge : result_lt;
19}
20
21static float fast_floor(float x) {
reed@google.comf3a8d8e2012-05-30 14:08:57 +000022// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
23 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
reed@google.com7f192412012-05-30 12:26:52 +000024 return (x + big) - big;
25}
26
tfarinaf168b862014-06-19 12:32:29 -070027class MathBench : public Benchmark {
reed@google.comddc518b2011-08-29 17:49:23 +000028 enum {
29 kBuffer = 100,
reed@google.comddc518b2011-08-29 17:49:23 +000030 };
31 SkString fName;
32 float fSrc[kBuffer], fDst[kBuffer];
33public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000034 MathBench(const char name[]) {
reed@google.comddc518b2011-08-29 17:49:23 +000035 fName.printf("math_%s", name);
36
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000037 SkRandom rand;
reed@google.comddc518b2011-08-29 17:49:23 +000038 for (int i = 0; i < kBuffer; ++i) {
39 fSrc[i] = rand.nextSScalar1();
40 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000041 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +000042
mtklein36352bf2015-03-25 18:17:31 -070043 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044 return backend == kNonRendering_Backend;
reed@google.comddc518b2011-08-29 17:49:23 +000045 }
46
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 virtual void performTest(float* SK_RESTRICT dst,
48 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000049 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000050
51protected:
52 virtual int mulLoopCount() const { return 1; }
53
mtkleinf0599002015-07-13 06:18:39 -070054 const char* onGetName() override {
reed@google.comddc518b2011-08-29 17:49:23 +000055 return fName.c_str();
56 }
57
mtkleina1ebeb22015-10-01 09:43:39 -070058 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000059 int n = loops * this->mulLoopCount();
reed@google.comddc518b2011-08-29 17:49:23 +000060 for (int i = 0; i < n; i++) {
61 this->performTest(fDst, fSrc, kBuffer);
62 }
63 }
64
65private:
tfarinaf168b862014-06-19 12:32:29 -070066 typedef Benchmark INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +000067};
68
reed@google.come05cc8e2011-10-10 14:19:40 +000069class MathBenchU32 : public MathBench {
70public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000071 MathBenchU32(const char name[]) : INHERITED(name) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +000072
reed@google.come05cc8e2011-10-10 14:19:40 +000073protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074 virtual void performITest(uint32_t* SK_RESTRICT dst,
75 const uint32_t* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000076 int count) = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +000077
mtkleinf0599002015-07-13 06:18:39 -070078 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
Mike Kleine72e7732018-06-14 14:41:22 -040079 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
80 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
reed@google.come05cc8e2011-10-10 14:19:40 +000081 this->performITest(d, s, count);
82 }
83private:
84 typedef MathBench INHERITED;
85};
86
87///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000088
89class NoOpMathBench : public MathBench {
90public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 NoOpMathBench() : INHERITED("noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000092protected:
mtkleinf0599002015-07-13 06:18:39 -070093 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
reed@google.comddc518b2011-08-29 17:49:23 +000094 for (int i = 0; i < count; ++i) {
95 dst[i] = src[i] + 1;
96 }
97 }
98private:
99 typedef MathBench INHERITED;
100};
101
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000102class SkRSqrtMathBench : public MathBench {
103public:
104 SkRSqrtMathBench() : INHERITED("sk_float_rsqrt") {}
105protected:
mtkleinf0599002015-07-13 06:18:39 -0700106 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000107 for (int i = 0; i < count; ++i) {
108 dst[i] = sk_float_rsqrt(src[i]);
109 }
110 }
111private:
112 typedef MathBench INHERITED;
113};
114
115
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000116class SlowISqrtMathBench : public MathBench {
reed@google.comddc518b2011-08-29 17:49:23 +0000117public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000118 SlowISqrtMathBench() : INHERITED("slowIsqrt") {}
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000119protected:
mtkleinf0599002015-07-13 06:18:39 -0700120 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000121 for (int i = 0; i < count; ++i) {
122 dst[i] = 1.0f / sk_float_sqrt(src[i]);
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000123 }
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000124 }
reed@google.comddc518b2011-08-29 17:49:23 +0000125private:
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000126 typedef MathBench INHERITED;
127};
128
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000129class FastISqrtMathBench : public MathBench {
130public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000131 FastISqrtMathBench() : INHERITED("fastIsqrt") {}
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000132protected:
mtkleinf0599002015-07-13 06:18:39 -0700133 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000134 for (int i = 0; i < count; ++i) {
Mike Klein1e114f12016-09-29 11:15:15 -0400135 dst[i] = sk_float_rsqrt(src[i]);
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000136 }
137 }
138private:
139 typedef MathBench INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +0000140};
141
reed@google.come05cc8e2011-10-10 14:19:40 +0000142static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
143 SkASSERT((uint8_t)alpha == alpha);
144 const uint32_t mask = 0xFF00FF;
145
146 uint64_t tmp = value;
147 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
148 tmp *= alpha;
caryclark@google.com19069a22012-06-06 12:11:45 +0000149 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
reed@google.come05cc8e2011-10-10 14:19:40 +0000150}
151
152class QMul64Bench : public MathBenchU32 {
153public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000154 QMul64Bench() : INHERITED("qmul64") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000155protected:
mtkleinf0599002015-07-13 06:18:39 -0700156 void performITest(uint32_t* SK_RESTRICT dst,
157 const uint32_t* SK_RESTRICT src,
158 int count) override {
reed@google.come05cc8e2011-10-10 14:19:40 +0000159 for (int i = 0; i < count; ++i) {
160 dst[i] = QMul64(src[i], (uint8_t)i);
161 }
162 }
163private:
164 typedef MathBenchU32 INHERITED;
165};
166
167class QMul32Bench : public MathBenchU32 {
168public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000169 QMul32Bench() : INHERITED("qmul32") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000170protected:
mtkleinf0599002015-07-13 06:18:39 -0700171 void performITest(uint32_t* SK_RESTRICT dst,
172 const uint32_t* SK_RESTRICT src,
173 int count) override {
reed@google.come05cc8e2011-10-10 14:19:40 +0000174 for (int i = 0; i < count; ++i) {
175 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
176 }
177 }
178private:
179 typedef MathBenchU32 INHERITED;
180};
181
reed@google.comddc518b2011-08-29 17:49:23 +0000182///////////////////////////////////////////////////////////////////////////////
183
reed@google.com0be5eb72011-12-05 21:53:22 +0000184static bool isFinite_int(float x) {
185 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
186 int exponent = bits << 1 >> 24;
187 return exponent != 0xFF;
188}
189
190static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000191 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000192}
193
194static bool isFinite_mulzero(float x) {
195 float y = x * 0;
196 return y == y;
197}
198
199static bool isfinite_and_int(const float data[4]) {
200 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
201}
202
203static bool isfinite_and_float(const float data[4]) {
204 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
205}
206
207static bool isfinite_and_mulzero(const float data[4]) {
208 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
209}
210
211#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
212
213static bool isfinite_plus_int(const float data[4]) {
214 return isFinite_int(mulzeroadd(data));
215}
216
217static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000218 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000219}
220
221static bool isfinite_plus_mulzero(const float data[4]) {
222 float x = mulzeroadd(data);
223 return x == x;
224}
225
226typedef bool (*IsFiniteProc)(const float[]);
227
228#define MAKEREC(name) { name, #name }
229
230static const struct {
231 IsFiniteProc fProc;
232 const char* fName;
233} gRec[] = {
234 MAKEREC(isfinite_and_int),
235 MAKEREC(isfinite_and_float),
236 MAKEREC(isfinite_and_mulzero),
237 MAKEREC(isfinite_plus_int),
238 MAKEREC(isfinite_plus_float),
239 MAKEREC(isfinite_plus_mulzero),
240};
241
242#undef MAKEREC
243
reed@google.com16078632011-12-06 18:56:37 +0000244static bool isFinite(const SkRect& r) {
245 // x * 0 will be NaN iff x is infinity or NaN.
246 // a + b will be NaN iff either a or b is NaN.
247 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000248
reed@google.com16078632011-12-06 18:56:37 +0000249 // value is either NaN or it is finite (zero).
250 // value==value will be true iff value is not NaN
251 return value == value;
252}
253
tfarinaf168b862014-06-19 12:32:29 -0700254class IsFiniteBench : public Benchmark {
reed@google.com0be5eb72011-12-05 21:53:22 +0000255 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000256 N = 1000,
reed@google.com0be5eb72011-12-05 21:53:22 +0000257 };
258 float fData[N];
259public:
260
mtklein@google.com410e6e82013-09-13 19:52:27 +0000261 IsFiniteBench(int index) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000262 SkRandom rand;
reed@google.com0be5eb72011-12-05 21:53:22 +0000263
264 for (int i = 0; i < N; ++i) {
265 fData[i] = rand.nextSScalar1();
266 }
reed@google.com16078632011-12-06 18:56:37 +0000267
268 if (index < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700269 fProc = nullptr;
reed@google.com16078632011-12-06 18:56:37 +0000270 fName = "isfinite_rect";
271 } else {
272 fProc = gRec[index].fProc;
273 fName = gRec[index].fName;
274 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000275 }
276
mtklein36352bf2015-03-25 18:17:31 -0700277 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000278 return backend == kNonRendering_Backend;
reed@google.com0be5eb72011-12-05 21:53:22 +0000279 }
280
281protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700282 void onDraw(int loops, SkCanvas*) override {
reed@google.com0be5eb72011-12-05 21:53:22 +0000283 IsFiniteProc proc = fProc;
284 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000285 // do this so the compiler won't throw away the function call
286 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000287
reed@google.com16078632011-12-06 18:56:37 +0000288 if (proc) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000289 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000290 for (int i = 0; i < N - 4; ++i) {
291 counter += proc(&data[i]);
292 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000293 }
reed@google.com16078632011-12-06 18:56:37 +0000294 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000295 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000296 for (int i = 0; i < N - 4; ++i) {
297 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
caryclark@google.com19069a22012-06-06 12:11:45 +0000298 if (false) { // avoid bit rot, suppress warning
299 isFinite(*r);
300 }
reed@google.com16078632011-12-06 18:56:37 +0000301 counter += r->isFinite();
302 }
303 }
304 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000305
reed@google.com16078632011-12-06 18:56:37 +0000306 SkPaint paint;
307 if (paint.getAlpha() == 0) {
308 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000309 }
310 }
311
mtkleinf0599002015-07-13 06:18:39 -0700312 const char* onGetName() override {
reed@google.com0be5eb72011-12-05 21:53:22 +0000313 return fName;
314 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000315
reed@google.com0be5eb72011-12-05 21:53:22 +0000316private:
317 IsFiniteProc fProc;
318 const char* fName;
319
tfarinaf168b862014-06-19 12:32:29 -0700320 typedef Benchmark INHERITED;
reed@google.com0be5eb72011-12-05 21:53:22 +0000321};
322
tfarinaf168b862014-06-19 12:32:29 -0700323class FloorBench : public Benchmark {
reed@google.com7f192412012-05-30 12:26:52 +0000324 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000325 ARRAY = 1000,
reed@google.com7f192412012-05-30 12:26:52 +0000326 };
327 float fData[ARRAY];
328 bool fFast;
329public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000330
mtklein@google.com410e6e82013-09-13 19:52:27 +0000331 FloorBench(bool fast) : fFast(fast) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000332 SkRandom rand;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000333
reed@google.com7f192412012-05-30 12:26:52 +0000334 for (int i = 0; i < ARRAY; ++i) {
335 fData[i] = rand.nextSScalar1();
336 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000337
reed@google.com7f192412012-05-30 12:26:52 +0000338 if (fast) {
339 fName = "floor_fast";
340 } else {
341 fName = "floor_std";
342 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000343 }
344
mtklein36352bf2015-03-25 18:17:31 -0700345 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000346 return backend == kNonRendering_Backend;
reed@google.com7f192412012-05-30 12:26:52 +0000347 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000348
reed@google.com7f192412012-05-30 12:26:52 +0000349 virtual void process(float) {}
350
351protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700352 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000353 SkRandom rand;
reed@google.com7f192412012-05-30 12:26:52 +0000354 float accum = 0;
355 const float* data = fData;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000356
reed@google.com7f192412012-05-30 12:26:52 +0000357 if (fFast) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000358 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000359 for (int i = 0; i < ARRAY; ++i) {
360 accum += fast_floor(data[i]);
361 }
362 this->process(accum);
363 }
364 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000365 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000366 for (int i = 0; i < ARRAY; ++i) {
367 accum += sk_float_floor(data[i]);
368 }
369 this->process(accum);
370 }
371 }
372 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000373
mtkleinf0599002015-07-13 06:18:39 -0700374 const char* onGetName() override {
reed@google.com7f192412012-05-30 12:26:52 +0000375 return fName;
376 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000377
reed@google.com7f192412012-05-30 12:26:52 +0000378private:
379 const char* fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000380
tfarinaf168b862014-06-19 12:32:29 -0700381 typedef Benchmark INHERITED;
reed@google.com7f192412012-05-30 12:26:52 +0000382};
383
tfarinaf168b862014-06-19 12:32:29 -0700384class CLZBench : public Benchmark {
reed@google.com0d7aac92013-04-29 13:55:11 +0000385 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000386 ARRAY = 1000,
reed@google.com0d7aac92013-04-29 13:55:11 +0000387 };
388 uint32_t fData[ARRAY];
389 bool fUsePortable;
390
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000391public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000392 CLZBench(bool usePortable) : fUsePortable(usePortable) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000393
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000394 SkRandom rand;
reed@google.com0d7aac92013-04-29 13:55:11 +0000395 for (int i = 0; i < ARRAY; ++i) {
396 fData[i] = rand.nextU();
397 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000398
reed@google.com0d7aac92013-04-29 13:55:11 +0000399 if (fUsePortable) {
400 fName = "clz_portable";
401 } else {
402 fName = "clz_intrinsic";
403 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000404 }
405
mtklein36352bf2015-03-25 18:17:31 -0700406 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000407 return backend == kNonRendering_Backend;
reed@google.com0d7aac92013-04-29 13:55:11 +0000408 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000409
reed@google.com0d7aac92013-04-29 13:55:11 +0000410 // just so the compiler doesn't remove our loops
411 virtual void process(int) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000412
reed@google.com0d7aac92013-04-29 13:55:11 +0000413protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700414 void onDraw(int loops, SkCanvas*) override {
reed@google.com0d7aac92013-04-29 13:55:11 +0000415 int accum = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000416
reed@google.com0d7aac92013-04-29 13:55:11 +0000417 if (fUsePortable) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000418 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000419 for (int i = 0; i < ARRAY; ++i) {
420 accum += SkCLZ_portable(fData[i]);
421 }
422 this->process(accum);
423 }
424 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000425 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000426 for (int i = 0; i < ARRAY; ++i) {
427 accum += SkCLZ(fData[i]);
428 }
429 this->process(accum);
430 }
431 }
432 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000433
mtkleinf0599002015-07-13 06:18:39 -0700434 const char* onGetName() override {
reed@google.com0d7aac92013-04-29 13:55:11 +0000435 return fName;
436 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000437
reed@google.com0d7aac92013-04-29 13:55:11 +0000438private:
439 const char* fName;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000440
tfarinaf168b862014-06-19 12:32:29 -0700441 typedef Benchmark INHERITED;
reed@google.com0d7aac92013-04-29 13:55:11 +0000442};
443
reed@google.com0be5eb72011-12-05 21:53:22 +0000444///////////////////////////////////////////////////////////////////////////////
445
tfarinaf168b862014-06-19 12:32:29 -0700446class NormalizeBench : public Benchmark {
reed@google.com0889f682013-05-03 12:56:39 +0000447 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000448 ARRAY =1000,
reed@google.com0889f682013-05-03 12:56:39 +0000449 };
450 SkVector fVec[ARRAY];
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000451
reed@google.com0889f682013-05-03 12:56:39 +0000452public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000453 NormalizeBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000454 SkRandom rand;
reed@google.com0889f682013-05-03 12:56:39 +0000455 for (int i = 0; i < ARRAY; ++i) {
456 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
457 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000458
reed@google.com0889f682013-05-03 12:56:39 +0000459 fName = "point_normalize";
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000460 }
461
mtklein36352bf2015-03-25 18:17:31 -0700462 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000463 return backend == kNonRendering_Backend;
reed@google.com0889f682013-05-03 12:56:39 +0000464 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000465
reed@google.com0889f682013-05-03 12:56:39 +0000466 // just so the compiler doesn't remove our loops
467 virtual void process(int) {}
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000468
reed@google.com0889f682013-05-03 12:56:39 +0000469protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700470 void onDraw(int loops, SkCanvas*) override {
reed@google.com0889f682013-05-03 12:56:39 +0000471 int accum = 0;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000472
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000473 for (int j = 0; j < loops; ++j) {
reed@google.com0889f682013-05-03 12:56:39 +0000474 for (int i = 0; i < ARRAY; ++i) {
475 accum += fVec[i].normalize();
476 }
477 this->process(accum);
478 }
479 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000480
mtkleinf0599002015-07-13 06:18:39 -0700481 const char* onGetName() override {
reed@google.com0889f682013-05-03 12:56:39 +0000482 return fName;
483 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000484
reed@google.com0889f682013-05-03 12:56:39 +0000485private:
486 const char* fName;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000487
tfarinaf168b862014-06-19 12:32:29 -0700488 typedef Benchmark INHERITED;
reed@google.com0889f682013-05-03 12:56:39 +0000489};
490
491///////////////////////////////////////////////////////////////////////////////
492
tfarinaf168b862014-06-19 12:32:29 -0700493class FixedMathBench : public Benchmark {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000494 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000495 N = 1000,
djsollen@google.com25a11e42013-07-18 19:11:30 +0000496 };
497 float fData[N];
498 SkFixed fResult[N];
499public:
500
mtklein@google.com410e6e82013-09-13 19:52:27 +0000501 FixedMathBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000502 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000503 for (int i = 0; i < N; ++i) {
504 fData[i] = rand.nextSScalar1();
djsollen@google.com25a11e42013-07-18 19:11:30 +0000505 }
506
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000507 }
508
mtklein36352bf2015-03-25 18:17:31 -0700509 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000510 return backend == kNonRendering_Backend;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000511 }
512
513protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700514 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000515 for (int j = 0; j < loops; ++j) {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000516 for (int i = 0; i < N - 4; ++i) {
517 fResult[i] = SkFloatToFixed(fData[i]);
518 }
519 }
520
521 SkPaint paint;
522 if (paint.getAlpha() == 0) {
523 SkDebugf("%d\n", fResult[0]);
524 }
525 }
526
mtkleinf0599002015-07-13 06:18:39 -0700527 const char* onGetName() override {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000528 return "float_to_fixed";
529 }
530
531private:
tfarinaf168b862014-06-19 12:32:29 -0700532 typedef Benchmark INHERITED;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000533};
534
535///////////////////////////////////////////////////////////////////////////////
536
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000537template <typename T>
tfarinaf168b862014-06-19 12:32:29 -0700538class DivModBench : public Benchmark {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000539 SkString fName;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000540public:
mtklein@google.com9cb51772013-09-27 13:39:14 +0000541 explicit DivModBench(const char* name) {
542 fName.printf("divmod_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000543 }
544
mtklein36352bf2015-03-25 18:17:31 -0700545 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000546 return backend == kNonRendering_Backend;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000547 }
548
549protected:
mtkleinf0599002015-07-13 06:18:39 -0700550 const char* onGetName() override {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000551 return fName.c_str();
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000552 }
553
mtkleina1ebeb22015-10-01 09:43:39 -0700554 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000555 volatile T a = 0, b = 0;
556 T div = 0, mod = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000557 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000558 if ((T)i == 0) continue; // Small T will wrap around.
559 SkTDivMod((T)(i+1), (T)i, &div, &mod);
560 a ^= div;
561 b ^= mod;
562 }
563 }
564};
565DEF_BENCH(return new DivModBench<uint8_t>("uint8_t"))
566DEF_BENCH(return new DivModBench<uint16_t>("uint16_t"))
567DEF_BENCH(return new DivModBench<uint32_t>("uint32_t"))
568DEF_BENCH(return new DivModBench<uint64_t>("uint64_t"))
569
570DEF_BENCH(return new DivModBench<int8_t>("int8_t"))
571DEF_BENCH(return new DivModBench<int16_t>("int16_t"))
572DEF_BENCH(return new DivModBench<int32_t>("int32_t"))
573DEF_BENCH(return new DivModBench<int64_t>("int64_t"))
574
575///////////////////////////////////////////////////////////////////////////////
576
mtklein@google.com410e6e82013-09-13 19:52:27 +0000577DEF_BENCH( return new NoOpMathBench(); )
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000578DEF_BENCH( return new SkRSqrtMathBench(); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000579DEF_BENCH( return new SlowISqrtMathBench(); )
580DEF_BENCH( return new FastISqrtMathBench(); )
581DEF_BENCH( return new QMul64Bench(); )
582DEF_BENCH( return new QMul32Bench(); )
reed@google.comddc518b2011-08-29 17:49:23 +0000583
mtklein@google.com410e6e82013-09-13 19:52:27 +0000584DEF_BENCH( return new IsFiniteBench(-1); )
585DEF_BENCH( return new IsFiniteBench(0); )
586DEF_BENCH( return new IsFiniteBench(1); )
587DEF_BENCH( return new IsFiniteBench(2); )
588DEF_BENCH( return new IsFiniteBench(3); )
589DEF_BENCH( return new IsFiniteBench(4); )
590DEF_BENCH( return new IsFiniteBench(5); )
reed@google.com0be5eb72011-12-05 21:53:22 +0000591
mtklein@google.com410e6e82013-09-13 19:52:27 +0000592DEF_BENCH( return new FloorBench(false); )
593DEF_BENCH( return new FloorBench(true); )
reed@google.com7f192412012-05-30 12:26:52 +0000594
mtklein@google.com410e6e82013-09-13 19:52:27 +0000595DEF_BENCH( return new CLZBench(false); )
596DEF_BENCH( return new CLZBench(true); )
reed@google.com0889f682013-05-03 12:56:39 +0000597
mtklein@google.com410e6e82013-09-13 19:52:27 +0000598DEF_BENCH( return new NormalizeBench(); )
djsollen@google.com25a11e42013-07-18 19:11:30 +0000599
mtklein@google.com410e6e82013-09-13 19:52:27 +0000600DEF_BENCH( return new FixedMathBench(); )
Mike Reed828f1d52017-08-09 11:06:53 -0400601
602//////////////////////////////////////////////////////////////
603
604#include "../private/SkFloatBits.h"
605class Floor2IntBench : public Benchmark {
606 enum {
607 ARRAY = 1000,
608 };
609 float fData[ARRAY];
610 const bool fSat;
611public:
612
613 Floor2IntBench(bool sat) : fSat(sat) {
614 SkRandom rand;
615
616 for (int i = 0; i < ARRAY; ++i) {
617 fData[i] = SkBits2Float(rand.nextU());
618 }
619
620 if (sat) {
621 fName = "floor2int_sat";
622 } else {
623 fName = "floor2int_undef";
624 }
625 }
626
627 bool isSuitableFor(Backend backend) override {
628 return backend == kNonRendering_Backend;
629 }
630
631 // These exist to try to stop the compiler from detecting what we doing, and throwing
632 // parts away (or knowing exactly how big the loop counts are).
Mike Reedc2704232017-08-09 13:32:04 -0400633 virtual void process(unsigned) {}
Mike Reed828f1d52017-08-09 11:06:53 -0400634 virtual int count() { return ARRAY; }
635
636protected:
637 void onDraw(int loops, SkCanvas*) override {
Mike Reedc2704232017-08-09 13:32:04 -0400638 // used unsigned to avoid undefined behavior if/when the += might overflow
639 unsigned accum = 0;
Mike Reed828f1d52017-08-09 11:06:53 -0400640
641 for (int j = 0; j < loops; ++j) {
642 int n = this->count();
643 if (fSat) {
644 for (int i = 0; i < n; ++i) {
645 accum += sk_float_floor2int(fData[i]);
646 }
647 } else {
648 for (int i = 0; i < n; ++i) {
649 accum += sk_float_floor2int_no_saturate(fData[i]);
650 }
651 }
652 this->process(accum);
653 }
654 }
655
656 const char* onGetName() override { return fName; }
657
658private:
659 const char* fName;
Ben Wagner63fd7602017-10-09 15:45:33 -0400660
Mike Reed828f1d52017-08-09 11:06:53 -0400661 typedef Benchmark INHERITED;
662};
663DEF_BENCH( return new Floor2IntBench(false); )
664DEF_BENCH( return new Floor2IntBench(true); )
665