blob: 8092f13823ac12c603d6b2be50d3de6272911fce [file] [log] [blame]
reed@google.comddc518b2011-08-29 17:49:23 +00001#include "SkBenchmark.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"
4#include "SkRandom.h"
5#include "SkString.h"
reed@google.com16078632011-12-06 18:56:37 +00006#include "SkPaint.h"
reed@google.comddc518b2011-08-29 17:49:23 +00007
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
reed@google.comddc518b2011-08-29 17:49:23 +000018class MathBench : public SkBenchmark {
19 enum {
20 kBuffer = 100,
21 kLoop = 10000
22 };
23 SkString fName;
24 float fSrc[kBuffer], fDst[kBuffer];
25public:
26 MathBench(void* param, const char name[]) : INHERITED(param) {
27 fName.printf("math_%s", name);
28
29 SkRandom rand;
30 for (int i = 0; i < kBuffer; ++i) {
31 fSrc[i] = rand.nextSScalar1();
32 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +000033
34 fIsRendering = false;
reed@google.comddc518b2011-08-29 17:49:23 +000035 }
36
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 virtual void performTest(float* SK_RESTRICT dst,
38 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000039 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000040
41protected:
42 virtual int mulLoopCount() const { return 1; }
43
44 virtual const char* onGetName() {
45 return fName.c_str();
46 }
47
sugoi@google.com77472f02013-03-05 18:50:01 +000048 virtual void onDraw(SkCanvas*) {
tomhudson@google.comca529d32011-10-28 15:34:49 +000049 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
reed@google.comddc518b2011-08-29 17:49:23 +000050 for (int i = 0; i < n; i++) {
51 this->performTest(fDst, fSrc, kBuffer);
52 }
53 }
54
55private:
56 typedef SkBenchmark INHERITED;
57};
58
reed@google.come05cc8e2011-10-10 14:19:40 +000059class MathBenchU32 : public MathBench {
60public:
61 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +000062
reed@google.come05cc8e2011-10-10 14:19:40 +000063protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 virtual void performITest(uint32_t* SK_RESTRICT dst,
65 const uint32_t* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000066 int count) = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +000067
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068 virtual void performTest(float* SK_RESTRICT dst,
reed@google.com0d7aac92013-04-29 13:55:11 +000069 const float* SK_RESTRICT src,
70 int count) SK_OVERRIDE {
reed@google.come05cc8e2011-10-10 14:19:40 +000071 uint32_t* d = SkTCast<uint32_t*>(dst);
72 const uint32_t* s = SkTCast<const uint32_t*>(src);
73 this->performITest(d, s, count);
74 }
75private:
76 typedef MathBench INHERITED;
77};
78
79///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000080
81class NoOpMathBench : public MathBench {
82public:
bungeman@google.com9399cac2011-08-31 19:47:59 +000083 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000084protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085 virtual void performTest(float* SK_RESTRICT dst,
86 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +000087 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +000088 for (int i = 0; i < count; ++i) {
89 dst[i] = src[i] + 1;
90 }
91 }
92private:
93 typedef MathBench INHERITED;
94};
95
96class SlowISqrtMathBench : public MathBench {
97public:
98 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
99protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100 virtual void performTest(float* SK_RESTRICT dst,
101 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000102 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000103 for (int i = 0; i < count; ++i) {
104 dst[i] = 1.0f / sk_float_sqrt(src[i]);
105 }
106 }
107private:
108 typedef MathBench INHERITED;
109};
110
111static inline float SkFastInvSqrt(float x) {
112 float xhalf = 0.5f*x;
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000113 int i = *SkTCast<int*>(&x);
reed@google.comddc518b2011-08-29 17:49:23 +0000114 i = 0x5f3759df - (i>>1);
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000115 x = *SkTCast<float*>(&i);
reed@google.comddc518b2011-08-29 17:49:23 +0000116 x = x*(1.5f-xhalf*x*x);
117// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
118 return x;
119}
120
121class FastISqrtMathBench : public MathBench {
122public:
123 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
124protected:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125 virtual void performTest(float* SK_RESTRICT dst,
126 const float* SK_RESTRICT src,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000127 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000128 for (int i = 0; i < count; ++i) {
129 dst[i] = SkFastInvSqrt(src[i]);
130 }
131 }
132private:
133 typedef MathBench INHERITED;
134};
135
reed@google.come05cc8e2011-10-10 14:19:40 +0000136static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
137 SkASSERT((uint8_t)alpha == alpha);
138 const uint32_t mask = 0xFF00FF;
139
140 uint64_t tmp = value;
141 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
142 tmp *= alpha;
caryclark@google.com19069a22012-06-06 12:11:45 +0000143 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
reed@google.come05cc8e2011-10-10 14:19:40 +0000144}
145
146class QMul64Bench : public MathBenchU32 {
147public:
148 QMul64Bench(void* param) : INHERITED(param, "qmul64") {}
149protected:
150 virtual void performITest(uint32_t* SK_RESTRICT dst,
151 const uint32_t* SK_RESTRICT src,
152 int count) SK_OVERRIDE {
153 for (int i = 0; i < count; ++i) {
154 dst[i] = QMul64(src[i], (uint8_t)i);
155 }
156 }
157private:
158 typedef MathBenchU32 INHERITED;
159};
160
161class QMul32Bench : public MathBenchU32 {
162public:
163 QMul32Bench(void* param) : INHERITED(param, "qmul32") {}
164protected:
165 virtual void performITest(uint32_t* SK_RESTRICT dst,
166 const uint32_t* SK_RESTRICT src,
167 int count) SK_OVERRIDE {
168 for (int i = 0; i < count; ++i) {
169 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
170 }
171 }
172private:
173 typedef MathBenchU32 INHERITED;
174};
175
reed@google.comddc518b2011-08-29 17:49:23 +0000176///////////////////////////////////////////////////////////////////////////////
177
reed@google.com0be5eb72011-12-05 21:53:22 +0000178static bool isFinite_int(float x) {
179 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
180 int exponent = bits << 1 >> 24;
181 return exponent != 0xFF;
182}
183
184static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000185 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000186}
187
188static bool isFinite_mulzero(float x) {
189 float y = x * 0;
190 return y == y;
191}
192
193static bool isfinite_and_int(const float data[4]) {
194 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
195}
196
197static bool isfinite_and_float(const float data[4]) {
198 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
199}
200
201static bool isfinite_and_mulzero(const float data[4]) {
202 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
203}
204
205#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
206
207static bool isfinite_plus_int(const float data[4]) {
208 return isFinite_int(mulzeroadd(data));
209}
210
211static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000212 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000213}
214
215static bool isfinite_plus_mulzero(const float data[4]) {
216 float x = mulzeroadd(data);
217 return x == x;
218}
219
220typedef bool (*IsFiniteProc)(const float[]);
221
222#define MAKEREC(name) { name, #name }
223
224static const struct {
225 IsFiniteProc fProc;
226 const char* fName;
227} gRec[] = {
228 MAKEREC(isfinite_and_int),
229 MAKEREC(isfinite_and_float),
230 MAKEREC(isfinite_and_mulzero),
231 MAKEREC(isfinite_plus_int),
232 MAKEREC(isfinite_plus_float),
233 MAKEREC(isfinite_plus_mulzero),
234};
235
236#undef MAKEREC
237
reed@google.com16078632011-12-06 18:56:37 +0000238static bool isFinite(const SkRect& r) {
239 // x * 0 will be NaN iff x is infinity or NaN.
240 // a + b will be NaN iff either a or b is NaN.
241 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000242
reed@google.com16078632011-12-06 18:56:37 +0000243 // value is either NaN or it is finite (zero).
244 // value==value will be true iff value is not NaN
245 return value == value;
246}
247
reed@google.com0be5eb72011-12-05 21:53:22 +0000248class IsFiniteBench : public SkBenchmark {
249 enum {
250 N = SkBENCHLOOP(1000),
251 NN = SkBENCHLOOP(1000),
252 };
253 float fData[N];
254public:
255
256 IsFiniteBench(void* param, int index) : INHERITED(param) {
257 SkRandom rand;
258
259 for (int i = 0; i < N; ++i) {
260 fData[i] = rand.nextSScalar1();
261 }
reed@google.com16078632011-12-06 18:56:37 +0000262
263 if (index < 0) {
264 fProc = NULL;
265 fName = "isfinite_rect";
266 } else {
267 fProc = gRec[index].fProc;
268 fName = gRec[index].fName;
269 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000270 fIsRendering = false;
reed@google.com0be5eb72011-12-05 21:53:22 +0000271 }
272
273protected:
sugoi@google.com77472f02013-03-05 18:50:01 +0000274 virtual void onDraw(SkCanvas*) {
reed@google.com0be5eb72011-12-05 21:53:22 +0000275 IsFiniteProc proc = fProc;
276 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000277 // do this so the compiler won't throw away the function call
278 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000279
reed@google.com16078632011-12-06 18:56:37 +0000280 if (proc) {
281 for (int j = 0; j < NN; ++j) {
282 for (int i = 0; i < N - 4; ++i) {
283 counter += proc(&data[i]);
284 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000285 }
reed@google.com16078632011-12-06 18:56:37 +0000286 } else {
287 for (int j = 0; j < NN; ++j) {
288 for (int i = 0; i < N - 4; ++i) {
289 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
caryclark@google.com19069a22012-06-06 12:11:45 +0000290 if (false) { // avoid bit rot, suppress warning
291 isFinite(*r);
292 }
reed@google.com16078632011-12-06 18:56:37 +0000293 counter += r->isFinite();
294 }
295 }
296 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000297
reed@google.com16078632011-12-06 18:56:37 +0000298 SkPaint paint;
299 if (paint.getAlpha() == 0) {
300 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000301 }
302 }
303
304 virtual const char* onGetName() {
305 return fName;
306 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000307
reed@google.com0be5eb72011-12-05 21:53:22 +0000308private:
309 IsFiniteProc fProc;
310 const char* fName;
311
312 typedef SkBenchmark INHERITED;
313};
314
reed@google.com7f192412012-05-30 12:26:52 +0000315class FloorBench : public SkBenchmark {
316 enum {
317 ARRAY = SkBENCHLOOP(1000),
318 LOOP = SkBENCHLOOP(1000),
319 };
320 float fData[ARRAY];
321 bool fFast;
322public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000323
reed@google.com7f192412012-05-30 12:26:52 +0000324 FloorBench(void* param, bool fast) : INHERITED(param), fFast(fast) {
325 SkRandom rand;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000326
reed@google.com7f192412012-05-30 12:26:52 +0000327 for (int i = 0; i < ARRAY; ++i) {
328 fData[i] = rand.nextSScalar1();
329 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000330
reed@google.com7f192412012-05-30 12:26:52 +0000331 if (fast) {
332 fName = "floor_fast";
333 } else {
334 fName = "floor_std";
335 }
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000336 fIsRendering = false;
reed@google.com7f192412012-05-30 12:26:52 +0000337 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000338
reed@google.com7f192412012-05-30 12:26:52 +0000339 virtual void process(float) {}
340
341protected:
sugoi@google.com77472f02013-03-05 18:50:01 +0000342 virtual void onDraw(SkCanvas*) {
reed@google.com7f192412012-05-30 12:26:52 +0000343 SkRandom rand;
344 float accum = 0;
345 const float* data = fData;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000346
reed@google.com7f192412012-05-30 12:26:52 +0000347 if (fFast) {
348 for (int j = 0; j < LOOP; ++j) {
349 for (int i = 0; i < ARRAY; ++i) {
350 accum += fast_floor(data[i]);
351 }
352 this->process(accum);
353 }
354 } else {
355 for (int j = 0; j < LOOP; ++j) {
356 for (int i = 0; i < ARRAY; ++i) {
357 accum += sk_float_floor(data[i]);
358 }
359 this->process(accum);
360 }
361 }
362 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000363
reed@google.com7f192412012-05-30 12:26:52 +0000364 virtual const char* onGetName() {
365 return fName;
366 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000367
reed@google.com7f192412012-05-30 12:26:52 +0000368private:
369 const char* fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000370
reed@google.com7f192412012-05-30 12:26:52 +0000371 typedef SkBenchmark INHERITED;
372};
373
reed@google.com0d7aac92013-04-29 13:55:11 +0000374class CLZBench : public SkBenchmark {
375 enum {
376 ARRAY = SkBENCHLOOP(1000),
reed@google.comcb8dce22013-04-29 14:11:23 +0000377 LOOP = SkBENCHLOOP(5000),
reed@google.com0d7aac92013-04-29 13:55:11 +0000378 };
379 uint32_t fData[ARRAY];
380 bool fUsePortable;
381
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000382public:
reed@google.com0d7aac92013-04-29 13:55:11 +0000383 CLZBench(void* param, bool usePortable)
384 : INHERITED(param)
385 , fUsePortable(usePortable) {
386
387 SkRandom rand;
388 for (int i = 0; i < ARRAY; ++i) {
389 fData[i] = rand.nextU();
390 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000391
reed@google.com0d7aac92013-04-29 13:55:11 +0000392 if (fUsePortable) {
393 fName = "clz_portable";
394 } else {
395 fName = "clz_intrinsic";
396 }
397 fIsRendering = false;
398 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000399
reed@google.com0d7aac92013-04-29 13:55:11 +0000400 // just so the compiler doesn't remove our loops
401 virtual void process(int) {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000402
reed@google.com0d7aac92013-04-29 13:55:11 +0000403protected:
404 virtual void onDraw(SkCanvas*) {
405 int accum = 0;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000406
reed@google.com0d7aac92013-04-29 13:55:11 +0000407 if (fUsePortable) {
408 for (int j = 0; j < LOOP; ++j) {
409 for (int i = 0; i < ARRAY; ++i) {
410 accum += SkCLZ_portable(fData[i]);
411 }
412 this->process(accum);
413 }
414 } else {
415 for (int j = 0; j < LOOP; ++j) {
416 for (int i = 0; i < ARRAY; ++i) {
417 accum += SkCLZ(fData[i]);
418 }
419 this->process(accum);
420 }
421 }
422 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000423
reed@google.com0d7aac92013-04-29 13:55:11 +0000424 virtual const char* onGetName() {
425 return fName;
426 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000427
reed@google.com0d7aac92013-04-29 13:55:11 +0000428private:
429 const char* fName;
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000430
reed@google.com0d7aac92013-04-29 13:55:11 +0000431 typedef SkBenchmark INHERITED;
432};
433
reed@google.com0be5eb72011-12-05 21:53:22 +0000434///////////////////////////////////////////////////////////////////////////////
435
reed@google.com0889f682013-05-03 12:56:39 +0000436class NormalizeBench : public SkBenchmark {
437 enum {
438 ARRAY = SkBENCHLOOP(1000),
439 LOOP = SkBENCHLOOP(1000),
440 };
441 SkVector fVec[ARRAY];
442 bool fUsePortable;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000443
reed@google.com0889f682013-05-03 12:56:39 +0000444public:
445 NormalizeBench(void* param, bool usePortable)
446 : INHERITED(param)
447 , fUsePortable(usePortable) {
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000448
reed@google.com0889f682013-05-03 12:56:39 +0000449 SkRandom rand;
450 for (int i = 0; i < ARRAY; ++i) {
451 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
452 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000453
reed@google.com0889f682013-05-03 12:56:39 +0000454 fName = "point_normalize";
455 fIsRendering = false;
456 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000457
reed@google.com0889f682013-05-03 12:56:39 +0000458 // just so the compiler doesn't remove our loops
459 virtual void process(int) {}
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000460
reed@google.com0889f682013-05-03 12:56:39 +0000461protected:
462 virtual void onDraw(SkCanvas*) {
463 int accum = 0;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000464
reed@google.com0889f682013-05-03 12:56:39 +0000465 for (int j = 0; j < LOOP; ++j) {
466 for (int i = 0; i < ARRAY; ++i) {
467 accum += fVec[i].normalize();
468 }
469 this->process(accum);
470 }
471 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000472
reed@google.com0889f682013-05-03 12:56:39 +0000473 virtual const char* onGetName() {
474 return fName;
475 }
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000476
reed@google.com0889f682013-05-03 12:56:39 +0000477private:
478 const char* fName;
skia.committer@gmail.comecc9d282013-05-04 07:01:15 +0000479
reed@google.com0889f682013-05-03 12:56:39 +0000480 typedef SkBenchmark INHERITED;
481};
482
483///////////////////////////////////////////////////////////////////////////////
484
reed@google.com553ad652013-04-29 13:48:34 +0000485DEF_BENCH( return new NoOpMathBench(p); )
486DEF_BENCH( return new SlowISqrtMathBench(p); )
487DEF_BENCH( return new FastISqrtMathBench(p); )
488DEF_BENCH( return new QMul64Bench(p); )
489DEF_BENCH( return new QMul32Bench(p); )
reed@google.comddc518b2011-08-29 17:49:23 +0000490
reed@google.com553ad652013-04-29 13:48:34 +0000491DEF_BENCH( return new IsFiniteBench(p, -1); )
492DEF_BENCH( return new IsFiniteBench(p, 0); )
493DEF_BENCH( return new IsFiniteBench(p, 1); )
494DEF_BENCH( return new IsFiniteBench(p, 2); )
495DEF_BENCH( return new IsFiniteBench(p, 3); )
496DEF_BENCH( return new IsFiniteBench(p, 4); )
497DEF_BENCH( return new IsFiniteBench(p, 5); )
reed@google.com0be5eb72011-12-05 21:53:22 +0000498
reed@google.com553ad652013-04-29 13:48:34 +0000499DEF_BENCH( return new FloorBench(p, false); )
500DEF_BENCH( return new FloorBench(p, true); )
reed@google.com7f192412012-05-30 12:26:52 +0000501
reed@google.com0d7aac92013-04-29 13:55:11 +0000502DEF_BENCH( return new CLZBench(p, false); )
503DEF_BENCH( return new CLZBench(p, true); )
reed@google.com0889f682013-05-03 12:56:39 +0000504
505DEF_BENCH( return new NormalizeBench(p, false); )