| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkNx_sse_DEFINED | 8 #ifndef SkNx_sse_DEFINED |
| 9 #define SkNx_sse_DEFINED | 9 #define SkNx_sse_DEFINED |
| 10 | 10 |
| 11 #include "SkCpu.h" | |
| 12 | |
| 13 // This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything mo
re recent. | 11 // This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything mo
re recent. |
| 14 // If you do, make sure this is in a static inline function... anywhere else ris
ks violating ODR. | 12 // If you do, make sure this is in a static inline function... anywhere else ris
ks violating ODR. |
| 15 | 13 |
| 16 #define SKNX_IS_FAST | 14 #define SKNX_IS_FAST |
| 17 | 15 |
| 16 // SSE 4.1 has _mm_floor_ps to floor 4 floats. We emulate it: |
| 17 // - roundtrip through integers via truncation |
| 18 // - subtract 1 if that's too big (possible for negative values). |
| 19 // This restricts the domain of our inputs to a maximum somehwere around 2^31.
Seems plenty big. |
| 20 static inline __m128 sse2_mm_floor_ps(__m128 v) { |
| 21 __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v)); |
| 22 __m128 too_big = _mm_cmpgt_ps(roundtrip, v); |
| 23 return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f))); |
| 24 } |
| 25 |
| 18 template <> | 26 template <> |
| 19 class SkNx<2, float> { | 27 class SkNx<2, float> { |
| 20 public: | 28 public: |
| 21 SkNx(const __m128& vec) : fVec(vec) {} | 29 SkNx(const __m128& vec) : fVec(vec) {} |
| 22 | 30 |
| 23 SkNx() {} | 31 SkNx() {} |
| 24 SkNx(float val) : fVec(_mm_set1_ps(val)) {} | 32 SkNx(float val) : fVec(_mm_set1_ps(val)) {} |
| 25 static SkNx Load(const void* ptr) { | 33 static SkNx Load(const void* ptr) { |
| 26 return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr)); | 34 return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr)); |
| 27 } | 35 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec);
} | 90 SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec);
} |
| 83 SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec);
} | 91 SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec);
} |
| 84 SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec);
} | 92 SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec);
} |
| 85 SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec);
} | 93 SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec);
} |
| 86 SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec);
} | 94 SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec);
} |
| 87 | 95 |
| 88 static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.
fVec); } | 96 static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.
fVec); } |
| 89 static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.
fVec); } | 97 static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.
fVec); } |
| 90 | 98 |
| 91 SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); } | 99 SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); } |
| 92 SkNx floor() const { | 100 SkNx floor() const { return sse2_mm_floor_ps(fVec); } |
| 93 if (SkCpu::Supports(SkCpu::SSE41)) { | |
| 94 __m128 r; | |
| 95 #if defined(__GNUC__) || defined(__clang__) | |
| 96 asm("roundps $0x1, %[fVec], %[r]" : [r]"=x"(r) : [fVec]"x"(fVec)); | |
| 97 #else | |
| 98 r = _mm_floor_ps(fVec); | |
| 99 #endif | |
| 100 return r; | |
| 101 } | |
| 102 // Emulate _mm_floor_ps() with SSE2: | |
| 103 // - roundtrip through integers via truncation | |
| 104 // - subtract 1 if that's too big (possible for negative values). | |
| 105 // This restricts the domain of our inputs to a maximum somehwere around
2^31. | |
| 106 // Seems plenty big. | |
| 107 __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(fVec)); | |
| 108 __m128 too_big = _mm_cmpgt_ps(roundtrip, fVec); | |
| 109 return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f))); | |
| 110 } | |
| 111 | 101 |
| 112 SkNx sqrt() const { return _mm_sqrt_ps (fVec); } | 102 SkNx sqrt() const { return _mm_sqrt_ps (fVec); } |
| 113 SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); } | 103 SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); } |
| 114 SkNx invert() const { return _mm_rcp_ps(fVec); } | 104 SkNx invert() const { return _mm_rcp_ps(fVec); } |
| 115 | 105 |
| 116 float operator[](int k) const { | 106 float operator[](int k) const { |
| 117 SkASSERT(0 <= k && k < 4); | 107 SkASSERT(0 <= k && k < 4); |
| 118 union { __m128 v; float fs[4]; } pun = {fVec}; | 108 union { __m128 v; float fs[4]; } pun = {fVec}; |
| 119 return pun.fs[k&3]; | 109 return pun.fs[k&3]; |
| 120 } | 110 } |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 354 |
| 365 template<> /*static*/ inline Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src)
{ | 355 template<> /*static*/ inline Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src)
{ |
| 366 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()); | 356 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()); |
| 367 } | 357 } |
| 368 | 358 |
| 369 template<> /*static*/ inline Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src)
{ | 359 template<> /*static*/ inline Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src)
{ |
| 370 return _mm_packus_epi16(src.fVec, src.fVec); | 360 return _mm_packus_epi16(src.fVec, src.fVec); |
| 371 } | 361 } |
| 372 | 362 |
| 373 #endif//SkNx_sse_DEFINED | 363 #endif//SkNx_sse_DEFINED |
| OLD | NEW |