OLD | NEW |
---|---|
1 #include "SkColorPriv.h" | 1 #include "SkColorPriv.h" |
2 #include <emmintrin.h> | 2 #include <emmintrin.h> |
3 | 3 |
4 // For set(), we widen our 8 bit components (fix8) to 8-bit components in 16 bit s (fix8_16), | 4 // For set(), we widen our 8 bit components (fix8) to 8-bit components in 16 bit s (fix8_16), |
5 // then widen those to 8-bit-in-32-bits (fix8_32), convert those to floats (scal ed), | 5 // then widen those to 8-bit-in-32-bits (fix8_32), and finally convert those to floats. |
6 // then finally scale those down from [0.0f, 255.0f] to [0.0f, 1.0f] into fColor . | |
7 | 6 |
8 // get() and clamped() do the opposite, working from [0.0f, 1.0f] floats to [0.0 f, 255.0f], | 7 // get() and clamped() do the opposite, working from floats to 8-bit-in-32-bit, |
9 // to 8-bit-in-32-bit, to 8-bit-in-16-bit, back down to 8-bit components. | 8 // to 8-bit-in-16-bit, back down to 8-bit components. |
10 // _mm_packus_epi16() gives us clamping for free while narrowing. | 9 // _mm_packus_epi16() gives us clamping for free while narrowing. |
11 | 10 |
12 inline void SkPMFloat::set(SkPMColor c) { | 11 inline void SkPMFloat::set(SkPMColor c) { |
13 SkPMColorAssert(c); | 12 SkPMColorAssert(c); |
14 __m128i fix8 = _mm_set_epi32(0,0,0,c), | 13 __m128i fix8 = _mm_set_epi32(0,0,0,c), |
15 fix8_16 = _mm_unpacklo_epi8 (fix8, _mm_setzero_si128()), | 14 fix8_16 = _mm_unpacklo_epi8 (fix8, _mm_setzero_si128()), |
16 fix8_32 = _mm_unpacklo_epi16(fix8_16, _mm_setzero_si128()); | 15 fix8_32 = _mm_unpacklo_epi16(fix8_16, _mm_setzero_si128()); |
17 __m128 scaled = _mm_cvtepi32_ps(fix8_32); | 16 _mm_store_ps(fColor, _mm_cvtepi32_ps(fix8_32)); |
msarett
2015/03/03 14:34:28
I think we might be able to improve performance a
mtklein
2015/03/03 15:02:17
The reason I've shied away from intrinsics like _m
| |
18 _mm_store_ps(fColor, _mm_mul_ps(scaled, _mm_set1_ps(1.0f/255.0f))); | |
19 SkASSERT(this->isValid()); | 17 SkASSERT(this->isValid()); |
msarett
2015/03/03 14:34:28
I'm starting another comment for another train of
mtklein
2015/03/03 15:02:17
Yep, totally agree. We're thinking the next logic
| |
20 } | 18 } |
21 | 19 |
22 inline SkPMColor SkPMFloat::get() const { | 20 inline SkPMColor SkPMFloat::get() const { |
23 SkASSERT(this->isValid()); | 21 SkASSERT(this->isValid()); |
24 return this->clamped(); // At the moment, we don't know anything faster. | 22 return this->clamped(); // At the moment, we don't know anything faster. |
25 } | 23 } |
26 | 24 |
27 inline SkPMColor SkPMFloat::clamped() const { | 25 inline SkPMColor SkPMFloat::clamped() const { |
28 __m128 scaled = _mm_mul_ps(_mm_load_ps(fColor), _mm_set1_ps(255.0f)); | 26 __m128i fix8_32 = _mm_cvtps_epi32(_mm_load_ps(fColor)), |
29 __m128i fix8_32 = _mm_cvtps_epi32(scaled), | |
30 fix8_16 = _mm_packus_epi16(fix8_32, fix8_32), | 27 fix8_16 = _mm_packus_epi16(fix8_32, fix8_32), |
31 fix8 = _mm_packus_epi16(fix8_16, fix8_16); | 28 fix8 = _mm_packus_epi16(fix8_16, fix8_16); |
32 SkPMColor c = _mm_cvtsi128_si32(fix8); | 29 SkPMColor c = _mm_cvtsi128_si32(fix8); |
33 SkPMColorAssert(c); | 30 SkPMColorAssert(c); |
34 return c; | 31 return c; |
35 } | 32 } |
OLD | NEW |