diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-29 17:49:23 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-29 17:49:23 +0000 |
commit | ddc518b90fdfa9f9c7b71e4c5cefd28f8da52e26 (patch) | |
tree | 2a073bb3d86e776ac125ae6870adcc2b1bd8abd9 /bench/MathBench.cpp | |
parent | 30db599c1de70b2832ff9ec05c266a4c9b515d94 (diff) |
add
git-svn-id: http://skia.googlecode.com/svn/trunk@2185 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'bench/MathBench.cpp')
-rw-r--r-- | bench/MathBench.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/bench/MathBench.cpp b/bench/MathBench.cpp new file mode 100644 index 0000000000..16eaebe979 --- /dev/null +++ b/bench/MathBench.cpp @@ -0,0 +1,109 @@ +#include "SkBenchmark.h" +#include "SkMatrix.h" +#include "SkRandom.h" +#include "SkString.h" + +class MathBench : public SkBenchmark { + enum { + kBuffer = 100, + kLoop = 10000 + }; + SkString fName; + float fSrc[kBuffer], fDst[kBuffer]; +public: + MathBench(void* param, const char name[]) : INHERITED(param) { + fName.printf("math_%s", name); + + SkRandom rand; + for (int i = 0; i < kBuffer; ++i) { + fSrc[i] = rand.nextSScalar1(); + } + } + + virtual void performTest(float dst[], const float src[], int count) = 0; + +protected: + virtual int mulLoopCount() const { return 1; } + + virtual const char* onGetName() { + return fName.c_str(); + } + + virtual void onDraw(SkCanvas* canvas) { + int n = kLoop * this->mulLoopCount(); + for (int i = 0; i < n; i++) { + this->performTest(fDst, fSrc, kBuffer); + } + } + +private: + typedef SkBenchmark INHERITED; +}; + +int gMathBench_NonStaticGlobal; + +#define always_do(pred) \ + do { \ + if (pred) { \ + ++gMathBench_NonStaticGlobal; \ + } \ + } while (0) + +class NoOpMathBench : public MathBench { +public: + NoOpMathBench(void* param) : INHERITED(param, "slowIsqrt") {} +protected: + virtual void performTest(float dst[], const float src[], int count) { + for (int i = 0; i < count; ++i) { + dst[i] = src[i] + 1; + } + } +private: + typedef MathBench INHERITED; +}; + +class SlowISqrtMathBench : public MathBench { +public: + SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} +protected: + virtual void performTest(float dst[], const float src[], int count) { + for (int i = 0; i < count; ++i) { + dst[i] = 1.0f / sk_float_sqrt(src[i]); + } + } +private: + typedef MathBench INHERITED; +}; + +static inline float SkFastInvSqrt(float x) { + float xhalf = 0.5f*x; + int i = *(int*)&x; + i = 0x5f3759df - (i>>1); + x = *(float*)&i; + x = x*(1.5f-xhalf*x*x); +// x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6 + return x; +} + +class FastISqrtMathBench : public MathBench { +public: + FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {} +protected: + virtual void performTest(float dst[], const float src[], int count) { + for (int i = 0; i < count; ++i) { + dst[i] = SkFastInvSqrt(src[i]); + } + } +private: + typedef MathBench INHERITED; +}; + +/////////////////////////////////////////////////////////////////////////////// + +static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); } +static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); } +static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); } + +static BenchRegistry gReg0(M0); +static BenchRegistry gReg1(M1); +static BenchRegistry gReg2(M2); |