aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2015-09-01 06:29:45 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-09-01 06:29:45 -0700
commita508f3c62d726867ad9f722623fbf0ab5d06e440 (patch)
treef674583ce163e231c7dbc92c695ce29f661f2a80
parent580c40ae71a4914a9f4fa247accf86810e3463db (diff)
Require Sk4f::toBytes() clamps
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
-rw-r--r--src/core/SkNx.h4
-rw-r--r--src/opts/SkNx_sse.h1
-rw-r--r--tests/SkNxTest.cpp15
3 files changed, 17 insertions, 3 deletions
diff --git a/src/core/SkNx.h b/src/core/SkNx.h
index 895b972dd1..ff94e05458 100644
--- a/src/core/SkNx.h
+++ b/src/core/SkNx.h
@@ -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); }
diff --git a/src/opts/SkNx_sse.h b/src/opts/SkNx_sse.h
index 093cd4c14c..a99531b770 100644
--- a/src/opts/SkNx_sse.h
+++ b/src/opts/SkNx_sse.h
@@ -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); }
diff --git a/tests/SkNxTest.cpp b/tests/SkNxTest.cpp
index 4005d2518f..185940f195 100644
--- a/tests/SkNxTest.cpp
+++ b/tests/SkNxTest.cpp
@@ -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);
+}