Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/common/simd/vint8_avx2.h
9912 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#define vboolf vboolf_impl
7
#define vboold vboold_impl
8
#define vint vint_impl
9
#define vuint vuint_impl
10
#define vllong vllong_impl
11
#define vfloat vfloat_impl
12
#define vdouble vdouble_impl
13
14
namespace embree
15
{
16
/* 8-wide AVX integer type */
17
template<>
18
struct vint<8>
19
{
20
ALIGNED_STRUCT_(32);
21
22
typedef vboolf8 Bool;
23
typedef vint8 Int;
24
typedef vfloat8 Float;
25
26
enum { size = 8 }; // number of SIMD elements
27
union { // data
28
__m256i v;
29
int i[8];
30
};
31
32
////////////////////////////////////////////////////////////////////////////////
33
/// Constructors, Assignment & Cast Operators
34
////////////////////////////////////////////////////////////////////////////////
35
36
__forceinline vint() {}
37
__forceinline vint(const vint8& a) { v = a.v; }
38
__forceinline vint8& operator =(const vint8& a) { v = a.v; return *this; }
39
40
__forceinline vint(__m256i a) : v(a) {}
41
__forceinline operator const __m256i&() const { return v; }
42
__forceinline operator __m256i&() { return v; }
43
44
__forceinline explicit vint(const vint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}
45
__forceinline vint(const vint4& a, const vint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
46
__forceinline vint(const __m128i& a, const __m128i& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}
47
48
__forceinline explicit vint(const int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}
49
__forceinline vint(int a) : v(_mm256_set1_epi32(a)) {}
50
__forceinline vint(int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}
51
__forceinline vint(int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}
52
__forceinline vint(int a, int b, int c, int d, int e, int f, int g, int h) : v(_mm256_set_epi32(h, g, f, e, d, c, b, a)) {}
53
54
__forceinline explicit vint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}
55
56
#if defined(__AVX512VL__)
57
__forceinline explicit vint(const vboolf8& a) : v(_mm256_movm_epi32(a)) {}
58
#else
59
__forceinline explicit vint(const vboolf8& a) : v(_mm256_castps_si256((__m256)a)) {}
60
#endif
61
62
////////////////////////////////////////////////////////////////////////////////
63
/// Constants
64
////////////////////////////////////////////////////////////////////////////////
65
66
__forceinline vint(ZeroTy) : v(_mm256_setzero_si256()) {}
67
__forceinline vint(OneTy) : v(_mm256_set1_epi32(1)) {}
68
__forceinline vint(PosInfTy) : v(_mm256_set1_epi32(pos_inf)) {}
69
__forceinline vint(NegInfTy) : v(_mm256_set1_epi32(neg_inf)) {}
70
__forceinline vint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}
71
__forceinline vint(ReverseStepTy) : v(_mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7)) {}
72
__forceinline vint(UndefinedTy) : v(_mm256_undefined_si256()) {}
73
74
////////////////////////////////////////////////////////////////////////////////
75
/// Loads and Stores
76
////////////////////////////////////////////////////////////////////////////////
77
78
static __forceinline vint8 load(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }
79
static __forceinline vint8 loadu(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }
80
static __forceinline vint8 load(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_load_si128((__m128i*)ptr)); }
81
static __forceinline vint8 loadu(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr)); }
82
83
static __forceinline vint8 load(const void* ptr) { return _mm256_load_si256((__m256i*)ptr); }
84
static __forceinline vint8 loadu(const void* ptr) { return _mm256_loadu_si256((__m256i*)ptr); }
85
86
static __forceinline void store (void* ptr, const vint8& v) { _mm256_store_si256((__m256i*)ptr,v); }
87
static __forceinline void storeu(void* ptr, const vint8& v) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(v)); }
88
89
#if defined(__AVX512VL__)
90
91
static __forceinline vint8 compact(const vboolf8& mask, vint8 &v) {
92
return _mm256_mask_compress_epi32(v, mask, v);
93
}
94
static __forceinline vint8 compact(const vboolf8& mask, vint8 &a, const vint8& b) {
95
return _mm256_mask_compress_epi32(a, mask, b);
96
}
97
98
static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_mask_load_epi32 (_mm256_setzero_si256(),mask,ptr); }
99
static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_mask_loadu_epi32(_mm256_setzero_si256(),mask,ptr); }
100
101
static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_store_epi32 (ptr,mask,v); }
102
static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_storeu_epi32(ptr,mask,v); }
103
#else
104
static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
105
static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }
106
107
static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
108
static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }
109
#endif
110
111
static __forceinline vint8 load_nt(void* ptr) {
112
return _mm256_stream_load_si256((__m256i*)ptr);
113
}
114
115
static __forceinline void store_nt(void* ptr, const vint8& v) {
116
_mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));
117
}
118
119
static __forceinline void store(unsigned char* ptr, const vint8& i)
120
{
121
for (size_t j=0; j<8; j++)
122
ptr[j] = i[j];
123
}
124
125
static __forceinline void store(unsigned short* ptr, const vint8& v) {
126
for (size_t i=0;i<8;i++)
127
ptr[i] = (unsigned short)v[i];
128
}
129
130
template<int scale = 4>
131
static __forceinline vint8 gather(const int *const ptr, const vint8& index) {
132
return _mm256_i32gather_epi32(ptr, index, scale);
133
}
134
135
template<int scale = 4>
136
static __forceinline vint8 gather(const vboolf8& mask, const int *const ptr, const vint8& index) {
137
vint8 r = zero;
138
#if defined(__AVX512VL__)
139
return _mm256_mmask_i32gather_epi32(r, mask, index, ptr, scale);
140
#else
141
return _mm256_mask_i32gather_epi32(r, ptr, index, mask, scale);
142
#endif
143
}
144
145
template<int scale = 4>
146
static __forceinline void scatter(void* ptr, const vint8& ofs, const vint8& v)
147
{
148
#if defined(__AVX512VL__)
149
_mm256_i32scatter_epi32((int*)ptr, ofs, v, scale);
150
#else
151
*(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
152
*(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
153
*(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
154
*(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
155
*(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
156
*(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
157
*(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
158
*(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
159
#endif
160
}
161
162
template<int scale = 4>
163
static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vint8& v)
164
{
165
#if defined(__AVX512VL__)
166
_mm256_mask_i32scatter_epi32((int*)ptr, mask, ofs, v, scale);
167
#else
168
if (likely(mask[0])) *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];
169
if (likely(mask[1])) *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];
170
if (likely(mask[2])) *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];
171
if (likely(mask[3])) *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];
172
if (likely(mask[4])) *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];
173
if (likely(mask[5])) *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];
174
if (likely(mask[6])) *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];
175
if (likely(mask[7])) *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];
176
#endif
177
}
178
179
static __forceinline vint8 broadcast64(const long long &a) { return _mm256_set1_epi64x(a); }
180
181
////////////////////////////////////////////////////////////////////////////////
182
/// Array Access
183
////////////////////////////////////////////////////////////////////////////////
184
185
__forceinline const int& operator [](size_t index) const { assert(index < 8); return i[index]; }
186
__forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }
187
};
188
189
////////////////////////////////////////////////////////////////////////////////
190
/// Unary Operators
191
////////////////////////////////////////////////////////////////////////////////
192
193
#if defined(__AVX512VL__)
194
static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_movepi32_mask(a); }
195
#else
196
static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_castsi256_ps(a); }
197
#endif
198
199
__forceinline vint8 operator +(const vint8& a) { return a; }
200
__forceinline vint8 operator -(const vint8& a) { return _mm256_sub_epi32(_mm256_setzero_si256(), a); }
201
__forceinline vint8 abs (const vint8& a) { return _mm256_abs_epi32(a); }
202
203
////////////////////////////////////////////////////////////////////////////////
204
/// Binary Operators
205
////////////////////////////////////////////////////////////////////////////////
206
207
__forceinline vint8 operator +(const vint8& a, const vint8& b) { return _mm256_add_epi32(a, b); }
208
__forceinline vint8 operator +(const vint8& a, int b) { return a + vint8(b); }
209
__forceinline vint8 operator +(int a, const vint8& b) { return vint8(a) + b; }
210
211
__forceinline vint8 operator -(const vint8& a, const vint8& b) { return _mm256_sub_epi32(a, b); }
212
__forceinline vint8 operator -(const vint8& a, int b) { return a - vint8(b); }
213
__forceinline vint8 operator -(int a, const vint8& b) { return vint8(a) - b; }
214
215
__forceinline vint8 operator *(const vint8& a, const vint8& b) { return _mm256_mullo_epi32(a, b); }
216
__forceinline vint8 operator *(const vint8& a, int b) { return a * vint8(b); }
217
__forceinline vint8 operator *(int a, const vint8& b) { return vint8(a) * b; }
218
219
__forceinline vint8 operator &(const vint8& a, const vint8& b) { return _mm256_and_si256(a, b); }
220
__forceinline vint8 operator &(const vint8& a, int b) { return a & vint8(b); }
221
__forceinline vint8 operator &(int a, const vint8& b) { return vint8(a) & b; }
222
223
__forceinline vint8 operator |(const vint8& a, const vint8& b) { return _mm256_or_si256(a, b); }
224
__forceinline vint8 operator |(const vint8& a, int b) { return a | vint8(b); }
225
__forceinline vint8 operator |(int a, const vint8& b) { return vint8(a) | b; }
226
227
__forceinline vint8 operator ^(const vint8& a, const vint8& b) { return _mm256_xor_si256(a, b); }
228
__forceinline vint8 operator ^(const vint8& a, int b) { return a ^ vint8(b); }
229
__forceinline vint8 operator ^(int a, const vint8& b) { return vint8(a) ^ b; }
230
231
__forceinline vint8 operator <<(const vint8& a, int n) { return _mm256_slli_epi32(a, n); }
232
__forceinline vint8 operator >>(const vint8& a, int n) { return _mm256_srai_epi32(a, n); }
233
234
__forceinline vint8 operator <<(const vint8& a, const vint8& n) { return _mm256_sllv_epi32(a, n); }
235
__forceinline vint8 operator >>(const vint8& a, const vint8& n) { return _mm256_srav_epi32(a, n); }
236
237
__forceinline vint8 sll(const vint8& a, int b) { return _mm256_slli_epi32(a, b); }
238
__forceinline vint8 sra(const vint8& a, int b) { return _mm256_srai_epi32(a, b); }
239
__forceinline vint8 srl(const vint8& a, int b) { return _mm256_srli_epi32(a, b); }
240
241
__forceinline vint8 sll(const vint8& a, const vint8& b) { return _mm256_sllv_epi32(a, b); }
242
__forceinline vint8 sra(const vint8& a, const vint8& b) { return _mm256_srav_epi32(a, b); }
243
__forceinline vint8 srl(const vint8& a, const vint8& b) { return _mm256_srlv_epi32(a, b); }
244
245
__forceinline vint8 min(const vint8& a, const vint8& b) { return _mm256_min_epi32(a, b); }
246
__forceinline vint8 min(const vint8& a, int b) { return min(a,vint8(b)); }
247
__forceinline vint8 min(int a, const vint8& b) { return min(vint8(a),b); }
248
249
__forceinline vint8 max(const vint8& a, const vint8& b) { return _mm256_max_epi32(a, b); }
250
__forceinline vint8 max(const vint8& a, int b) { return max(a,vint8(b)); }
251
__forceinline vint8 max(int a, const vint8& b) { return max(vint8(a),b); }
252
253
__forceinline vint8 umin(const vint8& a, const vint8& b) { return _mm256_min_epu32(a, b); }
254
__forceinline vint8 umax(const vint8& a, const vint8& b) { return _mm256_max_epu32(a, b); }
255
256
////////////////////////////////////////////////////////////////////////////////
257
/// Assignment Operators
258
////////////////////////////////////////////////////////////////////////////////
259
260
__forceinline vint8& operator +=(vint8& a, const vint8& b) { return a = a + b; }
261
__forceinline vint8& operator +=(vint8& a, int b) { return a = a + b; }
262
263
__forceinline vint8& operator -=(vint8& a, const vint8& b) { return a = a - b; }
264
__forceinline vint8& operator -=(vint8& a, int b) { return a = a - b; }
265
266
__forceinline vint8& operator *=(vint8& a, const vint8& b) { return a = a * b; }
267
__forceinline vint8& operator *=(vint8& a, int b) { return a = a * b; }
268
269
__forceinline vint8& operator &=(vint8& a, const vint8& b) { return a = a & b; }
270
__forceinline vint8& operator &=(vint8& a, int b) { return a = a & b; }
271
272
__forceinline vint8& operator |=(vint8& a, const vint8& b) { return a = a | b; }
273
__forceinline vint8& operator |=(vint8& a, int b) { return a = a | b; }
274
275
__forceinline vint8& operator <<=(vint8& a, const int b) { return a = a << b; }
276
__forceinline vint8& operator >>=(vint8& a, const int b) { return a = a >> b; }
277
278
////////////////////////////////////////////////////////////////////////////////
279
/// Comparison Operators + Select
280
////////////////////////////////////////////////////////////////////////////////
281
282
#if defined(__AVX512VL__)
283
static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
284
static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
285
static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
286
static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
287
static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
288
static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
289
290
static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {
291
return _mm256_mask_blend_epi32(m, (__m256i)f, (__m256i)t);
292
}
293
#else
294
static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b)); }
295
static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return !(a == b); }
296
static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(b, a)); }
297
static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return !(a < b); }
298
static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(a, b)); }
299
static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return !(a > b); }
300
301
static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {
302
return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));
303
}
304
#endif
305
306
template<int mask>
307
__forceinline vint8 select(const vint8& t, const vint8& f) {
308
return _mm256_blend_epi32(f, t, mask);
309
}
310
311
__forceinline vboolf8 operator ==(const vint8& a, int b) { return a == vint8(b); }
312
__forceinline vboolf8 operator ==(int a, const vint8& b) { return vint8(a) == b; }
313
314
__forceinline vboolf8 operator !=(const vint8& a, int b) { return a != vint8(b); }
315
__forceinline vboolf8 operator !=(int a, const vint8& b) { return vint8(a) != b; }
316
317
__forceinline vboolf8 operator < (const vint8& a, int b) { return a < vint8(b); }
318
__forceinline vboolf8 operator < (int a, const vint8& b) { return vint8(a) < b; }
319
320
__forceinline vboolf8 operator >=(const vint8& a, int b) { return a >= vint8(b); }
321
__forceinline vboolf8 operator >=(int a, const vint8& b) { return vint8(a) >= b; }
322
323
__forceinline vboolf8 operator > (const vint8& a, int b) { return a > vint8(b); }
324
__forceinline vboolf8 operator > (int a, const vint8& b) { return vint8(a) > b; }
325
326
__forceinline vboolf8 operator <=(const vint8& a, int b) { return a <= vint8(b); }
327
__forceinline vboolf8 operator <=(int a, const vint8& b) { return vint8(a) <= b; }
328
329
__forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }
330
__forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }
331
__forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }
332
__forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }
333
__forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }
334
__forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }
335
336
#if defined(__AVX512VL__)
337
static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
338
static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
339
static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
340
static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
341
static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
342
static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
343
#else
344
static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }
345
static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }
346
static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }
347
static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }
348
static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }
349
static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }
350
#endif
351
352
////////////////////////////////////////////////////////////////////////////////
353
/// Movement/Shifting/Shuffling Functions
354
////////////////////////////////////////////////////////////////////////////////
355
356
__forceinline vint8 unpacklo(const vint8& a, const vint8& b) { return _mm256_unpacklo_epi32(a, b); }
357
__forceinline vint8 unpackhi(const vint8& a, const vint8& b) { return _mm256_unpackhi_epi32(a, b); }
358
359
template<int i>
360
__forceinline vint8 shuffle(const vint8& v) {
361
return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));
362
}
363
364
template<int i0, int i1>
365
__forceinline vint8 shuffle4(const vint8& v) {
366
return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));
367
}
368
369
template<int i0, int i1>
370
__forceinline vint8 shuffle4(const vint8& a, const vint8& b) {
371
return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));
372
}
373
374
template<int i0, int i1, int i2, int i3>
375
__forceinline vint8 shuffle(const vint8& v) {
376
return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));
377
}
378
379
template<int i0, int i1, int i2, int i3>
380
__forceinline vint8 shuffle(const vint8& a, const vint8& b) {
381
return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));
382
}
383
384
template<> __forceinline vint8 shuffle<0, 0, 2, 2>(const vint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }
385
template<> __forceinline vint8 shuffle<1, 1, 3, 3>(const vint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }
386
template<> __forceinline vint8 shuffle<0, 1, 0, 1>(const vint8& v) { return _mm256_castps_si256(_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(v))))); }
387
388
__forceinline vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }
389
390
template<int i> __forceinline vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }
391
template<int i> __forceinline vint4 extract4(const vint8& a) { return _mm256_extractf128_si256(a, i); }
392
template<> __forceinline vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }
393
394
__forceinline int toScalar(const vint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }
395
396
#if !defined(__aarch64__)
397
__forceinline vint8 permute(const vint8& v, const __m256i& index) {
398
return _mm256_permutevar8x32_epi32(v, index);
399
}
400
401
__forceinline vint8 shuffle(const vint8& v, const __m256i& index) {
402
return _mm256_castps_si256(_mm256_permutevar_ps(_mm256_castsi256_ps(v), index));
403
}
404
405
template<int i>
406
static __forceinline vint8 align_shift_right(const vint8& a, const vint8& b) {
407
#if defined(__AVX512VL__)
408
return _mm256_alignr_epi32(a, b, i);
409
#else
410
return _mm256_alignr_epi8(a, b, 4*i);
411
#endif
412
}
413
414
#endif
415
416
417
////////////////////////////////////////////////////////////////////////////////
418
/// Reductions
419
////////////////////////////////////////////////////////////////////////////////
420
421
__forceinline vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }
422
__forceinline vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }
423
__forceinline vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }
424
425
__forceinline vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }
426
__forceinline vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }
427
__forceinline vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }
428
429
__forceinline vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }
430
__forceinline vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }
431
__forceinline vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }
432
433
__forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }
434
__forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }
435
__forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }
436
437
__forceinline size_t select_min(const vint8& v) { return bsf(movemask(v == vreduce_min(v))); }
438
__forceinline size_t select_max(const vint8& v) { return bsf(movemask(v == vreduce_max(v))); }
439
440
__forceinline size_t select_min(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); }
441
__forceinline size_t select_max(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); }
442
443
////////////////////////////////////////////////////////////////////////////////
444
/// Sorting networks
445
////////////////////////////////////////////////////////////////////////////////
446
447
__forceinline vint8 usort_ascending(const vint8& v)
448
{
449
const vint8 a0 = v;
450
const vint8 b0 = shuffle<1,0,3,2>(a0);
451
const vint8 c0 = umin(a0,b0);
452
const vint8 d0 = umax(a0,b0);
453
const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
454
const vint8 b1 = shuffle<2,3,0,1>(a1);
455
const vint8 c1 = umin(a1,b1);
456
const vint8 d1 = umax(a1,b1);
457
const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
458
const vint8 b2 = shuffle<1,0,3,2>(a2);
459
const vint8 c2 = umin(a2,b2);
460
const vint8 d2 = umax(a2,b2);
461
const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
462
const vint8 b3 = shuffle4<1,0>(a3);
463
const vint8 c3 = umin(a3,b3);
464
const vint8 d3 = umax(a3,b3);
465
const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
466
const vint8 b4 = shuffle<2,3,0,1>(a4);
467
const vint8 c4 = umin(a4,b4);
468
const vint8 d4 = umax(a4,b4);
469
const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
470
const vint8 b5 = shuffle<1,0,3,2>(a5);
471
const vint8 c5 = umin(a5,b5);
472
const vint8 d5 = umax(a5,b5);
473
const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
474
return a6;
475
}
476
477
__forceinline vint8 usort_descending(const vint8& v)
478
{
479
const vint8 a0 = v;
480
const vint8 b0 = shuffle<1,0,3,2>(a0);
481
const vint8 c0 = umax(a0,b0);
482
const vint8 d0 = umin(a0,b0);
483
const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);
484
const vint8 b1 = shuffle<2,3,0,1>(a1);
485
const vint8 c1 = umax(a1,b1);
486
const vint8 d1 = umin(a1,b1);
487
const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);
488
const vint8 b2 = shuffle<1,0,3,2>(a2);
489
const vint8 c2 = umax(a2,b2);
490
const vint8 d2 = umin(a2,b2);
491
const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);
492
const vint8 b3 = shuffle4<1,0>(a3);
493
const vint8 c3 = umax(a3,b3);
494
const vint8 d3 = umin(a3,b3);
495
const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);
496
const vint8 b4 = shuffle<2,3,0,1>(a4);
497
const vint8 c4 = umax(a4,b4);
498
const vint8 d4 = umin(a4,b4);
499
const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);
500
const vint8 b5 = shuffle<1,0,3,2>(a5);
501
const vint8 c5 = umax(a5,b5);
502
const vint8 d5 = umin(a5,b5);
503
const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);
504
return a6;
505
}
506
507
////////////////////////////////////////////////////////////////////////////////
508
/// Output Operators
509
////////////////////////////////////////////////////////////////////////////////
510
511
__forceinline embree_ostream operator <<(embree_ostream cout, const vint8& a) {
512
return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
513
}
514
}
515
516
#undef vboolf
517
#undef vboold
518
#undef vint
519
#undef vuint
520
#undef vllong
521
#undef vfloat
522
#undef vdouble
523
524