blob: ffee9015455bc1f01f2979a7a0897e4dedd5aa52 [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]) {
189 return !sk_float_isNaN(mulzeroadd(data));
190}
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 SkScalarIsNaN_new(SkScalar x) {
216 float y = x * 0;
217 return y == y;
218}
219
220static bool isFinite(const SkRect& r) {
221 // x * 0 will be NaN iff x is infinity or NaN.
222 // a + b will be NaN iff either a or b is NaN.
223 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
224
225 // value is either NaN or it is finite (zero).
226 // value==value will be true iff value is not NaN
227 return value == value;
228}
229
reed@google.com0be5eb72011-12-05 21:53:22 +0000230class IsFiniteBench : public SkBenchmark {
231 enum {
232 N = SkBENCHLOOP(1000),
233 NN = SkBENCHLOOP(1000),
234 };
235 float fData[N];
236public:
237
238 IsFiniteBench(void* param, int index) : INHERITED(param) {
239 SkRandom rand;
240
241 for (int i = 0; i < N; ++i) {
242 fData[i] = rand.nextSScalar1();
243 }
reed@google.com16078632011-12-06 18:56:37 +0000244
245 if (index < 0) {
246 fProc = NULL;
247 fName = "isfinite_rect";
248 } else {
249 fProc = gRec[index].fProc;
250 fName = gRec[index].fName;
251 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000252 }
253
254protected:
255 virtual void onDraw(SkCanvas* canvas) {
256 IsFiniteProc proc = fProc;
257 const float* data = fData;
reed@google.com16078632011-12-06 18:56:37 +0000258 // do this so the compiler won't throw away the function call
259 int counter = 0;
reed@google.com0be5eb72011-12-05 21:53:22 +0000260
reed@google.com16078632011-12-06 18:56:37 +0000261 if (proc) {
262 for (int j = 0; j < NN; ++j) {
263 for (int i = 0; i < N - 4; ++i) {
264 counter += proc(&data[i]);
265 }
reed@google.com0be5eb72011-12-05 21:53:22 +0000266 }
reed@google.com16078632011-12-06 18:56:37 +0000267 } else {
268 for (int j = 0; j < NN; ++j) {
269 for (int i = 0; i < N - 4; ++i) {
270 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
271 counter += r->isFinite();
272 }
273 }
274 }
275
276 SkPaint paint;
277 if (paint.getAlpha() == 0) {
278 SkDebugf("%d\n", counter);
reed@google.com0be5eb72011-12-05 21:53:22 +0000279 }
280 }
281
282 virtual const char* onGetName() {
283 return fName;
284 }
285
286private:
287 IsFiniteProc fProc;
288 const char* fName;
289
290 typedef SkBenchmark INHERITED;
291};
292
293///////////////////////////////////////////////////////////////////////////////
294
reed@google.comddc518b2011-08-29 17:49:23 +0000295static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
296static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
297static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
reed@google.come05cc8e2011-10-10 14:19:40 +0000298static SkBenchmark* M3(void* p) { return new QMul64Bench(p); }
299static SkBenchmark* M4(void* p) { return new QMul32Bench(p); }
reed@google.comddc518b2011-08-29 17:49:23 +0000300
reed@google.com16078632011-12-06 18:56:37 +0000301static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); }
reed@google.com0be5eb72011-12-05 21:53:22 +0000302static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); }
303static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); }
304static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); }
305static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); }
306static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); }
307static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); }
308
reed@google.comddc518b2011-08-29 17:49:23 +0000309static BenchRegistry gReg0(M0);
310static BenchRegistry gReg1(M1);
311static BenchRegistry gReg2(M2);
reed@google.come05cc8e2011-10-10 14:19:40 +0000312static BenchRegistry gReg3(M3);
313static BenchRegistry gReg4(M4);
reed@google.com0be5eb72011-12-05 21:53:22 +0000314
reed@google.com16078632011-12-06 18:56:37 +0000315static BenchRegistry gReg5neg1(M5neg1);
reed@google.com0be5eb72011-12-05 21:53:22 +0000316static BenchRegistry gReg50(M50);
317static BenchRegistry gReg51(M51);
318static BenchRegistry gReg52(M52);
319static BenchRegistry gReg53(M53);
320static BenchRegistry gReg54(M54);
321static BenchRegistry gReg55(M55);