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

Side by Side Diff: bench/MathBench.cpp

Issue 21755002: Experiments on calculating reciprocal of square root (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Better calculate reciprocal of square root Created 7 years, 4 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 | « no previous file | include/core/SkMath.h » ('j') | include/core/SkMath.h » ('J')
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 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 const float* SK_RESTRICT src, 86 const float* SK_RESTRICT src,
87 int count) { 87 int count) {
88 for (int i = 0; i < count; ++i) { 88 for (int i = 0; i < count; ++i) {
89 dst[i] = src[i] + 1; 89 dst[i] = src[i] + 1;
90 } 90 }
91 } 91 }
92 private: 92 private:
93 typedef MathBench INHERITED; 93 typedef MathBench INHERITED;
94 }; 94 };
95 95
96 class SlowISqrtMathBench : public MathBench { 96 class InvSqrtBench : public SkBenchmark {
97 enum {
98 ARRAY = SkBENCHLOOP(1000),
99 LOOP = SkBENCHLOOP(5000),
100 };
101 float fData[ARRAY];
102 const char *type;
103
97 public: 104 public:
98 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} 105 InvSqrtBench(void* param, const char *type)
106 : INHERITED(param)
107 , type(type) {
108 }
109
110 // just so the compiler doesn't remove our loops
111 virtual void process(int) {}
112
99 protected: 113 protected:
100 virtual void performTest(float* SK_RESTRICT dst, 114 virtual void onPreDraw() SK_OVERRIDE {
101 const float* SK_RESTRICT src, 115 SkRandom rand;
102 int count) { 116 for (int i = 0; i < ARRAY; ++i) {
103 for (int i = 0; i < count; ++i) { 117 fData[i] = rand.nextRangeF(0, 10000);
104 dst[i] = 1.0f / sk_float_sqrt(src[i]);
105 } 118 }
119
120 fIsRendering = false;
106 } 121 }
122
123 virtual void onDraw(SkCanvas*) {
124 float accum = 0;
125
126 if (strcmp(type, "float_slow") == 0) {
127 for (int j = 0; j < LOOP; ++j)
128 for (int i = 0; i < ARRAY; ++i)
129 accum += 1.0f / sk_float_sqrt(fData[i]);
130 } else if (strcmp(type, "float_fast") == 0) {
131 for (int j = 0; j < LOOP; ++j)
132 for (int i = 0; i < ARRAY; ++i)
133 accum += SkFloatInvSqrt(fData[i]);
134 }
135
136 this->process(accum);
137 }
138
139 virtual const char* onGetName() {
140 fName.printf("math_inv_sqrt");
141 fName.appendf("_%s", type);
142 return fName.c_str();
143 }
144
107 private: 145 private:
108 typedef MathBench INHERITED; 146 SkString fName;
109 }; 147 typedef SkBenchmark INHERITED;
110
111 static inline float SkFastInvSqrt(float x) {
112 float xhalf = 0.5f*x;
113 int i = *SkTCast<int*>(&x);
114 i = 0x5f3759df - (i>>1);
115 x = *SkTCast<float*>(&i);
116 x = x*(1.5f-xhalf*x*x);
117 // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
118 return x;
119 }
120
121 class FastISqrtMathBench : public MathBench {
122 public:
123 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
124 protected:
125 virtual void performTest(float* SK_RESTRICT dst,
126 const float* SK_RESTRICT src,
127 int count) {
128 for (int i = 0; i < count; ++i) {
129 dst[i] = SkFastInvSqrt(src[i]);
130 }
131 }
132 private:
133 typedef MathBench INHERITED;
134 }; 148 };
135 149
136 static inline uint32_t QMul64(uint32_t value, U8CPU alpha) { 150 static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
137 SkASSERT((uint8_t)alpha == alpha); 151 SkASSERT((uint8_t)alpha == alpha);
138 const uint32_t mask = 0xFF00FF; 152 const uint32_t mask = 0xFF00FF;
139 153
140 uint64_t tmp = value; 154 uint64_t tmp = value;
141 tmp = (tmp & mask) | ((tmp & ~mask) << 24); 155 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
142 tmp *= alpha; 156 tmp *= alpha;
143 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask)); 157 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 return "float_to_fixed"; 530 return "float_to_fixed";
517 } 531 }
518 532
519 private: 533 private:
520 typedef SkBenchmark INHERITED; 534 typedef SkBenchmark INHERITED;
521 }; 535 };
522 536
523 /////////////////////////////////////////////////////////////////////////////// 537 ///////////////////////////////////////////////////////////////////////////////
524 538
525 DEF_BENCH( return new NoOpMathBench(p); ) 539 DEF_BENCH( return new NoOpMathBench(p); )
526 DEF_BENCH( return new SlowISqrtMathBench(p); ) 540 DEF_BENCH( return new InvSqrtBench(p, "float_slow"); )
527 DEF_BENCH( return new FastISqrtMathBench(p); ) 541 DEF_BENCH( return new InvSqrtBench(p, "float_fast"); )
528 DEF_BENCH( return new QMul64Bench(p); ) 542 DEF_BENCH( return new QMul64Bench(p); )
529 DEF_BENCH( return new QMul32Bench(p); ) 543 DEF_BENCH( return new QMul32Bench(p); )
530 544
531 DEF_BENCH( return new IsFiniteBench(p, -1); ) 545 DEF_BENCH( return new IsFiniteBench(p, -1); )
532 DEF_BENCH( return new IsFiniteBench(p, 0); ) 546 DEF_BENCH( return new IsFiniteBench(p, 0); )
533 DEF_BENCH( return new IsFiniteBench(p, 1); ) 547 DEF_BENCH( return new IsFiniteBench(p, 1); )
534 DEF_BENCH( return new IsFiniteBench(p, 2); ) 548 DEF_BENCH( return new IsFiniteBench(p, 2); )
535 DEF_BENCH( return new IsFiniteBench(p, 3); ) 549 DEF_BENCH( return new IsFiniteBench(p, 3); )
536 DEF_BENCH( return new IsFiniteBench(p, 4); ) 550 DEF_BENCH( return new IsFiniteBench(p, 4); )
537 DEF_BENCH( return new IsFiniteBench(p, 5); ) 551 DEF_BENCH( return new IsFiniteBench(p, 5); )
538 552
539 DEF_BENCH( return new FloorBench(p, false); ) 553 DEF_BENCH( return new FloorBench(p, false); )
540 DEF_BENCH( return new FloorBench(p, true); ) 554 DEF_BENCH( return new FloorBench(p, true); )
541 555
542 DEF_BENCH( return new CLZBench(p, false); ) 556 DEF_BENCH( return new CLZBench(p, false); )
543 DEF_BENCH( return new CLZBench(p, true); ) 557 DEF_BENCH( return new CLZBench(p, true); )
544 558
545 DEF_BENCH( return new NormalizeBench(p); ) 559 DEF_BENCH( return new NormalizeBench(p); )
546 560
547 DEF_BENCH( return new FixedMathBench(p); ) 561 DEF_BENCH( return new FixedMathBench(p); )
OLDNEW
« no previous file with comments | « no previous file | include/core/SkMath.h » ('j') | include/core/SkMath.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698