blob: 0e35940bfa0ca74264b1cee0d990ec04ec9cfa27 [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 }
33 }
34
robertphillips@google.com6853e802012-04-16 15:50:18 +000035 virtual void performTest(float* SK_RESTRICT dst,
36 const float* SK_RESTRICT src,
37 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000038
39protected:
40 virtual int mulLoopCount() const { return 1; }
41
42 virtual const char* onGetName() {
43 return fName.c_str();
44 }
45
46 virtual void onDraw(SkCanvas* canvas) {
tomhudson@google.comca529d32011-10-28 15:34:49 +000047 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
reed@google.comddc518b2011-08-29 17:49:23 +000048 for (int i = 0; i < n; i++) {
49 this->performTest(fDst, fSrc, kBuffer);
50 }
51 }
52
53private:
54 typedef SkBenchmark INHERITED;
55};
56
reed@google.come05cc8e2011-10-10 14:19:40 +000057class MathBenchU32 : public MathBench {
58public:
59 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {}
reed@google.comddc518b2011-08-29 17:49:23 +000060
reed@google.come05cc8e2011-10-10 14:19:40 +000061protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000062 virtual void performITest(uint32_t* SK_RESTRICT dst,
63 const uint32_t* SK_RESTRICT src,
64 int count) = 0;
reed@google.come05cc8e2011-10-10 14:19:40 +000065
robertphillips@google.com6853e802012-04-16 15:50:18 +000066 virtual void performTest(float* SK_RESTRICT dst,
67 const float* SK_RESTRICT src,
68 int count) SK_OVERRIDE {
reed@google.come05cc8e2011-10-10 14:19:40 +000069 uint32_t* d = SkTCast<uint32_t*>(dst);
70 const uint32_t* s = SkTCast<const uint32_t*>(src);
71 this->performITest(d, s, count);
72 }
73private:
74 typedef MathBench INHERITED;
75};
76
77///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000078
79class NoOpMathBench : public MathBench {
80public:
bungeman@google.com9399cac2011-08-31 19:47:59 +000081 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000082protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000083 virtual void performTest(float* SK_RESTRICT dst,
84 const float* SK_RESTRICT src,
85 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +000086 for (int i = 0; i < count; ++i) {
87 dst[i] = src[i] + 1;
88 }
89 }
90private:
91 typedef MathBench INHERITED;
92};
93
94class SlowISqrtMathBench : public MathBench {
95public:
96 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
97protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000098 virtual void performTest(float* SK_RESTRICT dst,
99 const float* SK_RESTRICT src,
100 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000101 for (int i = 0; i < count; ++i) {
102 dst[i] = 1.0f / sk_float_sqrt(src[i]);
103 }
104 }
105private:
106 typedef MathBench INHERITED;
107};
108
109static inline float SkFastInvSqrt(float x) {
110 float xhalf = 0.5f*x;
111 int i = *(int*)&x;
112 i = 0x5f3759df - (i>>1);
113 x = *(float*)&i;
114 x = x*(1.5f-xhalf*x*x);
115// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
116 return x;
117}
118
119class FastISqrtMathBench : public MathBench {
120public:
121 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
122protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +0000123 virtual void performTest(float* SK_RESTRICT dst,
124 const float* SK_RESTRICT src,
125 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000126 for (int i = 0; i < count; ++i) {
127 dst[i] = SkFastInvSqrt(src[i]);
128 }
129 }
130private:
131 typedef MathBench INHERITED;
132};
133
reed@google.come05cc8e2011-10-10 14:19:40 +0000134static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
135 SkASSERT((uint8_t)alpha == alpha);
136 const uint32_t mask = 0xFF00FF;
137
138 uint64_t tmp = value;
139 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
140 tmp *= alpha;
141 return ((tmp >> 8) & mask) | ((tmp >> 32) & ~mask);
142}
143
144class QMul64Bench : public MathBenchU32 {
145public:
146 QMul64Bench(void* param) : INHERITED(param, "qmul64") {}
147protected:
148 virtual void performITest(uint32_t* SK_RESTRICT dst,
149 const uint32_t* SK_RESTRICT src,
150 int count) SK_OVERRIDE {
151 for (int i = 0; i < count; ++i) {
152 dst[i] = QMul64(src[i], (uint8_t)i);
153 }
154 }
155private:
156 typedef MathBenchU32 INHERITED;
157};
158
159class QMul32Bench : public MathBenchU32 {
160public:
161 QMul32Bench(void* param) : INHERITED(param, "qmul32") {}
162protected:
163 virtual void performITest(uint32_t* SK_RESTRICT dst,
164 const uint32_t* SK_RESTRICT src,
165 int count) SK_OVERRIDE {
166 for (int i = 0; i < count; ++i) {
167 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
168 }
169 }
170private:
171 typedef MathBenchU32 INHERITED;
172};
173
reed@google.comddc518b2011-08-29 17:49:23 +0000174///////////////////////////////////////////////////////////////////////////////
175
reed@google.com0be5eb72011-12-05 21:53:22 +0000176static bool isFinite_int(float x) {
177 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
178 int exponent = bits << 1 >> 24;
179 return exponent != 0xFF;
180}
181
182static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000183 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000184}
185
186static bool isFinite_mulzero(float x) {
187 float y = x * 0;
188 return y == y;
189}
190
191static bool isfinite_and_int(const float data[4]) {
192 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
193}
194
195static bool isfinite_and_float(const float data[4]) {
196 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
197}
198
199static bool isfinite_and_mulzero(const float data[4]) {
200 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
201}
202
203#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
204
205static bool isfinite_plus_int(const float data[4]) {
206 return isFinite_int(mulzeroadd(data));
207}
208
209static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000210 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000211}
212
213static bool isfinite_plus_mulzero(const float data[4]) {
214 float x = mulzeroadd(data);
215 return x == x;
216}
217
218typedef bool (*IsFiniteProc)(const float[]);
219
220#define MAKEREC(name) { name, #name }
221
222static const struct {
223 IsFiniteProc fProc;
224 const char* fName;
225} gRec[] = {
226 MAKEREC(isfinite_and_int),
227 MAKEREC(isfinite_and_float),
228 MAKEREC(isfinite_and_mulzero),
229 MAKEREC(isfinite_plus_int),
230 MAKEREC(isfinite_plus_float),
231 MAKEREC(isfinite_plus_mulzero),
232};
233
234#undef MAKEREC
235
reed@google.com16078632011-12-06 18:56:37 +0000236static bool isFinite(const SkRect& r) {
237 // x * 0 will be NaN iff x is infinity or NaN.
238 // a + b will be NaN iff either a or b is NaN.
239 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
240
241 // value is either NaN or it is finite (zero).
242 // value==value will be true iff value is not NaN
243 return value == value;
244}
245
reed@google.com0be5eb72011-12-05 21:53:22 +0000246class IsFiniteBench : public SkBenchmark {
247 enum {
248 N = SkBENCHLOOP(1000),
249 NN = SkBENCHLOOP(1000),
250 };
251 float fData[N];
252public:
253
254 IsFiniteBench(void* param, int index) : INHERITED(param) {
255 SkRandom rand;
256
257 for (int i = 0; i < N; ++i) {
258 fData[i] = rand.nextSScalar1();
259 }
reed@google.com16078632011-12-06 18:56:37 +0000260
261 if (index < 0) {
262 fProc = NULL;
263 fName = "isfinite_rect";
264 } else {
265 fProc = gRec[index].fProc;
266 fName = gRec[index].fName;
267 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000268 }
269
270protected:
271 virtual void onDraw(SkCanvas* canvas) {
272 IsFiniteProc proc = fProc;
273 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000274 // do this so the compiler won't throw away the function call
275 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000276
reed@google.com16078632011-12-06 18:56:37 +0000277 if (proc) {
278 for (int j = 0; j < NN; ++j) {
279 for (int i = 0; i < N - 4; ++i) {
280 counter += proc(&data[i]);
281 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000282 }
reed@google.com16078632011-12-06 18:56:37 +0000283 } else {
284 for (int j = 0; j < NN; ++j) {
285 for (int i = 0; i < N - 4; ++i) {
286 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
287 counter += r->isFinite();
288 }
289 }
290 }
291
292 SkPaint paint;
293 if (paint.getAlpha() == 0) {
294 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000295 }
296 }
297
298 virtual const char* onGetName() {
299 return fName;
300 }
301
302private:
303 IsFiniteProc fProc;
304 const char* fName;
305
306 typedef SkBenchmark INHERITED;
307};
308
reed@google.com7f192412012-05-30 12:26:52 +0000309class FloorBench : public SkBenchmark {
310 enum {
311 ARRAY = SkBENCHLOOP(1000),
312 LOOP = SkBENCHLOOP(1000),
313 };
314 float fData[ARRAY];
315 bool fFast;
316public:
317
318 FloorBench(void* param, bool fast) : INHERITED(param), fFast(fast) {
319 SkRandom rand;
320
321 for (int i = 0; i < ARRAY; ++i) {
322 fData[i] = rand.nextSScalar1();
323 }
324
325 if (fast) {
326 fName = "floor_fast";
327 } else {
328 fName = "floor_std";
329 }
330 }
331
332 virtual void process(float) {}
333
334protected:
335 virtual void onDraw(SkCanvas* canvas) {
336 SkRandom rand;
337 float accum = 0;
338 const float* data = fData;
339 float tmp[ARRAY] = {};
340
341 if (fFast) {
342 for (int j = 0; j < LOOP; ++j) {
343 for (int i = 0; i < ARRAY; ++i) {
344 accum += fast_floor(data[i]);
345 }
346 this->process(accum);
347 }
348 } else {
349 for (int j = 0; j < LOOP; ++j) {
350 for (int i = 0; i < ARRAY; ++i) {
351 accum += sk_float_floor(data[i]);
352 }
353 this->process(accum);
354 }
355 }
356 }
357
358 virtual const char* onGetName() {
359 return fName;
360 }
361
362private:
363 const char* fName;
364
365 typedef SkBenchmark INHERITED;
366};
367
reed@google.com0be5eb72011-12-05 21:53:22 +0000368///////////////////////////////////////////////////////////////////////////////
369
reed@google.comddc518b2011-08-29 17:49:23 +0000370static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
371static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
372static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
reed@google.come05cc8e2011-10-10 14:19:40 +0000373static SkBenchmark* M3(void* p) { return new QMul64Bench(p); }
374static SkBenchmark* M4(void* p) { return new QMul32Bench(p); }
reed@google.comddc518b2011-08-29 17:49:23 +0000375
reed@google.com16078632011-12-06 18:56:37 +0000376static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); }
reed@google.com0be5eb72011-12-05 21:53:22 +0000377static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); }
378static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); }
379static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); }
380static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); }
381static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); }
382static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); }
383
reed@google.com7f192412012-05-30 12:26:52 +0000384static SkBenchmark* F0(void* p) { return new FloorBench(p, false); }
385static SkBenchmark* F1(void* p) { return new FloorBench(p, true); }
386
reed@google.comddc518b2011-08-29 17:49:23 +0000387static BenchRegistry gReg0(M0);
388static BenchRegistry gReg1(M1);
389static BenchRegistry gReg2(M2);
reed@google.come05cc8e2011-10-10 14:19:40 +0000390static BenchRegistry gReg3(M3);
391static BenchRegistry gReg4(M4);
reed@google.com0be5eb72011-12-05 21:53:22 +0000392
reed@google.com16078632011-12-06 18:56:37 +0000393static BenchRegistry gReg5neg1(M5neg1);
reed@google.com0be5eb72011-12-05 21:53:22 +0000394static BenchRegistry gReg50(M50);
395static BenchRegistry gReg51(M51);
396static BenchRegistry gReg52(M52);
397static BenchRegistry gReg53(M53);
398static BenchRegistry gReg54(M54);
399static BenchRegistry gReg55(M55);
reed@google.com7f192412012-05-30 12:26:52 +0000400
401static BenchRegistry gRF0(F0);
402static BenchRegistry gRF1(F1);