blob: 33660b23cf15fa10d562bfa5d1be25078b8c76e8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkMatrix.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkString.h"
12#include "include/private/SkColorData.h"
13#include "include/private/SkFixed.h"
14#include "include/utils/SkRandom.h"
15#include "src/core/SkMathPriv.h"
reed@google.comddc518b2011-08-29 17:49:23 +000016
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:
John Stiles7571f9e2020-09-02 22:42:33 -040066 using INHERITED = Benchmark;
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:
John Stiles7571f9e2020-09-02 22:42:33 -040084 using INHERITED = MathBench;
reed@google.come05cc8e2011-10-10 14:19:40 +000085};
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:
John Stiles7571f9e2020-09-02 22:42:33 -040099 using INHERITED = MathBench;
reed@google.comddc518b2011-08-29 17:49:23 +0000100};
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:
John Stiles7571f9e2020-09-02 22:42:33 -0400112 using INHERITED = MathBench;
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000113};
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:
John Stiles7571f9e2020-09-02 22:42:33 -0400126 using INHERITED = MathBench;
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000127};
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:
John Stiles7571f9e2020-09-02 22:42:33 -0400139 using INHERITED = MathBench;
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:
John Stiles7571f9e2020-09-02 22:42:33 -0400164 using INHERITED = MathBenchU32;
reed@google.come05cc8e2011-10-10 14:19:40 +0000165};
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:
John Stiles7571f9e2020-09-02 22:42:33 -0400179 using INHERITED = MathBenchU32;
reed@google.come05cc8e2011-10-10 14:19:40 +0000180};
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
John Stiles7571f9e2020-09-02 22:42:33 -0400320 using INHERITED = Benchmark;
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
John Stiles7571f9e2020-09-02 22:42:33 -0400381 using INHERITED = Benchmark;
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
John Stiles7571f9e2020-09-02 22:42:33 -0400441 using INHERITED = Benchmark;
reed@google.com0d7aac92013-04-29 13:55:11 +0000442};
443
Ben Wagnere2a94432020-05-04 17:42:34 -0400444class CTZBench : public Benchmark {
445 enum {
446 ARRAY = 1000,
447 };
448 uint32_t fData[ARRAY];
449 bool fUsePortable;
450
451public:
452 CTZBench(bool usePortable) : fUsePortable(usePortable) {
453
454 SkRandom rand;
455 for (int i = 0; i < ARRAY; ++i) {
456 fData[i] = rand.nextU();
457 }
458
459 if (fUsePortable) {
460 fName = "ctz_portable";
461 } else {
462 fName = "ctz_intrinsic";
463 }
464 }
465
466 bool isSuitableFor(Backend backend) override {
467 return backend == kNonRendering_Backend;
468 }
469
470 // just so the compiler doesn't remove our loops
471 virtual void process(int) {}
472
473protected:
474 void onDraw(int loops, SkCanvas*) override {
475 int accum = 0;
476
477 if (fUsePortable) {
478 for (int j = 0; j < loops; ++j) {
479 for (int i = 0; i < ARRAY; ++i) {
480 accum += SkCTZ_portable(fData[i]);
481 }
482 this->process(accum);
483 }
484 } else {
485 for (int j = 0; j < loops; ++j) {
486 for (int i = 0; i < ARRAY; ++i) {
487 accum += SkCTZ(fData[i]);
488 }
489 this->process(accum);
490 }
491 }
492 }
493
494 const char* onGetName() override {
495 return fName;
496 }
497
498private:
499 const char* fName;
500
John Stiles7571f9e2020-09-02 22:42:33 -0400501 using INHERITED = Benchmark;
Ben Wagnere2a94432020-05-04 17:42:34 -0400502};
503
reed@google.com0be5eb72011-12-05 21:53:22 +0000504///////////////////////////////////////////////////////////////////////////////
505
tfarinaf168b862014-06-19 12:32:29 -0700506class NormalizeBench : public Benchmark {
reed@google.com0889f682013-05-03 12:56:39 +0000507 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000508 ARRAY =1000,
reed@google.com0889f682013-05-03 12:56:39 +0000509 };
510 SkVector fVec[ARRAY];
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000511
reed@google.com0889f682013-05-03 12:56:39 +0000512public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000513 NormalizeBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000514 SkRandom rand;
reed@google.com0889f682013-05-03 12:56:39 +0000515 for (int i = 0; i < ARRAY; ++i) {
516 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
517 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000518
reed@google.com0889f682013-05-03 12:56:39 +0000519 fName = "point_normalize";
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000520 }
521
mtklein36352bf2015-03-25 18:17:31 -0700522 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000523 return backend == kNonRendering_Backend;
reed@google.com0889f682013-05-03 12:56:39 +0000524 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000525
reed@google.com0889f682013-05-03 12:56:39 +0000526 // just so the compiler doesn't remove our loops
527 virtual void process(int) {}
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000528
reed@google.com0889f682013-05-03 12:56:39 +0000529protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700530 void onDraw(int loops, SkCanvas*) override {
reed@google.com0889f682013-05-03 12:56:39 +0000531 int accum = 0;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000532
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000533 for (int j = 0; j < loops; ++j) {
reed@google.com0889f682013-05-03 12:56:39 +0000534 for (int i = 0; i < ARRAY; ++i) {
535 accum += fVec[i].normalize();
536 }
537 this->process(accum);
538 }
539 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000540
mtkleinf0599002015-07-13 06:18:39 -0700541 const char* onGetName() override {
reed@google.com0889f682013-05-03 12:56:39 +0000542 return fName;
543 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000544
reed@google.com0889f682013-05-03 12:56:39 +0000545private:
546 const char* fName;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000547
John Stiles7571f9e2020-09-02 22:42:33 -0400548 using INHERITED = Benchmark;
reed@google.com0889f682013-05-03 12:56:39 +0000549};
550
551///////////////////////////////////////////////////////////////////////////////
552
tfarinaf168b862014-06-19 12:32:29 -0700553class FixedMathBench : public Benchmark {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000554 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000555 N = 1000,
djsollen@google.com25a11e42013-07-18 19:11:30 +0000556 };
557 float fData[N];
558 SkFixed fResult[N];
559public:
560
mtklein@google.com410e6e82013-09-13 19:52:27 +0000561 FixedMathBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000562 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000563 for (int i = 0; i < N; ++i) {
564 fData[i] = rand.nextSScalar1();
djsollen@google.com25a11e42013-07-18 19:11:30 +0000565 }
566
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000567 }
568
mtklein36352bf2015-03-25 18:17:31 -0700569 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000570 return backend == kNonRendering_Backend;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000571 }
572
573protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700574 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000575 for (int j = 0; j < loops; ++j) {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000576 for (int i = 0; i < N - 4; ++i) {
577 fResult[i] = SkFloatToFixed(fData[i]);
578 }
579 }
580
581 SkPaint paint;
582 if (paint.getAlpha() == 0) {
583 SkDebugf("%d\n", fResult[0]);
584 }
585 }
586
mtkleinf0599002015-07-13 06:18:39 -0700587 const char* onGetName() override {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000588 return "float_to_fixed";
589 }
590
591private:
John Stiles7571f9e2020-09-02 22:42:33 -0400592 using INHERITED = Benchmark;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000593};
594
595///////////////////////////////////////////////////////////////////////////////
596
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000597template <typename T>
tfarinaf168b862014-06-19 12:32:29 -0700598class DivModBench : public Benchmark {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000599 SkString fName;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000600public:
mtklein@google.com9cb51772013-09-27 13:39:14 +0000601 explicit DivModBench(const char* name) {
602 fName.printf("divmod_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000603 }
604
mtklein36352bf2015-03-25 18:17:31 -0700605 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000606 return backend == kNonRendering_Backend;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000607 }
608
609protected:
mtkleinf0599002015-07-13 06:18:39 -0700610 const char* onGetName() override {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000611 return fName.c_str();
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000612 }
613
mtkleina1ebeb22015-10-01 09:43:39 -0700614 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000615 volatile T a = 0, b = 0;
616 T div = 0, mod = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000617 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000618 if ((T)i == 0) continue; // Small T will wrap around.
619 SkTDivMod((T)(i+1), (T)i, &div, &mod);
620 a ^= div;
621 b ^= mod;
622 }
623 }
624};
625DEF_BENCH(return new DivModBench<uint8_t>("uint8_t"))
626DEF_BENCH(return new DivModBench<uint16_t>("uint16_t"))
627DEF_BENCH(return new DivModBench<uint32_t>("uint32_t"))
628DEF_BENCH(return new DivModBench<uint64_t>("uint64_t"))
629
630DEF_BENCH(return new DivModBench<int8_t>("int8_t"))
631DEF_BENCH(return new DivModBench<int16_t>("int16_t"))
632DEF_BENCH(return new DivModBench<int32_t>("int32_t"))
633DEF_BENCH(return new DivModBench<int64_t>("int64_t"))
634
635///////////////////////////////////////////////////////////////////////////////
636
mtklein@google.com410e6e82013-09-13 19:52:27 +0000637DEF_BENCH( return new NoOpMathBench(); )
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000638DEF_BENCH( return new SkRSqrtMathBench(); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000639DEF_BENCH( return new SlowISqrtMathBench(); )
640DEF_BENCH( return new FastISqrtMathBench(); )
641DEF_BENCH( return new QMul64Bench(); )
642DEF_BENCH( return new QMul32Bench(); )
reed@google.comddc518b2011-08-29 17:49:23 +0000643
mtklein@google.com410e6e82013-09-13 19:52:27 +0000644DEF_BENCH( return new IsFiniteBench(-1); )
645DEF_BENCH( return new IsFiniteBench(0); )
646DEF_BENCH( return new IsFiniteBench(1); )
647DEF_BENCH( return new IsFiniteBench(2); )
648DEF_BENCH( return new IsFiniteBench(3); )
649DEF_BENCH( return new IsFiniteBench(4); )
650DEF_BENCH( return new IsFiniteBench(5); )
reed@google.com0be5eb72011-12-05 21:53:22 +0000651
mtklein@google.com410e6e82013-09-13 19:52:27 +0000652DEF_BENCH( return new FloorBench(false); )
653DEF_BENCH( return new FloorBench(true); )
reed@google.com7f192412012-05-30 12:26:52 +0000654
mtklein@google.com410e6e82013-09-13 19:52:27 +0000655DEF_BENCH( return new CLZBench(false); )
656DEF_BENCH( return new CLZBench(true); )
Ben Wagnere2a94432020-05-04 17:42:34 -0400657DEF_BENCH( return new CTZBench(false); )
658DEF_BENCH( return new CTZBench(true); )
reed@google.com0889f682013-05-03 12:56:39 +0000659
mtklein@google.com410e6e82013-09-13 19:52:27 +0000660DEF_BENCH( return new NormalizeBench(); )
djsollen@google.com25a11e42013-07-18 19:11:30 +0000661
mtklein@google.com410e6e82013-09-13 19:52:27 +0000662DEF_BENCH( return new FixedMathBench(); )
Mike Reed828f1d52017-08-09 11:06:53 -0400663
664//////////////////////////////////////////////////////////////
665
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500666#include "include/private/SkFloatBits.h"
Mike Reed828f1d52017-08-09 11:06:53 -0400667class Floor2IntBench : public Benchmark {
668 enum {
669 ARRAY = 1000,
670 };
671 float fData[ARRAY];
672 const bool fSat;
673public:
674
675 Floor2IntBench(bool sat) : fSat(sat) {
676 SkRandom rand;
677
678 for (int i = 0; i < ARRAY; ++i) {
679 fData[i] = SkBits2Float(rand.nextU());
680 }
681
682 if (sat) {
683 fName = "floor2int_sat";
684 } else {
685 fName = "floor2int_undef";
686 }
687 }
688
689 bool isSuitableFor(Backend backend) override {
690 return backend == kNonRendering_Backend;
691 }
692
693 // These exist to try to stop the compiler from detecting what we doing, and throwing
694 // parts away (or knowing exactly how big the loop counts are).
Mike Reedc2704232017-08-09 13:32:04 -0400695 virtual void process(unsigned) {}
Mike Reed828f1d52017-08-09 11:06:53 -0400696 virtual int count() { return ARRAY; }
697
698protected:
699 void onDraw(int loops, SkCanvas*) override {
Mike Reedc2704232017-08-09 13:32:04 -0400700 // used unsigned to avoid undefined behavior if/when the += might overflow
701 unsigned accum = 0;
Mike Reed828f1d52017-08-09 11:06:53 -0400702
703 for (int j = 0; j < loops; ++j) {
704 int n = this->count();
705 if (fSat) {
706 for (int i = 0; i < n; ++i) {
707 accum += sk_float_floor2int(fData[i]);
708 }
709 } else {
710 for (int i = 0; i < n; ++i) {
711 accum += sk_float_floor2int_no_saturate(fData[i]);
712 }
713 }
714 this->process(accum);
715 }
716 }
717
718 const char* onGetName() override { return fName; }
719
720private:
721 const char* fName;
Ben Wagner63fd7602017-10-09 15:45:33 -0400722
John Stiles7571f9e2020-09-02 22:42:33 -0400723 using INHERITED = Benchmark;
Mike Reed828f1d52017-08-09 11:06:53 -0400724};
725DEF_BENCH( return new Floor2IntBench(false); )
726DEF_BENCH( return new Floor2IntBench(true); )
727