3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkFloatBits.h"
14 class ScalarBench : public Benchmark {
17 ScalarBench(const char name[]) {
18 fName.printf("scalar_%s", name);
21 bool isSuitableFor(Backend backend) override {
22 return backend == kNonRendering_Backend;
25 virtual void performTest() = 0;
28 virtual int mulLoopCount() const { return 1; }
30 const char* onGetName() override {
34 void onDraw(const int loops, SkCanvas* canvas) override {
35 for (int i = 0; i < loops; i++) {
41 typedef Benchmark INHERITED;
44 // having unknown values in our arrays can throw off the timing a lot, perhaps
45 // handling NaN values is a lot slower. Anyway, this guy is just meant to put
46 // reasonable values in our arrays.
47 template <typename T> void init9(T array[9]) {
49 for (int i = 0; i < 9; i++) {
50 array[i] = rand.nextSScalar1();
54 class FloatComparisonBench : public ScalarBench {
56 FloatComparisonBench() : INHERITED("compare_float") {
60 virtual int mulLoopCount() const { return 4; }
61 virtual void performTest() {
62 // xoring into a volatile prevents the compiler from optimizing these checks away.
63 volatile bool junk = false;
64 junk ^= (fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
65 junk ^= (fArray[2] != 0.0f || fArray[5] != 0.0f);
69 typedef ScalarBench INHERITED;
72 class ForcedIntComparisonBench : public ScalarBench {
74 ForcedIntComparisonBench()
75 : INHERITED("compare_forced_int") {
79 virtual int mulLoopCount() const { return 4; }
80 virtual void performTest() {
81 // xoring into a volatile prevents the compiler from optimizing these checks away.
82 volatile int32_t junk = 0;
83 junk ^= (SkScalarAs2sCompliment(fArray[6]) |
84 SkScalarAs2sCompliment(fArray[7]) |
85 (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
86 junk ^= (SkScalarAs2sCompliment(fArray[2]) |
87 SkScalarAs2sCompliment(fArray[5]));
90 static const int32_t kPersp1Int = 0x3f800000;
92 typedef ScalarBench INHERITED;
95 class IsFiniteScalarBench : public ScalarBench {
97 IsFiniteScalarBench() : INHERITED("isfinite") {
99 for (size_t i = 0; i < ARRAY_N; ++i) {
100 fArray[i] = rand.nextSScalar1();
104 int mulLoopCount() const override { return 1; }
105 void performTest() override {
107 for (size_t i = 0; i < ARRAY_N; ++i) {
108 // We pass -fArray[i], so the compiler can't cheat and treat the
109 // value as an int (even though we tell it that it is a float)
110 sum += SkScalarIsFinite(-fArray[i]);
112 // we do this so the compiler won't optimize our loop away...
113 this->doSomething(fArray, sum);
116 virtual void doSomething(SkScalar array[], int sum) {}
121 SkScalar fArray[ARRAY_N];
123 typedef ScalarBench INHERITED;
126 ///////////////////////////////////////////////////////////////////////////////
128 class RectBoundsBench : public Benchmark {
137 for (int i = 0; i < PTS; ++i) {
138 fPts[i].fX = rand.nextSScalar1();
139 fPts[i].fY = rand.nextSScalar1();
143 bool isSuitableFor(Backend backend) override {
144 return backend == kNonRendering_Backend;
148 const char* onGetName() override {
149 return "rect_bounds";
152 void onDraw(const int loops, SkCanvas* canvas) override {
154 for (int i = 0; i < loops; ++i) {
155 for (int i = 0; i < 1000; ++i) {
162 typedef Benchmark INHERITED;
165 ///////////////////////////////////////////////////////////////////////////////
167 DEF_BENCH( return new FloatComparisonBench(); )
168 DEF_BENCH( return new ForcedIntComparisonBench(); )
169 DEF_BENCH( return new RectBoundsBench(); )
170 DEF_BENCH( return new IsFiniteScalarBench(); )