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