blob: a8eb43eb4c33a8cf35a7e035bbc26b25e42d5433 [file] [log] [blame]
reed@google.comddc518b2011-08-29 17:49:23 +00001#include "SkBenchmark.h"
2#include "SkMatrix.h"
3#include "SkRandom.h"
4#include "SkString.h"
5
6class MathBench : public SkBenchmark {
7 enum {
8 kBuffer = 100,
9 kLoop = 10000
10 };
11 SkString fName;
12 float fSrc[kBuffer], fDst[kBuffer];
13public:
14 MathBench(void* param, const char name[]) : INHERITED(param) {
15 fName.printf("math_%s", name);
16
17 SkRandom rand;
18 for (int i = 0; i < kBuffer; ++i) {
19 fSrc[i] = rand.nextSScalar1();
20 }
21 }
22
23 virtual void performTest(float dst[], const float src[], int count) = 0;
24
25protected:
26 virtual int mulLoopCount() const { return 1; }
27
28 virtual const char* onGetName() {
29 return fName.c_str();
30 }
31
32 virtual void onDraw(SkCanvas* canvas) {
33 int n = kLoop * this->mulLoopCount();
34 for (int i = 0; i < n; i++) {
35 this->performTest(fDst, fSrc, kBuffer);
36 }
37 }
38
39private:
40 typedef SkBenchmark INHERITED;
41};
42
43int gMathBench_NonStaticGlobal;
44
45#define always_do(pred) \
46 do { \
47 if (pred) { \
48 ++gMathBench_NonStaticGlobal; \
49 } \
50 } while (0)
51
52class NoOpMathBench : public MathBench {
53public:
bungeman@google.com9399cac2011-08-31 19:47:59 +000054 NoOpMathBench(void* param) : INHERITED(param, "noOp") {}
reed@google.comddc518b2011-08-29 17:49:23 +000055protected:
56 virtual void performTest(float dst[], const float src[], int count) {
57 for (int i = 0; i < count; ++i) {
58 dst[i] = src[i] + 1;
59 }
60 }
61private:
62 typedef MathBench INHERITED;
63};
64
65class SlowISqrtMathBench : public MathBench {
66public:
67 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {}
68protected:
69 virtual void performTest(float dst[], const float src[], int count) {
70 for (int i = 0; i < count; ++i) {
71 dst[i] = 1.0f / sk_float_sqrt(src[i]);
72 }
73 }
74private:
75 typedef MathBench INHERITED;
76};
77
78static inline float SkFastInvSqrt(float x) {
79 float xhalf = 0.5f*x;
80 int i = *(int*)&x;
81 i = 0x5f3759df - (i>>1);
82 x = *(float*)&i;
83 x = x*(1.5f-xhalf*x*x);
84// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
85 return x;
86}
87
88class FastISqrtMathBench : public MathBench {
89public:
90 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {}
91protected:
92 virtual void performTest(float dst[], const float src[], int count) {
93 for (int i = 0; i < count; ++i) {
94 dst[i] = SkFastInvSqrt(src[i]);
95 }
96 }
97private:
98 typedef MathBench INHERITED;
99};
100
101///////////////////////////////////////////////////////////////////////////////
102
103static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); }
104static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); }
105static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); }
106
107static BenchRegistry gReg0(M0);
108static BenchRegistry gReg1(M1);
109static BenchRegistry gReg2(M2);