blob: 77ba2f04be78ae62f91eb2c90c834ddc5137f60b [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"
reed@google.come05cc8e2011-10-10 14:19:40 +00009#include "SkColorPriv.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
reed@google.comddc518b2011-08-29 17:49:23 +000011#include "SkMatrix.h"
tfarinaf168b862014-06-19 12:32:29 -070012#include "SkPaint.h"
reed@google.comddc518b2011-08-29 17:49:23 +000013#include "SkRandom.h"
14#include "SkString.h"
15
reed@google.com7f192412012-05-30 12:26:52 +000016static float sk_fsel(float pred, float result_ge, float result_lt) {
17 return pred >= 0 ? result_ge : result_lt;
18}
19
20static float fast_floor(float x) {
reed@google.comf3a8d8e2012-05-30 14:08:57 +000021// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
22 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
reed@google.com7f192412012-05-30 12:26:52 +000023 return (x + big) - big;
24}
25
tfarinaf168b862014-06-19 12:32:29 -070026class MathBench : public Benchmark {
reed@google.comddc518b2011-08-29 17:49:23 +000027 enum {
28 kBuffer = 100,
reed@google.comddc518b2011-08-29 17:49:23 +000029 };
30 SkString fName;
31 float fSrc[kBuffer], fDst[kBuffer];
32public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000033 MathBench(const char name[]) {
reed@google.comddc518b2011-08-29 17:49:23 +000034 fName.printf("math_%s", name);
35
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom rand;
reed@google.comddc518b2011-08-29 17:49:23 +000037 for (int i = 0; i < kBuffer; ++i) {
38 fSrc[i] = rand.nextSScalar1();
39 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000043 return backend == kNonRendering_Backend;
reed@google.comddc518b2011-08-29 17:49:23 +000044 }
45
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046 virtual void performTest(float* SK_RESTRICT dst,
47 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000048 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000049
50protected:
51 virtual int mulLoopCount() const { return 1; }
52
mtkleinf0599002015-07-13 06:18:39 -070053 const char* onGetName() override {
reed@google.comddc518b2011-08-29 17:49:23 +000054 return fName.c_str();
55 }
56
mtkleina1ebeb22015-10-01 09:43:39 -070057 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 int n = loops * this->mulLoopCount();
reed@google.comddc518b2011-08-29 17:49:23 +000059 for (int i = 0; i < n; i++) {
60 this->performTest(fDst, fSrc, kBuffer);
61 }
62 }
63
64private:
tfarinaf168b862014-06-19 12:32:29 -070065 typedef Benchmark INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +000066};
67
reed@google.come05cc8e2011-10-10 14:19:40 +000068class MathBenchU32 : public MathBench {
69public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000070 MathBenchU32(const char name[]) : INHERITED(name) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +000071
reed@google.come05cc8e2011-10-10 14:19:40 +000072protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073 virtual void performITest(uint32_t* SK_RESTRICT dst,
74 const uint32_t* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000075 int count) = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +000076
mtkleinf0599002015-07-13 06:18:39 -070077 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
reed@google.come05cc8e2011-10-10 14:19:40 +000078 uint32_t* d = SkTCast<uint32_t*>(dst);
79 const uint32_t* s = SkTCast<const uint32_t*>(src);
80 this->performITest(d, s, count);
81 }
82private:
83 typedef MathBench INHERITED;
84};
85
86///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000087
88class NoOpMathBench : public MathBench {
89public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000090 NoOpMathBench() : INHERITED("noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000091protected:
mtkleinf0599002015-07-13 06:18:39 -070092 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
reed@google.comddc518b2011-08-29 17:49:23 +000093 for (int i = 0; i < count; ++i) {
94 dst[i] = src[i] + 1;
95 }
96 }
97private:
98 typedef MathBench INHERITED;
99};
100
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000101class SkRSqrtMathBench : public MathBench {
102public:
103 SkRSqrtMathBench() : INHERITED("sk_float_rsqrt") {}
104protected:
mtkleinf0599002015-07-13 06:18:39 -0700105 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000106 for (int i = 0; i < count; ++i) {
107 dst[i] = sk_float_rsqrt(src[i]);
108 }
109 }
110private:
111 typedef MathBench INHERITED;
112};
113
114
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000115class SlowISqrtMathBench : public MathBench {
reed@google.comddc518b2011-08-29 17:49:23 +0000116public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000117 SlowISqrtMathBench() : INHERITED("slowIsqrt") {}
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000118protected:
mtkleinf0599002015-07-13 06:18:39 -0700119 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000120 for (int i = 0; i < count; ++i) {
121 dst[i] = 1.0f / sk_float_sqrt(src[i]);
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000122 }
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000123 }
reed@google.comddc518b2011-08-29 17:49:23 +0000124private:
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000125 typedef MathBench INHERITED;
126};
127
128static inline float SkFastInvSqrt(float x) {
129 float xhalf = 0.5f*x;
mtklein1f6a1bd2015-12-08 10:53:01 -0800130 uint32_t i = *SkTCast<uint32_t*>(&x);
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000131 i = 0x5f3759df - (i>>1);
132 x = *SkTCast<float*>(&i);
133 x = x*(1.5f-xhalf*x*x);
134// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
135 return x;
136}
137
138class FastISqrtMathBench : public MathBench {
139public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000140 FastISqrtMathBench() : INHERITED("fastIsqrt") {}
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000141protected:
mtkleinf0599002015-07-13 06:18:39 -0700142 void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, int count) override {
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000143 for (int i = 0; i < count; ++i) {
144 dst[i] = SkFastInvSqrt(src[i]);
145 }
146 }
147private:
148 typedef MathBench INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +0000149};
150
reed@google.come05cc8e2011-10-10 14:19:40 +0000151static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
152 SkASSERT((uint8_t)alpha == alpha);
153 const uint32_t mask = 0xFF00FF;
154
155 uint64_t tmp = value;
156 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
157 tmp *= alpha;
caryclark@google.com19069a22012-06-06 12:11:45 +0000158 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
reed@google.come05cc8e2011-10-10 14:19:40 +0000159}
160
161class QMul64Bench : public MathBenchU32 {
162public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163 QMul64Bench() : INHERITED("qmul64") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000164protected:
mtkleinf0599002015-07-13 06:18:39 -0700165 void performITest(uint32_t* SK_RESTRICT dst,
166 const uint32_t* SK_RESTRICT src,
167 int count) override {
reed@google.come05cc8e2011-10-10 14:19:40 +0000168 for (int i = 0; i < count; ++i) {
169 dst[i] = QMul64(src[i], (uint8_t)i);
170 }
171 }
172private:
173 typedef MathBenchU32 INHERITED;
174};
175
176class QMul32Bench : public MathBenchU32 {
177public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000178 QMul32Bench() : INHERITED("qmul32") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000179protected:
mtkleinf0599002015-07-13 06:18:39 -0700180 void performITest(uint32_t* SK_RESTRICT dst,
181 const uint32_t* SK_RESTRICT src,
182 int count) override {
reed@google.come05cc8e2011-10-10 14:19:40 +0000183 for (int i = 0; i < count; ++i) {
184 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
185 }
186 }
187private:
188 typedef MathBenchU32 INHERITED;
189};
190
reed@google.comddc518b2011-08-29 17:49:23 +0000191///////////////////////////////////////////////////////////////////////////////
192
reed@google.com0be5eb72011-12-05 21:53:22 +0000193static bool isFinite_int(float x) {
194 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
195 int exponent = bits << 1 >> 24;
196 return exponent != 0xFF;
197}
198
199static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000200 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000201}
202
203static bool isFinite_mulzero(float x) {
204 float y = x * 0;
205 return y == y;
206}
207
208static bool isfinite_and_int(const float data[4]) {
209 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
210}
211
212static bool isfinite_and_float(const float data[4]) {
213 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
214}
215
216static bool isfinite_and_mulzero(const float data[4]) {
217 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
218}
219
220#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
221
222static bool isfinite_plus_int(const float data[4]) {
223 return isFinite_int(mulzeroadd(data));
224}
225
226static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000227 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000228}
229
230static bool isfinite_plus_mulzero(const float data[4]) {
231 float x = mulzeroadd(data);
232 return x == x;
233}
234
235typedef bool (*IsFiniteProc)(const float[]);
236
237#define MAKEREC(name) { name, #name }
238
239static const struct {
240 IsFiniteProc fProc;
241 const char* fName;
242} gRec[] = {
243 MAKEREC(isfinite_and_int),
244 MAKEREC(isfinite_and_float),
245 MAKEREC(isfinite_and_mulzero),
246 MAKEREC(isfinite_plus_int),
247 MAKEREC(isfinite_plus_float),
248 MAKEREC(isfinite_plus_mulzero),
249};
250
251#undef MAKEREC
252
reed@google.com16078632011-12-06 18:56:37 +0000253static bool isFinite(const SkRect& r) {
254 // x * 0 will be NaN iff x is infinity or NaN.
255 // a + b will be NaN iff either a or b is NaN.
256 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000257
reed@google.com16078632011-12-06 18:56:37 +0000258 // value is either NaN or it is finite (zero).
259 // value==value will be true iff value is not NaN
260 return value == value;
261}
262
tfarinaf168b862014-06-19 12:32:29 -0700263class IsFiniteBench : public Benchmark {
reed@google.com0be5eb72011-12-05 21:53:22 +0000264 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000265 N = 1000,
reed@google.com0be5eb72011-12-05 21:53:22 +0000266 };
267 float fData[N];
268public:
269
mtklein@google.com410e6e82013-09-13 19:52:27 +0000270 IsFiniteBench(int index) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000271 SkRandom rand;
reed@google.com0be5eb72011-12-05 21:53:22 +0000272
273 for (int i = 0; i < N; ++i) {
274 fData[i] = rand.nextSScalar1();
275 }
reed@google.com16078632011-12-06 18:56:37 +0000276
277 if (index < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700278 fProc = nullptr;
reed@google.com16078632011-12-06 18:56:37 +0000279 fName = "isfinite_rect";
280 } else {
281 fProc = gRec[index].fProc;
282 fName = gRec[index].fName;
283 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000284 }
285
mtklein36352bf2015-03-25 18:17:31 -0700286 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000287 return backend == kNonRendering_Backend;
reed@google.com0be5eb72011-12-05 21:53:22 +0000288 }
289
290protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700291 void onDraw(int loops, SkCanvas*) override {
reed@google.com0be5eb72011-12-05 21:53:22 +0000292 IsFiniteProc proc = fProc;
293 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000294 // do this so the compiler won't throw away the function call
295 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000296
reed@google.com16078632011-12-06 18:56:37 +0000297 if (proc) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000298 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000299 for (int i = 0; i < N - 4; ++i) {
300 counter += proc(&data[i]);
301 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000302 }
reed@google.com16078632011-12-06 18:56:37 +0000303 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000304 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000305 for (int i = 0; i < N - 4; ++i) {
306 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
caryclark@google.com19069a22012-06-06 12:11:45 +0000307 if (false) { // avoid bit rot, suppress warning
308 isFinite(*r);
309 }
reed@google.com16078632011-12-06 18:56:37 +0000310 counter += r->isFinite();
311 }
312 }
313 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000314
reed@google.com16078632011-12-06 18:56:37 +0000315 SkPaint paint;
316 if (paint.getAlpha() == 0) {
317 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000318 }
319 }
320
mtkleinf0599002015-07-13 06:18:39 -0700321 const char* onGetName() override {
reed@google.com0be5eb72011-12-05 21:53:22 +0000322 return fName;
323 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000324
reed@google.com0be5eb72011-12-05 21:53:22 +0000325private:
326 IsFiniteProc fProc;
327 const char* fName;
328
tfarinaf168b862014-06-19 12:32:29 -0700329 typedef Benchmark INHERITED;
reed@google.com0be5eb72011-12-05 21:53:22 +0000330};
331
tfarinaf168b862014-06-19 12:32:29 -0700332class FloorBench : public Benchmark {
reed@google.com7f192412012-05-30 12:26:52 +0000333 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000334 ARRAY = 1000,
reed@google.com7f192412012-05-30 12:26:52 +0000335 };
336 float fData[ARRAY];
337 bool fFast;
338public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000339
mtklein@google.com410e6e82013-09-13 19:52:27 +0000340 FloorBench(bool fast) : fFast(fast) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000341 SkRandom rand;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000342
reed@google.com7f192412012-05-30 12:26:52 +0000343 for (int i = 0; i < ARRAY; ++i) {
344 fData[i] = rand.nextSScalar1();
345 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000346
reed@google.com7f192412012-05-30 12:26:52 +0000347 if (fast) {
348 fName = "floor_fast";
349 } else {
350 fName = "floor_std";
351 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000352 }
353
mtklein36352bf2015-03-25 18:17:31 -0700354 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000355 return backend == kNonRendering_Backend;
reed@google.com7f192412012-05-30 12:26:52 +0000356 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000357
reed@google.com7f192412012-05-30 12:26:52 +0000358 virtual void process(float) {}
359
360protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700361 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000362 SkRandom rand;
reed@google.com7f192412012-05-30 12:26:52 +0000363 float accum = 0;
364 const float* data = fData;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000365
reed@google.com7f192412012-05-30 12:26:52 +0000366 if (fFast) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000367 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000368 for (int i = 0; i < ARRAY; ++i) {
369 accum += fast_floor(data[i]);
370 }
371 this->process(accum);
372 }
373 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000374 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000375 for (int i = 0; i < ARRAY; ++i) {
376 accum += sk_float_floor(data[i]);
377 }
378 this->process(accum);
379 }
380 }
381 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000382
mtkleinf0599002015-07-13 06:18:39 -0700383 const char* onGetName() override {
reed@google.com7f192412012-05-30 12:26:52 +0000384 return fName;
385 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000386
reed@google.com7f192412012-05-30 12:26:52 +0000387private:
388 const char* fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000389
tfarinaf168b862014-06-19 12:32:29 -0700390 typedef Benchmark INHERITED;
reed@google.com7f192412012-05-30 12:26:52 +0000391};
392
tfarinaf168b862014-06-19 12:32:29 -0700393class CLZBench : public Benchmark {
reed@google.com0d7aac92013-04-29 13:55:11 +0000394 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000395 ARRAY = 1000,
reed@google.com0d7aac92013-04-29 13:55:11 +0000396 };
397 uint32_t fData[ARRAY];
398 bool fUsePortable;
399
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000400public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000401 CLZBench(bool usePortable) : fUsePortable(usePortable) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000402
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000403 SkRandom rand;
reed@google.com0d7aac92013-04-29 13:55:11 +0000404 for (int i = 0; i < ARRAY; ++i) {
405 fData[i] = rand.nextU();
406 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000407
reed@google.com0d7aac92013-04-29 13:55:11 +0000408 if (fUsePortable) {
409 fName = "clz_portable";
410 } else {
411 fName = "clz_intrinsic";
412 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000413 }
414
mtklein36352bf2015-03-25 18:17:31 -0700415 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000416 return backend == kNonRendering_Backend;
reed@google.com0d7aac92013-04-29 13:55:11 +0000417 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000418
reed@google.com0d7aac92013-04-29 13:55:11 +0000419 // just so the compiler doesn't remove our loops
420 virtual void process(int) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000421
reed@google.com0d7aac92013-04-29 13:55:11 +0000422protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700423 void onDraw(int loops, SkCanvas*) override {
reed@google.com0d7aac92013-04-29 13:55:11 +0000424 int accum = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000425
reed@google.com0d7aac92013-04-29 13:55:11 +0000426 if (fUsePortable) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000427 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000428 for (int i = 0; i < ARRAY; ++i) {
429 accum += SkCLZ_portable(fData[i]);
430 }
431 this->process(accum);
432 }
433 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000434 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000435 for (int i = 0; i < ARRAY; ++i) {
436 accum += SkCLZ(fData[i]);
437 }
438 this->process(accum);
439 }
440 }
441 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000442
mtkleinf0599002015-07-13 06:18:39 -0700443 const char* onGetName() override {
reed@google.com0d7aac92013-04-29 13:55:11 +0000444 return fName;
445 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000446
reed@google.com0d7aac92013-04-29 13:55:11 +0000447private:
448 const char* fName;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000449
tfarinaf168b862014-06-19 12:32:29 -0700450 typedef Benchmark INHERITED;
reed@google.com0d7aac92013-04-29 13:55:11 +0000451};
452
reed@google.com0be5eb72011-12-05 21:53:22 +0000453///////////////////////////////////////////////////////////////////////////////
454
tfarinaf168b862014-06-19 12:32:29 -0700455class NormalizeBench : public Benchmark {
reed@google.com0889f682013-05-03 12:56:39 +0000456 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000457 ARRAY =1000,
reed@google.com0889f682013-05-03 12:56:39 +0000458 };
459 SkVector fVec[ARRAY];
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000460
reed@google.com0889f682013-05-03 12:56:39 +0000461public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000462 NormalizeBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000463 SkRandom rand;
reed@google.com0889f682013-05-03 12:56:39 +0000464 for (int i = 0; i < ARRAY; ++i) {
465 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
466 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000467
reed@google.com0889f682013-05-03 12:56:39 +0000468 fName = "point_normalize";
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000469 }
470
mtklein36352bf2015-03-25 18:17:31 -0700471 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000472 return backend == kNonRendering_Backend;
reed@google.com0889f682013-05-03 12:56:39 +0000473 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000474
reed@google.com0889f682013-05-03 12:56:39 +0000475 // just so the compiler doesn't remove our loops
476 virtual void process(int) {}
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000477
reed@google.com0889f682013-05-03 12:56:39 +0000478protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700479 void onDraw(int loops, SkCanvas*) override {
reed@google.com0889f682013-05-03 12:56:39 +0000480 int accum = 0;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000481
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000482 for (int j = 0; j < loops; ++j) {
reed@google.com0889f682013-05-03 12:56:39 +0000483 for (int i = 0; i < ARRAY; ++i) {
484 accum += fVec[i].normalize();
485 }
486 this->process(accum);
487 }
488 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000489
mtkleinf0599002015-07-13 06:18:39 -0700490 const char* onGetName() override {
reed@google.com0889f682013-05-03 12:56:39 +0000491 return fName;
492 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000493
reed@google.com0889f682013-05-03 12:56:39 +0000494private:
495 const char* fName;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000496
tfarinaf168b862014-06-19 12:32:29 -0700497 typedef Benchmark INHERITED;
reed@google.com0889f682013-05-03 12:56:39 +0000498};
499
500///////////////////////////////////////////////////////////////////////////////
501
tfarinaf168b862014-06-19 12:32:29 -0700502class FixedMathBench : public Benchmark {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000503 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000504 N = 1000,
djsollen@google.com25a11e42013-07-18 19:11:30 +0000505 };
506 float fData[N];
507 SkFixed fResult[N];
508public:
509
mtklein@google.com410e6e82013-09-13 19:52:27 +0000510 FixedMathBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000511 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000512 for (int i = 0; i < N; ++i) {
513 fData[i] = rand.nextSScalar1();
djsollen@google.com25a11e42013-07-18 19:11:30 +0000514 }
515
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000516 }
517
mtklein36352bf2015-03-25 18:17:31 -0700518 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000519 return backend == kNonRendering_Backend;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000520 }
521
522protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700523 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000524 for (int j = 0; j < loops; ++j) {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000525 for (int i = 0; i < N - 4; ++i) {
526 fResult[i] = SkFloatToFixed(fData[i]);
527 }
528 }
529
530 SkPaint paint;
531 if (paint.getAlpha() == 0) {
532 SkDebugf("%d\n", fResult[0]);
533 }
534 }
535
mtkleinf0599002015-07-13 06:18:39 -0700536 const char* onGetName() override {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000537 return "float_to_fixed";
538 }
539
540private:
tfarinaf168b862014-06-19 12:32:29 -0700541 typedef Benchmark INHERITED;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000542};
543
544///////////////////////////////////////////////////////////////////////////////
545
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000546template <typename T>
tfarinaf168b862014-06-19 12:32:29 -0700547class DivModBench : public Benchmark {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000548 SkString fName;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000549public:
mtklein@google.com9cb51772013-09-27 13:39:14 +0000550 explicit DivModBench(const char* name) {
551 fName.printf("divmod_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000552 }
553
mtklein36352bf2015-03-25 18:17:31 -0700554 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000555 return backend == kNonRendering_Backend;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000556 }
557
558protected:
mtkleinf0599002015-07-13 06:18:39 -0700559 const char* onGetName() override {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000560 return fName.c_str();
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000561 }
562
mtkleina1ebeb22015-10-01 09:43:39 -0700563 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000564 volatile T a = 0, b = 0;
565 T div = 0, mod = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000566 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000567 if ((T)i == 0) continue; // Small T will wrap around.
568 SkTDivMod((T)(i+1), (T)i, &div, &mod);
569 a ^= div;
570 b ^= mod;
571 }
572 }
573};
574DEF_BENCH(return new DivModBench<uint8_t>("uint8_t"))
575DEF_BENCH(return new DivModBench<uint16_t>("uint16_t"))
576DEF_BENCH(return new DivModBench<uint32_t>("uint32_t"))
577DEF_BENCH(return new DivModBench<uint64_t>("uint64_t"))
578
579DEF_BENCH(return new DivModBench<int8_t>("int8_t"))
580DEF_BENCH(return new DivModBench<int16_t>("int16_t"))
581DEF_BENCH(return new DivModBench<int32_t>("int32_t"))
582DEF_BENCH(return new DivModBench<int64_t>("int64_t"))
583
584///////////////////////////////////////////////////////////////////////////////
585
mtklein@google.com410e6e82013-09-13 19:52:27 +0000586DEF_BENCH( return new NoOpMathBench(); )
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000587DEF_BENCH( return new SkRSqrtMathBench(); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000588DEF_BENCH( return new SlowISqrtMathBench(); )
589DEF_BENCH( return new FastISqrtMathBench(); )
590DEF_BENCH( return new QMul64Bench(); )
591DEF_BENCH( return new QMul32Bench(); )
reed@google.comddc518b2011-08-29 17:49:23 +0000592
mtklein@google.com410e6e82013-09-13 19:52:27 +0000593DEF_BENCH( return new IsFiniteBench(-1); )
594DEF_BENCH( return new IsFiniteBench(0); )
595DEF_BENCH( return new IsFiniteBench(1); )
596DEF_BENCH( return new IsFiniteBench(2); )
597DEF_BENCH( return new IsFiniteBench(3); )
598DEF_BENCH( return new IsFiniteBench(4); )
599DEF_BENCH( return new IsFiniteBench(5); )
reed@google.com0be5eb72011-12-05 21:53:22 +0000600
mtklein@google.com410e6e82013-09-13 19:52:27 +0000601DEF_BENCH( return new FloorBench(false); )
602DEF_BENCH( return new FloorBench(true); )
reed@google.com7f192412012-05-30 12:26:52 +0000603
mtklein@google.com410e6e82013-09-13 19:52:27 +0000604DEF_BENCH( return new CLZBench(false); )
605DEF_BENCH( return new CLZBench(true); )
reed@google.com0889f682013-05-03 12:56:39 +0000606
mtklein@google.com410e6e82013-09-13 19:52:27 +0000607DEF_BENCH( return new NormalizeBench(); )
djsollen@google.com25a11e42013-07-18 19:11:30 +0000608
mtklein@google.com410e6e82013-09-13 19:52:27 +0000609DEF_BENCH( return new FixedMathBench(); )