reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkMatrix.h" |
| 3 | #include "SkRandom.h" |
| 4 | #include "SkString.h" |
| 5 | |
| 6 | class MathBench : public SkBenchmark { |
| 7 | enum { |
| 8 | kBuffer = 100, |
| 9 | kLoop = 10000 |
| 10 | }; |
| 11 | SkString fName; |
| 12 | float fSrc[kBuffer], fDst[kBuffer]; |
| 13 | public: |
| 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 | |
| 25 | protected: |
| 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 | |
| 39 | private: |
| 40 | typedef SkBenchmark INHERITED; |
| 41 | }; |
| 42 | |
| 43 | int gMathBench_NonStaticGlobal; |
| 44 | |
| 45 | #define always_do(pred) \ |
| 46 | do { \ |
| 47 | if (pred) { \ |
| 48 | ++gMathBench_NonStaticGlobal; \ |
| 49 | } \ |
| 50 | } while (0) |
| 51 | |
| 52 | class NoOpMathBench : public MathBench { |
| 53 | public: |
bungeman@google.com | 9399cac | 2011-08-31 19:47:59 +0000 | [diff] [blame] | 54 | NoOpMathBench(void* param) : INHERITED(param, "noOp") {} |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 55 | protected: |
| 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 | } |
| 61 | private: |
| 62 | typedef MathBench INHERITED; |
| 63 | }; |
| 64 | |
| 65 | class SlowISqrtMathBench : public MathBench { |
| 66 | public: |
| 67 | SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} |
| 68 | protected: |
| 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 | } |
| 74 | private: |
| 75 | typedef MathBench INHERITED; |
| 76 | }; |
| 77 | |
| 78 | static 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 | |
| 88 | class FastISqrtMathBench : public MathBench { |
| 89 | public: |
| 90 | FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {} |
| 91 | protected: |
| 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 | } |
| 97 | private: |
| 98 | typedef MathBench INHERITED; |
| 99 | }; |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); } |
| 104 | static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); } |
| 105 | static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); } |
| 106 | |
| 107 | static BenchRegistry gReg0(M0); |
| 108 | static BenchRegistry gReg1(M1); |
| 109 | static BenchRegistry gReg2(M2); |