1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #pragma once |
5 | |
6 | #include "../math/math.h" |
7 | |
8 | #define vboolf vboolf_impl |
9 | #define vboold vboold_impl |
10 | #define vint vint_impl |
11 | #define vuint vuint_impl |
12 | #define vllong vllong_impl |
13 | #define vfloat vfloat_impl |
14 | #define vdouble vdouble_impl |
15 | |
16 | namespace embree |
17 | { |
18 | /* 4-wide SSE integer type */ |
19 | template<> |
20 | struct vuint<4> |
21 | { |
22 | ALIGNED_STRUCT_(16); |
23 | |
24 | typedef vboolf4 Bool; |
25 | typedef vuint4 Int; |
26 | typedef vfloat4 Float; |
27 | |
28 | enum { size = 4 }; // number of SIMD elements |
29 | union { __m128i v; unsigned int i[4]; }; // data |
30 | |
31 | //////////////////////////////////////////////////////////////////////////////// |
32 | /// Constructors, Assignment & Cast Operators |
33 | //////////////////////////////////////////////////////////////////////////////// |
34 | |
35 | __forceinline vuint() {} |
36 | __forceinline vuint(const vuint4& a) { v = a.v; } |
37 | __forceinline vuint4& operator =(const vuint4& a) { v = a.v; return *this; } |
38 | |
39 | __forceinline vuint(const __m128i a) : v(a) {} |
40 | __forceinline operator const __m128i&() const { return v; } |
41 | __forceinline operator __m128i&() { return v; } |
42 | |
43 | |
44 | __forceinline vuint(unsigned int a) : v(_mm_set1_epi32(i: a)) {} |
45 | __forceinline vuint(unsigned int a, unsigned int b, unsigned int c, unsigned int d) : v(_mm_set_epi32(i3: d, i2: c, i1: b, i0: a)) {} |
46 | |
47 | #if defined(__AVX512VL__) |
48 | __forceinline explicit vuint(__m128 a) : v(_mm_cvtps_epu32(a)) {} |
49 | #endif |
50 | |
51 | #if defined(__AVX512VL__) |
52 | __forceinline explicit vuint(const vboolf4& a) : v(_mm_movm_epi32(a)) {} |
53 | #else |
54 | __forceinline explicit vuint(const vboolf4& a) : v(_mm_castps_si128(a: (__m128)a)) {} |
55 | #endif |
56 | |
57 | //////////////////////////////////////////////////////////////////////////////// |
58 | /// Constants |
59 | //////////////////////////////////////////////////////////////////////////////// |
60 | |
61 | __forceinline vuint(ZeroTy) : v(_mm_setzero_si128()) {} |
62 | __forceinline vuint(OneTy) : v(_mm_set1_epi32(i: 1)) {} |
63 | __forceinline vuint(PosInfTy) : v(_mm_set1_epi32(i: unsigned(pos_inf))) {} |
64 | __forceinline vuint(StepTy) : v(_mm_set_epi32(i3: 3, i2: 2, i1: 1, i0: 0)) {} |
65 | __forceinline vuint(TrueTy) { v = _mm_cmpeq_epi32(a: v,b: v); } |
66 | __forceinline vuint(UndefinedTy) : v(_mm_castps_si128(a: _mm_undefined_ps())) {} |
67 | |
68 | //////////////////////////////////////////////////////////////////////////////// |
69 | /// Loads and Stores |
70 | //////////////////////////////////////////////////////////////////////////////// |
71 | |
72 | static __forceinline vuint4 load (const void* a) { return _mm_load_si128(p: (__m128i*)a); } |
73 | static __forceinline vuint4 loadu(const void* a) { return _mm_loadu_si128(p: (__m128i*)a); } |
74 | |
75 | static __forceinline void store (void* ptr, const vuint4& v) { _mm_store_si128(p: (__m128i*)ptr,b: v); } |
76 | static __forceinline void storeu(void* ptr, const vuint4& v) { _mm_storeu_si128(p: (__m128i*)ptr,b: v); } |
77 | |
78 | #if defined(__AVX512VL__) |
79 | static __forceinline vuint4 load (const vboolf4& mask, const void* ptr) { return _mm_mask_load_epi32 (_mm_setzero_si128(),mask,ptr); } |
80 | static __forceinline vuint4 loadu(const vboolf4& mask, const void* ptr) { return _mm_mask_loadu_epi32(_mm_setzero_si128(),mask,ptr); } |
81 | |
82 | static __forceinline void store (const vboolf4& mask, void* ptr, const vuint4& v) { _mm_mask_store_epi32 (ptr,mask,v); } |
83 | static __forceinline void storeu(const vboolf4& mask, void* ptr, const vuint4& v) { _mm_mask_storeu_epi32(ptr,mask,v); } |
84 | #elif defined(__AVX__) |
85 | static __forceinline vuint4 load (const vbool4& mask, const void* a) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); } |
86 | static __forceinline vuint4 loadu(const vbool4& mask, const void* a) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); } |
87 | |
88 | static __forceinline void store (const vboolf4& mask, void* ptr, const vuint4& i) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); } |
89 | static __forceinline void storeu(const vboolf4& mask, void* ptr, const vuint4& i) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); } |
90 | #else |
91 | static __forceinline vuint4 load (const vbool4& mask, const void* a) { return _mm_and_si128(a: _mm_load_si128 (p: (__m128i*)a),b: mask); } |
92 | static __forceinline vuint4 loadu(const vbool4& mask, const void* a) { return _mm_and_si128(a: _mm_loadu_si128(p: (__m128i*)a),b: mask); } |
93 | |
94 | static __forceinline void store (const vboolf4& mask, void* ptr, const vuint4& i) { store (ptr,v: select(m: mask,t: i,f: load (a: ptr))); } |
95 | static __forceinline void storeu(const vboolf4& mask, void* ptr, const vuint4& i) { storeu(ptr,v: select(m: mask,t: i,f: loadu(a: ptr))); } |
96 | #endif |
97 | |
98 | #if defined(__SSE4_1__) |
99 | static __forceinline vuint4 load(const unsigned char* ptr) { |
100 | return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); |
101 | } |
102 | |
103 | static __forceinline vuint4 loadu(const unsigned char* ptr) { |
104 | return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); |
105 | } |
106 | |
107 | #endif |
108 | |
109 | static __forceinline vuint4 load(const unsigned short* ptr) { |
110 | #if defined (__SSE4_1__) |
111 | return _mm_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr)); |
112 | #else |
113 | return vuint4(ptr[0],ptr[1],ptr[2],ptr[3]); |
114 | #endif |
115 | } |
116 | |
117 | static __forceinline vuint4 load_nt(void* ptr) { |
118 | #if defined(__SSE4_1__) |
119 | return _mm_stream_load_si128((__m128i*)ptr); |
120 | #else |
121 | return _mm_load_si128(p: (__m128i*)ptr); |
122 | #endif |
123 | } |
124 | |
125 | static __forceinline void store_nt(void* ptr, const vuint4& v) { |
126 | #if defined(__SSE4_1__) |
127 | _mm_stream_ps((float*)ptr,_mm_castsi128_ps(v)); |
128 | #else |
129 | _mm_store_si128(p: (__m128i*)ptr,b: v); |
130 | #endif |
131 | } |
132 | |
133 | template<int scale = 4> |
134 | static __forceinline vuint4 gather(const unsigned int* ptr, const vint4& index) { |
135 | #if defined(__AVX2__) |
136 | return _mm_i32gather_epi32((const int*)ptr, index, scale); |
137 | #else |
138 | return vuint4( |
139 | *(unsigned int*)(((char*)ptr)+scale*index[0]), |
140 | *(unsigned int*)(((char*)ptr)+scale*index[1]), |
141 | *(unsigned int*)(((char*)ptr)+scale*index[2]), |
142 | *(unsigned int*)(((char*)ptr)+scale*index[3])); |
143 | #endif |
144 | } |
145 | |
146 | template<int scale = 4> |
147 | static __forceinline vuint4 gather(const vboolf4& mask, const unsigned int* ptr, const vint4& index) { |
148 | vuint4 r = zero; |
149 | #if defined(__AVX512VL__) |
150 | return _mm_mmask_i32gather_epi32(r, mask, index, ptr, scale); |
151 | #elif defined(__AVX2__) |
152 | return _mm_mask_i32gather_epi32(r, (const int*)ptr, index, mask, scale); |
153 | #else |
154 | if (likely(mask[0])) r[0] = *(unsigned int*)(((char*)ptr)+scale*index[0]); |
155 | if (likely(mask[1])) r[1] = *(unsigned int*)(((char*)ptr)+scale*index[1]); |
156 | if (likely(mask[2])) r[2] = *(unsigned int*)(((char*)ptr)+scale*index[2]); |
157 | if (likely(mask[3])) r[3] = *(unsigned int*)(((char*)ptr)+scale*index[3]); |
158 | return r; |
159 | #endif |
160 | } |
161 | |
162 | //////////////////////////////////////////////////////////////////////////////// |
163 | /// Array Access |
164 | //////////////////////////////////////////////////////////////////////////////// |
165 | |
166 | __forceinline const unsigned int& operator [](size_t index) const { assert(index < 4); return i[index]; } |
167 | __forceinline unsigned int& operator [](size_t index) { assert(index < 4); return i[index]; } |
168 | |
169 | friend __forceinline vuint4 select(const vboolf4& m, const vuint4& t, const vuint4& f) { |
170 | #if defined(__AVX512VL__) |
171 | return _mm_mask_blend_epi32(m, (__m128i)f, (__m128i)t); |
172 | #elif defined(__SSE4_1__) |
173 | return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m)); |
174 | #else |
175 | return _mm_or_si128(a: _mm_and_si128(a: m, b: t), b: _mm_andnot_si128(a: m, b: f)); |
176 | #endif |
177 | } |
178 | }; |
179 | |
180 | //////////////////////////////////////////////////////////////////////////////// |
181 | /// Unary Operators |
182 | //////////////////////////////////////////////////////////////////////////////// |
183 | |
184 | #if defined(__AVX512VL__) |
185 | __forceinline vboolf4 asBool(const vuint4& a) { return _mm_movepi32_mask(a); } |
186 | #else |
187 | __forceinline vboolf4 asBool(const vuint4& a) { return _mm_castsi128_ps(a: a); } |
188 | #endif |
189 | |
190 | __forceinline vuint4 operator +(const vuint4& a) { return a; } |
191 | __forceinline vuint4 operator -(const vuint4& a) { return _mm_sub_epi32(a: _mm_setzero_si128(), b: a); } |
192 | |
193 | //////////////////////////////////////////////////////////////////////////////// |
194 | /// Binary Operators |
195 | //////////////////////////////////////////////////////////////////////////////// |
196 | |
197 | __forceinline vuint4 operator +(const vuint4& a, const vuint4& b) { return _mm_add_epi32(a: a, b: b); } |
198 | __forceinline vuint4 operator +(const vuint4& a, unsigned int b) { return a + vuint4(b); } |
199 | __forceinline vuint4 operator +(unsigned int a, const vuint4& b) { return vuint4(a) + b; } |
200 | |
201 | __forceinline vuint4 operator -(const vuint4& a, const vuint4& b) { return _mm_sub_epi32(a: a, b: b); } |
202 | __forceinline vuint4 operator -(const vuint4& a, unsigned int b) { return a - vuint4(b); } |
203 | __forceinline vuint4 operator -(unsigned int a, const vuint4& b) { return vuint4(a) - b; } |
204 | |
205 | //#if defined(__SSE4_1__) |
206 | // __forceinline vuint4 operator *(const vuint4& a, const vuint4& b) { return _mm_mullo_epu32(a, b); } |
207 | //#else |
208 | // __forceinline vuint4 operator *(const vuint4& a, const vuint4& b) { return vuint4(a[0]*b[0],a[1]*b[1],a[2]*b[2],a[3]*b[3]); } |
209 | //#endif |
210 | // __forceinline vuint4 operator *(const vuint4& a, unsigned int b) { return a * vuint4(b); } |
211 | // __forceinline vuint4 operator *(unsigned int a, const vuint4& b) { return vuint4(a) * b; } |
212 | |
213 | __forceinline vuint4 operator &(const vuint4& a, const vuint4& b) { return _mm_and_si128(a: a, b: b); } |
214 | __forceinline vuint4 operator &(const vuint4& a, unsigned int b) { return a & vuint4(b); } |
215 | __forceinline vuint4 operator &(unsigned int a, const vuint4& b) { return vuint4(a) & b; } |
216 | |
217 | __forceinline vuint4 operator |(const vuint4& a, const vuint4& b) { return _mm_or_si128(a: a, b: b); } |
218 | __forceinline vuint4 operator |(const vuint4& a, unsigned int b) { return a | vuint4(b); } |
219 | __forceinline vuint4 operator |(unsigned int a, const vuint4& b) { return vuint4(a) | b; } |
220 | |
221 | __forceinline vuint4 operator ^(const vuint4& a, const vuint4& b) { return _mm_xor_si128(a: a, b: b); } |
222 | __forceinline vuint4 operator ^(const vuint4& a, unsigned int b) { return a ^ vuint4(b); } |
223 | __forceinline vuint4 operator ^(unsigned int a, const vuint4& b) { return vuint4(a) ^ b; } |
224 | |
225 | __forceinline vuint4 operator <<(const vuint4& a, unsigned int n) { return _mm_slli_epi32(a: a, count: n); } |
226 | __forceinline vuint4 operator >>(const vuint4& a, unsigned int n) { return _mm_srli_epi32(a: a, count: n); } |
227 | |
228 | __forceinline vuint4 sll (const vuint4& a, unsigned int b) { return _mm_slli_epi32(a: a, count: b); } |
229 | __forceinline vuint4 sra (const vuint4& a, unsigned int b) { return _mm_srai_epi32(a: a, count: b); } |
230 | __forceinline vuint4 srl (const vuint4& a, unsigned int b) { return _mm_srli_epi32(a: a, count: b); } |
231 | |
232 | //////////////////////////////////////////////////////////////////////////////// |
233 | /// Assignment Operators |
234 | //////////////////////////////////////////////////////////////////////////////// |
235 | |
236 | __forceinline vuint4& operator +=(vuint4& a, const vuint4& b) { return a = a + b; } |
237 | __forceinline vuint4& operator +=(vuint4& a, unsigned int b) { return a = a + b; } |
238 | |
239 | __forceinline vuint4& operator -=(vuint4& a, const vuint4& b) { return a = a - b; } |
240 | __forceinline vuint4& operator -=(vuint4& a, unsigned int b) { return a = a - b; } |
241 | |
242 | //#if defined(__SSE4_1__) |
243 | // __forceinline vuint4& operator *=(vuint4& a, const vuint4& b) { return a = a * b; } |
244 | // __forceinline vuint4& operator *=(vuint4& a, unsigned int b) { return a = a * b; } |
245 | //#endif |
246 | |
247 | __forceinline vuint4& operator &=(vuint4& a, const vuint4& b) { return a = a & b; } |
248 | __forceinline vuint4& operator &=(vuint4& a, unsigned int b) { return a = a & b; } |
249 | |
250 | __forceinline vuint4& operator |=(vuint4& a, const vuint4& b) { return a = a | b; } |
251 | __forceinline vuint4& operator |=(vuint4& a, unsigned int b) { return a = a | b; } |
252 | |
253 | __forceinline vuint4& operator <<=(vuint4& a, unsigned int b) { return a = a << b; } |
254 | __forceinline vuint4& operator >>=(vuint4& a, unsigned int b) { return a = a >> b; } |
255 | |
256 | //////////////////////////////////////////////////////////////////////////////// |
257 | /// Comparison Operators + Select |
258 | //////////////////////////////////////////////////////////////////////////////// |
259 | |
260 | #if defined(__AVX512VL__) |
261 | __forceinline vboolf4 operator ==(const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_EQ); } |
262 | __forceinline vboolf4 operator !=(const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_NE); } |
263 | //__forceinline vboolf4 operator < (const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_LT); } |
264 | //__forceinline vboolf4 operator >=(const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_GE); } |
265 | //__forceinline vboolf4 operator > (const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_GT); } |
266 | //__forceinline vboolf4 operator <=(const vuint4& a, const vuint4& b) { return _mm_cmp_epu32_mask(a,b,_MM_CMPINT_LE); } |
267 | #else |
268 | __forceinline vboolf4 operator ==(const vuint4& a, const vuint4& b) { return _mm_castsi128_ps(a: _mm_cmpeq_epi32(a: a, b: b)); } |
269 | __forceinline vboolf4 operator !=(const vuint4& a, const vuint4& b) { return !(a == b); } |
270 | //__forceinline vboolf4 operator < (const vuint4& a, const vuint4& b) { return _mm_castsi128_ps(_mm_cmplt_epu32(a, b)); } |
271 | //__forceinline vboolf4 operator >=(const vuint4& a, const vuint4& b) { return !(a < b); } |
272 | //__forceinline vboolf4 operator > (const vuint4& a, const vuint4& b) { return _mm_castsi128_ps(_mm_cmpgt_epu32(a, b)); } |
273 | //__forceinline vboolf4 operator <=(const vuint4& a, const vuint4& b) { return !(a > b); } |
274 | #endif |
275 | |
276 | __forceinline vboolf4 operator ==(const vuint4& a, unsigned int b) { return a == vuint4(b); } |
277 | __forceinline vboolf4 operator ==(unsigned int a, const vuint4& b) { return vuint4(a) == b; } |
278 | |
279 | __forceinline vboolf4 operator !=(const vuint4& a, unsigned int b) { return a != vuint4(b); } |
280 | __forceinline vboolf4 operator !=(unsigned int a, const vuint4& b) { return vuint4(a) != b; } |
281 | |
282 | //__forceinline vboolf4 operator < (const vuint4& a, unsigned int b) { return a < vuint4(b); } |
283 | //__forceinline vboolf4 operator < (unsigned int a, const vuint4& b) { return vuint4(a) < b; } |
284 | |
285 | //__forceinline vboolf4 operator >=(const vuint4& a, unsigned int b) { return a >= vuint4(b); } |
286 | //__forceinline vboolf4 operator >=(unsigned int a, const vuint4& b) { return vuint4(a) >= b; } |
287 | |
288 | //__forceinline vboolf4 operator > (const vuint4& a, unsigned int b) { return a > vuint4(b); } |
289 | //__forceinline vboolf4 operator > (unsigned int a, const vuint4& b) { return vuint4(a) > b; } |
290 | |
291 | //__forceinline vboolf4 operator <=(const vuint4& a, unsigned int b) { return a <= vuint4(b); } |
292 | //__forceinline vboolf4 operator <=(unsigned int a, const vuint4& b) { return vuint4(a) <= b; } |
293 | |
294 | __forceinline vboolf4 eq(const vuint4& a, const vuint4& b) { return a == b; } |
295 | __forceinline vboolf4 ne(const vuint4& a, const vuint4& b) { return a != b; } |
296 | //__forceinline vboolf4 lt(const vuint4& a, const vuint4& b) { return a < b; } |
297 | //__forceinline vboolf4 ge(const vuint4& a, const vuint4& b) { return a >= b; } |
298 | //__forceinline vboolf4 gt(const vuint4& a, const vuint4& b) { return a > b; } |
299 | //__forceinline vboolf4 le(const vuint4& a, const vuint4& b) { return a <= b; } |
300 | |
301 | #if defined(__AVX512VL__) |
302 | __forceinline vboolf4 eq(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_EQ); } |
303 | __forceinline vboolf4 ne(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_NE); } |
304 | //__forceinline vboolf4 lt(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_LT); } |
305 | //__forceinline vboolf4 ge(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_GE); } |
306 | //__forceinline vboolf4 gt(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_GT); } |
307 | //__forceinline vboolf4 le(const vboolf4& mask, const vuint4& a, const vuint4& b) { return _mm_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_LE); } |
308 | #else |
309 | __forceinline vboolf4 eq(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a == b); } |
310 | __forceinline vboolf4 ne(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a != b); } |
311 | //__forceinline vboolf4 lt(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a < b); } |
312 | //__forceinline vboolf4 ge(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a >= b); } |
313 | //__forceinline vboolf4 gt(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a > b); } |
314 | //__forceinline vboolf4 le(const vboolf4& mask, const vuint4& a, const vuint4& b) { return mask & (a <= b); } |
315 | #endif |
316 | |
317 | template<int mask> |
318 | __forceinline vuint4 select(const vuint4& t, const vuint4& f) { |
319 | #if defined(__SSE4_1__) |
320 | return _mm_castps_si128(_mm_blend_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), mask)); |
321 | #else |
322 | return select(m: vboolf4(mask), t, f); |
323 | #endif |
324 | } |
325 | |
326 | /*#if defined(__SSE4_1__) |
327 | __forceinline vuint4 min(const vuint4& a, const vuint4& b) { return _mm_min_epu32(a, b); } |
328 | __forceinline vuint4 max(const vuint4& a, const vuint4& b) { return _mm_max_epu32(a, b); } |
329 | |
330 | #else |
331 | __forceinline vuint4 min(const vuint4& a, const vuint4& b) { return select(a < b,a,b); } |
332 | __forceinline vuint4 max(const vuint4& a, const vuint4& b) { return select(a < b,b,a); } |
333 | #endif |
334 | |
335 | __forceinline vuint4 min(const vuint4& a, unsigned int b) { return min(a,vuint4(b)); } |
336 | __forceinline vuint4 min(unsigned int a, const vuint4& b) { return min(vuint4(a),b); } |
337 | __forceinline vuint4 max(const vuint4& a, unsigned int b) { return max(a,vuint4(b)); } |
338 | __forceinline vuint4 max(unsigned int a, const vuint4& b) { return max(vuint4(a),b); }*/ |
339 | |
340 | //////////////////////////////////////////////////////////////////////////////// |
341 | // Movement/Shifting/Shuffling Functions |
342 | //////////////////////////////////////////////////////////////////////////////// |
343 | |
344 | __forceinline vuint4 unpacklo(const vuint4& a, const vuint4& b) { return _mm_castps_si128(a: _mm_unpacklo_ps(a: _mm_castsi128_ps(a: a), b: _mm_castsi128_ps(a: b))); } |
345 | __forceinline vuint4 unpackhi(const vuint4& a, const vuint4& b) { return _mm_castps_si128(a: _mm_unpackhi_ps(a: _mm_castsi128_ps(a: a), b: _mm_castsi128_ps(a: b))); } |
346 | |
347 | template<int i0, int i1, int i2, int i3> |
348 | __forceinline vuint4 shuffle(const vuint4& v) { |
349 | return _mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0)); |
350 | } |
351 | |
352 | template<int i0, int i1, int i2, int i3> |
353 | __forceinline vuint4 shuffle(const vuint4& a, const vuint4& b) { |
354 | return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0))); |
355 | } |
356 | |
357 | #if defined(__SSE3__) |
358 | template<> __forceinline vuint4 shuffle<0, 0, 2, 2>(const vuint4& v) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(v))); } |
359 | template<> __forceinline vuint4 shuffle<1, 1, 3, 3>(const vuint4& v) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(v))); } |
360 | template<> __forceinline vuint4 shuffle<0, 1, 0, 1>(const vuint4& v) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(v))); } |
361 | #endif |
362 | |
363 | template<int i> |
364 | __forceinline vuint4 shuffle(const vuint4& v) { |
365 | return shuffle<i,i,i,i>(v); |
366 | } |
367 | |
368 | #if defined(__SSE4_1__) |
369 | template<int src> __forceinline unsigned int extract(const vuint4& b) { return _mm_extract_epi32(b, src); } |
370 | template<int dst> __forceinline vuint4 insert(const vuint4& a, const unsigned b) { return _mm_insert_epi32(a, b, dst); } |
371 | #else |
372 | template<int src> __forceinline unsigned int (const vuint4& b) { return b[src&3]; } |
373 | template<int dst> __forceinline vuint4 insert(const vuint4& a, const unsigned b) { vuint4 c = a; c[dst&3] = b; return c; } |
374 | #endif |
375 | |
376 | |
377 | template<> __forceinline unsigned int <0>(const vuint4& b) { return _mm_cvtsi128_si32(a: b); } |
378 | |
379 | __forceinline unsigned int toScalar(const vuint4& v) { return _mm_cvtsi128_si32(a: v); } |
380 | |
381 | //////////////////////////////////////////////////////////////////////////////// |
382 | /// Reductions |
383 | //////////////////////////////////////////////////////////////////////////////// |
384 | |
385 | #if 0 |
386 | #if defined(__SSE4_1__) |
387 | |
388 | __forceinline vuint4 vreduce_min(const vuint4& v) { vuint4 h = min(shuffle<1,0,3,2>(v),v); return min(shuffle<2,3,0,1>(h),h); } |
389 | __forceinline vuint4 vreduce_max(const vuint4& v) { vuint4 h = max(shuffle<1,0,3,2>(v),v); return max(shuffle<2,3,0,1>(h),h); } |
390 | __forceinline vuint4 vreduce_add(const vuint4& v) { vuint4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; } |
391 | |
392 | __forceinline unsigned int reduce_min(const vuint4& v) { return toScalar(vreduce_min(v)); } |
393 | __forceinline unsigned int reduce_max(const vuint4& v) { return toScalar(vreduce_max(v)); } |
394 | __forceinline unsigned int reduce_add(const vuint4& v) { return toScalar(vreduce_add(v)); } |
395 | |
396 | __forceinline size_t select_min(const vuint4& v) { return bsf(movemask(v == vreduce_min(v))); } |
397 | __forceinline size_t select_max(const vuint4& v) { return bsf(movemask(v == vreduce_max(v))); } |
398 | |
399 | //__forceinline size_t select_min(const vboolf4& valid, const vuint4& v) { const vuint4 a = select(valid,v,vuint4(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); } |
400 | //__forceinline size_t select_max(const vboolf4& valid, const vuint4& v) { const vuint4 a = select(valid,v,vuint4(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); } |
401 | |
402 | #else |
403 | |
404 | __forceinline unsigned int reduce_min(const vuint4& v) { return min(v[0],v[1],v[2],v[3]); } |
405 | __forceinline unsigned int reduce_max(const vuint4& v) { return max(v[0],v[1],v[2],v[3]); } |
406 | __forceinline unsigned int reduce_add(const vuint4& v) { return v[0]+v[1]+v[2]+v[3]; } |
407 | |
408 | #endif |
409 | #endif |
410 | |
411 | //////////////////////////////////////////////////////////////////////////////// |
412 | /// Output Operators |
413 | //////////////////////////////////////////////////////////////////////////////// |
414 | |
415 | __forceinline embree_ostream operator <<(embree_ostream cout, const vuint4& a) { |
416 | return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">" ; |
417 | } |
418 | } |
419 | |
420 | #undef vboolf |
421 | #undef vboold |
422 | #undef vint |
423 | #undef vuint |
424 | #undef vllong |
425 | #undef vfloat |
426 | #undef vdouble |
427 | |