blob: 5b46d1c704c835e6390a7df60ee50a03206cc5c4 [file] [log] [blame]
tfarinaf168b862014-06-19 12:32:29 -07001#include "Benchmark.h"
reed@google.come05cc8e2011-10-10 14:19:40 +00002#include "SkColorPriv.h"
reed@google.comddc518b2011-08-29 17:49:23 +00003#include "SkMatrix.h"
tfarinaf168b862014-06-19 12:32:29 -07004#include "SkPaint.h"
reed@google.comddc518b2011-08-29 17:49:23 +00005#include "SkRandom.h"
6#include "SkString.h"
7
reed@google.com7f192412012-05-30 12:26:52 +00008static float sk_fsel(float pred, float result_ge, float result_lt) {
9 return pred >= 0 ? result_ge : result_lt;
10}
11
12static float fast_floor(float x) {
reed@google.comf3a8d8e2012-05-30 14:08:57 +000013// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
14 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
reed@google.com7f192412012-05-30 12:26:52 +000015 return (x + big) - big;
16}
17
tfarinaf168b862014-06-19 12:32:29 -070018class MathBench : public Benchmark {
reed@google.comddc518b2011-08-29 17:49:23 +000019 enum {
20 kBuffer = 100,
reed@google.comddc518b2011-08-29 17:49:23 +000021 };
22 SkString fName;
23 float fSrc[kBuffer], fDst[kBuffer];
24public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000025 MathBench(const char name[]) {
reed@google.comddc518b2011-08-29 17:49:23 +000026 fName.printf("math_%s", name);
27
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000028 SkRandom rand;
reed@google.comddc518b2011-08-29 17:49:23 +000029 for (int i = 0; i < kBuffer; ++i) {
30 fSrc[i] = rand.nextSScalar1();
31 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000032 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +000033
mtklein72c9faa2015-01-09 10:06:39 -080034 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 return backend == kNonRendering_Backend;
reed@google.comddc518b2011-08-29 17:49:23 +000036 }
37
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038 virtual void performTest(float* SK_RESTRICT dst,
39 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000040 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000041
42protected:
43 virtual int mulLoopCount() const { return 1; }
44
45 virtual const char* onGetName() {
46 return fName.c_str();
47 }
48
commit-bot@chromium.org33614712013-12-03 18:17:16 +000049 virtual void onDraw(const int loops, SkCanvas*) {
50 int n = loops * this->mulLoopCount();
reed@google.comddc518b2011-08-29 17:49:23 +000051 for (int i = 0; i < n; i++) {
52 this->performTest(fDst, fSrc, kBuffer);
53 }
54 }
55
56private:
tfarinaf168b862014-06-19 12:32:29 -070057 typedef Benchmark INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +000058};
59
reed@google.come05cc8e2011-10-10 14:19:40 +000060class MathBenchU32 : public MathBench {
61public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000062 MathBenchU32(const char name[]) : INHERITED(name) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +000063
reed@google.come05cc8e2011-10-10 14:19:40 +000064protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065 virtual void performITest(uint32_t* SK_RESTRICT dst,
66 const uint32_t* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000067 int count) = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +000068
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069 virtual void performTest(float* SK_RESTRICT dst,
reed@google.com0d7aac92013-04-29 13:55:11 +000070 const float* SK_RESTRICT src,
71 int count) SK_OVERRIDE {
reed@google.come05cc8e2011-10-10 14:19:40 +000072 uint32_t* d = SkTCast<uint32_t*>(dst);
73 const uint32_t* s = SkTCast<const uint32_t*>(src);
74 this->performITest(d, s, count);
75 }
76private:
77 typedef MathBench INHERITED;
78};
79
80///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000081
82class NoOpMathBench : public MathBench {
83public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000084 NoOpMathBench() : INHERITED("noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000085protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086 virtual void performTest(float* SK_RESTRICT dst,
87 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000088 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +000089 for (int i = 0; i < count; ++i) {
90 dst[i] = src[i] + 1;
91 }
92 }
93private:
94 typedef MathBench INHERITED;
95};
96
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +000097class SkRSqrtMathBench : public MathBench {
98public:
99 SkRSqrtMathBench() : INHERITED("sk_float_rsqrt") {}
100protected:
101 virtual void performTest(float* SK_RESTRICT dst,
102 const float* SK_RESTRICT src,
103 int count) {
104 for (int i = 0; i < count; ++i) {
105 dst[i] = sk_float_rsqrt(src[i]);
106 }
107 }
108private:
109 typedef MathBench INHERITED;
110};
111
112
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000113class SlowISqrtMathBench : public MathBench {
reed@google.comddc518b2011-08-29 17:49:23 +0000114public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000115 SlowISqrtMathBench() : INHERITED("slowIsqrt") {}
commit-bot@chromium.orgb3ecdc42013-08-12 08:37:51 +0000116protected:
robertphillips@google.com36bb2702013-08-12 12:02:28 +0000117 virtual void performTest(float* SK_RESTRICT dst,
118 const float* SK_RESTRICT src,
119 int count) {
120 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;
130 int i = *SkTCast<int*>(&x);
131 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:
142 virtual void performTest(float* SK_RESTRICT dst,
143 const float* SK_RESTRICT src,
144 int count) {
145 for (int i = 0; i < count; ++i) {
146 dst[i] = SkFastInvSqrt(src[i]);
147 }
148 }
149private:
150 typedef MathBench INHERITED;
reed@google.comddc518b2011-08-29 17:49:23 +0000151};
152
reed@google.come05cc8e2011-10-10 14:19:40 +0000153static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
154 SkASSERT((uint8_t)alpha == alpha);
155 const uint32_t mask = 0xFF00FF;
156
157 uint64_t tmp = value;
158 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
159 tmp *= alpha;
caryclark@google.com19069a22012-06-06 12:11:45 +0000160 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
reed@google.come05cc8e2011-10-10 14:19:40 +0000161}
162
163class QMul64Bench : public MathBenchU32 {
164public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000165 QMul64Bench() : INHERITED("qmul64") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000166protected:
167 virtual void performITest(uint32_t* SK_RESTRICT dst,
168 const uint32_t* SK_RESTRICT src,
169 int count) SK_OVERRIDE {
170 for (int i = 0; i < count; ++i) {
171 dst[i] = QMul64(src[i], (uint8_t)i);
172 }
173 }
174private:
175 typedef MathBenchU32 INHERITED;
176};
177
178class QMul32Bench : public MathBenchU32 {
179public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000180 QMul32Bench() : INHERITED("qmul32") {}
reed@google.come05cc8e2011-10-10 14:19:40 +0000181protected:
182 virtual void performITest(uint32_t* SK_RESTRICT dst,
183 const uint32_t* SK_RESTRICT src,
184 int count) SK_OVERRIDE {
185 for (int i = 0; i < count; ++i) {
186 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
187 }
188 }
189private:
190 typedef MathBenchU32 INHERITED;
191};
192
reed@google.comddc518b2011-08-29 17:49:23 +0000193///////////////////////////////////////////////////////////////////////////////
194
reed@google.com0be5eb72011-12-05 21:53:22 +0000195static bool isFinite_int(float x) {
196 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
197 int exponent = bits << 1 >> 24;
198 return exponent != 0xFF;
199}
200
201static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000202 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000203}
204
205static bool isFinite_mulzero(float x) {
206 float y = x * 0;
207 return y == y;
208}
209
210static bool isfinite_and_int(const float data[4]) {
211 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
212}
213
214static bool isfinite_and_float(const float data[4]) {
215 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
216}
217
218static bool isfinite_and_mulzero(const float data[4]) {
219 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
220}
221
222#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
223
224static bool isfinite_plus_int(const float data[4]) {
225 return isFinite_int(mulzeroadd(data));
226}
227
228static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000229 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000230}
231
232static bool isfinite_plus_mulzero(const float data[4]) {
233 float x = mulzeroadd(data);
234 return x == x;
235}
236
237typedef bool (*IsFiniteProc)(const float[]);
238
239#define MAKEREC(name) { name, #name }
240
241static const struct {
242 IsFiniteProc fProc;
243 const char* fName;
244} gRec[] = {
245 MAKEREC(isfinite_and_int),
246 MAKEREC(isfinite_and_float),
247 MAKEREC(isfinite_and_mulzero),
248 MAKEREC(isfinite_plus_int),
249 MAKEREC(isfinite_plus_float),
250 MAKEREC(isfinite_plus_mulzero),
251};
252
253#undef MAKEREC
254
reed@google.com16078632011-12-06 18:56:37 +0000255static bool isFinite(const SkRect& r) {
256 // x * 0 will be NaN iff x is infinity or NaN.
257 // a + b will be NaN iff either a or b is NaN.
258 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259
reed@google.com16078632011-12-06 18:56:37 +0000260 // value is either NaN or it is finite (zero).
261 // value==value will be true iff value is not NaN
262 return value == value;
263}
264
tfarinaf168b862014-06-19 12:32:29 -0700265class IsFiniteBench : public Benchmark {
reed@google.com0be5eb72011-12-05 21:53:22 +0000266 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000267 N = 1000,
reed@google.com0be5eb72011-12-05 21:53:22 +0000268 };
269 float fData[N];
270public:
271
mtklein@google.com410e6e82013-09-13 19:52:27 +0000272 IsFiniteBench(int index) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000273 SkRandom rand;
reed@google.com0be5eb72011-12-05 21:53:22 +0000274
275 for (int i = 0; i < N; ++i) {
276 fData[i] = rand.nextSScalar1();
277 }
reed@google.com16078632011-12-06 18:56:37 +0000278
279 if (index < 0) {
280 fProc = NULL;
281 fName = "isfinite_rect";
282 } else {
283 fProc = gRec[index].fProc;
284 fName = gRec[index].fName;
285 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000286 }
287
mtklein72c9faa2015-01-09 10:06:39 -0800288 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000289 return backend == kNonRendering_Backend;
reed@google.com0be5eb72011-12-05 21:53:22 +0000290 }
291
292protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000293 virtual void onDraw(const int loops, SkCanvas*) {
reed@google.com0be5eb72011-12-05 21:53:22 +0000294 IsFiniteProc proc = fProc;
295 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000296 // do this so the compiler won't throw away the function call
297 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000298
reed@google.com16078632011-12-06 18:56:37 +0000299 if (proc) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000300 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000301 for (int i = 0; i < N - 4; ++i) {
302 counter += proc(&data[i]);
303 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000304 }
reed@google.com16078632011-12-06 18:56:37 +0000305 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000306 for (int j = 0; j < loops; ++j) {
reed@google.com16078632011-12-06 18:56:37 +0000307 for (int i = 0; i < N - 4; ++i) {
308 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
caryclark@google.com19069a22012-06-06 12:11:45 +0000309 if (false) { // avoid bit rot, suppress warning
310 isFinite(*r);
311 }
reed@google.com16078632011-12-06 18:56:37 +0000312 counter += r->isFinite();
313 }
314 }
315 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000316
reed@google.com16078632011-12-06 18:56:37 +0000317 SkPaint paint;
318 if (paint.getAlpha() == 0) {
319 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000320 }
321 }
322
323 virtual const char* onGetName() {
324 return fName;
325 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000326
reed@google.com0be5eb72011-12-05 21:53:22 +0000327private:
328 IsFiniteProc fProc;
329 const char* fName;
330
tfarinaf168b862014-06-19 12:32:29 -0700331 typedef Benchmark INHERITED;
reed@google.com0be5eb72011-12-05 21:53:22 +0000332};
333
tfarinaf168b862014-06-19 12:32:29 -0700334class FloorBench : public Benchmark {
reed@google.com7f192412012-05-30 12:26:52 +0000335 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000336 ARRAY = 1000,
reed@google.com7f192412012-05-30 12:26:52 +0000337 };
338 float fData[ARRAY];
339 bool fFast;
340public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000341
mtklein@google.com410e6e82013-09-13 19:52:27 +0000342 FloorBench(bool fast) : fFast(fast) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000343 SkRandom rand;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000344
reed@google.com7f192412012-05-30 12:26:52 +0000345 for (int i = 0; i < ARRAY; ++i) {
346 fData[i] = rand.nextSScalar1();
347 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000348
reed@google.com7f192412012-05-30 12:26:52 +0000349 if (fast) {
350 fName = "floor_fast";
351 } else {
352 fName = "floor_std";
353 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000354 }
355
mtklein72c9faa2015-01-09 10:06:39 -0800356 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000357 return backend == kNonRendering_Backend;
reed@google.com7f192412012-05-30 12:26:52 +0000358 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000359
reed@google.com7f192412012-05-30 12:26:52 +0000360 virtual void process(float) {}
361
362protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000363 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000364 SkRandom rand;
reed@google.com7f192412012-05-30 12:26:52 +0000365 float accum = 0;
366 const float* data = fData;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000367
reed@google.com7f192412012-05-30 12:26:52 +0000368 if (fFast) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000369 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000370 for (int i = 0; i < ARRAY; ++i) {
371 accum += fast_floor(data[i]);
372 }
373 this->process(accum);
374 }
375 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000376 for (int j = 0; j < loops; ++j) {
reed@google.com7f192412012-05-30 12:26:52 +0000377 for (int i = 0; i < ARRAY; ++i) {
378 accum += sk_float_floor(data[i]);
379 }
380 this->process(accum);
381 }
382 }
383 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000384
reed@google.com7f192412012-05-30 12:26:52 +0000385 virtual const char* onGetName() {
386 return fName;
387 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000388
reed@google.com7f192412012-05-30 12:26:52 +0000389private:
390 const char* fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000391
tfarinaf168b862014-06-19 12:32:29 -0700392 typedef Benchmark INHERITED;
reed@google.com7f192412012-05-30 12:26:52 +0000393};
394
tfarinaf168b862014-06-19 12:32:29 -0700395class CLZBench : public Benchmark {
reed@google.com0d7aac92013-04-29 13:55:11 +0000396 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000397 ARRAY = 1000,
reed@google.com0d7aac92013-04-29 13:55:11 +0000398 };
399 uint32_t fData[ARRAY];
400 bool fUsePortable;
401
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000402public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000403 CLZBench(bool usePortable) : fUsePortable(usePortable) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000404
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000405 SkRandom rand;
reed@google.com0d7aac92013-04-29 13:55:11 +0000406 for (int i = 0; i < ARRAY; ++i) {
407 fData[i] = rand.nextU();
408 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000409
reed@google.com0d7aac92013-04-29 13:55:11 +0000410 if (fUsePortable) {
411 fName = "clz_portable";
412 } else {
413 fName = "clz_intrinsic";
414 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000415 }
416
mtklein72c9faa2015-01-09 10:06:39 -0800417 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000418 return backend == kNonRendering_Backend;
reed@google.com0d7aac92013-04-29 13:55:11 +0000419 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000420
reed@google.com0d7aac92013-04-29 13:55:11 +0000421 // just so the compiler doesn't remove our loops
422 virtual void process(int) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000423
reed@google.com0d7aac92013-04-29 13:55:11 +0000424protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000425 virtual void onDraw(const int loops, SkCanvas*) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000426 int accum = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000427
reed@google.com0d7aac92013-04-29 13:55:11 +0000428 if (fUsePortable) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000429 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000430 for (int i = 0; i < ARRAY; ++i) {
431 accum += SkCLZ_portable(fData[i]);
432 }
433 this->process(accum);
434 }
435 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000436 for (int j = 0; j < loops; ++j) {
reed@google.com0d7aac92013-04-29 13:55:11 +0000437 for (int i = 0; i < ARRAY; ++i) {
438 accum += SkCLZ(fData[i]);
439 }
440 this->process(accum);
441 }
442 }
443 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000444
reed@google.com0d7aac92013-04-29 13:55:11 +0000445 virtual const char* onGetName() {
446 return fName;
447 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000448
reed@google.com0d7aac92013-04-29 13:55:11 +0000449private:
450 const char* fName;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000451
tfarinaf168b862014-06-19 12:32:29 -0700452 typedef Benchmark INHERITED;
reed@google.com0d7aac92013-04-29 13:55:11 +0000453};
454
reed@google.com0be5eb72011-12-05 21:53:22 +0000455///////////////////////////////////////////////////////////////////////////////
456
tfarinaf168b862014-06-19 12:32:29 -0700457class NormalizeBench : public Benchmark {
reed@google.com0889f682013-05-03 12:56:39 +0000458 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000459 ARRAY =1000,
reed@google.com0889f682013-05-03 12:56:39 +0000460 };
461 SkVector fVec[ARRAY];
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000462
reed@google.com0889f682013-05-03 12:56:39 +0000463public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000464 NormalizeBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000465 SkRandom rand;
reed@google.com0889f682013-05-03 12:56:39 +0000466 for (int i = 0; i < ARRAY; ++i) {
467 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
468 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000469
reed@google.com0889f682013-05-03 12:56:39 +0000470 fName = "point_normalize";
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000471 }
472
mtklein72c9faa2015-01-09 10:06:39 -0800473 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000474 return backend == kNonRendering_Backend;
reed@google.com0889f682013-05-03 12:56:39 +0000475 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000476
reed@google.com0889f682013-05-03 12:56:39 +0000477 // just so the compiler doesn't remove our loops
478 virtual void process(int) {}
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000479
reed@google.com0889f682013-05-03 12:56:39 +0000480protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000481 virtual void onDraw(const int loops, SkCanvas*) {
reed@google.com0889f682013-05-03 12:56:39 +0000482 int accum = 0;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000483
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000484 for (int j = 0; j < loops; ++j) {
reed@google.com0889f682013-05-03 12:56:39 +0000485 for (int i = 0; i < ARRAY; ++i) {
486 accum += fVec[i].normalize();
487 }
488 this->process(accum);
489 }
490 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000491
reed@google.com0889f682013-05-03 12:56:39 +0000492 virtual const char* onGetName() {
493 return fName;
494 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000495
reed@google.com0889f682013-05-03 12:56:39 +0000496private:
497 const char* fName;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000498
tfarinaf168b862014-06-19 12:32:29 -0700499 typedef Benchmark INHERITED;
reed@google.com0889f682013-05-03 12:56:39 +0000500};
501
502///////////////////////////////////////////////////////////////////////////////
503
tfarinaf168b862014-06-19 12:32:29 -0700504class FixedMathBench : public Benchmark {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000505 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000506 N = 1000,
djsollen@google.com25a11e42013-07-18 19:11:30 +0000507 };
508 float fData[N];
509 SkFixed fResult[N];
510public:
511
mtklein@google.com410e6e82013-09-13 19:52:27 +0000512 FixedMathBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000513 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000514 for (int i = 0; i < N; ++i) {
515 fData[i] = rand.nextSScalar1();
djsollen@google.com25a11e42013-07-18 19:11:30 +0000516 }
517
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000518 }
519
mtklein72c9faa2015-01-09 10:06:39 -0800520 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000521 return backend == kNonRendering_Backend;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000522 }
523
524protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000525 virtual void onDraw(const int loops, SkCanvas*) {
526 for (int j = 0; j < loops; ++j) {
djsollen@google.com25a11e42013-07-18 19:11:30 +0000527 for (int i = 0; i < N - 4; ++i) {
528 fResult[i] = SkFloatToFixed(fData[i]);
529 }
530 }
531
532 SkPaint paint;
533 if (paint.getAlpha() == 0) {
534 SkDebugf("%d\n", fResult[0]);
535 }
536 }
537
538 virtual const char* onGetName() {
539 return "float_to_fixed";
540 }
541
542private:
tfarinaf168b862014-06-19 12:32:29 -0700543 typedef Benchmark INHERITED;
djsollen@google.com25a11e42013-07-18 19:11:30 +0000544};
545
546///////////////////////////////////////////////////////////////////////////////
547
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000548template <typename T>
tfarinaf168b862014-06-19 12:32:29 -0700549class DivModBench : public Benchmark {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000550 SkString fName;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000551public:
mtklein@google.com9cb51772013-09-27 13:39:14 +0000552 explicit DivModBench(const char* name) {
553 fName.printf("divmod_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000554 }
555
mtklein72c9faa2015-01-09 10:06:39 -0800556 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000557 return backend == kNonRendering_Backend;
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000558 }
559
560protected:
561 virtual const char* onGetName() {
mtklein@google.com9cb51772013-09-27 13:39:14 +0000562 return fName.c_str();
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000563 }
564
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000565 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000566 volatile T a = 0, b = 0;
567 T div = 0, mod = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000568 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000569 if ((T)i == 0) continue; // Small T will wrap around.
570 SkTDivMod((T)(i+1), (T)i, &div, &mod);
571 a ^= div;
572 b ^= mod;
573 }
574 }
575};
576DEF_BENCH(return new DivModBench<uint8_t>("uint8_t"))
577DEF_BENCH(return new DivModBench<uint16_t>("uint16_t"))
578DEF_BENCH(return new DivModBench<uint32_t>("uint32_t"))
579DEF_BENCH(return new DivModBench<uint64_t>("uint64_t"))
580
581DEF_BENCH(return new DivModBench<int8_t>("int8_t"))
582DEF_BENCH(return new DivModBench<int16_t>("int16_t"))
583DEF_BENCH(return new DivModBench<int32_t>("int32_t"))
584DEF_BENCH(return new DivModBench<int64_t>("int64_t"))
585
586///////////////////////////////////////////////////////////////////////////////
587
mtklein@google.com410e6e82013-09-13 19:52:27 +0000588DEF_BENCH( return new NoOpMathBench(); )
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000589DEF_BENCH( return new SkRSqrtMathBench(); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000590DEF_BENCH( return new SlowISqrtMathBench(); )
591DEF_BENCH( return new FastISqrtMathBench(); )
592DEF_BENCH( return new QMul64Bench(); )
593DEF_BENCH( return new QMul32Bench(); )
reed@google.comddc518b2011-08-29 17:49:23 +0000594
mtklein@google.com410e6e82013-09-13 19:52:27 +0000595DEF_BENCH( return new IsFiniteBench(-1); )
596DEF_BENCH( return new IsFiniteBench(0); )
597DEF_BENCH( return new IsFiniteBench(1); )
598DEF_BENCH( return new IsFiniteBench(2); )
599DEF_BENCH( return new IsFiniteBench(3); )
600DEF_BENCH( return new IsFiniteBench(4); )
601DEF_BENCH( return new IsFiniteBench(5); )
reed@google.com0be5eb72011-12-05 21:53:22 +0000602
mtklein@google.com410e6e82013-09-13 19:52:27 +0000603DEF_BENCH( return new FloorBench(false); )
604DEF_BENCH( return new FloorBench(true); )
reed@google.com7f192412012-05-30 12:26:52 +0000605
mtklein@google.com410e6e82013-09-13 19:52:27 +0000606DEF_BENCH( return new CLZBench(false); )
607DEF_BENCH( return new CLZBench(true); )
reed@google.com0889f682013-05-03 12:56:39 +0000608
mtklein@google.com410e6e82013-09-13 19:52:27 +0000609DEF_BENCH( return new NormalizeBench(); )
djsollen@google.com25a11e42013-07-18 19:11:30 +0000610
mtklein@google.com410e6e82013-09-13 19:52:27 +0000611DEF_BENCH( return new FixedMathBench(); )