From 2719552fb1469b9697a3ee8b15a537044fa6dd96 Mon Sep 17 00:00:00 2001 From: mtklein Date: Thu, 26 Feb 2015 12:21:25 -0800 Subject: [PATCH] Think implicit promotion is a good idea? Things like foo.multiply(Sk4f(t,t,t,t)) can just be foo.multiply(t). BUG=skia: Review URL: https://codereview.chromium.org/960773005 --- src/core/Sk4x.h | 3 ++- src/core/Sk4x_portable.h | 1 + src/core/Sk4x_sse.h | 2 ++ tests/Sk4xTest.cpp | 4 ++++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/Sk4x.h b/src/core/Sk4x.h index 7c246df..058c400 100644 --- a/src/core/Sk4x.h +++ b/src/core/Sk4x.h @@ -22,7 +22,8 @@ typedef Sk4x Sk4i; template class Sk4x { public: - Sk4x(); // Uninitialized; use Sk4x(0,0,0,0) for zero. + Sk4x(); // Uninitialized; use Sk4x(0) for zero. + /*implicit*/ Sk4x(T); // Same as Sk4x(T,T,T,T); Sk4x(T, T, T, T); Sk4x(const Sk4x&); diff --git a/src/core/Sk4x_portable.h b/src/core/Sk4x_portable.h index 2f985d0..bd056c7 100644 --- a/src/core/Sk4x_portable.h +++ b/src/core/Sk4x_portable.h @@ -19,6 +19,7 @@ #define M(...) template __VA_ARGS__ Sk4x:: M() Sk4x() {} +M() Sk4x(T v) { fVec[0] = fVec[1] = fVec[2] = fVec[3] = v; } M() Sk4x(T a, T b, T c, T d) { fVec[0] = a; fVec[1] = b; fVec[2] = c; fVec[3] = d; } M() Sk4x(const Sk4x& other) { this->set(other.fVec); } diff --git a/src/core/Sk4x_sse.h b/src/core/Sk4x_sse.h index 10a0a83..ee09f77 100644 --- a/src/core/Sk4x_sse.h +++ b/src/core/Sk4x_sse.h @@ -75,6 +75,7 @@ Sk4x Sk4x::ZWCD(const Sk4x& a, const Sk4x& b) { // Now we'll write all Sk4f specific methods. This M() macro will remove some noise. #define M(...) template <> inline __VA_ARGS__ Sk4f:: +M() Sk4x(float v) : fVec(_mm_set1_ps(v)) {} M() Sk4x(float a, float b, float c, float d) : fVec(_mm_set_ps(d,c,b,a)) {} M(Sk4f) Load (const float fs[4]) { return _mm_loadu_ps(fs); } @@ -112,6 +113,7 @@ M(Sk4f) Max(const Sk4f& a, const Sk4f& b) { return _mm_max_ps(a.fVec, b.fVec); } #undef M #define M(...) template <> inline __VA_ARGS__ Sk4i:: +M() Sk4x(int32_t v) : fVec(_mm_set1_epi32(v)) {} M() Sk4x(int32_t a, int32_t b, int32_t c, int32_t d) : fVec(_mm_set_epi32(d,c,b,a)) {} M(Sk4i) Load (const int32_t is[4]) { return _mm_loadu_si128((const __m128i*)is); } diff --git a/tests/Sk4xTest.cpp b/tests/Sk4xTest.cpp index d6e7e51..0985c3b 100644 --- a/tests/Sk4xTest.cpp +++ b/tests/Sk4xTest.cpp @@ -83,6 +83,10 @@ DEF_TEST(Sk4x_Arith, r) { ASSERT_EQ(Sk4i(3,8,15,24), Sk4i(1,2,3,4).multiply(Sk4i(3,4,5,6))); } +DEF_TEST(Sk4x_ImplicitPromotion, r) { + ASSERT_EQ(Sk4f(2,4,6,8), Sk4f(1,2,3,4).multiply(2.0f)); +} + DEF_TEST(Sk4x_Comparison, r) { ASSERT_EQ(Sk4f(1,2,3,4), Sk4f(1,2,3,4)); ASSERT_NE(Sk4f(4,3,2,1), Sk4f(1,2,3,4)); -- 2.7.4