| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2015 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 namespace { // See SkPMFloat.h | |
| 9 | |
| 10 // For SkPMFloat(SkPMColor), we widen our 8 bit components (fix8) to 8-bit compo
nents in 32 bits | |
| 11 // (fix8_32), then convert those to floats. | |
| 12 | |
| 13 // round() does the opposite, working from floats to 8-bit-in-32-bits, then back
to packed 8 bit. | |
| 14 | |
| 15 // roundClamp() is the same as _SSE2: floats to 8-in-32, to 8-in-16, to packed 8
bit, with | |
| 16 // _mm_packus_epi16() both clamping and narrowing. | |
| 17 | |
| 18 inline SkPMFloat::SkPMFloat(SkPMColor c) { | |
| 19 SkPMColorAssert(c); | |
| 20 const int _ = 255; // _ means to zero that byte. | |
| 21 __m128i fix8 = _mm_set_epi32(0,0,0,c), | |
| 22 fix8_32 = _mm_shuffle_epi8(fix8, _mm_set_epi8(_,_,_,3, _,_,_,2, _,_,
_,1, _,_,_,0)); | |
| 23 fVec = _mm_cvtepi32_ps(fix8_32); | |
| 24 SkASSERT(this->isValid()); | |
| 25 } | |
| 26 | |
| 27 inline SkPMColor SkPMFloat::trunc() const { | |
| 28 const int _ = 255; // _ means to zero that byte. | |
| 29 __m128i fix8_32 = _mm_cvttps_epi32(fVec), | |
| 30 fix8 = _mm_shuffle_epi8(fix8_32, _mm_set_epi8(_,_,_,_, _,_,_,_, _
,_,_,_, 12,8,4,0)); | |
| 31 SkPMColor c = _mm_cvtsi128_si32(fix8); | |
| 32 SkPMColorAssert(c); | |
| 33 return c; | |
| 34 } | |
| 35 | |
| 36 inline SkPMColor SkPMFloat::round() const { | |
| 37 return SkPMFloat(Sk4f(0.5f) + *this).trunc(); | |
| 38 } | |
| 39 | |
| 40 inline SkPMColor SkPMFloat::roundClamp() const { | |
| 41 // We don't use _mm_cvtps_epi32, because we want precise control over how 0.
5 rounds (up). | |
| 42 __m128i fix8_32 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), fVec)), | |
| 43 fix8_16 = _mm_packus_epi16(fix8_32, fix8_32), | |
| 44 fix8 = _mm_packus_epi16(fix8_16, fix8_16); | |
| 45 SkPMColor c = _mm_cvtsi128_si32(fix8); | |
| 46 SkPMColorAssert(c); | |
| 47 return c; | |
| 48 } | |
| 49 | |
| 50 inline void SkPMFloat::From4PMColors(const SkPMColor colors[4], | |
| 51 SkPMFloat* a, SkPMFloat* b, SkPMFloat* c, S
kPMFloat* d) { | |
| 52 // Haven't beaten this yet. | |
| 53 *a = FromPMColor(colors[0]); | |
| 54 *b = FromPMColor(colors[1]); | |
| 55 *c = FromPMColor(colors[2]); | |
| 56 *d = FromPMColor(colors[3]); | |
| 57 } | |
| 58 | |
| 59 inline void SkPMFloat::RoundTo4PMColors( | |
| 60 const SkPMFloat& a, const SkPMFloat& b, const SkPMFloat&c, const SkPMFlo
at& d, | |
| 61 SkPMColor colors[4]) { | |
| 62 // Haven't beaten this yet. Still faster than RoundClampTo4PMColors? | |
| 63 colors[0] = a.round(); | |
| 64 colors[1] = b.round(); | |
| 65 colors[2] = c.round(); | |
| 66 colors[3] = d.round(); | |
| 67 } | |
| 68 | |
| 69 inline void SkPMFloat::RoundClampTo4PMColors( | |
| 70 const SkPMFloat& a, const SkPMFloat& b, const SkPMFloat&c, const SkPMFlo
at& d, | |
| 71 SkPMColor colors[4]) { | |
| 72 // Same as _SSE2.h's. We use 3 _mm_packus_epi16() where the naive loop uses
8. | |
| 73 // We don't use _mm_cvtps_epi32, because we want precise control over how 0.
5 rounds (up). | |
| 74 __m128i c0 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), a.fVec)), | |
| 75 c1 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), b.fVec)), | |
| 76 c2 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), c.fVec)), | |
| 77 c3 = _mm_cvttps_epi32(_mm_add_ps(_mm_set1_ps(0.5f), d.fVec)); | |
| 78 __m128i c3210 = _mm_packus_epi16(_mm_packus_epi16(c0, c1), | |
| 79 _mm_packus_epi16(c2, c3)); | |
| 80 _mm_storeu_si128((__m128i*)colors, c3210); | |
| 81 SkPMColorAssert(colors[0]); | |
| 82 SkPMColorAssert(colors[1]); | |
| 83 SkPMColorAssert(colors[2]); | |
| 84 SkPMColorAssert(colors[3]); | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| OLD | NEW |