Index: src/core/SkNx.h |
diff --git a/src/core/SkNx.h b/src/core/SkNx.h |
index dadb3ec132edf563b40da9df14500c5254db3cf8..1342266b934f9c007062323a677a5d6bf904a0e6 100644 |
--- a/src/core/SkNx.h |
+++ b/src/core/SkNx.h |
@@ -26,6 +26,22 @@ |
// The default implementations just fall back on a pair of size N/2. |
+// SkNb is a _very_ minimal class representing a vector of bools returned by comparison operators. |
+// We pass along the byte size of the compared types (Bytes) to help platform specializations. |
+template <int N, int Bytes> |
+class SkNb { |
+public: |
+ SkNb() {} |
+ SkNb(const SkNb<N/2, Bytes>& lo, const SkNb<N/2, Bytes>& hi) : fLo(lo), fHi(hi) {} |
+ |
+ bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
+ bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
+ |
+protected: |
+ REQUIRE(0 == (N & (N-1))); |
+ SkNb<N/2, Bytes> fLo, fHi; |
+}; |
+ |
template <int N, typename T> |
class SkNi { |
public: |
@@ -62,19 +78,14 @@ |
static SkNi Min(const SkNi& a, const SkNi& b) { |
return SkNi(SkNi<N/2, T>::Min(a.fLo, b.fLo), SkNi<N/2, T>::Min(a.fHi, b.fHi)); |
} |
- SkNi operator < (const SkNi& o) const { return SkNi(fLo < o.fLo, fHi < o.fHi); } |
+ |
+ // TODO: comparisons, 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>(); |
} |
- bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
- bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
- SkNi thenElse(const SkNi& t, const SkNi& e) const { |
- return SkNi(fLo.thenElse(t.fLo, e.fLo), fHi.thenElse(t.fHi, e.fHi)); |
- } |
- |
protected: |
REQUIRE(0 == (N & (N-1))); |
@@ -83,9 +94,11 @@ |
template <int N, typename T> |
class SkNf { |
+ typedef SkNb<N, sizeof(T)> Nb; |
+ |
static int32_t MyNi(float); |
static int64_t MyNi(double); |
- typedef decltype(MyNi(T())) I; |
+ typedef SkNi<N, decltype(MyNi(T()))> Ni; |
public: |
SkNf() {} |
explicit SkNf(T val) : fLo(val), fHi(val) {} |
@@ -102,19 +115,19 @@ |
fHi.store(vals+N/2); |
} |
- SkNi<N,I> castTrunc() const { return SkNi<N,I>(fLo.castTrunc(), fHi.castTrunc()); } |
+ Ni castTrunc() const { return Ni(fLo.castTrunc(), fHi.castTrunc()); } |
SkNf operator + (const SkNf& o) const { return SkNf(fLo + o.fLo, fHi + o.fHi); } |
SkNf operator - (const SkNf& o) const { return SkNf(fLo - o.fLo, fHi - o.fHi); } |
SkNf operator * (const SkNf& o) const { return SkNf(fLo * o.fLo, fHi * o.fHi); } |
SkNf operator / (const SkNf& o) const { return SkNf(fLo / o.fLo, fHi / o.fHi); } |
- SkNf operator == (const SkNf& o) const { return SkNf(fLo == o.fLo, fHi == o.fHi); } |
- SkNf operator != (const SkNf& o) const { return SkNf(fLo != o.fLo, fHi != o.fHi); } |
- SkNf operator < (const SkNf& o) const { return SkNf(fLo < o.fLo, fHi < o.fHi); } |
- SkNf operator > (const SkNf& o) const { return SkNf(fLo > o.fLo, fHi > o.fHi); } |
- SkNf operator <= (const SkNf& o) const { return SkNf(fLo <= o.fLo, fHi <= o.fHi); } |
- SkNf operator >= (const SkNf& o) const { return SkNf(fLo >= o.fLo, fHi >= o.fHi); } |
+ Nb operator == (const SkNf& o) const { return Nb(fLo == o.fLo, fHi == o.fHi); } |
+ Nb operator != (const SkNf& o) const { return Nb(fLo != o.fLo, fHi != o.fHi); } |
+ Nb operator < (const SkNf& o) const { return Nb(fLo < o.fLo, fHi < o.fHi); } |
+ Nb operator > (const SkNf& o) const { return Nb(fLo > o.fLo, fHi > o.fHi); } |
+ Nb operator <= (const SkNf& o) const { return Nb(fLo <= o.fLo, fHi <= o.fHi); } |
+ Nb operator >= (const SkNf& o) const { return Nb(fLo >= o.fLo, fHi >= o.fHi); } |
static SkNf Min(const SkNf& l, const SkNf& r) { |
return SkNf(SkNf<N/2,T>::Min(l.fLo, r.fLo), SkNf<N/2,T>::Min(l.fHi, r.fHi)); |
@@ -138,12 +151,6 @@ |
return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>(); |
} |
- bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
- bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
- SkNf thenElse(const SkNf& t, const SkNf& e) const { |
- return SkNf(fLo.thenElse(t.fLo, t.fHi), fHi.thenElse(t.fLo, t.fHi)); |
- } |
- |
protected: |
REQUIRE(0 == (N & (N-1))); |
SkNf(const SkNf<N/2, T>& lo, const SkNf<N/2, T>& hi) : fLo(lo), fHi(hi) {} |
@@ -153,6 +160,17 @@ |
// Bottom out the default implementations with scalars when nothing's been specialized. |
+ |
+template <int Bytes> |
+class SkNb<1, Bytes> { |
+public: |
+ SkNb() {} |
+ explicit SkNb(bool val) : fVal(val) {} |
+ bool allTrue() const { return fVal; } |
+ bool anyTrue() const { return fVal; } |
+protected: |
+ bool fVal; |
+}; |
template <typename T> |
class SkNi<1,T> { |
@@ -177,26 +195,23 @@ |
SkNi operator >> (int bits) const { return SkNi(fVal >> bits); } |
static SkNi Min(const SkNi& a, const SkNi& b) { return SkNi(SkTMin(a.fVal, b.fVal)); } |
- SkNi operator <(const SkNi& o) const { return SkNi(fVal < o.fVal); } |
template <int k> T kth() const { |
SkASSERT(0 == k); |
return fVal; |
} |
- bool allTrue() const { return fVal; } |
- bool anyTrue() const { return fVal; } |
- SkNi thenElse(const SkNi& t, const SkNi& e) const { return fVal ? t : e; } |
- |
protected: |
T fVal; |
}; |
template <typename T> |
class SkNf<1,T> { |
+ typedef SkNb<1, sizeof(T)> Nb; |
+ |
static int32_t MyNi(float); |
static int64_t MyNi(double); |
- typedef decltype(MyNi(T())) I; |
+ typedef SkNi<1, decltype(MyNi(T()))> Ni; |
public: |
SkNf() {} |
explicit SkNf(T val) : fVal(val) {} |
@@ -204,19 +219,19 @@ |
void store(T vals[1]) const { vals[0] = fVal; } |
- SkNi<1,I> castTrunc() const { return SkNi<1,I>(fVal); } |
+ Ni castTrunc() const { return Ni(fVal); } |
SkNf operator + (const SkNf& o) const { return SkNf(fVal + o.fVal); } |
SkNf operator - (const SkNf& o) const { return SkNf(fVal - o.fVal); } |
SkNf operator * (const SkNf& o) const { return SkNf(fVal * o.fVal); } |
SkNf operator / (const SkNf& o) const { return SkNf(fVal / o.fVal); } |
- SkNf operator == (const SkNf& o) const { return SkNf(fVal == o.fVal); } |
- SkNf operator != (const SkNf& o) const { return SkNf(fVal != o.fVal); } |
- SkNf operator < (const SkNf& o) const { return SkNf(fVal < o.fVal); } |
- SkNf operator > (const SkNf& o) const { return SkNf(fVal > o.fVal); } |
- SkNf operator <= (const SkNf& o) const { return SkNf(fVal <= o.fVal); } |
- SkNf operator >= (const SkNf& o) const { return SkNf(fVal >= o.fVal); } |
+ Nb operator == (const SkNf& o) const { return Nb(fVal == o.fVal); } |
+ Nb operator != (const SkNf& o) const { return Nb(fVal != o.fVal); } |
+ Nb operator < (const SkNf& o) const { return Nb(fVal < o.fVal); } |
+ Nb operator > (const SkNf& o) const { return Nb(fVal > o.fVal); } |
+ Nb operator <= (const SkNf& o) const { return Nb(fVal <= o.fVal); } |
+ Nb operator >= (const SkNf& o) const { return Nb(fVal >= o.fVal); } |
static SkNf Min(const SkNf& l, const SkNf& r) { return SkNf(SkTMin(l.fVal, r.fVal)); } |
static SkNf Max(const SkNf& l, const SkNf& r) { return SkNf(SkTMax(l.fVal, r.fVal)); } |
@@ -234,20 +249,11 @@ |
return fVal; |
} |
- bool allTrue() const { return this->pun(); } |
- bool anyTrue() const { return this->pun(); } |
- SkNf thenElse(const SkNf& t, const SkNf& e) const { return this->pun() ? t : e; } |
- |
protected: |
// We do double sqrts natively, or via floats for any other type. |
template <typename U> |
static U Sqrt(U val) { return (U) ::sqrtf((float)val); } |
static double Sqrt(double val) { return ::sqrt ( val); } |
- |
- I pun() const { |
- union { T f; I i; } pun = { fVal }; |
- return pun.i; |
- } |
T fVal; |
}; |