1 /*  This file is part of the Vc library. {{{
2 Copyright © 2009-2015 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright
9       notice, this list of conditions and the following disclaimer in the
10       documentation and/or other materials provided with the distribution.
11     * Neither the names of contributing organizations nor the
12       names of its contributors may be used to endorse or promote products
13       derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_SSE_CASTS_H_
29 #define VC_SSE_CASTS_H_
30 
31 #include "intrinsics.h"
32 #include "types.h"
33 #include "macros.h"
34 
35 namespace Vc_VERSIONED_NAMESPACE
36 {
37 namespace SSE
38 {
39 using uint = unsigned int;
40 using ushort = unsigned short;
41 using uchar = unsigned char;
42 using schar = signed char;
43 
44 // sse_cast {{{1
sse_cast(From v)45 template <typename To, typename From> Vc_ALWAYS_INLINE Vc_CONST To sse_cast(From v)
46 {
47     return v;
48 }
49 template<> Vc_ALWAYS_INLINE Vc_CONST __m128i sse_cast<__m128i, __m128 >(__m128  v) { return _mm_castps_si128(v); }
50 template<> Vc_ALWAYS_INLINE Vc_CONST __m128i sse_cast<__m128i, __m128d>(__m128d v) { return _mm_castpd_si128(v); }
51 template<> Vc_ALWAYS_INLINE Vc_CONST __m128  sse_cast<__m128 , __m128d>(__m128d v) { return _mm_castpd_ps(v);    }
52 template<> Vc_ALWAYS_INLINE Vc_CONST __m128  sse_cast<__m128 , __m128i>(__m128i v) { return _mm_castsi128_ps(v); }
53 template<> Vc_ALWAYS_INLINE Vc_CONST __m128d sse_cast<__m128d, __m128i>(__m128i v) { return _mm_castsi128_pd(v); }
54 template<> Vc_ALWAYS_INLINE Vc_CONST __m128d sse_cast<__m128d, __m128 >(__m128  v) { return _mm_castps_pd(v);    }
55 
56 // convert {{{1
57 template <typename From, typename To> struct ConvertTag
58 {
59 };
60 template <typename From, typename To>
convert(typename VectorTraits<From>::VectorType v)61 Vc_INTRINSIC typename VectorTraits<To>::VectorType convert(
62     typename VectorTraits<From>::VectorType v)
63 {
64     return convert(v, ConvertTag<From, To>());
65 }
66 
convert(__m128 v,ConvertTag<float,int>)67 Vc_INTRINSIC __m128i convert(__m128  v, ConvertTag<float , int   >) { return _mm_cvttps_epi32(v); }
convert(__m128d v,ConvertTag<double,int>)68 Vc_INTRINSIC __m128i convert(__m128d v, ConvertTag<double, int   >) { return _mm_cvttpd_epi32(v); }
convert(__m128i v,ConvertTag<int,int>)69 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<int   , int   >) { return v; }
convert(__m128i v,ConvertTag<uint,int>)70 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<uint  , int   >) { return v; }
convert(__m128i v,ConvertTag<short,int>)71 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<short , int   >) {
72 #ifdef Vc_IMPL_SSE4_1
73     return _mm_cvtepi16_epi32(v);
74 #else
75     return _mm_srai_epi32(_mm_unpacklo_epi16(v, v), 16);
76 #endif
77 }
convert(__m128i v,ConvertTag<ushort,int>)78 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<ushort, int   >) {
79 #ifdef Vc_IMPL_SSE4_1
80     return _mm_cvtepu16_epi32(v);
81 #else
82     return _mm_srli_epi32(_mm_unpacklo_epi16(v, v), 16);
83 #endif
84 }
convert(__m128 v,ConvertTag<float,uint>)85 Vc_INTRINSIC __m128i convert(__m128  v, ConvertTag<float , uint  >) {
86     return _mm_castps_si128(
87         blendv_ps(_mm_castsi128_ps(_mm_cvttps_epi32(v)),
88                   _mm_castsi128_ps(_mm_xor_si128(
89                       _mm_cvttps_epi32(_mm_sub_ps(v, _mm_set1_ps(1u << 31))),
90                       _mm_set1_epi32(1 << 31))),
91                   _mm_cmpge_ps(v, _mm_set1_ps(1u << 31))));
92 }
convert(__m128d v,ConvertTag<double,uint>)93 Vc_INTRINSIC __m128i convert(__m128d v, ConvertTag<double, uint  >) {
94 #ifdef Vc_IMPL_SSE4_1
95     return _mm_xor_si128(_mm_cvttpd_epi32(_mm_sub_pd(_mm_floor_pd(v), _mm_set1_pd(0x80000000u))),
96                          _mm_cvtsi64_si128(0x8000000080000000ull));
97 #else
98     return blendv_epi8(_mm_cvttpd_epi32(v),
99                        _mm_xor_si128(_mm_cvttpd_epi32(_mm_sub_pd(v, _mm_set1_pd(0x80000000u))),
100                                      _mm_cvtsi64_si128(0x8000000080000000ull)),
101                        _mm_castpd_si128(_mm_cmpge_pd(v, _mm_set1_pd(0x80000000u))));
102 #endif
103 }
convert(__m128i v,ConvertTag<int,uint>)104 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<int   , uint  >) { return v; }
convert(__m128i v,ConvertTag<uint,uint>)105 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<uint  , uint  >) { return v; }
convert(__m128i v,ConvertTag<short,uint>)106 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<short , uint  >) { return convert(v, ConvertTag<short, int>()); }
convert(__m128i v,ConvertTag<ushort,uint>)107 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<ushort, uint  >) { return convert(v, ConvertTag<ushort, int>()); }
convert(__m128 v,ConvertTag<float,float>)108 Vc_INTRINSIC __m128  convert(__m128  v, ConvertTag<float , float >) { return v; }
convert(__m128d v,ConvertTag<double,float>)109 Vc_INTRINSIC __m128  convert(__m128d v, ConvertTag<double, float >) { return _mm_cvtpd_ps(v); }
convert(__m128i v,ConvertTag<int,float>)110 Vc_INTRINSIC __m128  convert(__m128i v, ConvertTag<int   , float >) { return _mm_cvtepi32_ps(v); }
convert(__m128i v,ConvertTag<uint,float>)111 Vc_INTRINSIC __m128  convert(__m128i v, ConvertTag<uint  , float >) {
112     // see AVX::convert<uint, float> for an explanation of the math behind the
113     // implementation
114     using namespace SSE;
115     return blendv_ps(_mm_cvtepi32_ps(v),
116         _mm_add_ps(_mm_cvtepi32_ps(_mm_and_si128(v, _mm_set1_epi32(0x7ffffe00))),
117                       _mm_add_ps(_mm_set1_ps(1u << 31), _mm_cvtepi32_ps(_mm_and_si128(
118                                                           v, _mm_set1_epi32(0x000001ff))))),
119         _mm_castsi128_ps(_mm_cmplt_epi32(v, _mm_setzero_si128())));
120 }
convert(__m128i v,ConvertTag<short,float>)121 Vc_INTRINSIC __m128  convert(__m128i v, ConvertTag<short , float >) { return convert(convert(v, ConvertTag<short, int>()), ConvertTag<int, float>()); }
convert(__m128i v,ConvertTag<ushort,float>)122 Vc_INTRINSIC __m128  convert(__m128i v, ConvertTag<ushort, float >) { return convert(convert(v, ConvertTag<ushort, int>()), ConvertTag<int, float>()); }
convert(__m128 v,ConvertTag<float,double>)123 Vc_INTRINSIC __m128d convert(__m128  v, ConvertTag<float , double>) { return _mm_cvtps_pd(v); }
convert(__m128d v,ConvertTag<double,double>)124 Vc_INTRINSIC __m128d convert(__m128d v, ConvertTag<double, double>) { return v; }
convert(__m128i v,ConvertTag<int,double>)125 Vc_INTRINSIC __m128d convert(__m128i v, ConvertTag<int   , double>) { return _mm_cvtepi32_pd(v); }
convert(__m128i v,ConvertTag<uint,double>)126 Vc_INTRINSIC __m128d convert(__m128i v, ConvertTag<uint  , double>) { return _mm_add_pd(_mm_cvtepi32_pd(_mm_xor_si128(v, setmin_epi32())), _mm_set1_pd(1u << 31)); }
convert(__m128i v,ConvertTag<short,double>)127 Vc_INTRINSIC __m128d convert(__m128i v, ConvertTag<short , double>) { return convert(convert(v, ConvertTag<short, int>()), ConvertTag<int, double>()); }
convert(__m128i v,ConvertTag<ushort,double>)128 Vc_INTRINSIC __m128d convert(__m128i v, ConvertTag<ushort, double>) { return convert(convert(v, ConvertTag<ushort, int>()), ConvertTag<int, double>()); }
convert(__m128 v,ConvertTag<float,short>)129 Vc_INTRINSIC __m128i convert(__m128  v, ConvertTag<float , short >) { return _mm_packs_epi32(_mm_cvttps_epi32(v), _mm_setzero_si128()); }
convert(__m128i v,ConvertTag<int,short>)130 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<int   , short >) { return _mm_packs_epi32(v, _mm_setzero_si128()); }
convert(__m128i v,ConvertTag<uint,short>)131 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<uint  , short >) { return _mm_packs_epi32(v, _mm_setzero_si128()); }
convert(__m128i v,ConvertTag<short,short>)132 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<short , short >) { return v; }
convert(__m128i v,ConvertTag<ushort,short>)133 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<ushort, short >) { return v; }
convert(__m128d v,ConvertTag<double,short>)134 Vc_INTRINSIC __m128i convert(__m128d v, ConvertTag<double, short >) { return convert(convert(v, ConvertTag<double, int>()), ConvertTag<int, short>()); }
convert(__m128i v,ConvertTag<int,ushort>)135 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<int   , ushort>) {
136     auto tmp0 = _mm_unpacklo_epi16(v, _mm_setzero_si128());  // 0 4 X X 1 5 X X
137     auto tmp1 = _mm_unpackhi_epi16(v, _mm_setzero_si128());  // 2 6 X X 3 7 X X
138     auto tmp2 = _mm_unpacklo_epi16(tmp0, tmp1);              // 0 2 4 6 X X X X
139     auto tmp3 = _mm_unpackhi_epi16(tmp0, tmp1);              // 1 3 5 7 X X X X
140     return _mm_unpacklo_epi16(tmp2, tmp3);                   // 0 1 2 3 4 5 6 7
141 }
convert(__m128i v,ConvertTag<uint,ushort>)142 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<uint  , ushort>) {
143     auto tmp0 = _mm_unpacklo_epi16(v, _mm_setzero_si128());  // 0 4 X X 1 5 X X
144     auto tmp1 = _mm_unpackhi_epi16(v, _mm_setzero_si128());  // 2 6 X X 3 7 X X
145     auto tmp2 = _mm_unpacklo_epi16(tmp0, tmp1);              // 0 2 4 6 X X X X
146     auto tmp3 = _mm_unpackhi_epi16(tmp0, tmp1);              // 1 3 5 7 X X X X
147     return _mm_unpacklo_epi16(tmp2, tmp3);                   // 0 1 2 3 4 5 6 7
148 }
convert(__m128 v,ConvertTag<float,ushort>)149 Vc_INTRINSIC __m128i convert(__m128  v, ConvertTag<float , ushort>) { return convert(_mm_cvttps_epi32(v), ConvertTag<int, ushort>()); }
convert(__m128i v,ConvertTag<short,ushort>)150 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<short , ushort>) { return v; }
convert(__m128i v,ConvertTag<ushort,ushort>)151 Vc_INTRINSIC __m128i convert(__m128i v, ConvertTag<ushort, ushort>) { return v; }
convert(__m128d v,ConvertTag<double,ushort>)152 Vc_INTRINSIC __m128i convert(__m128d v, ConvertTag<double, ushort>) { return convert(convert(v, ConvertTag<double, int>()), ConvertTag<int, ushort>()); }
153 
154 // }}}1
155 }  // namespace SSE
156 }  // namespace Vc
157 
158 #endif // VC_SSE_CASTS_H_
159 
160 // vim: foldmethod=marker
161