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