2 #include "SkColorPriv.h"
8 static float sk_fsel(float pred, float result_ge, float result_lt) {
9 return pred >= 0 ? result_ge : result_lt;
12 static float fast_floor(float x) {
13 // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
14 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
15 return (x + big) - big;
18 class MathBench : public Benchmark {
23 float fSrc[kBuffer], fDst[kBuffer];
25 MathBench(const char name[]) {
26 fName.printf("math_%s", name);
29 for (int i = 0; i < kBuffer; ++i) {
30 fSrc[i] = rand.nextSScalar1();
34 bool isSuitableFor(Backend backend) override {
35 return backend == kNonRendering_Backend;
38 virtual void performTest(float* SK_RESTRICT dst,
39 const float* SK_RESTRICT src,
43 virtual int mulLoopCount() const { return 1; }
45 virtual const char* onGetName() {
49 virtual void onDraw(const int loops, SkCanvas*) {
50 int n = loops * this->mulLoopCount();
51 for (int i = 0; i < n; i++) {
52 this->performTest(fDst, fSrc, kBuffer);
57 typedef Benchmark INHERITED;
60 class MathBenchU32 : public MathBench {
62 MathBenchU32(const char name[]) : INHERITED(name) {}
65 virtual void performITest(uint32_t* SK_RESTRICT dst,
66 const uint32_t* SK_RESTRICT src,
69 virtual void performTest(float* SK_RESTRICT dst,
70 const float* SK_RESTRICT src,
72 uint32_t* d = SkTCast<uint32_t*>(dst);
73 const uint32_t* s = SkTCast<const uint32_t*>(src);
74 this->performITest(d, s, count);
77 typedef MathBench INHERITED;
80 ///////////////////////////////////////////////////////////////////////////////
82 class NoOpMathBench : public MathBench {
84 NoOpMathBench() : INHERITED("noOp") {}
86 virtual void performTest(float* SK_RESTRICT dst,
87 const float* SK_RESTRICT src,
89 for (int i = 0; i < count; ++i) {
94 typedef MathBench INHERITED;
97 class SkRSqrtMathBench : public MathBench {
99 SkRSqrtMathBench() : INHERITED("sk_float_rsqrt") {}
101 virtual void performTest(float* SK_RESTRICT dst,
102 const float* SK_RESTRICT src,
104 for (int i = 0; i < count; ++i) {
105 dst[i] = sk_float_rsqrt(src[i]);
109 typedef MathBench INHERITED;
113 class SlowISqrtMathBench : public MathBench {
115 SlowISqrtMathBench() : INHERITED("slowIsqrt") {}
117 virtual void performTest(float* SK_RESTRICT dst,
118 const float* SK_RESTRICT src,
120 for (int i = 0; i < count; ++i) {
121 dst[i] = 1.0f / sk_float_sqrt(src[i]);
125 typedef MathBench INHERITED;
128 static inline float SkFastInvSqrt(float x) {
129 float xhalf = 0.5f*x;
130 int i = *SkTCast<int*>(&x);
131 i = 0x5f3759df - (i>>1);
132 x = *SkTCast<float*>(&i);
133 x = x*(1.5f-xhalf*x*x);
134 // x = x*(1.5f-xhalf*x*x); // this line takes err from 10^-3 to 10^-6
138 class FastISqrtMathBench : public MathBench {
140 FastISqrtMathBench() : INHERITED("fastIsqrt") {}
142 virtual void performTest(float* SK_RESTRICT dst,
143 const float* SK_RESTRICT src,
145 for (int i = 0; i < count; ++i) {
146 dst[i] = SkFastInvSqrt(src[i]);
150 typedef MathBench INHERITED;
153 static inline uint32_t QMul64(uint32_t value, U8CPU alpha) {
154 SkASSERT((uint8_t)alpha == alpha);
155 const uint32_t mask = 0xFF00FF;
157 uint64_t tmp = value;
158 tmp = (tmp & mask) | ((tmp & ~mask) << 24);
160 return (uint32_t) (((tmp >> 8) & mask) | ((tmp >> 32) & ~mask));
163 class QMul64Bench : public MathBenchU32 {
165 QMul64Bench() : INHERITED("qmul64") {}
167 virtual void performITest(uint32_t* SK_RESTRICT dst,
168 const uint32_t* SK_RESTRICT src,
169 int count) override {
170 for (int i = 0; i < count; ++i) {
171 dst[i] = QMul64(src[i], (uint8_t)i);
175 typedef MathBenchU32 INHERITED;
178 class QMul32Bench : public MathBenchU32 {
180 QMul32Bench() : INHERITED("qmul32") {}
182 virtual void performITest(uint32_t* SK_RESTRICT dst,
183 const uint32_t* SK_RESTRICT src,
184 int count) override {
185 for (int i = 0; i < count; ++i) {
186 dst[i] = SkAlphaMulQ(src[i], (uint8_t)i);
190 typedef MathBenchU32 INHERITED;
193 ///////////////////////////////////////////////////////////////////////////////
195 static bool isFinite_int(float x) {
196 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
197 int exponent = bits << 1 >> 24;
198 return exponent != 0xFF;
201 static bool isFinite_float(float x) {
202 return SkToBool(sk_float_isfinite(x));
205 static bool isFinite_mulzero(float x) {
210 static bool isfinite_and_int(const float data[4]) {
211 return isFinite_int(data[0]) && isFinite_int(data[1]) && isFinite_int(data[2]) && isFinite_int(data[3]);
214 static bool isfinite_and_float(const float data[4]) {
215 return isFinite_float(data[0]) && isFinite_float(data[1]) && isFinite_float(data[2]) && isFinite_float(data[3]);
218 static bool isfinite_and_mulzero(const float data[4]) {
219 return isFinite_mulzero(data[0]) && isFinite_mulzero(data[1]) && isFinite_mulzero(data[2]) && isFinite_mulzero(data[3]);
222 #define mulzeroadd(data) (data[0]*0 + data[1]*0 + data[2]*0 + data[3]*0)
224 static bool isfinite_plus_int(const float data[4]) {
225 return isFinite_int(mulzeroadd(data));
228 static bool isfinite_plus_float(const float data[4]) {
229 return !sk_float_isnan(mulzeroadd(data));
232 static bool isfinite_plus_mulzero(const float data[4]) {
233 float x = mulzeroadd(data);
237 typedef bool (*IsFiniteProc)(const float[]);
239 #define MAKEREC(name) { name, #name }
241 static const struct {
245 MAKEREC(isfinite_and_int),
246 MAKEREC(isfinite_and_float),
247 MAKEREC(isfinite_and_mulzero),
248 MAKEREC(isfinite_plus_int),
249 MAKEREC(isfinite_plus_float),
250 MAKEREC(isfinite_plus_mulzero),
255 static bool isFinite(const SkRect& r) {
256 // x * 0 will be NaN iff x is infinity or NaN.
257 // a + b will be NaN iff either a or b is NaN.
258 float value = r.fLeft * 0 + r.fTop * 0 + r.fRight * 0 + r.fBottom * 0;
260 // value is either NaN or it is finite (zero).
261 // value==value will be true iff value is not NaN
262 return value == value;
265 class IsFiniteBench : public Benchmark {
272 IsFiniteBench(int index) {
275 for (int i = 0; i < N; ++i) {
276 fData[i] = rand.nextSScalar1();
281 fName = "isfinite_rect";
283 fProc = gRec[index].fProc;
284 fName = gRec[index].fName;
288 bool isSuitableFor(Backend backend) override {
289 return backend == kNonRendering_Backend;
293 virtual void onDraw(const int loops, SkCanvas*) {
294 IsFiniteProc proc = fProc;
295 const float* data = fData;
296 // do this so the compiler won't throw away the function call
300 for (int j = 0; j < loops; ++j) {
301 for (int i = 0; i < N - 4; ++i) {
302 counter += proc(&data[i]);
306 for (int j = 0; j < loops; ++j) {
307 for (int i = 0; i < N - 4; ++i) {
308 const SkRect* r = reinterpret_cast<const SkRect*>(&data[i]);
309 if (false) { // avoid bit rot, suppress warning
312 counter += r->isFinite();
318 if (paint.getAlpha() == 0) {
319 SkDebugf("%d\n", counter);
323 virtual const char* onGetName() {
331 typedef Benchmark INHERITED;
334 class FloorBench : public Benchmark {
342 FloorBench(bool fast) : fFast(fast) {
345 for (int i = 0; i < ARRAY; ++i) {
346 fData[i] = rand.nextSScalar1();
350 fName = "floor_fast";
356 bool isSuitableFor(Backend backend) override {
357 return backend == kNonRendering_Backend;
360 virtual void process(float) {}
363 virtual void onDraw(const int loops, SkCanvas*) {
366 const float* data = fData;
369 for (int j = 0; j < loops; ++j) {
370 for (int i = 0; i < ARRAY; ++i) {
371 accum += fast_floor(data[i]);
373 this->process(accum);
376 for (int j = 0; j < loops; ++j) {
377 for (int i = 0; i < ARRAY; ++i) {
378 accum += sk_float_floor(data[i]);
380 this->process(accum);
385 virtual const char* onGetName() {
392 typedef Benchmark INHERITED;
395 class CLZBench : public Benchmark {
399 uint32_t fData[ARRAY];
403 CLZBench(bool usePortable) : fUsePortable(usePortable) {
406 for (int i = 0; i < ARRAY; ++i) {
407 fData[i] = rand.nextU();
411 fName = "clz_portable";
413 fName = "clz_intrinsic";
417 bool isSuitableFor(Backend backend) override {
418 return backend == kNonRendering_Backend;
421 // just so the compiler doesn't remove our loops
422 virtual void process(int) {}
425 virtual void onDraw(const int loops, SkCanvas*) {
429 for (int j = 0; j < loops; ++j) {
430 for (int i = 0; i < ARRAY; ++i) {
431 accum += SkCLZ_portable(fData[i]);
433 this->process(accum);
436 for (int j = 0; j < loops; ++j) {
437 for (int i = 0; i < ARRAY; ++i) {
438 accum += SkCLZ(fData[i]);
440 this->process(accum);
445 virtual const char* onGetName() {
452 typedef Benchmark INHERITED;
455 ///////////////////////////////////////////////////////////////////////////////
457 class NormalizeBench : public Benchmark {
461 SkVector fVec[ARRAY];
466 for (int i = 0; i < ARRAY; ++i) {
467 fVec[i].set(rand.nextSScalar1(), rand.nextSScalar1());
470 fName = "point_normalize";
473 bool isSuitableFor(Backend backend) override {
474 return backend == kNonRendering_Backend;
477 // just so the compiler doesn't remove our loops
478 virtual void process(int) {}
481 virtual void onDraw(const int loops, SkCanvas*) {
484 for (int j = 0; j < loops; ++j) {
485 for (int i = 0; i < ARRAY; ++i) {
486 accum += fVec[i].normalize();
488 this->process(accum);
492 virtual const char* onGetName() {
499 typedef Benchmark INHERITED;
502 ///////////////////////////////////////////////////////////////////////////////
504 class FixedMathBench : public Benchmark {
514 for (int i = 0; i < N; ++i) {
515 fData[i] = rand.nextSScalar1();
520 bool isSuitableFor(Backend backend) override {
521 return backend == kNonRendering_Backend;
525 virtual void onDraw(const int loops, SkCanvas*) {
526 for (int j = 0; j < loops; ++j) {
527 for (int i = 0; i < N - 4; ++i) {
528 fResult[i] = SkFloatToFixed(fData[i]);
533 if (paint.getAlpha() == 0) {
534 SkDebugf("%d\n", fResult[0]);
538 virtual const char* onGetName() {
539 return "float_to_fixed";
543 typedef Benchmark INHERITED;
546 ///////////////////////////////////////////////////////////////////////////////
548 template <typename T>
549 class DivModBench : public Benchmark {
552 explicit DivModBench(const char* name) {
553 fName.printf("divmod_%s", name);
556 bool isSuitableFor(Backend backend) override {
557 return backend == kNonRendering_Backend;
561 virtual const char* onGetName() {
562 return fName.c_str();
565 virtual void onDraw(const int loops, SkCanvas*) {
566 volatile T a = 0, b = 0;
568 for (int i = 0; i < loops; i++) {
569 if ((T)i == 0) continue; // Small T will wrap around.
570 SkTDivMod((T)(i+1), (T)i, &div, &mod);
576 DEF_BENCH(return new DivModBench<uint8_t>("uint8_t"))
577 DEF_BENCH(return new DivModBench<uint16_t>("uint16_t"))
578 DEF_BENCH(return new DivModBench<uint32_t>("uint32_t"))
579 DEF_BENCH(return new DivModBench<uint64_t>("uint64_t"))
581 DEF_BENCH(return new DivModBench<int8_t>("int8_t"))
582 DEF_BENCH(return new DivModBench<int16_t>("int16_t"))
583 DEF_BENCH(return new DivModBench<int32_t>("int32_t"))
584 DEF_BENCH(return new DivModBench<int64_t>("int64_t"))
586 ///////////////////////////////////////////////////////////////////////////////
588 DEF_BENCH( return new NoOpMathBench(); )
589 DEF_BENCH( return new SkRSqrtMathBench(); )
590 DEF_BENCH( return new SlowISqrtMathBench(); )
591 DEF_BENCH( return new FastISqrtMathBench(); )
592 DEF_BENCH( return new QMul64Bench(); )
593 DEF_BENCH( return new QMul32Bench(); )
595 DEF_BENCH( return new IsFiniteBench(-1); )
596 DEF_BENCH( return new IsFiniteBench(0); )
597 DEF_BENCH( return new IsFiniteBench(1); )
598 DEF_BENCH( return new IsFiniteBench(2); )
599 DEF_BENCH( return new IsFiniteBench(3); )
600 DEF_BENCH( return new IsFiniteBench(4); )
601 DEF_BENCH( return new IsFiniteBench(5); )
603 DEF_BENCH( return new FloorBench(false); )
604 DEF_BENCH( return new FloorBench(true); )
606 DEF_BENCH( return new CLZBench(false); )
607 DEF_BENCH( return new CLZBench(true); )
609 DEF_BENCH( return new NormalizeBench(); )
611 DEF_BENCH( return new FixedMathBench(); )