Require Sk4f::toBytes() clamps
authormtklein <mtklein@chromium.org>
Tue, 1 Sep 2015 13:29:45 +0000 (06:29 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 1 Sep 2015 13:29:45 +0000 (06:29 -0700)
BUG=skia:4117

CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot;client.skia.android:Test-Android-GCC-Nexus9-CPU-Denver-Arm64-Release-Trybot

Review URL: https://codereview.chromium.org/1312053004

src/core/SkNx.h
src/opts/SkNx_sse.h
tests/SkNxTest.cpp

index 895b972..ff94e05 100644 (file)
@@ -108,7 +108,7 @@ public:
         fHi.store(vals+N/2);
     }
     // Please see note on FromBytes().
-    // Truncates [0.0,256.0) floats to [0,255] bytes.  Other inputs are unspecified.
+    // Clamps to [0.0,255.0] floats and truncates to [0,255] bytes.
     void toBytes(uint8_t bytes[N]) const {
         fLo.toBytes(bytes);
         fHi.toBytes(bytes+N/2);
@@ -216,7 +216,7 @@ public:
     static SkNf FromBytes(const uint8_t bytes[1]) { return SkNf((T)bytes[0]); }
 
     void store(T vals[1]) const { vals[0] = fVal; }
-    void toBytes(uint8_t bytes[1]) const { bytes[0] = (uint8_t)(fVal); }
+    void toBytes(uint8_t bytes[1]) const { bytes[0] = (uint8_t)(SkTMin(fVal, (T)255.0)); }
 
     SkNi<1,I> castTrunc() const { return SkNi<1,I>(fVal); }
 
index 093cd4c..a99531b 100644 (file)
@@ -177,7 +177,6 @@ public:
                 fix8_16 = _mm_packus_epi16(fix8_32, fix8_32),
                 fix8    = _mm_packus_epi16(fix8_16, fix8_16);
         *(int*)bytes = _mm_cvtsi128_si32(fix8);
-        // TODO: use _mm_shuffle_epi8 w/SSSE3?
     }
 
     SkNi<4, int> castTrunc() const { return _mm_cvttps_epi32(fVec); }
index 4005d25..185940f 100644 (file)
@@ -208,3 +208,18 @@ DEF_TEST(Sk4px_widening, r) {
          wideLoHiAlt = wideLo + wideHi;
     REPORTER_ASSERT(r, 0 == memcmp(&wideLoHi, &wideLoHiAlt, sizeof(wideLoHi)));
 }
+
+DEF_TEST(Sk4f_toBytes, r) {
+    uint8_t bytes[4];
+
+    // toBytes truncates, not rounds.
+    Sk4f(0.7f).toBytes(bytes);
+    REPORTER_ASSERT(r, bytes[0] == 0);
+
+    // Clamping edge cases.
+    Sk4f(-2.0f, -0.7f, 255.9f, 256.0f).toBytes(bytes);
+    REPORTER_ASSERT(r, bytes[0] == 0);
+    REPORTER_ASSERT(r, bytes[1] == 0);
+    REPORTER_ASSERT(r, bytes[2] == 255);
+    REPORTER_ASSERT(r, bytes[3] == 255);
+}