};
template <int N, typename T>
+class SkNi {
+public:
+ SkNi() {}
+ explicit SkNi(T val) : fLo(val), fHi(val) {}
+ static SkNi Load(const T vals[N]) {
+ return SkNi(SkNi<N/2,T>::Load(vals), SkNi<N/2,T>::Load(vals+N/2));
+ }
+
+ SkNi(T a, T b) : fLo(a), fHi(b) { REQUIRE(N==2); }
+ SkNi(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) { REQUIRE(N==4); }
+ SkNi(T a, T b, T c, T d, T e, T f, T g, T h) : fLo(a,b,c,d), fHi(e,f,g,h) { REQUIRE(N==8); }
+
+ void store(T vals[N]) const {
+ fLo.store(vals);
+ fHi.store(vals+N/2);
+ }
+
+ SkNi operator + (const SkNi& o) const { return SkNi(fLo + o.fLo, fHi + o.fHi); }
+ SkNi operator - (const SkNi& o) const { return SkNi(fLo - o.fLo, fHi - o.fHi); }
+ SkNi operator * (const SkNi& o) const { return SkNi(fLo * o.fLo, fHi * o.fHi); }
+
+ SkNi operator << (int bits) const { return SkNi(fLo << bits, fHi << bits); }
+ SkNi operator >> (int bits) const { return SkNi(fLo >> bits, fHi >> bits); }
+
+ // TODO: comparisons, min, max?
+
+ template <int k> T kth() const {
+ SkASSERT(0 <= k && k < N);
+ return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>();
+ }
+
+private:
+ REQUIRE(0 == (N & (N-1)));
+ SkNi(const SkNi<N/2, T>& lo, const SkNi<N/2, T>& hi) : fLo(lo), fHi(hi) {}
+
+ SkNi<N/2, T> fLo, fHi;
+};
+
+template <int N, typename T>
class SkNf {
typedef SkNb<N, sizeof(T)> Nb;
public:
};
template <typename T>
+class SkNi<1,T> {
+public:
+ SkNi() {}
+ explicit SkNi(T val) : fVal(val) {}
+ static SkNi Load(const T vals[1]) { return SkNi(vals[0]); }
+
+ void store(T vals[1]) const { vals[0] = fVal; }
+
+ SkNi operator + (const SkNi& o) const { return SkNi(fVal + o.fVal); }
+ SkNi operator - (const SkNi& o) const { return SkNi(fVal - o.fVal); }
+ SkNi operator * (const SkNi& o) const { return SkNi(fVal * o.fVal); }
+
+ SkNi operator << (int bits) const { return SkNi(fVal << bits); }
+ SkNi operator >> (int bits) const { return SkNi(fVal >> bits); }
+
+ template <int k> T kth() const {
+ SkASSERT(0 == k);
+ return fVal;
+ }
+
+private:
+ T fVal;
+};
+
+template <typename T>
class SkNf<1,T> {
typedef SkNb<1, sizeof(T)> Nb;
public:
template <typename L, typename R> L& operator *= (L& l, const R& r) { return (l = l * r); }
template <typename L, typename R> L& operator /= (L& l, const R& r) { return (l = l / r); }
+template <typename L> L& operator <<= (L& l, int bits) { return (l = l << bits); }
+template <typename L> L& operator >>= (L& l, int bits) { return (l = l >> bits); }
// Include platform specific specializations if available.
#ifndef SKNX_NO_SIMD
typedef SkNf<4, double> Sk4d;
typedef SkNf<4, SkScalar> Sk4s;
+typedef SkNi<4, uint16_t> Sk4h;
+typedef SkNi<8, uint16_t> Sk8h;
+
#endif//SkNx_DEFINED
__m128 fVec;
};
+template <>
+class SkNi<4, uint16_t> {
+public:
+ SkNi(const __m128i& vec) : fVec(vec) {}
+
+ SkNi() {}
+ explicit SkNi(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
+ static SkNi Load(const uint16_t vals[4]) { return _mm_loadl_epi64((const __m128i*)vals); }
+ SkNi(uint16_t a, uint16_t b, uint16_t c, uint16_t d) : fVec(_mm_setr_epi16(a,b,c,d,0,0,0,0)) {}
+
+ void store(uint16_t vals[4]) const { _mm_storel_epi64((__m128i*)vals, fVec); }
+
+ SkNi operator + (const SkNi& o) const { return _mm_add_epi16(fVec, o.fVec); }
+ SkNi operator - (const SkNi& o) const { return _mm_sub_epi16(fVec, o.fVec); }
+ SkNi operator * (const SkNi& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
+
+ SkNi operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
+ SkNi operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
+
+ template <int k> uint16_t kth() const {
+ SkASSERT(0 <= k && k < 4);
+ return _mm_extract_epi16(fVec, k);
+ }
+protected:
+ __m128i fVec;
+};
+
+template <>
+class SkNi<8, uint16_t> {
+public:
+ SkNi(const __m128i& vec) : fVec(vec) {}
+
+ SkNi() {}
+ explicit SkNi(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
+ static SkNi Load(const uint16_t vals[8]) { return _mm_loadu_si128((const __m128i*)vals); }
+ SkNi(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
+ uint16_t e, uint16_t f, uint16_t g, uint16_t h) : fVec(_mm_setr_epi16(a,b,c,d,e,f,g,h)) {}
+
+ void store(uint16_t vals[8]) const { _mm_storeu_si128((__m128i*)vals, fVec); }
+
+ SkNi operator + (const SkNi& o) const { return _mm_add_epi16(fVec, o.fVec); }
+ SkNi operator - (const SkNi& o) const { return _mm_sub_epi16(fVec, o.fVec); }
+ SkNi operator * (const SkNi& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
+
+ SkNi operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
+ SkNi operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
+
+ template <int k> uint16_t kth() const {
+ SkASSERT(0 <= k && k < 8);
+ return _mm_extract_epi16(fVec, k);
+ }
+protected:
+ __m128i fVec;
+};
#endif//SkNx_sse_DEFINED
test_Nf<4, float>(r);
test_Nf<4, double>(r);
}
+
+template <int N, typename T>
+void test_Ni(skiatest::Reporter* r) {
+ auto assert_eq = [&](const SkNi<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) {
+ T vals[8];
+ v.store(vals);
+
+ switch (N) {
+ case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h);
+ case 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d);
+ case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b);
+ }
+ };
+
+ T vals[] = { 1,2,3,4,5,6,7,8 };
+ SkNi<N,T> a = SkNi<N,T>::Load(vals),
+ b(a),
+ c = a;
+ SkNi<N,T> d;
+ d = a;
+
+ assert_eq(a, 1,2,3,4,5,6,7,8);
+ assert_eq(b, 1,2,3,4,5,6,7,8);
+ assert_eq(c, 1,2,3,4,5,6,7,8);
+ assert_eq(d, 1,2,3,4,5,6,7,8);
+
+ assert_eq(a+a, 2,4,6,8,10,12,14,16);
+ assert_eq(a*a, 1,4,9,16,25,36,49,64);
+ assert_eq(a*a-a, 0,2,6,12,20,30,42,56);
+
+ assert_eq(a >> 2, 0,0,0,1,1,1,1,2);
+ assert_eq(a << 1, 2,4,6,8,10,12,14,16);
+
+ REPORTER_ASSERT(r, a.template kth<1>() == 2);
+}
+
+DEF_TEST(SkNi, r) {
+ test_Ni<2, uint16_t>(r);
+ test_Ni<4, uint16_t>(r);
+ test_Ni<8, uint16_t>(r);
+}