reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 2 | #include "SkColorPriv.h" |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 3 | #include "SkMatrix.h" |
| 4 | #include "SkRandom.h" |
| 5 | #include "SkString.h" |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 6 | #include "SkPaint.h" |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 7 | |
| 8 | class MathBench : public SkBenchmark { |
| 9 | enum { |
| 10 | kBuffer = 100, |
| 11 | kLoop = 10000 |
| 12 | }; |
| 13 | SkString fName; |
| 14 | float fSrc[kBuffer], fDst[kBuffer]; |
| 15 | public: |
| 16 | MathBench(void* param, const char name[]) : INHERITED(param) { |
| 17 | fName.printf("math_%s", name); |
| 18 | |
| 19 | SkRandom rand; |
| 20 | for (int i = 0; i < kBuffer; ++i) { |
| 21 | fSrc[i] = rand.nextSScalar1(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | virtual void performTest(float dst[], const float src[], int count) = 0; |
| 26 | |
| 27 | protected: |
| 28 | virtual int mulLoopCount() const { return 1; } |
| 29 | |
| 30 | virtual const char* onGetName() { |
| 31 | return fName.c_str(); |
| 32 | } |
| 33 | |
| 34 | virtual void onDraw(SkCanvas* canvas) { |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 35 | int n = SkBENCHLOOP(kLoop * this->mulLoopCount()); |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 36 | for (int i = 0; i < n; i++) { |
| 37 | this->performTest(fDst, fSrc, kBuffer); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | typedef SkBenchmark INHERITED; |
| 43 | }; |
| 44 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 45 | class MathBenchU32 : public MathBench { |
| 46 | public: |
| 47 | MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {} |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 48 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 49 | protected: |
| 50 | virtual void performITest(uint32_t* dst, const uint32_t* src, int count) = 0; |
| 51 | |
| 52 | virtual void performTest(float* SK_RESTRICT dst, const float* SK_RESTRICT src, |
| 53 | int count) SK_OVERRIDE { |
| 54 | uint32_t* d = SkTCast<uint32_t*>(dst); |
| 55 | const uint32_t* s = SkTCast<const uint32_t*>(src); |
| 56 | this->performITest(d, s, count); |
| 57 | } |
| 58 | private: |
| 59 | typedef MathBench INHERITED; |
| 60 | }; |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 63 | |
| 64 | class NoOpMathBench : public MathBench { |
| 65 | public: |
bungeman@google.com | 9399cac | 2011-08-31 19:47:59 +0000 | [diff] [blame] | 66 | NoOpMathBench(void* param) : INHERITED(param, "noOp") {} |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 67 | protected: |
| 68 | virtual void performTest(float dst[], const float src[], int count) { |
| 69 | for (int i = 0; i < count; ++i) { |
| 70 | dst[i] = src[i] + 1; |
| 71 | } |
| 72 | } |
| 73 | private: |
| 74 | typedef MathBench INHERITED; |
| 75 | }; |
| 76 | |
| 77 | class SlowISqrtMathBench : public MathBench { |
| 78 | public: |
| 79 | SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} |
| 80 | protected: |
| 81 | virtual void performTest(float dst[], const float src[], int count) { |
| 82 | for (int i = 0; i < count; ++i) { |
| 83 | dst[i] = 1.0f / sk_float_sqrt(src[i]); |
| 84 | } |
| 85 | } |
| 86 | private: |
| 87 | typedef MathBench INHERITED; |
| 88 | }; |
| 89 | |
| 90 | static inline float SkFastInvSqrt(float x) { |
| 91 | float xhalf = 0.5f*x; |
| 92 | int i = *(int*)&x; |
| 93 | i = 0x5f3759df - (i>>1); |
| 94 | x = *(float*)&i; |
| 95 | x = x*(1.5f-xhalf*x*x); |
| 96 | // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6 |
| 97 | return x; |
| 98 | } |
| 99 | |
| 100 | class FastISqrtMathBench : public MathBench { |
| 101 | public: |
| 102 | FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {} |
| 103 | protected: |
| 104 | virtual void performTest(float dst[], const float src[], int count) { |
| 105 | for (int i = 0; i < count; ++i) { |
| 106 | dst[i] = SkFastInvSqrt(src[i]); |
| 107 | } |
| 108 | } |
| 109 | private: |
| 110 | typedef MathBench INHERITED; |
| 111 | }; |
| 112 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 113 | static inline uint32_t QMul64(uint32_t value, U8CPU alpha) { |
| 114 | SkASSERT((uint8_t)alpha == alpha); |
| 115 | const uint32_t mask = 0xFF00FF; |
| 116 | |
| 117 | uint64_t tmp = value; |
| 118 | tmp = (tmp & mask) | ((tmp & ~mask) << 24); |
| 119 | tmp *= alpha; |
| 120 | return ((tmp >> 8) & mask) | ((tmp >> 32) & ~mask); |
| 121 | } |
| 122 | |
| 123 | class QMul64Bench : public MathBenchU32 { |
| 124 | public: |
| 125 | QMul64Bench(void* param) : INHERITED(param, "qmul64") {} |
| 126 | protected: |
| 127 | virtual void performITest(uint32_t* SK_RESTRICT dst, |
| 128 | const uint32_t* SK_RESTRICT src, |
| 129 | int count) SK_OVERRIDE { |
| 130 | for (int i = 0; i < count; ++i) { |
| 131 | dst[i] = QMul64(src[i], (uint8_t)i); |
| 132 | } |
| 133 | } |
| 134 | private: |
| 135 | typedef MathBenchU32 INHERITED; |
| 136 | }; |
| 137 | |
| 138 | class QMul32Bench : public MathBenchU32 { |
| 139 | public: |
| 140 | QMul32Bench(void* param) : INHERITED(param, "qmul32") {} |
| 141 | protected: |
| 142 | virtual void performITest(uint32_t* SK_RESTRICT dst, |
| 143 | const uint32_t* SK_RESTRICT src, |
| 144 | int count) SK_OVERRIDE { |
| 145 | for (int i = 0; i < count; ++i) { |
| 146 | dst[i] = SkAlphaMulQ(src[i], (uint8_t)i); |
| 147 | } |
| 148 | } |
| 149 | private: |
| 150 | typedef MathBenchU32 INHERITED; |
| 151 | }; |
| 152 | |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 153 | /////////////////////////////////////////////////////////////////////////////// |
| 154 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 155 | static bool isFinite_int(float x) { |
| 156 | uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts |
| 157 | int exponent = bits << 1 >> 24; |
| 158 | return exponent != 0xFF; |
| 159 | } |
| 160 | |
| 161 | static bool isFinite_float(float x) { |
| 162 | return sk_float_isfinite(x); |
| 163 | } |
| 164 | |
| 165 | static bool isFinite_mulzero(float x) { |
| 166 | float y = x * 0; |
| 167 | return y == y; |
| 168 | } |
| 169 | |
| 170 | static bool isfinite_and_int(const float data[4]) { |
| 171 | return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]); |
| 172 | } |
| 173 | |
| 174 | static bool isfinite_and_float(const float data[4]) { |
| 175 | return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]); |
| 176 | } |
| 177 | |
| 178 | static bool isfinite_and_mulzero(const float data[4]) { |
| 179 | return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]); |
| 180 | } |
| 181 | |
| 182 | #define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0) |
| 183 | |
| 184 | static bool isfinite_plus_int(const float data[4]) { |
| 185 | return isFinite_int(mulzeroadd(data)); |
| 186 | } |
| 187 | |
| 188 | static bool isfinite_plus_float(const float data[4]) { |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 189 | return !sk_float_isnan(mulzeroadd(data)); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static bool isfinite_plus_mulzero(const float data[4]) { |
| 193 | float x = mulzeroadd(data); |
| 194 | return x == x; |
| 195 | } |
| 196 | |
| 197 | typedef bool (*IsFiniteProc)(const float[]); |
| 198 | |
| 199 | #define MAKEREC(name) { name, #name } |
| 200 | |
| 201 | static const struct { |
| 202 | IsFiniteProc fProc; |
| 203 | const char* fName; |
| 204 | } gRec[] = { |
| 205 | MAKEREC(isfinite_and_int), |
| 206 | MAKEREC(isfinite_and_float), |
| 207 | MAKEREC(isfinite_and_mulzero), |
| 208 | MAKEREC(isfinite_plus_int), |
| 209 | MAKEREC(isfinite_plus_float), |
| 210 | MAKEREC(isfinite_plus_mulzero), |
| 211 | }; |
| 212 | |
| 213 | #undef MAKEREC |
| 214 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 215 | static bool isFinite(const SkRect& r) { |
| 216 | // x * 0 will be NaN iff x is infinity or NaN. |
| 217 | // a + b will be NaN iff either a or b is NaN. |
| 218 | float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0; |
| 219 | |
| 220 | // value is either NaN or it is finite (zero). |
| 221 | // value==value will be true iff value is not NaN |
| 222 | return value == value; |
| 223 | } |
| 224 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 225 | class IsFiniteBench : public SkBenchmark { |
| 226 | enum { |
| 227 | N = SkBENCHLOOP(1000), |
| 228 | NN = SkBENCHLOOP(1000), |
| 229 | }; |
| 230 | float fData[N]; |
| 231 | public: |
| 232 | |
| 233 | IsFiniteBench(void* param, int index) : INHERITED(param) { |
| 234 | SkRandom rand; |
| 235 | |
| 236 | for (int i = 0; i < N; ++i) { |
| 237 | fData[i] = rand.nextSScalar1(); |
| 238 | } |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 239 | |
| 240 | if (index < 0) { |
| 241 | fProc = NULL; |
| 242 | fName = "isfinite_rect"; |
| 243 | } else { |
| 244 | fProc = gRec[index].fProc; |
| 245 | fName = gRec[index].fName; |
| 246 | } |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | protected: |
| 250 | virtual void onDraw(SkCanvas* canvas) { |
| 251 | IsFiniteProc proc = fProc; |
| 252 | const float* data = fData; |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 253 | // do this so the compiler won't throw away the function call |
| 254 | int counter = 0; |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 255 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 256 | if (proc) { |
| 257 | for (int j = 0; j < NN; ++j) { |
| 258 | for (int i = 0; i < N - 4; ++i) { |
| 259 | counter += proc(&data[i]); |
| 260 | } |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 261 | } |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 262 | } else { |
| 263 | for (int j = 0; j < NN; ++j) { |
| 264 | for (int i = 0; i < N - 4; ++i) { |
| 265 | const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]); |
| 266 | counter += r->isFinite(); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | SkPaint paint; |
| 272 | if (paint.getAlpha() == 0) { |
| 273 | SkDebugf("%d\n", counter); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | virtual const char* onGetName() { |
| 278 | return fName; |
| 279 | } |
| 280 | |
| 281 | private: |
| 282 | IsFiniteProc fProc; |
| 283 | const char* fName; |
| 284 | |
| 285 | typedef SkBenchmark INHERITED; |
| 286 | }; |
| 287 | |
| 288 | /////////////////////////////////////////////////////////////////////////////// |
| 289 | |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 290 | static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); } |
| 291 | static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); } |
| 292 | static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); } |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 293 | static SkBenchmark* M3(void* p) { return new QMul64Bench(p); } |
| 294 | static SkBenchmark* M4(void* p) { return new QMul32Bench(p); } |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 295 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 296 | static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); } |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 297 | static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); } |
| 298 | static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); } |
| 299 | static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); } |
| 300 | static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); } |
| 301 | static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); } |
| 302 | static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); } |
| 303 | |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 304 | static BenchRegistry gReg0(M0); |
| 305 | static BenchRegistry gReg1(M1); |
| 306 | static BenchRegistry gReg2(M2); |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 307 | static BenchRegistry gReg3(M3); |
| 308 | static BenchRegistry gReg4(M4); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 309 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 310 | static BenchRegistry gReg5neg1(M5neg1); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 311 | static BenchRegistry gReg50(M50); |
| 312 | static BenchRegistry gReg51(M51); |
| 313 | static BenchRegistry gReg52(M52); |
| 314 | static BenchRegistry gReg53(M53); |
| 315 | static BenchRegistry gReg54(M54); |
| 316 | static BenchRegistry gReg55(M55); |