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