blob: 9feb5afb32a090ed413a2181e094ef49caa91d2c [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
8class MathBench : public SkBenchmark {
9 enum {
10 kBuffer = 100,
11 kLoop = 10000
12 };
13 SkString fName;
14 float fSrc[kBuffer], fDst[kBuffer];
15public:
16 MathBench(void* param, const char name[]) : INHERITED(param) {
17 fName.printf("math_%s", name);
18
19 SkRandom rand;
20 for (int i = 0; i < kBuffer; ++i) {
21 fSrc[i] = rand.nextSScalar1();
22 }
23 }
24
25 virtual void performTest(float dst[], const float src[], int count) = 0;
26
27protected:
28 virtual int mulLoopCount() const { return 1; }
29
30 virtual const char* onGetName() {
31 return fName.c_str();
32 }
33
34 virtual void onDraw(SkCanvas* canvas) {
tomhudson@google.comca529d32011-10-28 15:34:49 +000035 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
reed@google.comddc518b2011-08-29 17:49:23 +000036 for (int i = 0; i < n; i++) {
37 this->performTest(fDst, fSrc, kBuffer);
38 }
39 }
40
41private:
42 typedef SkBenchmark INHERITED;
43};
44
reed@google.come05cc8e2011-10-10 14:19:40 +000045class MathBenchU32 : public MathBench {
46public:
47 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {}
reed@google.comddc518b2011-08-29 17:49:23 +000048
reed@google.come05cc8e2011-10-10 14:19:40 +000049protected:
50 virtual void performITest(uint32_t* dst, const uint32_t* src, int count) = 0;
51
52 virtual void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src,
53 int count) SK_OVERRIDE {
54 uint32_t* d = SkTCast<uint32_t*>(dst);
55 const uint32_t* s = SkTCast<const uint32_t*>(src);
56 this->performITest(d, s, count);
57 }
58private:
59 typedef MathBench INHERITED;
60};
61
62///////////////////////////////////////////////////////////////////////////////
reed@google.comddc518b2011-08-29 17:49:23 +000063
64class NoOpMathBench : public MathBench {
65public:
bungeman@google.com9399cac2011-08-31 19:47:59 +000066 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000067protected:
68 virtual void performTest(float dst[], const float src[], int count) {
69 for (int i = 0; i < count; ++i) {
70 dst[i] = src[i] + 1;
71 }
72 }
73private:
74 typedef MathBench INHERITED;
75};
76
77class SlowISqrtMathBench : public MathBench {
78public:
79 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
80protected:
81 virtual void performTest(float dst[], const float src[], int count) {
82 for (int i = 0; i < count; ++i) {
83 dst[i] = 1.0f / sk_float_sqrt(src[i]);
84 }
85 }
86private:
87 typedef MathBench INHERITED;
88};
89
90static inline float SkFastInvSqrt(float x) {
91 float xhalf = 0.5f*x;
92 int i = *(int*)&x;
93 i = 0x5f3759df - (i>>1);
94 x = *(float*)&i;
95 x = x*(1.5f-xhalf*x*x);
96// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
97 return x;
98}
99
100class FastISqrtMathBench : public MathBench {
101public:
102 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
103protected:
104 virtual void performTest(float dst[], const float src[], int count) {
105 for (int i = 0; i < count; ++i) {
106 dst[i] = SkFastInvSqrt(src[i]);
107 }
108 }
109private:
110 typedef MathBench INHERITED;
111};
112
reed@google.come05cc8e2011-10-10 14:19:40 +0000113static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
114 SkASSERT((uint8_t)alpha == alpha);
115 const uint32_t mask = 0xFF00FF;
116
117 uint64_t tmp = value;
118 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
119 tmp *= alpha;
120 return ((tmp >> 8) & mask) | ((tmp >> 32) & ~mask);
121}
122
123class QMul64Bench : public MathBenchU32 {
124public:
125 QMul64Bench(void* param) : INHERITED(param, "qmul64") {}
126protected:
127 virtual void performITest(uint32_t* SK_RESTRICT dst,
128 const uint32_t* SK_RESTRICT src,
129 int count) SK_OVERRIDE {
130 for (int i = 0; i < count; ++i) {
131 dst[i] = QMul64(src[i], (uint8_t)i);
132 }
133 }
134private:
135 typedef MathBenchU32 INHERITED;
136};
137
138class QMul32Bench : public MathBenchU32 {
139public:
140 QMul32Bench(void* param) : INHERITED(param, "qmul32") {}
141protected:
142 virtual void performITest(uint32_t* SK_RESTRICT dst,
143 const uint32_t* SK_RESTRICT src,
144 int count) SK_OVERRIDE {
145 for (int i = 0; i < count; ++i) {
146 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
147 }
148 }
149private:
150 typedef MathBenchU32 INHERITED;
151};
152
reed@google.comddc518b2011-08-29 17:49:23 +0000153///////////////////////////////////////////////////////////////////////////////
154
reed@google.com0be5eb72011-12-05 21:53:22 +0000155static bool isFinite_int(float x) {
156 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
157 int exponent = bits << 1 >> 24;
158 return exponent != 0xFF;
159}
160
161static bool isFinite_float(float x) {
162 return sk_float_isfinite(x);
163}
164
165static bool isFinite_mulzero(float x) {
166 float y = x * 0;
167 return y == y;
168}
169
170static bool isfinite_and_int(const float data[4]) {
171 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
172}
173
174static bool isfinite_and_float(const float data[4]) {
175 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
176}
177
178static bool isfinite_and_mulzero(const float data[4]) {
179 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
180}
181
182#define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
183
184static bool isfinite_plus_int(const float data[4]) {
185 return isFinite_int(mulzeroadd(data));
186}
187
188static bool isfinite_plus_float(const float data[4]) {
reed@google.com5ae777d2011-12-06 20:18:05 +0000189 return !sk_float_isnan(mulzeroadd(data));
reed@google.com0be5eb72011-12-05 21:53:22 +0000190}
191
192static bool isfinite_plus_mulzero(const float data[4]) {
193 float x = mulzeroadd(data);
194 return x == x;
195}
196
197typedef bool (*IsFiniteProc)(const float[]);
198
199#define MAKEREC(name) { name, #name }
200
201static const struct {
202 IsFiniteProc fProc;
203 const char* fName;
204} gRec[] = {
205 MAKEREC(isfinite_and_int),
206 MAKEREC(isfinite_and_float),
207 MAKEREC(isfinite_and_mulzero),
208 MAKEREC(isfinite_plus_int),
209 MAKEREC(isfinite_plus_float),
210 MAKEREC(isfinite_plus_mulzero),
211};
212
213#undef MAKEREC
214
reed@google.com16078632011-12-06 18:56:37 +0000215static bool isFinite(const SkRect& r) {
216 // x * 0 will be NaN iff x is infinity or NaN.
217 // a + b will be NaN iff either a or b is NaN.
218 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
219
220 // value is either NaN or it is finite (zero).
221 // value==value will be true iff value is not NaN
222 return value == value;
223}
224
reed@google.com0be5eb72011-12-05 21:53:22 +0000225class IsFiniteBench : public SkBenchmark {
226 enum {
227 N = SkBENCHLOOP(1000),
228 NN = SkBENCHLOOP(1000),
229 };
230 float fData[N];
231public:
232
233 IsFiniteBench(void* param, int index) : INHERITED(param) {
234 SkRandom rand;
235
236 for (int i = 0; i < N; ++i) {
237 fData[i] = rand.nextSScalar1();
238 }
reed@google.com16078632011-12-06 18:56:37 +0000239
240 if (index < 0) {
241 fProc = NULL;
242 fName = "isfinite_rect";
243 } else {
244 fProc = gRec[index].fProc;
245 fName = gRec[index].fName;
246 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000247 }
248
249protected:
250 virtual void onDraw(SkCanvas* canvas) {
251 IsFiniteProc proc = fProc;
252 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000253 // do this so the compiler won't throw away the function call
254 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000255
reed@google.com16078632011-12-06 18:56:37 +0000256 if (proc) {
257 for (int j = 0; j < NN; ++j) {
258 for (int i = 0; i < N - 4; ++i) {
259 counter += proc(&data[i]);
260 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000261 }
reed@google.com16078632011-12-06 18:56:37 +0000262 } else {
263 for (int j = 0; j < NN; ++j) {
264 for (int i = 0; i < N - 4; ++i) {
265 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
266 counter += r->isFinite();
267 }
268 }
269 }
270
271 SkPaint paint;
272 if (paint.getAlpha() == 0) {
273 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000274 }
275 }
276
277 virtual const char* onGetName() {
278 return fName;
279 }
280
281private:
282 IsFiniteProc fProc;
283 const char* fName;
284
285 typedef SkBenchmark INHERITED;
286};
287
288///////////////////////////////////////////////////////////////////////////////
289
reed@google.comddc518b2011-08-29 17:49:23 +0000290static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
291static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
292static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
reed@google.come05cc8e2011-10-10 14:19:40 +0000293static SkBenchmark* M3(void* p) { return new QMul64Bench(p); }
294static SkBenchmark* M4(void* p) { return new QMul32Bench(p); }
reed@google.comddc518b2011-08-29 17:49:23 +0000295
reed@google.com16078632011-12-06 18:56:37 +0000296static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); }
reed@google.com0be5eb72011-12-05 21:53:22 +0000297static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); }
298static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); }
299static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); }
300static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); }
301static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); }
302static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); }
303
reed@google.comddc518b2011-08-29 17:49:23 +0000304static BenchRegistry gReg0(M0);
305static BenchRegistry gReg1(M1);
306static BenchRegistry gReg2(M2);
reed@google.come05cc8e2011-10-10 14:19:40 +0000307static BenchRegistry gReg3(M3);
308static BenchRegistry gReg4(M4);
reed@google.com0be5eb72011-12-05 21:53:22 +0000309
reed@google.com16078632011-12-06 18:56:37 +0000310static BenchRegistry gReg5neg1(M5neg1);
reed@google.com0be5eb72011-12-05 21:53:22 +0000311static BenchRegistry gReg50(M50);
312static BenchRegistry gReg51(M51);
313static BenchRegistry gReg52(M52);
314static BenchRegistry gReg53(M53);
315static BenchRegistry gReg54(M54);
316static BenchRegistry gReg55(M55);