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