M(void) store (float fs[4]) const { _mm_storeu_ps(fs, fVec); }
M(void) storeAligned(float fs[4]) const { _mm_store_ps (fs, fVec); }
-template <>
-M(Sk4i) reinterpret<Sk4i>() const { return as_4i(fVec); }
+template <> M(Sk4i) reinterpret<Sk4i>() const { return as_4i(fVec); }
-template <>
-M(Sk4i) cast<Sk4i>() const { return _mm_cvtps_epi32(fVec); }
+// cvttps truncates, same as (int) when positive.
+template <> M(Sk4i) cast<Sk4i>() const { return _mm_cvttps_epi32(fVec); }
// We're going to try a little experiment here and skip allTrue(), anyTrue(), and bit-manipulators
// for Sk4f. Code that calls them probably does so accidentally.
}
inline SkPMColor SkPMFloat::clamped() const {
- __m128i fix8_32 = _mm_cvtps_epi32(fColors), // _mm_cvtps_epi32 rounds for us!
+ // We don't use _mm_cvtps_epi32, because we want precise control over how 0.5 rounds (up).
+ __m128i fix8_32 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), fColors)),
fix8_16 = _mm_packus_epi16(fix8_32, fix8_32),
fix8 = _mm_packus_epi16(fix8_16, fix8_16);
SkPMColor c = _mm_cvtsi128_si32(fix8);
inline void SkPMFloat::ClampTo4PMColors(SkPMColor colors[4], const SkPMFloat floats[4]) {
// Same as _SSSE3.h's. We use 3 _mm_packus_epi16() where the naive loop uses 8.
- __m128i c0 = _mm_cvtps_epi32(floats[0].fColors), // _mm_cvtps_epi32 rounds for us!
- c1 = _mm_cvtps_epi32(floats[1].fColors),
- c2 = _mm_cvtps_epi32(floats[2].fColors),
- c3 = _mm_cvtps_epi32(floats[3].fColors);
+ // We don't use _mm_cvtps_epi32, because we want precise control over how 0.5 rounds (up).
+ __m128i c0 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[0].fColors)),
+ c1 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[1].fColors)),
+ c2 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[2].fColors)),
+ c3 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[3].fColors));
__m128i c3210 = _mm_packus_epi16(_mm_packus_epi16(c0, c1),
_mm_packus_epi16(c2, c3));
_mm_storeu_si128((__m128i*)colors, c3210);
inline SkPMColor SkPMFloat::get() const {
SkASSERT(this->isValid());
const int _ = 255; // _ means to zero that byte.
- __m128i fix8_32 = _mm_cvtps_epi32(fColors), // _mm_cvtps_epi32 rounds for us!
+ // We don't use _mm_cvtps_epi32, because we want precise control over how 0.5 rounds (up).
+ __m128i fix8_32 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), fColors)),
fix8 = _mm_shuffle_epi8(fix8_32, _mm_set_epi8(_,_,_,_, _,_,_,_, _,_,_,_, 12,8,4,0));
SkPMColor c = _mm_cvtsi128_si32(fix8);
SkPMColorAssert(c);
}
inline SkPMColor SkPMFloat::clamped() const {
- __m128i fix8_32 = _mm_cvtps_epi32(fColors), // _mm_cvtps_epi32 rounds for us!
+ // We don't use _mm_cvtps_epi32, because we want precise control over how 0.5 rounds (up).
+ __m128i fix8_32 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), fColors)),
fix8_16 = _mm_packus_epi16(fix8_32, fix8_32),
fix8 = _mm_packus_epi16(fix8_16, fix8_16);
SkPMColor c = _mm_cvtsi128_si32(fix8);
inline void SkPMFloat::ClampTo4PMColors(SkPMColor colors[4], const SkPMFloat floats[4]) {
// Same as _SSE2.h's. We use 3 _mm_packus_epi16() where the naive loop uses 8.
- __m128i c0 = _mm_cvtps_epi32(floats[0].fColors), // _mm_cvtps_epi32 rounds for us!
- c1 = _mm_cvtps_epi32(floats[1].fColors),
- c2 = _mm_cvtps_epi32(floats[2].fColors),
- c3 = _mm_cvtps_epi32(floats[3].fColors);
+ // We don't use _mm_cvtps_epi32, because we want precise control over how 0.5 rounds (up).
+ __m128i c0 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[0].fColors)),
+ c1 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[1].fColors)),
+ c2 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[2].fColors)),
+ c3 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), floats[3].fColors));
__m128i c3210 = _mm_packus_epi16(_mm_packus_epi16(c0, c1),
_mm_packus_epi16(c2, c3));
_mm_storeu_si128((__m128i*)colors, c3210);
REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, pmf.b()));
REPORTER_ASSERT(r, c == pmf.get());
- // Test rounding. (Don't bother testing .5... we don't care which way it goes.)
- pmf = SkPMFloat(254.6f, 204.3f, 153.1f, 50.8f);
+ // Test rounding.
+ pmf = SkPMFloat(254.5f, 203.5f, 153.1f, 50.8f);
REPORTER_ASSERT(r, c == pmf.get());
// Test clamping.
ASSERT_NE(twoi, twof.reinterpret<Sk4i>());
ASSERT_EQ(twof, twoi.cast<Sk4f>());
ASSERT_NE(twof, twoi.reinterpret<Sk4f>());
+
+ ASSERT_EQ(Sk4i(0,0,0,0), Sk4f(0.5f, 0.49f, 0.51f, 0.99f).cast<Sk4i>());
+ ASSERT_EQ(Sk4i(1,1,1,1), Sk4f(1.5f, 1.49f, 1.51f, 1.99f).cast<Sk4i>());
}
DEF_TEST(Sk4x_Bits, r) {