diff options
-rw-r--r-- | bench/Sk4fBench.cpp | 16 | ||||
-rw-r--r-- | src/core/SkNx.h | 5 | ||||
-rw-r--r-- | src/opts/SkNx_neon.h | 19 | ||||
-rw-r--r-- | src/opts/SkNx_sse.h | 12 | ||||
-rw-r--r-- | tests/SkNxTest.cpp | 8 |
5 files changed, 59 insertions, 1 deletions
diff --git a/bench/Sk4fBench.cpp b/bench/Sk4fBench.cpp index d5201fa2f0..a932c295d4 100644 --- a/bench/Sk4fBench.cpp +++ b/bench/Sk4fBench.cpp @@ -40,6 +40,22 @@ DEF_BENCH(return new Sk4fRoundtripBench<uint8_t>;) DEF_BENCH(return new Sk4fRoundtripBench<uint16_t>;) DEF_BENCH(return new Sk4fRoundtripBench<int>;) +struct Sk4fFloorBench : public Benchmark { + Sk4fFloorBench() {} + + const char* onGetName() override { return "Sk4f_floor"; } + bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } + + void onDraw(int loops, SkCanvas* canvas) override { + Sk4f fs(1,2,3,4); + while (loops --> 0) { + fs = fs.floor(); + } + fs.store((float*)blackhole); + } +}; +DEF_BENCH(return new Sk4fFloorBench;) + struct Sk4fGradientBench : public Benchmark { const char* onGetName() override { return "Sk4f_gradient"; } bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } diff --git a/src/core/SkNx.h b/src/core/SkNx.h index 69295d4fc3..986c133415 100644 --- a/src/core/SkNx.h +++ b/src/core/SkNx.h @@ -50,7 +50,7 @@ public: #undef OP #define OP(op) SkNx op() const { return {fLo.op(), fHi.op()}; } - OP(abs) + OP(abs) OP(floor) OP(sqrt) OP(rsqrt0) OP(rsqrt1) OP(rsqrt2) OP(invert) OP(approxInvert) #undef OP @@ -124,6 +124,7 @@ public: static SkNx Max(const SkNx& a, const SkNx& b) { return SkTMax(a.fVal, b.fVal); } SkNx abs() const { return SkTAbs(fVal); } + SkNx floor() const { return Floor(fVal); } SkNx sqrt () const { return Sqrt(fVal); } SkNx rsqrt0() const { return this->sqrt().invert(); } @@ -145,6 +146,8 @@ public: SkNx thenElse(const SkNx& t, const SkNx& e) const { return fVal != 0 ? t : e; } protected: + static double Floor(double val) { return ::floor (val); } + static float Floor(float val) { return ::floorf(val); } static double Sqrt(double val) { return ::sqrt (val); } static float Sqrt(float val) { return ::sqrtf(val); } diff --git a/src/opts/SkNx_neon.h b/src/opts/SkNx_neon.h index 2cb8eb348d..cdc4615849 100644 --- a/src/opts/SkNx_neon.h +++ b/src/opts/SkNx_neon.h @@ -10,6 +10,17 @@ #define SKNX_IS_FAST +// ARMv8 has vrndmq_f32 to floor 4 floats. Here we emulate it: +// - round by adding (1<<23) with our sign, then subtracting it; +// - if that rounded value is bigger than our input, subtract 1. +static inline float32x4_t armv7_vrndmq_f32(float32x4_t v) { + auto sign = vandq_u32((uint32x4_t)v, vdupq_n_u32(1<<31)); + auto bias = (float32x4_t)(vorrq_u32((uint32x4_t)vdupq_n_f32(1<<23), sign)); + auto rounded = vsubq_f32(vaddq_f32(v, bias), bias); + auto too_big = vcgtq_f32(rounded, v); + return vsubq_f32(rounded, (float32x4_t)vandq_u32(too_big, (uint32x4_t)vdupq_n_f32(1))); +} + // Well, this is absurd. The shifts require compile-time constant arguments. #define SHIFT8(op, v, bits) switch(bits) { \ @@ -161,6 +172,14 @@ public: static SkNx Max(const SkNx& l, const SkNx& r) { return vmaxq_f32(l.fVec, r.fVec); } SkNx abs() const { return vabsq_f32(fVec); } + SkNx floor() const { + #if defined(SK_CPU_ARM64) + return vrndmq_f32(fVec); + #else + return armv7_vrndmq_f32(fVec); + #endif + } + SkNx rsqrt0() const { return vrsqrteq_f32(fVec); } SkNx rsqrt1() const { diff --git a/src/opts/SkNx_sse.h b/src/opts/SkNx_sse.h index 889253b9d8..def7ba1719 100644 --- a/src/opts/SkNx_sse.h +++ b/src/opts/SkNx_sse.h @@ -13,6 +13,17 @@ #define SKNX_IS_FAST +// SSE 4.1 has _mm_floor_ps to floor 4 floats. We emulate it: +// - round by adding (1<<23) with our sign, then subtracting it; +// - if that rounded value is bigger than our input, subtract 1. +static inline __m128 sse2_mm_floor_ps(__m128 v) { + __m128 sign = _mm_and_ps(v, _mm_set1_ps(-0.0f)); + __m128 bias = _mm_or_ps(sign, _mm_set1_ps(1<<23)); + __m128 rounded = _mm_sub_ps(_mm_add_ps(v, bias), bias); + __m128 too_big = _mm_cmpgt_ps(rounded, v); + return _mm_sub_ps(rounded, _mm_and_ps(too_big, _mm_set1_ps(1.0f))); +} + template <> class SkNx<2, float> { public: @@ -92,6 +103,7 @@ public: static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); } SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); } + SkNx floor() const { return sse2_mm_floor_ps(fVec); } SkNx sqrt () const { return _mm_sqrt_ps (fVec); } SkNx rsqrt0() const { return _mm_rsqrt_ps(fVec); } diff --git a/tests/SkNxTest.cpp b/tests/SkNxTest.cpp index 13e0a9f61f..e329e17bfa 100644 --- a/tests/SkNxTest.cpp +++ b/tests/SkNxTest.cpp @@ -215,6 +215,14 @@ DEF_TEST(SkNx_abs, r) { REPORTER_ASSERT(r, fs.kth<3>() == 4.0f); } +DEF_TEST(SkNx_floor, r) { + auto fs = Sk4f(0.4f, -0.4f, 0.6f, -0.6f).floor(); + REPORTER_ASSERT(r, fs.kth<0>() == 0.0f); + REPORTER_ASSERT(r, fs.kth<1>() == -1.0f); + REPORTER_ASSERT(r, fs.kth<2>() == 0.0f); + REPORTER_ASSERT(r, fs.kth<3>() == -1.0f); +} + DEF_TEST(SkNx_shuffle, r) { Sk4f f4(0,10,20,30); |