blob: 41176b47d6b7750f0d169dea36156e3f2b49f91e [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) {
13 float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
14 return (x + big) - big;
15}
16
reed@google.comddc518b2011-08-29 17:49:23 +000017class MathBench : public SkBenchmark {
18 enum {
19 kBuffer = 100,
20 kLoop = 10000
21 };
22 SkString fName;
23 float fSrc[kBuffer], fDst[kBuffer];
24public:
25 MathBench(void* param, const char name[]) : INHERITED(param) {
26 fName.printf("math_%s", name);
27
28 SkRandom rand;
29 for (int i = 0; i < kBuffer; ++i) {
30 fSrc[i] = rand.nextSScalar1();
31 }
32 }
33
robertphillips@google.com6853e802012-04-16 15:50:18 +000034 virtual void performTest(float* SK_RESTRICT dst,
35 const float* SK_RESTRICT src,
36 int count) = 0;
reed@google.comddc518b2011-08-29 17:49:23 +000037
38protected:
39 virtual int mulLoopCount() const { return 1; }
40
41 virtual const char* onGetName() {
42 return fName.c_str();
43 }
44
45 virtual void onDraw(SkCanvas* canvas) {
tomhudson@google.comca529d32011-10-28 15:34:49 +000046 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
reed@google.comddc518b2011-08-29 17:49:23 +000047 for (int i = 0; i < n; i++) {
48 this->performTest(fDst, fSrc, kBuffer);
49 }
50 }
51
52private:
53 typedef SkBenchmark INHERITED;
54};
55
reed@google.come05cc8e2011-10-10 14:19:40 +000056class MathBenchU32 : public MathBench {
57public:
58 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {}
reed@google.comddc518b2011-08-29 17:49:23 +000059
reed@google.come05cc8e2011-10-10 14:19:40 +000060protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000061 virtual void performITest(uint32_t* SK_RESTRICT dst,
62 const uint32_t* SK_RESTRICT src,
63 int count) = 0;
reed@google.come05cc8e2011-10-10 14:19:40 +000064
robertphillips@google.com6853e802012-04-16 15:50:18 +000065 virtual void performTest(float* SK_RESTRICT dst,
66 const float* SK_RESTRICT src,
67 int count) SK_OVERRIDE {
reed@google.come05cc8e2011-10-10 14:19:40 +000068 uint32_t* d = SkTCast<uint32_t*>(dst);
69 const uint32_t* s = SkTCast<const uint32_t*>(src);
70 this->performITest(d, s, count);
71 }
72private:
73 typedef MathBench INHERITED;
74};
75
76///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000077
78class NoOpMathBench : public MathBench {
79public:
bungeman@google.com9399cac2011-08-31 19:47:59 +000080 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000081protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000082 virtual void performTest(float* SK_RESTRICT dst,
83 const float* SK_RESTRICT src,
84 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +000085 for (int i = 0; i < count; ++i) {
86 dst[i] = src[i] + 1;
87 }
88 }
89private:
90 typedef MathBench INHERITED;
91};
92
93class SlowISqrtMathBench : public MathBench {
94public:
95 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
96protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +000097 virtual void performTest(float* SK_RESTRICT dst,
98 const float* SK_RESTRICT src,
99 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000100 for (int i = 0; i < count; ++i) {
101 dst[i] = 1.0f / sk_float_sqrt(src[i]);
102 }
103 }
104private:
105 typedef MathBench INHERITED;
106};
107
108static inline float SkFastInvSqrt(float x) {
109 float xhalf = 0.5f*x;
110 int i = *(int*)&x;
111 i = 0x5f3759df - (i>>1);
112 x = *(float*)&i;
113 x = x*(1.5f-xhalf*x*x);
114// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
115 return x;
116}
117
118class FastISqrtMathBench : public MathBench {
119public:
120 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
121protected:
robertphillips@google.com6853e802012-04-16 15:50:18 +0000122 virtual void performTest(float* SK_RESTRICT dst,
123 const float* SK_RESTRICT src,
124 int count) {
reed@google.comddc518b2011-08-29 17:49:23 +0000125 for (int i = 0; i < count; ++i) {
126 dst[i] = SkFastInvSqrt(src[i]);
127 }
128 }
129private:
130 typedef MathBench INHERITED;
131};
132
reed@google.come05cc8e2011-10-10 14:19:40 +0000133static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
134 SkASSERT((uint8_t)alpha == alpha);
135 const uint32_t mask = 0xFF00FF;
136
137 uint64_t tmp = value;
138 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
139 tmp *= alpha;
140 return ((tmp >> 8) & mask) | ((tmp >> 32) & ~mask);
141}
142
143class QMul64Bench : public MathBenchU32 {
144public:
145 QMul64Bench(void* param) : INHERITED(param, "qmul64") {}
146protected:
147 virtual void performITest(uint32_t* SK_RESTRICT dst,
148 const uint32_t* SK_RESTRICT src,
149 int count) SK_OVERRIDE {
150 for (int i = 0; i < count; ++i) {
151 dst[i] = QMul64(src[i], (uint8_t)i);
152 }
153 }
154private:
155 typedef MathBenchU32 INHERITED;
156};
157
158class QMul32Bench : public MathBenchU32 {
159public:
160 QMul32Bench(void* param) : INHERITED(param, "qmul32") {}
161protected:
162 virtual void performITest(uint32_t* SK_RESTRICT dst,
163 const uint32_t* SK_RESTRICT src,
164 int count) SK_OVERRIDE {
165 for (int i = 0; i < count; ++i) {
166 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
167 }
168 }
169private:
170 typedef MathBenchU32 INHERITED;
171};
172
reed@google.comddc518b2011-08-29 17:49:23 +0000173///////////////////////////////////////////////////////////////////////////////
174
reed@google.com0be5eb72011-12-05 21:53:22 +0000175static bool isFinite_int(float x) {
176 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
177 int exponent = bits << 1 >> 24;
178 return exponent != 0xFF;
179}
180
181static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +0000182 return SkToBool(sk_float_isfinite(x));
reed@google.com0be5eb72011-12-05 21:53:22 +0000183}
184
185static bool isFinite_mulzero(float x) {
186 float y = x * 0;
187 return y == y;
188}
189
190static bool isfinite_and_int(const float data[4]) {
191 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
192}
193
194static bool isfinite_and_float(const float data[4]) {
195 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
196}
197
198static bool isfinite_and_mulzero(const float data[4]) {
199 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
200}
201
202#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
203
204static bool isfinite_plus_int(const float data[4]) {
205 return isFinite_int(mulzeroadd(data));
206}
207
208static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000209 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000210}
211
212static bool isfinite_plus_mulzero(const float data[4]) {
213 float x = mulzeroadd(data);
214 return x == x;
215}
216
217typedef bool (*IsFiniteProc)(const float[]);
218
219#define MAKEREC(name) { name, #name }
220
221static const struct {
222 IsFiniteProc fProc;
223 const char* fName;
224} gRec[] = {
225 MAKEREC(isfinite_and_int),
226 MAKEREC(isfinite_and_float),
227 MAKEREC(isfinite_and_mulzero),
228 MAKEREC(isfinite_plus_int),
229 MAKEREC(isfinite_plus_float),
230 MAKEREC(isfinite_plus_mulzero),
231};
232
233#undef MAKEREC
234
reed@google.com16078632011-12-06 18:56:37 +0000235static bool isFinite(const SkRect& r) {
236 // x * 0 will be NaN iff x is infinity or NaN.
237 // a + b will be NaN iff either a or b is NaN.
238 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
239
240 // value is either NaN or it is finite (zero).
241 // value==value will be true iff value is not NaN
242 return value == value;
243}
244
reed@google.com0be5eb72011-12-05 21:53:22 +0000245class IsFiniteBench : public SkBenchmark {
246 enum {
247 N = SkBENCHLOOP(1000),
248 NN = SkBENCHLOOP(1000),
249 };
250 float fData[N];
251public:
252
253 IsFiniteBench(void* param, int index) : INHERITED(param) {
254 SkRandom rand;
255
256 for (int i = 0; i < N; ++i) {
257 fData[i] = rand.nextSScalar1();
258 }
reed@google.com16078632011-12-06 18:56:37 +0000259
260 if (index < 0) {
261 fProc = NULL;
262 fName = "isfinite_rect";
263 } else {
264 fProc = gRec[index].fProc;
265 fName = gRec[index].fName;
266 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000267 }
268
269protected:
270 virtual void onDraw(SkCanvas* canvas) {
271 IsFiniteProc proc = fProc;
272 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000273 // do this so the compiler won't throw away the function call
274 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000275
reed@google.com16078632011-12-06 18:56:37 +0000276 if (proc) {
277 for (int j = 0; j < NN; ++j) {
278 for (int i = 0; i < N - 4; ++i) {
279 counter += proc(&data[i]);
280 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000281 }
reed@google.com16078632011-12-06 18:56:37 +0000282 } else {
283 for (int j = 0; j < NN; ++j) {
284 for (int i = 0; i < N - 4; ++i) {
285 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
286 counter += r->isFinite();
287 }
288 }
289 }
290
291 SkPaint paint;
292 if (paint.getAlpha() == 0) {
293 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000294 }
295 }
296
297 virtual const char* onGetName() {
298 return fName;
299 }
300
301private:
302 IsFiniteProc fProc;
303 const char* fName;
304
305 typedef SkBenchmark INHERITED;
306};
307
reed@google.com7f192412012-05-30 12:26:52 +0000308class FloorBench : public SkBenchmark {
309 enum {
310 ARRAY = SkBENCHLOOP(1000),
311 LOOP = SkBENCHLOOP(1000),
312 };
313 float fData[ARRAY];
314 bool fFast;
315public:
316
317 FloorBench(void* param, bool fast) : INHERITED(param), fFast(fast) {
318 SkRandom rand;
319
320 for (int i = 0; i < ARRAY; ++i) {
321 fData[i] = rand.nextSScalar1();
322 }
323
324 if (fast) {
325 fName = "floor_fast";
326 } else {
327 fName = "floor_std";
328 }
329 }
330
331 virtual void process(float) {}
332
333protected:
334 virtual void onDraw(SkCanvas* canvas) {
335 SkRandom rand;
336 float accum = 0;
337 const float* data = fData;
338 float tmp[ARRAY] = {};
339
340 if (fFast) {
341 for (int j = 0; j < LOOP; ++j) {
342 for (int i = 0; i < ARRAY; ++i) {
343 accum += fast_floor(data[i]);
344 }
345 this->process(accum);
346 }
347 } else {
348 for (int j = 0; j < LOOP; ++j) {
349 for (int i = 0; i < ARRAY; ++i) {
350 accum += sk_float_floor(data[i]);
351 }
352 this->process(accum);
353 }
354 }
355 }
356
357 virtual const char* onGetName() {
358 return fName;
359 }
360
361private:
362 const char* fName;
363
364 typedef SkBenchmark INHERITED;
365};
366
reed@google.com0be5eb72011-12-05 21:53:22 +0000367///////////////////////////////////////////////////////////////////////////////
368
reed@google.comddc518b2011-08-29 17:49:23 +0000369static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
370static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
371static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
reed@google.come05cc8e2011-10-10 14:19:40 +0000372static SkBenchmark* M3(void* p) { return new QMul64Bench(p); }
373static SkBenchmark* M4(void* p) { return new QMul32Bench(p); }
reed@google.comddc518b2011-08-29 17:49:23 +0000374
reed@google.com16078632011-12-06 18:56:37 +0000375static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); }
reed@google.com0be5eb72011-12-05 21:53:22 +0000376static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); }
377static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); }
378static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); }
379static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); }
380static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); }
381static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); }
382
reed@google.com7f192412012-05-30 12:26:52 +0000383static SkBenchmark* F0(void* p) { return new FloorBench(p, false); }
384static SkBenchmark* F1(void* p) { return new FloorBench(p, true); }
385
reed@google.comddc518b2011-08-29 17:49:23 +0000386static BenchRegistry gReg0(M0);
387static BenchRegistry gReg1(M1);
388static BenchRegistry gReg2(M2);
reed@google.come05cc8e2011-10-10 14:19:40 +0000389static BenchRegistry gReg3(M3);
390static BenchRegistry gReg4(M4);
reed@google.com0be5eb72011-12-05 21:53:22 +0000391
reed@google.com16078632011-12-06 18:56:37 +0000392static BenchRegistry gReg5neg1(M5neg1);
reed@google.com0be5eb72011-12-05 21:53:22 +0000393static BenchRegistry gReg50(M50);
394static BenchRegistry gReg51(M51);
395static BenchRegistry gReg52(M52);
396static BenchRegistry gReg53(M53);
397static BenchRegistry gReg54(M54);
398static BenchRegistry gReg55(M55);
reed@google.com7f192412012-05-30 12:26:52 +0000399
400static BenchRegistry gRF0(F0);
401static BenchRegistry gRF1(F1);