aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2015-05-14 17:53:04 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-05-14 17:53:04 -0700
commit27e517ae533775889c98c65fa2f07b98357ecbc2 (patch)
treed704d214466ff9b951f5aa6c97326c382fddd9d0
parent9d214295e47405019d1494182a5182a92a22f0a6 (diff)
add Min to SkNi, specialized for u8 and u16 on SSE and NEON
0x8001 / 0x7fff don't seem to work, but we were close: 0x8000 does. I plan to use this to implement the Difference xfermode, and it seems generally handy. BUG=skia: Review URL: https://codereview.chromium.org/1133933004
-rw-r--r--src/core/SkNx.h8
-rw-r--r--src/opts/SkNx_neon.h4
-rw-r--r--src/opts/SkNx_sse.h11
-rw-r--r--tests/SkNxTest.cpp24
4 files changed, 46 insertions, 1 deletions
diff --git a/src/core/SkNx.h b/src/core/SkNx.h
index ed939c3e4f..af7194840a 100644
--- a/src/core/SkNx.h
+++ b/src/core/SkNx.h
@@ -68,7 +68,11 @@ public:
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?
+ 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?
template <int k> T kth() const {
SkASSERT(0 <= k && k < N);
@@ -183,6 +187,8 @@ public:
SkNi operator << (int bits) const { return SkNi(fVal << bits); }
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)); }
+
template <int k> T kth() const {
SkASSERT(0 == k);
return fVal;
diff --git a/src/opts/SkNx_neon.h b/src/opts/SkNx_neon.h
index e4dbec9083..08691fef42 100644
--- a/src/opts/SkNx_neon.h
+++ b/src/opts/SkNx_neon.h
@@ -329,6 +329,8 @@ public:
SkNi operator << (int bits) const { SHIFT16(vshlq_n_u16, fVec, bits); }
SkNi operator >> (int bits) const { SHIFT16(vshrq_n_u16, fVec, bits); }
+ static SkNi Min(const SkNi& a, const SkNi& b) { return vminq_u16(a.fVec, b.fVec); }
+
template <int k> uint16_t kth() const {
SkASSERT(0 <= k && k < 8);
return vgetq_lane_u16(fVec, k&7);
@@ -364,6 +366,8 @@ public:
SkNi operator << (int bits) const { SHIFT8(vshlq_n_u8, fVec, bits); }
SkNi operator >> (int bits) const { SHIFT8(vshrq_n_u8, fVec, bits); }
+ static SkNi Min(const SkNi& a, const SkNi& b) { return vminq_u8(a.fVec, b.fVec); }
+
template <int k> uint8_t kth() const {
SkASSERT(0 <= k && k < 15);
return vgetq_lane_u8(fVec, k&16);
diff --git a/src/opts/SkNx_sse.h b/src/opts/SkNx_sse.h
index 9423d04551..0e9494c238 100644
--- a/src/opts/SkNx_sse.h
+++ b/src/opts/SkNx_sse.h
@@ -272,6 +272,15 @@ public:
SkNi operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
SkNi operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
+ static SkNi Min(const SkNi& a, const SkNi& b) {
+ // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the
+ // signed version, _mm_min_epi16, then shift back.
+ const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine.
+ const __m128i top_8x = _mm_set1_epi16(top);
+ return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x),
+ _mm_sub_epi8(b.fVec, top_8x)));
+ }
+
template <int k> uint16_t kth() const {
SkASSERT(0 <= k && k < 8);
return _mm_extract_epi16(fVec, k);
@@ -306,6 +315,8 @@ public:
SkNi operator << (int bits) const { SkASSERT(false); return fVec; }
SkNi operator >> (int bits) const { SkASSERT(false); return fVec; }
+ static SkNi Min(const SkNi& a, const SkNi& b) { return _mm_min_epu8(a.fVec, b.fVec); }
+
template <int k> uint8_t kth() const {
SkASSERT(0 <= k && k < 16);
// SSE4.1 would just `return _mm_extract_epi8(fVec, k)`. We have to read 16-bits instead.
diff --git a/tests/SkNxTest.cpp b/tests/SkNxTest.cpp
index dec7329c2c..f8801500db 100644
--- a/tests/SkNxTest.cpp
+++ b/tests/SkNxTest.cpp
@@ -6,6 +6,7 @@
*/
#include "SkNx.h"
+#include "SkRandom.h"
#include "Test.h"
template <int N, typename T>
@@ -130,3 +131,26 @@ DEF_TEST(SkNi, r) {
test_Ni<4, int>(r);
test_Ni<8, int>(r);
}
+
+DEF_TEST(SkNi_min, r) {
+ // Exhaustively check the 8x8 bit space.
+ for (int a = 0; a < (1<<8); a++) {
+ for (int b = 0; b < (1<<8); b++) {
+ REPORTER_ASSERT(r, Sk16b::Min(Sk16b(a), Sk16b(b)).kth<0>() == SkTMin(a, b));
+ }}
+
+ // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds.
+#ifdef SK_DEBUG
+ SkRandom rand;
+ for (int i = 0; i < (1<<16); i++) {
+ uint16_t a = rand.nextU() >> 16,
+ b = rand.nextU() >> 16;
+ REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b));
+ }
+#else
+ for (int a = 0; a < (1<<16); a++) {
+ for (int b = 0; b < (1<<16); b++) {
+ REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b));
+ }}
+#endif
+}