| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkNx_DEFINED | 8 #ifndef SkNx_DEFINED |
| 9 #define SkNx_DEFINED | 9 #define SkNx_DEFINED |
| 10 | 10 |
| 11 | 11 |
| 12 #define SKNX_NO_SIMDx // Remove the x to disable SIMD for all SkNx types. | 12 #define SKNX_NO_SIMDx // Remove the x to disable SIMD for all SkNx types. |
| 13 | 13 |
| 14 | 14 |
| 15 #include "SkScalar.h" | 15 #include "SkScalar.h" |
| 16 #include "SkTypes.h" | 16 #include "SkTypes.h" |
| 17 #include <math.h> | 17 #include <math.h> |
| 18 #define REQUIRE(x) static_assert(x, #x) | 18 #define REQUIRE(x) static_assert(x, #x) |
| 19 | 19 |
| 20 // The default implementations of SkNi<N,T> and SkNf<N,T> just fall back on a pa
ir of size N/2. | 20 // The default implementations just fall back on a pair of size N/2. |
| 21 template <int N, typename T> | 21 |
| 22 class SkNi { | 22 // SkNb is a _very_ minimal class representing a vector of bools returned by com
parison operators. |
| 23 // We pass along the byte size of the compared types (Bytes) to help platform sp
ecializations. |
| 24 template <int N, int Bytes> |
| 25 class SkNb { |
| 23 public: | 26 public: |
| 24 // For now SkNi is a _very_ minimal sketch just to support comparison operat
ors on SkNf. | 27 SkNb() {} |
| 25 SkNi() {} | 28 SkNb(const SkNb<N/2, Bytes>& lo, const SkNb<N/2, Bytes>& hi) : fLo(lo), fHi(
hi) {} |
| 26 SkNi(const SkNi<N/2, T>& lo, const SkNi<N/2, T>& hi) : fLo(lo), fHi(hi) {} | 29 |
| 27 bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } | 30 bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
| 28 bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } | 31 bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
| 29 | 32 |
| 30 private: | 33 private: |
| 31 REQUIRE(0 == (N & (N-1))); | 34 REQUIRE(0 == (N & (N-1))); |
| 32 SkNi<N/2, T> fLo, fHi; | 35 SkNb<N/2, Bytes> fLo, fHi; |
| 33 }; | 36 }; |
| 34 | 37 |
| 35 template <int N, typename T> | 38 template <int N, typename T> |
| 36 class SkNf { | 39 class SkNf { |
| 37 static SkNi<N,int32_t> ToNi(float); | 40 typedef SkNb<N, sizeof(T)> Nb; |
| 38 static SkNi<N,int64_t> ToNi(double); | |
| 39 typedef decltype(ToNi(T())) Ni; | |
| 40 public: | 41 public: |
| 41 SkNf() {} | 42 SkNf() {} |
| 42 explicit SkNf(T val) : fLo(val), fHi(val) {} | 43 explicit SkNf(T val) : fLo(val), fHi(val) {} |
| 43 static SkNf Load(const T vals[N]) { | 44 static SkNf Load(const T vals[N]) { |
| 44 return SkNf(SkNf<N/2,T>::Load(vals), SkNf<N/2,T>::Load(vals+N/2)); | 45 return SkNf(SkNf<N/2,T>::Load(vals), SkNf<N/2,T>::Load(vals+N/2)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 SkNf(T a, T b) : fLo(a), fHi(b) {
REQUIRE(N==2); } | 48 SkNf(T a, T b) : fLo(a), fHi(b) {
REQUIRE(N==2); } |
| 48 SkNf(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) {
REQUIRE(N==4); } | 49 SkNf(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) {
REQUIRE(N==4); } |
| 49 SkNf(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); } | 50 SkNf(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); } |
| 50 | 51 |
| 51 void store(T vals[N]) const { | 52 void store(T vals[N]) const { |
| 52 fLo.store(vals); | 53 fLo.store(vals); |
| 53 fHi.store(vals+N/2); | 54 fHi.store(vals+N/2); |
| 54 } | 55 } |
| 55 | 56 |
| 56 SkNf operator + (const SkNf& o) const { return SkNf(fLo + o.fLo, fHi + o.fHi
); } | 57 SkNf operator + (const SkNf& o) const { return SkNf(fLo + o.fLo, fHi + o.fHi
); } |
| 57 SkNf operator - (const SkNf& o) const { return SkNf(fLo - o.fLo, fHi - o.fHi
); } | 58 SkNf operator - (const SkNf& o) const { return SkNf(fLo - o.fLo, fHi - o.fHi
); } |
| 58 SkNf operator * (const SkNf& o) const { return SkNf(fLo * o.fLo, fHi * o.fHi
); } | 59 SkNf operator * (const SkNf& o) const { return SkNf(fLo * o.fLo, fHi * o.fHi
); } |
| 59 SkNf operator / (const SkNf& o) const { return SkNf(fLo / o.fLo, fHi / o.fHi
); } | 60 SkNf operator / (const SkNf& o) const { return SkNf(fLo / o.fLo, fHi / o.fHi
); } |
| 60 | 61 |
| 61 Ni operator == (const SkNf& o) const { return Ni(fLo == o.fLo, fHi == o.fHi)
; } | 62 Nb operator == (const SkNf& o) const { return Nb(fLo == o.fLo, fHi == o.fHi)
; } |
| 62 Ni operator != (const SkNf& o) const { return Ni(fLo != o.fLo, fHi != o.fHi)
; } | 63 Nb operator != (const SkNf& o) const { return Nb(fLo != o.fLo, fHi != o.fHi)
; } |
| 63 Ni operator < (const SkNf& o) const { return Ni(fLo < o.fLo, fHi < o.fHi)
; } | 64 Nb operator < (const SkNf& o) const { return Nb(fLo < o.fLo, fHi < o.fHi)
; } |
| 64 Ni operator > (const SkNf& o) const { return Ni(fLo > o.fLo, fHi > o.fHi)
; } | 65 Nb operator > (const SkNf& o) const { return Nb(fLo > o.fLo, fHi > o.fHi)
; } |
| 65 Ni operator <= (const SkNf& o) const { return Ni(fLo <= o.fLo, fHi <= o.fHi)
; } | 66 Nb operator <= (const SkNf& o) const { return Nb(fLo <= o.fLo, fHi <= o.fHi)
; } |
| 66 Ni operator >= (const SkNf& o) const { return Ni(fLo >= o.fLo, fHi >= o.fHi)
; } | 67 Nb operator >= (const SkNf& o) const { return Nb(fLo >= o.fLo, fHi >= o.fHi)
; } |
| 67 | 68 |
| 68 static SkNf Min(const SkNf& l, const SkNf& r) { | 69 static SkNf Min(const SkNf& l, const SkNf& r) { |
| 69 return SkNf(SkNf<N/2,T>::Min(l.fLo, r.fLo), SkNf<N/2,T>::Min(l.fHi, r.fH
i)); | 70 return SkNf(SkNf<N/2,T>::Min(l.fLo, r.fLo), SkNf<N/2,T>::Min(l.fHi, r.fH
i)); |
| 70 } | 71 } |
| 71 static SkNf Max(const SkNf& l, const SkNf& r) { | 72 static SkNf Max(const SkNf& l, const SkNf& r) { |
| 72 return SkNf(SkNf<N/2,T>::Max(l.fLo, r.fLo), SkNf<N/2,T>::Max(l.fHi, r.fH
i)); | 73 return SkNf(SkNf<N/2,T>::Max(l.fLo, r.fLo), SkNf<N/2,T>::Max(l.fHi, r.fH
i)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 SkNf sqrt() const { return SkNf(fLo. sqrt(), fHi. sqrt()); } | 76 SkNf sqrt() const { return SkNf(fLo. sqrt(), fHi. sqrt()); } |
| 76 SkNf rsqrt() const { return SkNf(fLo.rsqrt(), fHi.rsqrt()); } | 77 SkNf rsqrt() const { return SkNf(fLo.rsqrt(), fHi.rsqrt()); } |
| 77 | 78 |
| 78 SkNf invert() const { return SkNf(fLo. invert(), fHi. invert
()); } | 79 SkNf invert() const { return SkNf(fLo. invert(), fHi. invert
()); } |
| 79 SkNf approxInvert() const { return SkNf(fLo.approxInvert(), fHi.approxInvert
()); } | 80 SkNf approxInvert() const { return SkNf(fLo.approxInvert(), fHi.approxInvert
()); } |
| 80 | 81 |
| 81 template <int k> T kth() const { | 82 template <int k> T kth() const { |
| 82 SkASSERT(0 <= k && k < N); | 83 SkASSERT(0 <= k && k < N); |
| 83 return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>(); | 84 return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 private: | 87 private: |
| 87 REQUIRE(0 == (N & (N-1))); | 88 REQUIRE(0 == (N & (N-1))); |
| 88 SkNf(const SkNf<N/2, T>& lo, const SkNf<N/2, T>& hi) : fLo(lo), fHi(hi) {} | 89 SkNf(const SkNf<N/2, T>& lo, const SkNf<N/2, T>& hi) : fLo(lo), fHi(hi) {} |
| 89 | 90 |
| 90 SkNf<N/2, T> fLo, fHi; | 91 SkNf<N/2, T> fLo, fHi; |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 | 94 |
| 94 // Bottom out the default implementation with scalars when nothing's been specia
lized. | 95 // Bottom out the default implementations with scalars when nothing's been speci
alized. |
| 95 template <typename T> | 96 |
| 96 class SkNi<1,T> { | 97 template <int Bytes> |
| 98 class SkNb<1, Bytes> { |
| 97 public: | 99 public: |
| 98 SkNi() {} | 100 SkNb() {} |
| 99 explicit SkNi(T val) : fVal(val) {} | 101 explicit SkNb(bool val) : fVal(val) {} |
| 100 bool allTrue() const { return (bool)fVal; } | 102 bool allTrue() const { return fVal; } |
| 101 bool anyTrue() const { return (bool)fVal; } | 103 bool anyTrue() const { return fVal; } |
| 102 | |
| 103 private: | 104 private: |
| 104 T fVal; | 105 bool fVal; |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 template <typename T> | 108 template <typename T> |
| 108 class SkNf<1,T> { | 109 class SkNf<1,T> { |
| 109 static SkNi<1,int32_t> ToNi(float); | 110 typedef SkNb<1, sizeof(T)> Nb; |
| 110 static SkNi<1,int64_t> ToNi(double); | |
| 111 typedef decltype(ToNi(T())) Ni; | |
| 112 public: | 111 public: |
| 113 SkNf() {} | 112 SkNf() {} |
| 114 explicit SkNf(T val) : fVal(val) {} | 113 explicit SkNf(T val) : fVal(val) {} |
| 115 static SkNf Load(const T vals[1]) { return SkNf(vals[0]); } | 114 static SkNf Load(const T vals[1]) { return SkNf(vals[0]); } |
| 116 | 115 |
| 117 void store(T vals[1]) const { vals[0] = fVal; } | 116 void store(T vals[1]) const { vals[0] = fVal; } |
| 118 | 117 |
| 119 SkNf operator + (const SkNf& o) const { return SkNf(fVal + o.fVal); } | 118 SkNf operator + (const SkNf& o) const { return SkNf(fVal + o.fVal); } |
| 120 SkNf operator - (const SkNf& o) const { return SkNf(fVal - o.fVal); } | 119 SkNf operator - (const SkNf& o) const { return SkNf(fVal - o.fVal); } |
| 121 SkNf operator * (const SkNf& o) const { return SkNf(fVal * o.fVal); } | 120 SkNf operator * (const SkNf& o) const { return SkNf(fVal * o.fVal); } |
| 122 SkNf operator / (const SkNf& o) const { return SkNf(fVal / o.fVal); } | 121 SkNf operator / (const SkNf& o) const { return SkNf(fVal / o.fVal); } |
| 123 | 122 |
| 124 Ni operator == (const SkNf& o) const { return Ni(fVal == o.fVal); } | 123 Nb operator == (const SkNf& o) const { return Nb(fVal == o.fVal); } |
| 125 Ni operator != (const SkNf& o) const { return Ni(fVal != o.fVal); } | 124 Nb operator != (const SkNf& o) const { return Nb(fVal != o.fVal); } |
| 126 Ni operator < (const SkNf& o) const { return Ni(fVal < o.fVal); } | 125 Nb operator < (const SkNf& o) const { return Nb(fVal < o.fVal); } |
| 127 Ni operator > (const SkNf& o) const { return Ni(fVal > o.fVal); } | 126 Nb operator > (const SkNf& o) const { return Nb(fVal > o.fVal); } |
| 128 Ni operator <= (const SkNf& o) const { return Ni(fVal <= o.fVal); } | 127 Nb operator <= (const SkNf& o) const { return Nb(fVal <= o.fVal); } |
| 129 Ni operator >= (const SkNf& o) const { return Ni(fVal >= o.fVal); } | 128 Nb operator >= (const SkNf& o) const { return Nb(fVal >= o.fVal); } |
| 130 | 129 |
| 131 static SkNf Min(const SkNf& l, const SkNf& r) { return SkNf(SkTMin(l.fVal, r
.fVal)); } | 130 static SkNf Min(const SkNf& l, const SkNf& r) { return SkNf(SkTMin(l.fVal, r
.fVal)); } |
| 132 static SkNf Max(const SkNf& l, const SkNf& r) { return SkNf(SkTMax(l.fVal, r
.fVal)); } | 131 static SkNf Max(const SkNf& l, const SkNf& r) { return SkNf(SkTMax(l.fVal, r
.fVal)); } |
| 133 | 132 |
| 134 SkNf sqrt() const { return SkNf(Sqrt(fVal)); } | 133 SkNf sqrt() const { return SkNf(Sqrt(fVal)); } |
| 135 SkNf rsqrt() const { return SkNf((T)1 / Sqrt(fVal)); } | 134 SkNf rsqrt() const { return SkNf((T)1 / Sqrt(fVal)); } |
| 136 | 135 |
| 137 SkNf invert() const { return SkNf((T)1 / fVal); } | 136 SkNf invert() const { return SkNf((T)1 / fVal); } |
| 138 SkNf approxInvert() const { return this->invert(); } | 137 SkNf approxInvert() const { return this->invert(); } |
| 139 | 138 |
| 140 template <int k> T kth() const { | 139 template <int k> T kth() const { |
| 141 SkASSERT(k == 0); | 140 SkASSERT(k == 0); |
| 142 return fVal; | 141 return fVal; |
| 143 } | 142 } |
| 144 | 143 |
| 145 private: | 144 private: |
| 146 // We do double sqrts natively, or via floats for any other type. | 145 // We do double sqrts natively, or via floats for any other type. |
| 147 template <typename U> | 146 template <typename U> |
| 148 static U Sqrt(U val) { return (U) ::sqrtf((float)val); } | 147 static U Sqrt(U val) { return (U) ::sqrtf((float)val); } |
| 149 static double Sqrt(double val) { return ::sqrt ( val); } | 148 static double Sqrt(double val) { return ::sqrt ( val); } |
| 150 | 149 |
| 151 T fVal; | 150 T fVal; |
| 152 }; | 151 }; |
| 153 | 152 |
| 154 | 153 |
| 155 // Generic syntax sugar that should work equally well for all SkNi and SkNf impl
ementations. | 154 // Generic syntax sugar that should work equally well for all implementations. |
| 156 template <typename SkNx> SkNx operator - (const SkNx& l) { return SkNx(0) - l; } | 155 template <typename T> T operator - (const T& l) { return T(0) - l; } |
| 157 | 156 |
| 158 template <typename SkNx> SkNx& operator += (SkNx& l, const SkNx& r) { return (l
= l + r); } | 157 template <typename L, typename R> L& operator += (L& l, const R& r) { return (l
= l + r); } |
| 159 template <typename SkNx> SkNx& operator -= (SkNx& l, const SkNx& r) { return (l
= l - r); } | 158 template <typename L, typename R> L& operator -= (L& l, const R& r) { return (l
= l - r); } |
| 160 template <typename SkNx> SkNx& operator *= (SkNx& l, const SkNx& r) { return (l
= l * r); } | 159 template <typename L, typename R> L& operator *= (L& l, const R& r) { return (l
= l * r); } |
| 161 template <typename SkNx> SkNx& operator /= (SkNx& l, const SkNx& r) { return (l
= l / r); } | 160 template <typename L, typename R> L& operator /= (L& l, const R& r) { return (l
= l / r); } |
| 162 | 161 |
| 163 | 162 |
| 164 // Include platform specific specializations if available. | 163 // Include platform specific specializations if available. |
| 165 #ifndef SKNX_NO_SIMD | 164 #ifndef SKNX_NO_SIMD |
| 166 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | 165 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 167 #include "../opts/SkNx_sse.h" | 166 #include "../opts/SkNx_sse.h" |
| 168 #elif defined(SK_ARM_HAS_NEON) | 167 #elif defined(SK_ARM_HAS_NEON) |
| 169 #include "../opts/SkNx_neon.h" | 168 #include "../opts/SkNx_neon.h" |
| 170 #endif | 169 #endif |
| 171 #endif | 170 #endif |
| 172 | 171 |
| 173 #undef REQUIRE | 172 #undef REQUIRE |
| 174 | 173 |
| 175 typedef SkNf<2, float> Sk2f; | 174 typedef SkNf<2, float> Sk2f; |
| 176 typedef SkNf<2, double> Sk2d; | 175 typedef SkNf<2, double> Sk2d; |
| 177 typedef SkNf<2, SkScalar> Sk2s; | 176 typedef SkNf<2, SkScalar> Sk2s; |
| 178 | 177 |
| 179 typedef SkNf<4, float> Sk4f; | 178 typedef SkNf<4, float> Sk4f; |
| 180 typedef SkNf<4, double> Sk4d; | 179 typedef SkNf<4, double> Sk4d; |
| 181 typedef SkNf<4, SkScalar> Sk4s; | 180 typedef SkNf<4, SkScalar> Sk4s; |
| 182 | 181 |
| 183 typedef SkNi<4, int32_t> Sk4i; | |
| 184 | |
| 185 #endif//SkNx_DEFINED | 182 #endif//SkNx_DEFINED |
| OLD | NEW |