Home | History | Annotate | Download | only in bench
      1 #include "SkBenchmark.h"
      2 #include "SkFloatBits.h"
      3 #include "SkRandom.h"
      4 #include "SkString.h"
      5 
      6 class ScalarBench : public SkBenchmark {
      7     SkString    fName;
      8     enum { N = 100000 };
      9 public:
     10     ScalarBench(void* param, const char name[]) : INHERITED(param) {
     11         fName.printf("scalar_%s", name);
     12     }
     13 
     14     virtual void performTest() = 0;
     15 
     16 protected:
     17     virtual int mulLoopCount() const { return 1; }
     18 
     19     virtual const char* onGetName() {
     20         return fName.c_str();
     21     }
     22 
     23     virtual void onDraw(SkCanvas* canvas) {
     24         int n = N * this->mulLoopCount();
     25         for (int i = 0; i < n; i++) {
     26             this->performTest();
     27         }
     28     }
     29 
     30 private:
     31     typedef SkBenchmark INHERITED;
     32 };
     33 
     34 // we want to stop the compiler from eliminating code that it thinks is a no-op
     35 // so we have a non-static global we increment, hoping that will convince the
     36 // compiler to execute everything
     37 int gScalarBench_NonStaticGlobal;
     38 
     39 #define always_do(pred)                     \
     40     do {                                    \
     41         if (pred) {                         \
     42             ++gScalarBench_NonStaticGlobal; \
     43         }                                   \
     44     } while (0)
     45 
     46 // having unknown values in our arrays can throw off the timing a lot, perhaps
     47 // handling NaN values is a lot slower. Anyway, this guy is just meant to put
     48 // reasonable values in our arrays.
     49 template <typename T> void init9(T array[9]) {
     50     SkRandom rand;
     51     for (int i = 0; i < 9; i++) {
     52         array[i] = rand.nextSScalar1();
     53     }
     54 }
     55 
     56 class FloatComparisonBench : public ScalarBench {
     57 public:
     58     FloatComparisonBench(void* param) : INHERITED(param, "compare_float") {
     59         init9(fArray);
     60     }
     61 protected:
     62     virtual int mulLoopCount() const { return 4; }
     63     virtual void performTest() {
     64         always_do(fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
     65         always_do(fArray[2] != 0.0f || fArray[5] != 0.0f);
     66     }
     67 private:
     68     float fArray[9];
     69     typedef ScalarBench INHERITED;
     70 };
     71 
     72 class ForcedIntComparisonBench : public ScalarBench {
     73 public:
     74     ForcedIntComparisonBench(void* param)
     75         : INHERITED(param, "compare_forced_int") {
     76         init9(fArray);
     77     }
     78 protected:
     79     virtual int mulLoopCount() const { return 4; }
     80     virtual void performTest() {
     81         always_do(SkScalarAs2sCompliment(fArray[6]) |
     82                   SkScalarAs2sCompliment(fArray[7]) |
     83                   (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
     84         always_do(SkScalarAs2sCompliment(fArray[2]) |
     85                   SkScalarAs2sCompliment(fArray[5]));
     86     }
     87 private:
     88     static const int32_t kPersp1Int = 0x3f800000;
     89     SkScalar fArray[9];
     90     typedef ScalarBench INHERITED;
     91 };
     92 
     93 static SkBenchmark* S0(void* p) { return new FloatComparisonBench(p); }
     94 static SkBenchmark* S1(void* p) { return new ForcedIntComparisonBench(p); }
     95 
     96 static BenchRegistry gReg0(S0);
     97 static BenchRegistry gReg1(S1);
     98