diff options
-rw-r--r-- | bench/MathBench.cpp | 64 | ||||
-rw-r--r-- | include/private/SkFloatingPoint.h | 22 | ||||
-rw-r--r-- | tests/MathTest.cpp | 22 |
3 files changed, 105 insertions, 3 deletions
diff --git a/bench/MathBench.cpp b/bench/MathBench.cpp index 7768253544..1ae683aab8 100644 --- a/bench/MathBench.cpp +++ b/bench/MathBench.cpp @@ -598,3 +598,67 @@ DEF_BENCH( return new CLZBench(true); ) DEF_BENCH( return new NormalizeBench(); ) DEF_BENCH( return new FixedMathBench(); ) + +////////////////////////////////////////////////////////////// + +#include "../private/SkFloatBits.h" +class Floor2IntBench : public Benchmark { + enum { + ARRAY = 1000, + }; + float fData[ARRAY]; + const bool fSat; +public: + + Floor2IntBench(bool sat) : fSat(sat) { + SkRandom rand; + + for (int i = 0; i < ARRAY; ++i) { + fData[i] = SkBits2Float(rand.nextU()); + } + + if (sat) { + fName = "floor2int_sat"; + } else { + fName = "floor2int_undef"; + } + } + + bool isSuitableFor(Backend backend) override { + return backend == kNonRendering_Backend; + } + + // These exist to try to stop the compiler from detecting what we doing, and throwing + // parts away (or knowing exactly how big the loop counts are). + virtual void process(int) {} + virtual int count() { return ARRAY; } + +protected: + void onDraw(int loops, SkCanvas*) override { + int accum = 0; + + for (int j = 0; j < loops; ++j) { + int n = this->count(); + if (fSat) { + for (int i = 0; i < n; ++i) { + accum += sk_float_floor2int(fData[i]); + } + } else { + for (int i = 0; i < n; ++i) { + accum += sk_float_floor2int_no_saturate(fData[i]); + } + } + this->process(accum); + } + } + + const char* onGetName() override { return fName; } + +private: + const char* fName; + + typedef Benchmark INHERITED; +}; +DEF_BENCH( return new Floor2IntBench(false); ) +DEF_BENCH( return new Floor2IntBench(true); ) + diff --git a/include/private/SkFloatingPoint.h b/include/private/SkFloatingPoint.h index c35c95194d..79a39a2840 100644 --- a/include/private/SkFloatingPoint.h +++ b/include/private/SkFloatingPoint.h @@ -78,9 +78,25 @@ static inline float sk_float_pow(float base, float exp) { #define sk_double_isnan(a) sk_float_isnan(a) -#define sk_float_floor2int(x) (int)sk_float_floor(x) -#define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) -#define sk_float_ceil2int(x) (int)sk_float_ceil(x) +#define SK_MaxS32FitsInFloat 2147483520 +#define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat + +/** + * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN. + */ +static inline int sk_float_saturate2int(float x) { + x = SkTMin<float>(x, SK_MaxS32FitsInFloat); + x = SkTMax<float>(x, SK_MinS32FitsInFloat); + return (int)x; +} + +#define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x)) +#define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f)) +#define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x)) + +#define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x) +#define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f) +#define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x) #define sk_double_floor(x) floor(x) #define sk_double_round(x) floor((x) + 0.5) diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp index 3d30f0db2a..bbd551e1e9 100644 --- a/tests/MathTest.cpp +++ b/tests/MathTest.cpp @@ -671,3 +671,25 @@ DEF_TEST(GrNextSizePow2, reporter) { test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX); } + +DEF_TEST(FloatSaturate, reporter) { + const struct { + float fFloat; + int fExpectedInt; + } recs[] = { + { 0, 0 }, + { 100.5f, 100 }, + { (float)SK_MaxS32, SK_MaxS32FitsInFloat }, + { (float)SK_MinS32, SK_MinS32FitsInFloat }, + { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat }, + { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat }, + { SK_ScalarInfinity, SK_MaxS32FitsInFloat }, + { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat }, + { SK_ScalarNaN, SK_MaxS32FitsInFloat }, + }; + + for (auto r : recs) { + int i = sk_float_saturate2int(r.fFloat); + REPORTER_ASSERT(reporter, r.fExpectedInt == i); + } +} |