Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: bench/MathBench.cpp

Issue 23876006: Refactoring: get rid of the SkBenchmark void* parameter. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: sync to head Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « bench/MagnifierBench.cpp ('k') | bench/Matrix44Bench.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "SkBenchmark.h" 1 #include "SkBenchmark.h"
2 #include "SkColorPriv.h" 2 #include "SkColorPriv.h"
3 #include "SkMatrix.h" 3 #include "SkMatrix.h"
4 #include "SkRandom.h" 4 #include "SkRandom.h"
5 #include "SkString.h" 5 #include "SkString.h"
6 #include "SkPaint.h" 6 #include "SkPaint.h"
7 7
8 static float sk_fsel(float pred, float result_ge, float result_lt) { 8 static float sk_fsel(float pred, float result_ge, float result_lt) {
9 return pred >= 0 ? result_ge : result_lt; 9 return pred >= 0 ? result_ge : result_lt;
10 } 10 }
11 11
12 static float fast_floor(float x) { 12 static float fast_floor(float x) {
13 // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23); 13 // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
14 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23)); 14 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
15 return (x + big) - big; 15 return (x + big) - big;
16 } 16 }
17 17
18 class MathBench : public SkBenchmark { 18 class MathBench : public SkBenchmark {
19 enum { 19 enum {
20 kBuffer = 100, 20 kBuffer = 100,
21 }; 21 };
22 SkString fName; 22 SkString fName;
23 float fSrc[kBuffer], fDst[kBuffer]; 23 float fSrc[kBuffer], fDst[kBuffer];
24 public: 24 public:
25 MathBench(void* param, const char name[]) : INHERITED(param) { 25 MathBench(const char name[]) {
26 fName.printf("math_%s", name); 26 fName.printf("math_%s", name);
27 27
28 SkRandom rand; 28 SkRandom rand;
29 for (int i = 0; i < kBuffer; ++i) { 29 for (int i = 0; i < kBuffer; ++i) {
30 fSrc[i] = rand.nextSScalar1(); 30 fSrc[i] = rand.nextSScalar1();
31 } 31 }
32 32
33 fIsRendering = false; 33 fIsRendering = false;
34 } 34 }
35 35
(...skipping 14 matching lines...) Expand all
50 this->performTest(fDst, fSrc, kBuffer); 50 this->performTest(fDst, fSrc, kBuffer);
51 } 51 }
52 } 52 }
53 53
54 private: 54 private:
55 typedef SkBenchmark INHERITED; 55 typedef SkBenchmark INHERITED;
56 }; 56 };
57 57
58 class MathBenchU32 : public MathBench { 58 class MathBenchU32 : public MathBench {
59 public: 59 public:
60 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {} 60 MathBenchU32(const char name[]) : INHERITED(name) {}
61 61
62 protected: 62 protected:
63 virtual void performITest(uint32_t* SK_RESTRICT dst, 63 virtual void performITest(uint32_t* SK_RESTRICT dst,
64 const uint32_t* SK_RESTRICT src, 64 const uint32_t* SK_RESTRICT src,
65 int count) = 0; 65 int count) = 0;
66 66
67 virtual void performTest(float* SK_RESTRICT dst, 67 virtual void performTest(float* SK_RESTRICT dst,
68 const float* SK_RESTRICT src, 68 const float* SK_RESTRICT src,
69 int count) SK_OVERRIDE { 69 int count) SK_OVERRIDE {
70 uint32_t* d = SkTCast<uint32_t*>(dst); 70 uint32_t* d = SkTCast<uint32_t*>(dst);
71 const uint32_t* s = SkTCast<const uint32_t*>(src); 71 const uint32_t* s = SkTCast<const uint32_t*>(src);
72 this->performITest(d, s, count); 72 this->performITest(d, s, count);
73 } 73 }
74 private: 74 private:
75 typedef MathBench INHERITED; 75 typedef MathBench INHERITED;
76 }; 76 };
77 77
78 /////////////////////////////////////////////////////////////////////////////// 78 ///////////////////////////////////////////////////////////////////////////////
79 79
80 class NoOpMathBench : public MathBench { 80 class NoOpMathBench : public MathBench {
81 public: 81 public:
82 NoOpMathBench(void* param) : INHERITED(param, "noOp") {} 82 NoOpMathBench() : INHERITED("noOp") {}
83 protected: 83 protected:
84 virtual void performTest(float* SK_RESTRICT dst, 84 virtual void performTest(float* SK_RESTRICT dst,
85 const float* SK_RESTRICT src, 85 const float* SK_RESTRICT src,
86 int count) { 86 int count) {
87 for (int i = 0; i < count; ++i) { 87 for (int i = 0; i < count; ++i) {
88 dst[i] = src[i] + 1; 88 dst[i] = src[i] + 1;
89 } 89 }
90 } 90 }
91 private: 91 private:
92 typedef MathBench INHERITED; 92 typedef MathBench INHERITED;
93 }; 93 };
94 94
95 class SlowISqrtMathBench : public MathBench { 95 class SlowISqrtMathBench : public MathBench {
96 public: 96 public:
97 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} 97 SlowISqrtMathBench() : INHERITED("slowIsqrt") {}
98 protected: 98 protected:
99 virtual void performTest(float* SK_RESTRICT dst, 99 virtual void performTest(float* SK_RESTRICT dst,
100 const float* SK_RESTRICT src, 100 const float* SK_RESTRICT src,
101 int count) { 101 int count) {
102 for (int i = 0; i < count; ++i) { 102 for (int i = 0; i < count; ++i) {
103 dst[i] = 1.0f / sk_float_sqrt(src[i]); 103 dst[i] = 1.0f / sk_float_sqrt(src[i]);
104 } 104 }
105 } 105 }
106 private: 106 private:
107 typedef MathBench INHERITED; 107 typedef MathBench INHERITED;
108 }; 108 };
109 109
110 static inline float SkFastInvSqrt(float x) { 110 static inline float SkFastInvSqrt(float x) {
111 float xhalf = 0.5f*x; 111 float xhalf = 0.5f*x;
112 int i = *SkTCast<int*>(&x); 112 int i = *SkTCast<int*>(&x);
113 i = 0x5f3759df - (i>>1); 113 i = 0x5f3759df - (i>>1);
114 x = *SkTCast<float*>(&i); 114 x = *SkTCast<float*>(&i);
115 x = x*(1.5f-xhalf*x*x); 115 x = x*(1.5f-xhalf*x*x);
116 // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6 116 // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
117 return x; 117 return x;
118 } 118 }
119 119
120 class FastISqrtMathBench : public MathBench { 120 class FastISqrtMathBench : public MathBench {
121 public: 121 public:
122 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {} 122 FastISqrtMathBench() : INHERITED("fastIsqrt") {}
123 protected: 123 protected:
124 virtual void performTest(float* SK_RESTRICT dst, 124 virtual void performTest(float* SK_RESTRICT dst,
125 const float* SK_RESTRICT src, 125 const float* SK_RESTRICT src,
126 int count) { 126 int count) {
127 for (int i = 0; i < count; ++i) { 127 for (int i = 0; i < count; ++i) {
128 dst[i] = SkFastInvSqrt(src[i]); 128 dst[i] = SkFastInvSqrt(src[i]);
129 } 129 }
130 } 130 }
131 private: 131 private:
132 typedef MathBench INHERITED; 132 typedef MathBench INHERITED;
133 }; 133 };
134 134
135 static inline uint32_t QMul64(uint32_t value, U8CPU alpha) { 135 static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
136 SkASSERT((uint8_t)alpha == alpha); 136 SkASSERT((uint8_t)alpha == alpha);
137 const uint32_t mask = 0xFF00FF; 137 const uint32_t mask = 0xFF00FF;
138 138
139 uint64_t tmp = value; 139 uint64_t tmp = value;
140 tmp = (tmp & mask) | ((tmp & ~mask) << 24); 140 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
141 tmp *= alpha; 141 tmp *= alpha;
142 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask)); 142 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
143 } 143 }
144 144
145 class QMul64Bench : public MathBenchU32 { 145 class QMul64Bench : public MathBenchU32 {
146 public: 146 public:
147 QMul64Bench(void* param) : INHERITED(param, "qmul64") {} 147 QMul64Bench() : INHERITED("qmul64") {}
148 protected: 148 protected:
149 virtual void performITest(uint32_t* SK_RESTRICT dst, 149 virtual void performITest(uint32_t* SK_RESTRICT dst,
150 const uint32_t* SK_RESTRICT src, 150 const uint32_t* SK_RESTRICT src,
151 int count) SK_OVERRIDE { 151 int count) SK_OVERRIDE {
152 for (int i = 0; i < count; ++i) { 152 for (int i = 0; i < count; ++i) {
153 dst[i] = QMul64(src[i], (uint8_t)i); 153 dst[i] = QMul64(src[i], (uint8_t)i);
154 } 154 }
155 } 155 }
156 private: 156 private:
157 typedef MathBenchU32 INHERITED; 157 typedef MathBenchU32 INHERITED;
158 }; 158 };
159 159
160 class QMul32Bench : public MathBenchU32 { 160 class QMul32Bench : public MathBenchU32 {
161 public: 161 public:
162 QMul32Bench(void* param) : INHERITED(param, "qmul32") {} 162 QMul32Bench() : INHERITED("qmul32") {}
163 protected: 163 protected:
164 virtual void performITest(uint32_t* SK_RESTRICT dst, 164 virtual void performITest(uint32_t* SK_RESTRICT dst,
165 const uint32_t* SK_RESTRICT src, 165 const uint32_t* SK_RESTRICT src,
166 int count) SK_OVERRIDE { 166 int count) SK_OVERRIDE {
167 for (int i = 0; i < count; ++i) { 167 for (int i = 0; i < count; ++i) {
168 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i); 168 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
169 } 169 }
170 } 170 }
171 private: 171 private:
172 typedef MathBenchU32 INHERITED; 172 typedef MathBenchU32 INHERITED;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 return value == value; 244 return value == value;
245 } 245 }
246 246
247 class IsFiniteBench : public SkBenchmark { 247 class IsFiniteBench : public SkBenchmark {
248 enum { 248 enum {
249 N = 1000, 249 N = 1000,
250 }; 250 };
251 float fData[N]; 251 float fData[N];
252 public: 252 public:
253 253
254 IsFiniteBench(void* param, int index) : INHERITED(param) { 254 IsFiniteBench(int index) {
255 SkRandom rand; 255 SkRandom rand;
256 256
257 for (int i = 0; i < N; ++i) { 257 for (int i = 0; i < N; ++i) {
258 fData[i] = rand.nextSScalar1(); 258 fData[i] = rand.nextSScalar1();
259 } 259 }
260 260
261 if (index < 0) { 261 if (index < 0) {
262 fProc = NULL; 262 fProc = NULL;
263 fName = "isfinite_rect"; 263 fName = "isfinite_rect";
264 } else { 264 } else {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 }; 311 };
312 312
313 class FloorBench : public SkBenchmark { 313 class FloorBench : public SkBenchmark {
314 enum { 314 enum {
315 ARRAY = 1000, 315 ARRAY = 1000,
316 }; 316 };
317 float fData[ARRAY]; 317 float fData[ARRAY];
318 bool fFast; 318 bool fFast;
319 public: 319 public:
320 320
321 FloorBench(void* param, bool fast) : INHERITED(param), fFast(fast) { 321 FloorBench(bool fast) : fFast(fast) {
322 SkRandom rand; 322 SkRandom rand;
323 323
324 for (int i = 0; i < ARRAY; ++i) { 324 for (int i = 0; i < ARRAY; ++i) {
325 fData[i] = rand.nextSScalar1(); 325 fData[i] = rand.nextSScalar1();
326 } 326 }
327 327
328 if (fast) { 328 if (fast) {
329 fName = "floor_fast"; 329 fName = "floor_fast";
330 } else { 330 } else {
331 fName = "floor_std"; 331 fName = "floor_std";
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 }; 369 };
370 370
371 class CLZBench : public SkBenchmark { 371 class CLZBench : public SkBenchmark {
372 enum { 372 enum {
373 ARRAY = 1000, 373 ARRAY = 1000,
374 }; 374 };
375 uint32_t fData[ARRAY]; 375 uint32_t fData[ARRAY];
376 bool fUsePortable; 376 bool fUsePortable;
377 377
378 public: 378 public:
379 CLZBench(void* param, bool usePortable) 379 CLZBench(bool usePortable) : fUsePortable(usePortable) {
380 : INHERITED(param)
381 , fUsePortable(usePortable) {
382 380
383 SkRandom rand; 381 SkRandom rand;
384 for (int i = 0; i < ARRAY; ++i) { 382 for (int i = 0; i < ARRAY; ++i) {
385 fData[i] = rand.nextU(); 383 fData[i] = rand.nextU();
386 } 384 }
387 385
388 if (fUsePortable) { 386 if (fUsePortable) {
389 fName = "clz_portable"; 387 fName = "clz_portable";
390 } else { 388 } else {
391 fName = "clz_intrinsic"; 389 fName = "clz_intrinsic";
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 427
430 /////////////////////////////////////////////////////////////////////////////// 428 ///////////////////////////////////////////////////////////////////////////////
431 429
432 class NormalizeBench : public SkBenchmark { 430 class NormalizeBench : public SkBenchmark {
433 enum { 431 enum {
434 ARRAY =1000, 432 ARRAY =1000,
435 }; 433 };
436 SkVector fVec[ARRAY]; 434 SkVector fVec[ARRAY];
437 435
438 public: 436 public:
439 NormalizeBench(void* param) 437 NormalizeBench() {
440 : INHERITED(param) {
441
442 SkRandom rand; 438 SkRandom rand;
443 for (int i = 0; i < ARRAY; ++i) { 439 for (int i = 0; i < ARRAY; ++i) {
444 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1()); 440 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
445 } 441 }
446 442
447 fName = "point_normalize"; 443 fName = "point_normalize";
448 fIsRendering = false; 444 fIsRendering = false;
449 } 445 }
450 446
451 // just so the compiler doesn't remove our loops 447 // just so the compiler doesn't remove our loops
(...skipping 24 matching lines...) Expand all
476 /////////////////////////////////////////////////////////////////////////////// 472 ///////////////////////////////////////////////////////////////////////////////
477 473
478 class FixedMathBench : public SkBenchmark { 474 class FixedMathBench : public SkBenchmark {
479 enum { 475 enum {
480 N = 1000, 476 N = 1000,
481 }; 477 };
482 float fData[N]; 478 float fData[N];
483 SkFixed fResult[N]; 479 SkFixed fResult[N];
484 public: 480 public:
485 481
486 FixedMathBench(void* param) : INHERITED(param) { 482 FixedMathBench() {
487 SkRandom rand; 483 SkRandom rand;
488 for (int i = 0; i < this->getLoops(); ++i) { 484 for (int i = 0; i < this->getLoops(); ++i) {
489 fData[i%N] = rand.nextSScalar1(); 485 fData[i%N] = rand.nextSScalar1();
490 } 486 }
491 487
492 fIsRendering = false; 488 fIsRendering = false;
493 } 489 }
494 490
495 protected: 491 protected:
496 virtual void onDraw(SkCanvas*) { 492 virtual void onDraw(SkCanvas*) {
(...skipping 12 matching lines...) Expand all
509 virtual const char* onGetName() { 505 virtual const char* onGetName() {
510 return "float_to_fixed"; 506 return "float_to_fixed";
511 } 507 }
512 508
513 private: 509 private:
514 typedef SkBenchmark INHERITED; 510 typedef SkBenchmark INHERITED;
515 }; 511 };
516 512
517 /////////////////////////////////////////////////////////////////////////////// 513 ///////////////////////////////////////////////////////////////////////////////
518 514
519 DEF_BENCH( return new NoOpMathBench(p); ) 515 DEF_BENCH( return new NoOpMathBench(); )
520 DEF_BENCH( return new SlowISqrtMathBench(p); ) 516 DEF_BENCH( return new SlowISqrtMathBench(); )
521 DEF_BENCH( return new FastISqrtMathBench(p); ) 517 DEF_BENCH( return new FastISqrtMathBench(); )
522 DEF_BENCH( return new QMul64Bench(p); ) 518 DEF_BENCH( return new QMul64Bench(); )
523 DEF_BENCH( return new QMul32Bench(p); ) 519 DEF_BENCH( return new QMul32Bench(); )
524 520
525 DEF_BENCH( return new IsFiniteBench(p, -1); ) 521 DEF_BENCH( return new IsFiniteBench(-1); )
526 DEF_BENCH( return new IsFiniteBench(p, 0); ) 522 DEF_BENCH( return new IsFiniteBench(0); )
527 DEF_BENCH( return new IsFiniteBench(p, 1); ) 523 DEF_BENCH( return new IsFiniteBench(1); )
528 DEF_BENCH( return new IsFiniteBench(p, 2); ) 524 DEF_BENCH( return new IsFiniteBench(2); )
529 DEF_BENCH( return new IsFiniteBench(p, 3); ) 525 DEF_BENCH( return new IsFiniteBench(3); )
530 DEF_BENCH( return new IsFiniteBench(p, 4); ) 526 DEF_BENCH( return new IsFiniteBench(4); )
531 DEF_BENCH( return new IsFiniteBench(p, 5); ) 527 DEF_BENCH( return new IsFiniteBench(5); )
532 528
533 DEF_BENCH( return new FloorBench(p, false); ) 529 DEF_BENCH( return new FloorBench(false); )
534 DEF_BENCH( return new FloorBench(p, true); ) 530 DEF_BENCH( return new FloorBench(true); )
535 531
536 DEF_BENCH( return new CLZBench(p, false); ) 532 DEF_BENCH( return new CLZBench(false); )
537 DEF_BENCH( return new CLZBench(p, true); ) 533 DEF_BENCH( return new CLZBench(true); )
538 534
539 DEF_BENCH( return new NormalizeBench(p); ) 535 DEF_BENCH( return new NormalizeBench(); )
540 536
541 DEF_BENCH( return new FixedMathBench(p); ) 537 DEF_BENCH( return new FixedMathBench(); )
OLDNEW
« no previous file with comments | « bench/MagnifierBench.cpp ('k') | bench/Matrix44Bench.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698