Index: src/core/SkNx.h |
diff --git a/src/core/SkNx.h b/src/core/SkNx.h |
index 1342266b934f9c007062323a677a5d6bf904a0e6..dadb3ec132edf563b40da9df14500c5254db3cf8 100644 |
--- a/src/core/SkNx.h |
+++ b/src/core/SkNx.h |
@@ -26,22 +26,6 @@ namespace { |
// 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: |
@@ -78,14 +62,19 @@ public: |
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)); |
} |
- |
- // TODO: comparisons, max? |
+ SkNi operator < (const SkNi& o) const { return SkNi(fLo < o.fLo, fHi < o.fHi); } |
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))); |
@@ -94,11 +83,9 @@ protected: |
template <int N, typename T> |
class SkNf { |
- typedef SkNb<N, sizeof(T)> Nb; |
- |
static int32_t MyNi(float); |
static int64_t MyNi(double); |
- typedef SkNi<N, decltype(MyNi(T()))> Ni; |
+ typedef decltype(MyNi(T())) I; |
public: |
SkNf() {} |
explicit SkNf(T val) : fLo(val), fHi(val) {} |
@@ -115,19 +102,19 @@ public: |
fHi.store(vals+N/2); |
} |
- Ni castTrunc() const { return Ni(fLo.castTrunc(), fHi.castTrunc()); } |
+ SkNi<N,I> castTrunc() const { return SkNi<N,I>(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); } |
- 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); } |
+ 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); } |
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)); |
@@ -151,6 +138,12 @@ public: |
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) {} |
@@ -161,17 +154,6 @@ protected: |
// 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> { |
public: |
@@ -195,23 +177,26 @@ public: |
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 SkNi<1, decltype(MyNi(T()))> Ni; |
+ typedef decltype(MyNi(T())) I; |
public: |
SkNf() {} |
explicit SkNf(T val) : fVal(val) {} |
@@ -219,19 +204,19 @@ public: |
void store(T vals[1]) const { vals[0] = fVal; } |
- Ni castTrunc() const { return Ni(fVal); } |
+ SkNi<1,I> castTrunc() const { return SkNi<1,I>(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); } |
+ 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); } |
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)); } |
@@ -249,12 +234,21 @@ public: |
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; |
}; |