| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkMatrix.h" |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 3 | #include "SkRandom.h" |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 4 | #include "SkString.h" |
| 5 | |
| 6 | class MatrixBench : public SkBenchmark { |
| 7 | SkString fName; |
| 8 | enum { N = 100000 }; |
| 9 | public: |
| 10 | MatrixBench(void* param, const char name[]) : INHERITED(param) { |
| 11 | fName.printf("matrix_%s", name); |
| 12 | } |
| 13 | |
| 14 | virtual void performTest() = 0; |
| 15 | |
| 16 | protected: |
| reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame^] | 17 | virtual int mulLoopCount() const { return 1; } |
| 18 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 19 | virtual const char* onGetName() { |
| 20 | return fName.c_str(); |
| 21 | } |
| 22 | |
| 23 | virtual void onDraw(SkCanvas* canvas) { |
| reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame^] | 24 | int n = N * this->mulLoopCount(); |
| 25 | for (int i = 0; i < n; i++) { |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 26 | this->performTest(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | private: |
| 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 |
| 37 | int gMatrixBench_NonStaticGlobal; |
| 38 | |
| 39 | #define always_do(pred) \ |
| 40 | do { \ |
| 41 | if (pred) { \ |
| 42 | ++gMatrixBench_NonStaticGlobal; \ |
| 43 | } \ |
| 44 | } while (0) |
| 45 | |
| 46 | class EqualsMatrixBench : public MatrixBench { |
| 47 | public: |
| 48 | EqualsMatrixBench(void* param) : INHERITED(param, "equals") {} |
| 49 | protected: |
| 50 | virtual void performTest() { |
| 51 | SkMatrix m0, m1, m2; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 52 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 53 | m0.reset(); |
| 54 | m1.reset(); |
| 55 | m2.reset(); |
| 56 | always_do(m0 == m1); |
| 57 | always_do(m1 == m2); |
| 58 | always_do(m2 == m0); |
| 59 | always_do(m0.getType()); |
| 60 | always_do(m1.getType()); |
| 61 | always_do(m2.getType()); |
| 62 | } |
| 63 | private: |
| 64 | typedef MatrixBench INHERITED; |
| 65 | }; |
| 66 | |
| 67 | class ScaleMatrixBench : public MatrixBench { |
| 68 | public: |
| 69 | ScaleMatrixBench(void* param) : INHERITED(param, "scale") { |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 70 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 71 | fM0.reset(); |
| 72 | fM1.setScale(fSX, fSY); |
| 73 | fM2.setTranslate(fSX, fSY); |
| 74 | fSX = fSY = SkFloatToScalar(1.5f); |
| 75 | } |
| 76 | protected: |
| 77 | virtual void performTest() { |
| 78 | SkMatrix m; |
| 79 | m = fM0; m.preScale(fSX, fSY); |
| 80 | m = fM1; m.preScale(fSX, fSY); |
| 81 | m = fM2; m.preScale(fSX, fSY); |
| 82 | } |
| 83 | private: |
| 84 | SkMatrix fM0, fM1, fM2; |
| 85 | SkScalar fSX, fSY; |
| 86 | typedef MatrixBench INHERITED; |
| 87 | }; |
| 88 | |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 89 | // having unknown values in our arrays can throw off the timing a lot, perhaps |
| 90 | // handling NaN values is a lot slower. Anyway, this guy is just meant to put |
| 91 | // reasonable values in our arrays. |
| 92 | template <typename T> void init9(T array[9]) { |
| 93 | SkRandom rand; |
| 94 | for (int i = 0; i < 9; i++) { |
| 95 | array[i] = rand.nextSScalar1(); |
| 96 | } |
| 97 | } |
| 98 | |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 99 | // Test the performance of setConcat() non-perspective case: |
| 100 | // using floating point precision only. |
| 101 | class FloatConcatMatrixBench : public MatrixBench { |
| 102 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 103 | FloatConcatMatrixBench(void* p) : INHERITED(p, "concat_floatfloat") { |
| 104 | init9(mya); |
| 105 | init9(myb); |
| 106 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 107 | } |
| 108 | protected: |
| reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame^] | 109 | virtual int mulLoopCount() const { return 4; } |
| 110 | |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 111 | static inline void muladdmul(float a, float b, float c, float d, |
| 112 | float* result) { |
| 113 | *result = a * b + c * d; |
| 114 | } |
| 115 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 116 | const float* a = mya; |
| 117 | const float* b = myb; |
| 118 | float* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 119 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 120 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 121 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 122 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 123 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 124 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 125 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 126 | r[5] += a[5]; |
| 127 | r[6] = r[7] = 0.0f; |
| 128 | r[8] = 1.0f; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 129 | } |
| 130 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 131 | float mya [9]; |
| 132 | float myb [9]; |
| 133 | float myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 134 | typedef MatrixBench INHERITED; |
| 135 | }; |
| 136 | |
| 137 | static inline float SkDoubleToFloat(double x) { |
| 138 | return static_cast<float>(x); |
| 139 | } |
| 140 | |
| 141 | // Test the performance of setConcat() non-perspective case: |
| 142 | // using floating point precision but casting up to float for |
| 143 | // intermediate results during computations. |
| 144 | class FloatDoubleConcatMatrixBench : public MatrixBench { |
| 145 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 146 | FloatDoubleConcatMatrixBench(void* p) : INHERITED(p, "concat_floatdouble") { |
| 147 | init9(mya); |
| 148 | init9(myb); |
| 149 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 150 | } |
| 151 | protected: |
| reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame^] | 152 | virtual int mulLoopCount() const { return 4; } |
| 153 | |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 154 | static inline void muladdmul(float a, float b, float c, float d, |
| 155 | float* result) { |
| 156 | *result = SkDoubleToFloat((double)a * b + (double)c * d); |
| 157 | } |
| 158 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 159 | const float* a = mya; |
| 160 | const float* b = myb; |
| 161 | float* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 162 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 163 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 164 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 165 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 166 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 167 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 168 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 169 | r[5] += a[5]; |
| 170 | r[6] = r[7] = 0.0f; |
| 171 | r[8] = 1.0f; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 172 | } |
| 173 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 174 | float mya [9]; |
| 175 | float myb [9]; |
| 176 | float myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 177 | typedef MatrixBench INHERITED; |
| 178 | }; |
| 179 | |
| 180 | // Test the performance of setConcat() non-perspective case: |
| 181 | // using double precision only. |
| 182 | class DoubleConcatMatrixBench : public MatrixBench { |
| 183 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 184 | DoubleConcatMatrixBench(void* p) : INHERITED(p, "concat_double") { |
| 185 | init9(mya); |
| 186 | init9(myb); |
| 187 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 188 | } |
| 189 | protected: |
| reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame^] | 190 | virtual int mulLoopCount() const { return 4; } |
| 191 | |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 192 | static inline void muladdmul(double a, double b, double c, double d, |
| 193 | double* result) { |
| 194 | *result = a * b + c * d; |
| 195 | } |
| 196 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 197 | const double* a = mya; |
| 198 | const double* b = myb; |
| 199 | double* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 200 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 201 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 202 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 203 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 204 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 205 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 206 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 207 | r[5] += a[5]; |
| 208 | r[6] = r[7] = 0.0; |
| 209 | r[8] = 1.0; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 210 | } |
| 211 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 212 | double mya [9]; |
| 213 | double myb [9]; |
| 214 | double myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 215 | typedef MatrixBench INHERITED; |
| 216 | }; |
| 217 | |
| 218 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 219 | static SkBenchmark* M0(void* p) { return new EqualsMatrixBench(p); } |
| 220 | static SkBenchmark* M1(void* p) { return new ScaleMatrixBench(p); } |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 221 | static SkBenchmark* M2(void* p) { return new FloatConcatMatrixBench(p); } |
| 222 | static SkBenchmark* M3(void* p) { return new FloatDoubleConcatMatrixBench(p); } |
| 223 | static SkBenchmark* M4(void* p) { return new DoubleConcatMatrixBench(p); } |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 224 | |
| 225 | static BenchRegistry gReg0(M0); |
| 226 | static BenchRegistry gReg1(M1); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 227 | static BenchRegistry gReg2(M2); |
| 228 | static BenchRegistry gReg3(M3); |
| 229 | static BenchRegistry gReg4(M4); |