blob: 639d10d936f81ef62bbf457f2984245279c3e7b2 [file] [log] [blame]
tomhudson@google.comf910b362011-06-06 15:16:31 +00001#include "SkBenchmark.h"
2#include "SkRandom.h"
3#include "SkString.h"
4
5class ScalarBench : public SkBenchmark {
6 SkString fName;
7 enum { N = 100000 };
8public:
9 ScalarBench(void* param, const char name[]) : INHERITED(param) {
10 fName.printf("scalar_%s", name);
11 }
12
13 virtual void performTest() = 0;
14
15protected:
16 virtual int mulLoopCount() const { return 1; }
17
18 virtual const char* onGetName() {
19 return fName.c_str();
20 }
21
22 virtual void onDraw(SkCanvas* canvas) {
23 int n = N * this->mulLoopCount();
24 for (int i = 0; i < n; i++) {
25 this->performTest();
26 }
27 }
28
29private:
30 typedef SkBenchmark INHERITED;
31};
32
33// we want to stop the compiler from eliminating code that it thinks is a no-op
34// so we have a non-static global we increment, hoping that will convince the
35// compiler to execute everything
36int gScalarBench_NonStaticGlobal;
37
38#define always_do(pred) \
39 do { \
40 if (pred) { \
41 ++gScalarBench_NonStaticGlobal; \
42 } \
43 } while (0)
44
45// having unknown values in our arrays can throw off the timing a lot, perhaps
46// handling NaN values is a lot slower. Anyway, this guy is just meant to put
47// reasonable values in our arrays.
48template <typename T> void init9(T array[9]) {
49 SkRandom rand;
50 for (int i = 0; i < 9; i++) {
51 array[i] = rand.nextSScalar1();
52 }
53}
54
55class FloatComparisonBench : public ScalarBench {
56public:
57 FloatComparisonBench(void* param) : INHERITED(param, "compare_float") {
58 init9(fArray);
59 }
60protected:
61 virtual int mulLoopCount() const { return 4; }
62 virtual void performTest() {
63 always_do(fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
64 always_do(fArray[2] != 0.0f || fArray[5] != 0.0f);
65 }
66private:
67 float fArray[9];
68 typedef ScalarBench INHERITED;
69};
70
71class ForcedIntComparisonBench : public ScalarBench {
72public:
73 ForcedIntComparisonBench(void* param)
74 : INHERITED(param, "compare_forced_int") {
75 init9(fArray);
76 }
77protected:
78 virtual int mulLoopCount() const { return 4; }
79 virtual void performTest() {
80 always_do(SkScalarAs2sCompliment(fArray[6]) |
81 SkScalarAs2sCompliment(fArray[7]) |
82 (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
83 always_do(SkScalarAs2sCompliment(fArray[2]) |
84 SkScalarAs2sCompliment(fArray[5]));
85 }
86private:
87 static const int32_t kPersp1Int = 0x3f800000;
88 float fArray[9];
89 typedef ScalarBench INHERITED;
90};
91
92static SkBenchmark* S0(void* p) { return new FloatComparisonBench(p); }
93static SkBenchmark* S1(void* p) { return new ForcedIntComparisonBench(p); }
94
95static BenchRegistry gReg0(S0);
96static BenchRegistry gReg1(S1);