1#include "SkBenchmark.h" 2#include "SkColorPriv.h" 3#include "SkMatrix.h" 4#include "SkRandom.h" 5#include "SkString.h" 6#include "SkPaint.h" 7 8class MathBench : public SkBenchmark { 9 enum { 10 kBuffer = 100, 11 kLoop = 10000 12 }; 13 SkString fName; 14 float fSrc[kBuffer], fDst[kBuffer]; 15public: 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 27protected: 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) { 35 int n = SkBENCHLOOP(kLoop * this->mulLoopCount()); 36 for (int i = 0; i < n; i++) { 37 this->performTest(fDst, fSrc, kBuffer); 38 } 39 } 40 41private: 42 typedef SkBenchmark INHERITED; 43}; 44 45class MathBenchU32 : public MathBench { 46public: 47 MathBenchU32(void* param, const char name[]) : INHERITED(param, name) {} 48 49protected: 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 } 58private: 59 typedef MathBench INHERITED; 60}; 61 62/////////////////////////////////////////////////////////////////////////////// 63 64class NoOpMathBench : public MathBench { 65public: 66 NoOpMathBench(void* param) : INHERITED(param, "noOp") {} 67protected: 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 } 73private: 74 typedef MathBench INHERITED; 75}; 76 77class SlowISqrtMathBench : public MathBench { 78public: 79 SlowISqrtMathBench(void* param) : INHERITED(param, "slowIsqrt") {} 80protected: 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 } 86private: 87 typedef MathBench INHERITED; 88}; 89 90static 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 100class FastISqrtMathBench : public MathBench { 101public: 102 FastISqrtMathBench(void* param) : INHERITED(param, "fastIsqrt") {} 103protected: 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 } 109private: 110 typedef MathBench INHERITED; 111}; 112 113static 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 123class QMul64Bench : public MathBenchU32 { 124public: 125 QMul64Bench(void* param) : INHERITED(param, "qmul64") {} 126protected: 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 } 134private: 135 typedef MathBenchU32 INHERITED; 136}; 137 138class QMul32Bench : public MathBenchU32 { 139public: 140 QMul32Bench(void* param) : INHERITED(param, "qmul32") {} 141protected: 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 } 149private: 150 typedef MathBenchU32 INHERITED; 151}; 152 153/////////////////////////////////////////////////////////////////////////////// 154 155static 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 161static bool isFinite_float(float x) { 162 return sk_float_isfinite(x); 163} 164 165static bool isFinite_mulzero(float x) { 166 float y = x * 0; 167 return y == y; 168} 169 170static 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 174static 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 178static 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 184static bool isfinite_plus_int(const float data[4]) { 185 return isFinite_int(mulzeroadd(data)); 186} 187 188static bool isfinite_plus_float(const float data[4]) { 189 return !sk_float_isnan(mulzeroadd(data)); 190} 191 192static bool isfinite_plus_mulzero(const float data[4]) { 193 float x = mulzeroadd(data); 194 return x == x; 195} 196 197typedef bool (*IsFiniteProc)(const float[]); 198 199#define MAKEREC(name) { name, #name } 200 201static 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 215static 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 225class IsFiniteBench : public SkBenchmark { 226 enum { 227 N = SkBENCHLOOP(1000), 228 NN = SkBENCHLOOP(1000), 229 }; 230 float fData[N]; 231public: 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 } 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 } 247 } 248 249protected: 250 virtual void onDraw(SkCanvas* canvas) { 251 IsFiniteProc proc = fProc; 252 const float* data = fData; 253 // do this so the compiler won't throw away the function call 254 int counter = 0; 255 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 } 261 } 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); 274 } 275 } 276 277 virtual const char* onGetName() { 278 return fName; 279 } 280 281private: 282 IsFiniteProc fProc; 283 const char* fName; 284 285 typedef SkBenchmark INHERITED; 286}; 287 288/////////////////////////////////////////////////////////////////////////////// 289 290static SkBenchmark* M0(void* p) { return new NoOpMathBench(p); } 291static SkBenchmark* M1(void* p) { return new SlowISqrtMathBench(p); } 292static SkBenchmark* M2(void* p) { return new FastISqrtMathBench(p); } 293static SkBenchmark* M3(void* p) { return new QMul64Bench(p); } 294static SkBenchmark* M4(void* p) { return new QMul32Bench(p); } 295 296static SkBenchmark* M5neg1(void* p) { return new IsFiniteBench(p, -1); } 297static SkBenchmark* M50(void* p) { return new IsFiniteBench(p, 0); } 298static SkBenchmark* M51(void* p) { return new IsFiniteBench(p, 1); } 299static SkBenchmark* M52(void* p) { return new IsFiniteBench(p, 2); } 300static SkBenchmark* M53(void* p) { return new IsFiniteBench(p, 3); } 301static SkBenchmark* M54(void* p) { return new IsFiniteBench(p, 4); } 302static SkBenchmark* M55(void* p) { return new IsFiniteBench(p, 5); } 303 304static BenchRegistry gReg0(M0); 305static BenchRegistry gReg1(M1); 306static BenchRegistry gReg2(M2); 307static BenchRegistry gReg3(M3); 308static BenchRegistry gReg4(M4); 309 310static BenchRegistry gReg5neg1(M5neg1); 311static BenchRegistry gReg50(M50); 312static BenchRegistry gReg51(M51); 313static BenchRegistry gReg52(M52); 314static BenchRegistry gReg53(M53); 315static BenchRegistry gReg54(M54); 316static BenchRegistry gReg55(M55); 317