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, |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 21 | }; |
| 22 | SkString fName; |
| 23 | float fSrc[kBuffer], fDst[kBuffer]; |
| 24 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 25 | MathBench(const char name[]) { |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 26 | fName.printf("math_%s", name); |
| 27 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 28 | SkRandom rand; |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 29 | for (int i = 0; i < kBuffer; ++i) { |
| 30 | fSrc[i] = rand.nextSScalar1(); |
| 31 | } |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 32 | |
| 33 | fIsRendering = false; |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 34 | } |
| 35 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | virtual void performTest(float* SK_RESTRICT dst, |
| 37 | const float* SK_RESTRICT src, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 38 | int count) = 0; |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 39 | |
| 40 | protected: |
| 41 | virtual int mulLoopCount() const { return 1; } |
| 42 | |
| 43 | virtual const char* onGetName() { |
| 44 | return fName.c_str(); |
| 45 | } |
| 46 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 47 | virtual void onDraw(SkCanvas*) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 48 | int n = this->getLoops() * this->mulLoopCount(); |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 49 | for (int i = 0; i < n; i++) { |
| 50 | this->performTest(fDst, fSrc, kBuffer); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | typedef SkBenchmark INHERITED; |
| 56 | }; |
| 57 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 58 | class MathBenchU32 : public MathBench { |
| 59 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 60 | MathBenchU32(const char name[]) : INHERITED(name) {} |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 61 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 62 | protected: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 63 | virtual void performITest(uint32_t* SK_RESTRICT dst, |
| 64 | const uint32_t* SK_RESTRICT src, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 65 | int count) = 0; |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 66 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 67 | virtual void performTest(float* SK_RESTRICT dst, |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 68 | const float* SK_RESTRICT src, |
| 69 | int count) SK_OVERRIDE { |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 70 | uint32_t* d = SkTCast<uint32_t*>(dst); |
| 71 | const uint32_t* s = SkTCast<const uint32_t*>(src); |
| 72 | this->performITest(d, s, count); |
| 73 | } |
| 74 | private: |
| 75 | typedef MathBench INHERITED; |
| 76 | }; |
| 77 | |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 79 | |
| 80 | class NoOpMathBench : public MathBench { |
| 81 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 82 | NoOpMathBench() : INHERITED("noOp") {} |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 83 | protected: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | virtual void performTest(float* SK_RESTRICT dst, |
| 85 | const float* SK_RESTRICT src, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 86 | int count) { |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 87 | for (int i = 0; i < count; ++i) { |
| 88 | dst[i] = src[i] + 1; |
| 89 | } |
| 90 | } |
| 91 | private: |
| 92 | typedef MathBench INHERITED; |
| 93 | }; |
| 94 | |
robertphillips@google.com | 36bb270 | 2013-08-12 12:02:28 +0000 | [diff] [blame] | 95 | class SlowISqrtMathBench : public MathBench { |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 96 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 97 | SlowISqrtMathBench() : INHERITED("slowIsqrt") {} |
commit-bot@chromium.org | b3ecdc4 | 2013-08-12 08:37:51 +0000 | [diff] [blame] | 98 | protected: |
robertphillips@google.com | 36bb270 | 2013-08-12 12:02:28 +0000 | [diff] [blame] | 99 | virtual void performTest(float* SK_RESTRICT dst, |
| 100 | const float* SK_RESTRICT src, |
| 101 | int count) { |
| 102 | for (int i = 0; i < count; ++i) { |
| 103 | dst[i] = 1.0f / sk_float_sqrt(src[i]); |
commit-bot@chromium.org | b3ecdc4 | 2013-08-12 08:37:51 +0000 | [diff] [blame] | 104 | } |
commit-bot@chromium.org | b3ecdc4 | 2013-08-12 08:37:51 +0000 | [diff] [blame] | 105 | } |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 106 | private: |
robertphillips@google.com | 36bb270 | 2013-08-12 12:02:28 +0000 | [diff] [blame] | 107 | typedef MathBench INHERITED; |
| 108 | }; |
| 109 | |
| 110 | static inline float SkFastInvSqrt(float x) { |
| 111 | float xhalf = 0.5f*x; |
| 112 | int i = *SkTCast<int*>(&x); |
| 113 | i = 0x5f3759df - (i>>1); |
| 114 | x = *SkTCast<float*>(&i); |
| 115 | x = x*(1.5f-xhalf*x*x); |
| 116 | // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6 |
| 117 | return x; |
| 118 | } |
| 119 | |
| 120 | class FastISqrtMathBench : public MathBench { |
| 121 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 122 | FastISqrtMathBench() : INHERITED("fastIsqrt") {} |
robertphillips@google.com | 36bb270 | 2013-08-12 12:02:28 +0000 | [diff] [blame] | 123 | protected: |
| 124 | virtual void performTest(float* SK_RESTRICT dst, |
| 125 | const float* SK_RESTRICT src, |
| 126 | int count) { |
| 127 | for (int i = 0; i < count; ++i) { |
| 128 | dst[i] = SkFastInvSqrt(src[i]); |
| 129 | } |
| 130 | } |
| 131 | private: |
| 132 | typedef MathBench INHERITED; |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 135 | static inline uint32_t QMul64(uint32_t value, U8CPU alpha) { |
| 136 | SkASSERT((uint8_t)alpha == alpha); |
| 137 | const uint32_t mask = 0xFF00FF; |
| 138 | |
| 139 | uint64_t tmp = value; |
| 140 | tmp = (tmp & mask) | ((tmp & ~mask) << 24); |
| 141 | tmp *= alpha; |
caryclark@google.com | 19069a2 | 2012-06-06 12:11:45 +0000 | [diff] [blame] | 142 | return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask)); |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | class QMul64Bench : public MathBenchU32 { |
| 146 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 147 | QMul64Bench() : INHERITED("qmul64") {} |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 148 | protected: |
| 149 | virtual void performITest(uint32_t* SK_RESTRICT dst, |
| 150 | const uint32_t* SK_RESTRICT src, |
| 151 | int count) SK_OVERRIDE { |
| 152 | for (int i = 0; i < count; ++i) { |
| 153 | dst[i] = QMul64(src[i], (uint8_t)i); |
| 154 | } |
| 155 | } |
| 156 | private: |
| 157 | typedef MathBenchU32 INHERITED; |
| 158 | }; |
| 159 | |
| 160 | class QMul32Bench : public MathBenchU32 { |
| 161 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 162 | QMul32Bench() : INHERITED("qmul32") {} |
reed@google.com | e05cc8e | 2011-10-10 14:19:40 +0000 | [diff] [blame] | 163 | protected: |
| 164 | virtual void performITest(uint32_t* SK_RESTRICT dst, |
| 165 | const uint32_t* SK_RESTRICT src, |
| 166 | int count) SK_OVERRIDE { |
| 167 | for (int i = 0; i < count; ++i) { |
| 168 | dst[i] = SkAlphaMulQ(src[i], (uint8_t)i); |
| 169 | } |
| 170 | } |
| 171 | private: |
| 172 | typedef MathBenchU32 INHERITED; |
| 173 | }; |
| 174 | |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
| 176 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 177 | static bool isFinite_int(float x) { |
| 178 | uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts |
| 179 | int exponent = bits << 1 >> 24; |
| 180 | return exponent != 0xFF; |
| 181 | } |
| 182 | |
| 183 | static bool isFinite_float(float x) { |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 184 | return SkToBool(sk_float_isfinite(x)); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static bool isFinite_mulzero(float x) { |
| 188 | float y = x * 0; |
| 189 | return y == y; |
| 190 | } |
| 191 | |
| 192 | static bool isfinite_and_int(const float data[4]) { |
| 193 | return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]); |
| 194 | } |
| 195 | |
| 196 | static bool isfinite_and_float(const float data[4]) { |
| 197 | return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]); |
| 198 | } |
| 199 | |
| 200 | static bool isfinite_and_mulzero(const float data[4]) { |
| 201 | return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]); |
| 202 | } |
| 203 | |
| 204 | #define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0) |
| 205 | |
| 206 | static bool isfinite_plus_int(const float data[4]) { |
| 207 | return isFinite_int(mulzeroadd(data)); |
| 208 | } |
| 209 | |
| 210 | static bool isfinite_plus_float(const float data[4]) { |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 211 | return !sk_float_isnan(mulzeroadd(data)); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static bool isfinite_plus_mulzero(const float data[4]) { |
| 215 | float x = mulzeroadd(data); |
| 216 | return x == x; |
| 217 | } |
| 218 | |
| 219 | typedef bool (*IsFiniteProc)(const float[]); |
| 220 | |
| 221 | #define MAKEREC(name) { name, #name } |
| 222 | |
| 223 | static const struct { |
| 224 | IsFiniteProc fProc; |
| 225 | const char* fName; |
| 226 | } gRec[] = { |
| 227 | MAKEREC(isfinite_and_int), |
| 228 | MAKEREC(isfinite_and_float), |
| 229 | MAKEREC(isfinite_and_mulzero), |
| 230 | MAKEREC(isfinite_plus_int), |
| 231 | MAKEREC(isfinite_plus_float), |
| 232 | MAKEREC(isfinite_plus_mulzero), |
| 233 | }; |
| 234 | |
| 235 | #undef MAKEREC |
| 236 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 237 | static bool isFinite(const SkRect& r) { |
| 238 | // x * 0 will be NaN iff x is infinity or NaN. |
| 239 | // a + b will be NaN iff either a or b is NaN. |
| 240 | float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 241 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 242 | // value is either NaN or it is finite (zero). |
| 243 | // value==value will be true iff value is not NaN |
| 244 | return value == value; |
| 245 | } |
| 246 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 247 | class IsFiniteBench : public SkBenchmark { |
| 248 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 249 | N = 1000, |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 250 | }; |
| 251 | float fData[N]; |
| 252 | public: |
| 253 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 254 | IsFiniteBench(int index) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 255 | SkRandom rand; |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 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 | } |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 268 | fIsRendering = false; |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | protected: |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 272 | virtual void onDraw(SkCanvas*) { |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 273 | IsFiniteProc proc = fProc; |
| 274 | const float* data = fData; |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 275 | // do this so the compiler won't throw away the function call |
| 276 | int counter = 0; |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 277 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 278 | if (proc) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 279 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 280 | for (int i = 0; i < N - 4; ++i) { |
| 281 | counter += proc(&data[i]); |
| 282 | } |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 283 | } |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 284 | } else { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 285 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 286 | for (int i = 0; i < N - 4; ++i) { |
| 287 | const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]); |
caryclark@google.com | 19069a2 | 2012-06-06 12:11:45 +0000 | [diff] [blame] | 288 | if (false) { // avoid bit rot, suppress warning |
| 289 | isFinite(*r); |
| 290 | } |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 291 | counter += r->isFinite(); |
| 292 | } |
| 293 | } |
| 294 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 295 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 296 | SkPaint paint; |
| 297 | if (paint.getAlpha() == 0) { |
| 298 | SkDebugf("%d\n", counter); |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | virtual const char* onGetName() { |
| 303 | return fName; |
| 304 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 305 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 306 | private: |
| 307 | IsFiniteProc fProc; |
| 308 | const char* fName; |
| 309 | |
| 310 | typedef SkBenchmark INHERITED; |
| 311 | }; |
| 312 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 313 | class FloorBench : public SkBenchmark { |
| 314 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 315 | ARRAY = 1000, |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 316 | }; |
| 317 | float fData[ARRAY]; |
| 318 | bool fFast; |
| 319 | public: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 320 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 321 | FloorBench(bool fast) : fFast(fast) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 322 | SkRandom rand; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 323 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 324 | for (int i = 0; i < ARRAY; ++i) { |
| 325 | fData[i] = rand.nextSScalar1(); |
| 326 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 327 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 328 | if (fast) { |
| 329 | fName = "floor_fast"; |
| 330 | } else { |
| 331 | fName = "floor_std"; |
| 332 | } |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 333 | fIsRendering = false; |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 334 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 335 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 336 | virtual void process(float) {} |
| 337 | |
| 338 | protected: |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 339 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 340 | SkRandom rand; |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 341 | float accum = 0; |
| 342 | const float* data = fData; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 343 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 344 | if (fFast) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 345 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 346 | for (int i = 0; i < ARRAY; ++i) { |
| 347 | accum += fast_floor(data[i]); |
| 348 | } |
| 349 | this->process(accum); |
| 350 | } |
| 351 | } else { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 352 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 353 | for (int i = 0; i < ARRAY; ++i) { |
| 354 | accum += sk_float_floor(data[i]); |
| 355 | } |
| 356 | this->process(accum); |
| 357 | } |
| 358 | } |
| 359 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 360 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 361 | virtual const char* onGetName() { |
| 362 | return fName; |
| 363 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 364 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 365 | private: |
| 366 | const char* fName; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 367 | |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 368 | typedef SkBenchmark INHERITED; |
| 369 | }; |
| 370 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 371 | class CLZBench : public SkBenchmark { |
| 372 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 373 | ARRAY = 1000, |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 374 | }; |
| 375 | uint32_t fData[ARRAY]; |
| 376 | bool fUsePortable; |
| 377 | |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 378 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 379 | CLZBench(bool usePortable) : fUsePortable(usePortable) { |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 380 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 381 | SkRandom rand; |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 382 | for (int i = 0; i < ARRAY; ++i) { |
| 383 | fData[i] = rand.nextU(); |
| 384 | } |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 385 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 386 | if (fUsePortable) { |
| 387 | fName = "clz_portable"; |
| 388 | } else { |
| 389 | fName = "clz_intrinsic"; |
| 390 | } |
| 391 | fIsRendering = false; |
| 392 | } |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 393 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 394 | // just so the compiler doesn't remove our loops |
| 395 | virtual void process(int) {} |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 396 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 397 | protected: |
| 398 | virtual void onDraw(SkCanvas*) { |
| 399 | int accum = 0; |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 400 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 401 | if (fUsePortable) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 402 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 403 | for (int i = 0; i < ARRAY; ++i) { |
| 404 | accum += SkCLZ_portable(fData[i]); |
| 405 | } |
| 406 | this->process(accum); |
| 407 | } |
| 408 | } else { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 409 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 410 | for (int i = 0; i < ARRAY; ++i) { |
| 411 | accum += SkCLZ(fData[i]); |
| 412 | } |
| 413 | this->process(accum); |
| 414 | } |
| 415 | } |
| 416 | } |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 417 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 418 | virtual const char* onGetName() { |
| 419 | return fName; |
| 420 | } |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 421 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 422 | private: |
| 423 | const char* fName; |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 424 | |
reed@google.com | 0d7aac9 | 2013-04-29 13:55:11 +0000 | [diff] [blame] | 425 | typedef SkBenchmark INHERITED; |
| 426 | }; |
| 427 | |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 428 | /////////////////////////////////////////////////////////////////////////////// |
| 429 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 430 | class NormalizeBench : public SkBenchmark { |
| 431 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 432 | ARRAY =1000, |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 433 | }; |
| 434 | SkVector fVec[ARRAY]; |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 435 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 436 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 437 | NormalizeBench() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 438 | SkRandom rand; |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 439 | for (int i = 0; i < ARRAY; ++i) { |
| 440 | fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1()); |
| 441 | } |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 442 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 443 | fName = "point_normalize"; |
| 444 | fIsRendering = false; |
| 445 | } |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 446 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 447 | // just so the compiler doesn't remove our loops |
| 448 | virtual void process(int) {} |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 449 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 450 | protected: |
| 451 | virtual void onDraw(SkCanvas*) { |
| 452 | int accum = 0; |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 453 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 454 | for (int j = 0; j < this->getLoops(); ++j) { |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 455 | for (int i = 0; i < ARRAY; ++i) { |
| 456 | accum += fVec[i].normalize(); |
| 457 | } |
| 458 | this->process(accum); |
| 459 | } |
| 460 | } |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 461 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 462 | virtual const char* onGetName() { |
| 463 | return fName; |
| 464 | } |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 465 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 466 | private: |
| 467 | const char* fName; |
skia.committer@gmail.com | ecc9d28 | 2013-05-04 07:01:15 +0000 | [diff] [blame] | 468 | |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 469 | typedef SkBenchmark INHERITED; |
| 470 | }; |
| 471 | |
| 472 | /////////////////////////////////////////////////////////////////////////////// |
| 473 | |
djsollen@google.com | 25a11e4 | 2013-07-18 19:11:30 +0000 | [diff] [blame] | 474 | class FixedMathBench : public SkBenchmark { |
| 475 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 476 | N = 1000, |
djsollen@google.com | 25a11e4 | 2013-07-18 19:11:30 +0000 | [diff] [blame] | 477 | }; |
| 478 | float fData[N]; |
| 479 | SkFixed fResult[N]; |
| 480 | public: |
| 481 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 482 | FixedMathBench() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 483 | SkRandom rand; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 484 | for (int i = 0; i < this->getLoops(); ++i) { |
| 485 | fData[i%N] = rand.nextSScalar1(); |
djsollen@google.com | 25a11e4 | 2013-07-18 19:11:30 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | fIsRendering = false; |
| 489 | } |
| 490 | |
| 491 | protected: |
| 492 | virtual void onDraw(SkCanvas*) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 493 | for (int j = 0; j < this->getLoops(); ++j) { |
djsollen@google.com | 25a11e4 | 2013-07-18 19:11:30 +0000 | [diff] [blame] | 494 | for (int i = 0; i < N - 4; ++i) { |
| 495 | fResult[i] = SkFloatToFixed(fData[i]); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | SkPaint paint; |
| 500 | if (paint.getAlpha() == 0) { |
| 501 | SkDebugf("%d\n", fResult[0]); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | virtual const char* onGetName() { |
| 506 | return "float_to_fixed"; |
| 507 | } |
| 508 | |
| 509 | private: |
| 510 | typedef SkBenchmark INHERITED; |
| 511 | }; |
| 512 | |
| 513 | /////////////////////////////////////////////////////////////////////////////// |
| 514 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 515 | DEF_BENCH( return new NoOpMathBench(); ) |
| 516 | DEF_BENCH( return new SlowISqrtMathBench(); ) |
| 517 | DEF_BENCH( return new FastISqrtMathBench(); ) |
| 518 | DEF_BENCH( return new QMul64Bench(); ) |
| 519 | DEF_BENCH( return new QMul32Bench(); ) |
reed@google.com | ddc518b | 2011-08-29 17:49:23 +0000 | [diff] [blame] | 520 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 521 | DEF_BENCH( return new IsFiniteBench(-1); ) |
| 522 | DEF_BENCH( return new IsFiniteBench(0); ) |
| 523 | DEF_BENCH( return new IsFiniteBench(1); ) |
| 524 | DEF_BENCH( return new IsFiniteBench(2); ) |
| 525 | DEF_BENCH( return new IsFiniteBench(3); ) |
| 526 | DEF_BENCH( return new IsFiniteBench(4); ) |
| 527 | DEF_BENCH( return new IsFiniteBench(5); ) |
reed@google.com | 0be5eb7 | 2011-12-05 21:53:22 +0000 | [diff] [blame] | 528 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 529 | DEF_BENCH( return new FloorBench(false); ) |
| 530 | DEF_BENCH( return new FloorBench(true); ) |
reed@google.com | 7f19241 | 2012-05-30 12:26:52 +0000 | [diff] [blame] | 531 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 532 | DEF_BENCH( return new CLZBench(false); ) |
| 533 | DEF_BENCH( return new CLZBench(true); ) |
reed@google.com | 0889f68 | 2013-05-03 12:56:39 +0000 | [diff] [blame] | 534 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 535 | DEF_BENCH( return new NormalizeBench(); ) |
djsollen@google.com | 25a11e4 | 2013-07-18 19:11:30 +0000 | [diff] [blame] | 536 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 537 | DEF_BENCH( return new FixedMathBench(); ) |