1 #include "SkBenchmark.h"
2 #include "SkColorPriv.h"
3 #include "SkMatrix.h"
4 #include "SkRandom.h"
5 #include "SkString.h"
6 #include "SkPaint.h"
7
8 class MathBench : public SkBenchmark {
9 enum {
10 kBuffer = 100,
11 kLoop = 10000
12 };
13 SkString fName;
14 float fSrc[kBuffer], fDst[kBuffer];
15 public:
MathBench(void * param,const char name[])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
27 protected:
mulLoopCount() const28 virtual int mulLoopCount() const { return 1; }
29
onGetName()30 virtual const char* onGetName() {
31 return fName.c_str();
32 }
33
onDraw(SkCanvas * canvas)34 virtual void onDraw(SkCanvas* canvas) {
35 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
36 for (int i = 0; i < n; i++) {
37 this->performTest(fDst, fSrc, kBuffer);
38 }
39 }
40
41 private:
42 typedef SkBenchmark INHERITED;
43 };
44
45 class MathBenchU32 : public MathBench {
46 public:
MathBenchU32(void * param,const char name[])47 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {}
48
49 protected:
50 virtual void performITest(uint32_t* dst, const uint32_t* src, int count) = 0;
51
performTest(float * SK_RESTRICT dst,const float * SK_RESTRICT src,int count)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 }
58 private:
59 typedef MathBench INHERITED;
60 };
61
62 ///////////////////////////////////////////////////////////////////////////////
63
64 class NoOpMathBench : public MathBench {
65 public:
NoOpMathBench(void * param)66 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
67 protected:
performTest(float dst[],const float src[],int count)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 }
73 private:
74 typedef MathBench INHERITED;
75 };
76
77 class SlowISqrtMathBench : public MathBench {
78 public:
SlowISqrtMathBench(void * param)79 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
80 protected:
performTest(float dst[],const float src[],int count)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 }
86 private:
87 typedef MathBench INHERITED;
88 };
89
SkFastInvSqrt(float x)90 static 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
100 class FastISqrtMathBench : public MathBench {
101 public:
FastISqrtMathBench(void * param)102 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
103 protected:
performTest(float dst[],const float src[],int count)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 }
109 private:
110 typedef MathBench INHERITED;
111 };
112
QMul64(uint32_t value,U8CPU alpha)113 static 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
123 class QMul64Bench : public MathBenchU32 {
124 public:
QMul64Bench(void * param)125 QMul64Bench(void* param) : INHERITED(param, "qmul64") {}
126 protected:
performITest(uint32_t * SK_RESTRICT dst,const uint32_t * SK_RESTRICT src,int count)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 }
134 private:
135 typedef MathBenchU32 INHERITED;
136 };
137
138 class QMul32Bench : public MathBenchU32 {
139 public:
QMul32Bench(void * param)140 QMul32Bench(void* param) : INHERITED(param, "qmul32") {}
141 protected:
performITest(uint32_t * SK_RESTRICT dst,const uint32_t * SK_RESTRICT src,int count)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 }
149 private:
150 typedef MathBenchU32 INHERITED;
151 };
152
153 ///////////////////////////////////////////////////////////////////////////////
154
isFinite_int(float x)155 static 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
isFinite_float(float x)161 static bool isFinite_float(float x) {
162 return sk_float_isfinite(x);
163 }
164
isFinite_mulzero(float x)165 static bool isFinite_mulzero(float x) {
166 float y = x * 0;
167 return y == y;
168 }
169
isfinite_and_int(const float data[4])170 static 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
isfinite_and_float(const float data[4])174 static 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
isfinite_and_mulzero(const float data[4])178 static 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
isfinite_plus_int(const float data[4])184 static bool isfinite_plus_int(const float data[4]) {
185 return isFinite_int(mulzeroadd(data));
186 }
187
isfinite_plus_float(const float data[4])188 static bool isfinite_plus_float(const float data[4]) {
189 return !sk_float_isnan(mulzeroadd(data));
190 }
191
isfinite_plus_mulzero(const float data[4])192 static bool isfinite_plus_mulzero(const float data[4]) {
193 float x = mulzeroadd(data);
194 return x == x;
195 }
196
197 typedef bool (*IsFiniteProc)(const float[]);
198
199 #define MAKEREC(name) { name, #name }
200
201 static 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
isFinite(const SkRect & r)215 static 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
225 class IsFiniteBench : public SkBenchmark {
226 enum {
227 N = SkBENCHLOOP(1000),
228 NN = SkBENCHLOOP(1000),
229 };
230 float fData[N];
231 public:
232
IsFiniteBench(void * param,int index)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 }
239
240 if (index < 0) {
241 fProc = NULL;
242 fName = "isfinite_rect";
243 } else {
244 fProc = gRec[index].fProc;
245 fName = gRec[index].fName;
246 }
247 }
248
249 protected:
onDraw(SkCanvas * canvas)250 virtual void onDraw(SkCanvas* canvas) {
251 IsFiniteProc proc = fProc;
252 const float* data = fData;
253 // do this so the compiler won't throw away the function call
254 int counter = 0;
255
256 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 }
261 }
262 } 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);
274 }
275 }
276
onGetName()277 virtual const char* onGetName() {
278 return fName;
279 }
280
281 private:
282 IsFiniteProc fProc;
283 const char* fName;
284
285 typedef SkBenchmark INHERITED;
286 };
287
288 ///////////////////////////////////////////////////////////////////////////////
289
M0(void * p)290 static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
M1(void * p)291 static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
M2(void * p)292 static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
M3(void * p)293 static SkBenchmark* M3(void* p) { return new QMul64Bench(p); }
M4(void * p)294 static SkBenchmark* M4(void* p) { return new QMul32Bench(p); }
295
M5neg1(void * p)296 static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); }
M50(void * p)297 static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); }
M51(void * p)298 static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); }
M52(void * p)299 static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); }
M53(void * p)300 static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); }
M54(void * p)301 static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); }
M55(void * p)302 static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); }
303
304 static BenchRegistry gReg0(M0);
305 static BenchRegistry gReg1(M1);
306 static BenchRegistry gReg2(M2);
307 static BenchRegistry gReg3(M3);
308 static BenchRegistry gReg4(M4);
309
310 static BenchRegistry gReg5neg1(M5neg1);
311 static BenchRegistry gReg50(M50);
312 static BenchRegistry gReg51(M51);
313 static BenchRegistry gReg52(M52);
314 static BenchRegistry gReg53(M53);
315 static BenchRegistry gReg54(M54);
316 static BenchRegistry gReg55(M55);
317