Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/common/simd/vboolf8_avx.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 bool type */
17
template<>
18
struct vboolf<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
__m256 v;
29
struct { __m128 vl,vh; };
30
int i[8];
31
};
32
33
////////////////////////////////////////////////////////////////////////////////
34
/// Constructors, Assignment & Cast Operators
35
////////////////////////////////////////////////////////////////////////////////
36
37
__forceinline vboolf() {}
38
__forceinline vboolf(const vboolf8& a) { v = a.v; }
39
__forceinline vboolf8& operator =(const vboolf8& a) { v = a.v; return *this; }
40
41
__forceinline vboolf(__m256 a) : v(a) {}
42
__forceinline operator const __m256&() const { return v; }
43
__forceinline operator const __m256i() const { return _mm256_castps_si256(v); }
44
__forceinline operator const __m256d() const { return _mm256_castps_pd(v); }
45
46
__forceinline vboolf(int a)
47
{
48
assert(a >= 0 && a <= 255);
49
#if defined (__AVX2__)
50
const __m256i mask = _mm256_set_epi32(0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1);
51
const __m256i b = _mm256_set1_epi32(a);
52
const __m256i c = _mm256_and_si256(b,mask);
53
v = _mm256_castsi256_ps(_mm256_cmpeq_epi32(c,mask));
54
#else
55
vl = mm_lookupmask_ps[a & 0xF];
56
vh = mm_lookupmask_ps[a >> 4];
57
#endif
58
}
59
60
__forceinline vboolf(const vboolf4& a) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),a,1)) {}
61
__forceinline vboolf(const vboolf4& a, const vboolf4& b) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),b,1)) {}
62
__forceinline vboolf(__m128 a, __m128 b) : vl(a), vh(b) {}
63
64
__forceinline vboolf(bool a) : v(vboolf8(vboolf4(a), vboolf4(a))) {}
65
__forceinline vboolf(bool a, bool b) : v(vboolf8(vboolf4(a), vboolf4(b))) {}
66
__forceinline vboolf(bool a, bool b, bool c, bool d) : v(vboolf8(vboolf4(a,b), vboolf4(c,d))) {}
67
__forceinline vboolf(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h) : v(vboolf8(vboolf4(a,b,c,d), vboolf4(e,f,g,h))) {}
68
69
/* return int32 mask */
70
__forceinline __m256i mask32() const {
71
return _mm256_castps_si256(v);
72
}
73
74
////////////////////////////////////////////////////////////////////////////////
75
/// Constants
76
////////////////////////////////////////////////////////////////////////////////
77
78
__forceinline vboolf(FalseTy) : v(_mm256_setzero_ps()) {}
79
__forceinline vboolf(TrueTy) : v(_mm256_castsi256_ps(_mm256_set1_epi32(0xFFFFFFFF))) {}
80
81
////////////////////////////////////////////////////////////////////////////////
82
/// Array Access
83
////////////////////////////////////////////////////////////////////////////////
84
85
__forceinline bool operator [](size_t index) const { assert(index < 8); return (_mm256_movemask_ps(v) >> index) & 1; }
86
__forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }
87
};
88
89
////////////////////////////////////////////////////////////////////////////////
90
/// Unary Operators
91
////////////////////////////////////////////////////////////////////////////////
92
93
__forceinline vboolf8 operator !(const vboolf8& a) { return _mm256_xor_ps(a, vboolf8(embree::True)); }
94
95
////////////////////////////////////////////////////////////////////////////////
96
/// Binary Operators
97
////////////////////////////////////////////////////////////////////////////////
98
99
__forceinline vboolf8 operator &(const vboolf8& a, const vboolf8& b) { return _mm256_and_ps(a, b); }
100
__forceinline vboolf8 operator |(const vboolf8& a, const vboolf8& b) { return _mm256_or_ps (a, b); }
101
__forceinline vboolf8 operator ^(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }
102
103
__forceinline vboolf8 andn(const vboolf8& a, const vboolf8& b) { return _mm256_andnot_ps(b, a); }
104
105
__forceinline vboolf8& operator &=(vboolf8& a, const vboolf8& b) { return a = a & b; }
106
__forceinline vboolf8& operator |=(vboolf8& a, const vboolf8& b) { return a = a | b; }
107
__forceinline vboolf8& operator ^=(vboolf8& a, const vboolf8& b) { return a = a ^ b; }
108
109
////////////////////////////////////////////////////////////////////////////////
110
/// Comparison Operators + Select
111
////////////////////////////////////////////////////////////////////////////////
112
113
__forceinline vboolf8 operator !=(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }
114
__forceinline vboolf8 operator ==(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(_mm256_xor_ps(a,b),vboolf8(embree::True)); }
115
116
__forceinline vboolf8 select(const vboolf8& mask, const vboolf8& t, const vboolf8& f) {
117
return _mm256_blendv_ps(f, t, mask);
118
}
119
120
////////////////////////////////////////////////////////////////////////////////
121
/// Movement/Shifting/Shuffling Functions
122
////////////////////////////////////////////////////////////////////////////////
123
124
__forceinline vboolf8 unpacklo(const vboolf8& a, const vboolf8& b) { return _mm256_unpacklo_ps(a, b); }
125
__forceinline vboolf8 unpackhi(const vboolf8& a, const vboolf8& b) { return _mm256_unpackhi_ps(a, b); }
126
127
template<int i>
128
__forceinline vboolf8 shuffle(const vboolf8& v) {
129
return _mm256_permute_ps(v, _MM_SHUFFLE(i, i, i, i));
130
}
131
132
template<int i0, int i1>
133
__forceinline vboolf8 shuffle4(const vboolf8& v) {
134
return _mm256_permute2f128_ps(v, v, (i1 << 4) | (i0 << 0));
135
}
136
137
template<int i0, int i1>
138
__forceinline vboolf8 shuffle4(const vboolf8& a, const vboolf8& b) {
139
return _mm256_permute2f128_ps(a, b, (i1 << 4) | (i0 << 0));
140
}
141
142
template<int i0, int i1, int i2, int i3>
143
__forceinline vboolf8 shuffle(const vboolf8& v) {
144
return _mm256_permute_ps(v, _MM_SHUFFLE(i3, i2, i1, i0));
145
}
146
147
template<int i0, int i1, int i2, int i3>
148
__forceinline vboolf8 shuffle(const vboolf8& a, const vboolf8& b) {
149
return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
150
}
151
152
template<> __forceinline vboolf8 shuffle<0, 0, 2, 2>(const vboolf8& v) { return _mm256_moveldup_ps(v); }
153
template<> __forceinline vboolf8 shuffle<1, 1, 3, 3>(const vboolf8& v) { return _mm256_movehdup_ps(v); }
154
template<> __forceinline vboolf8 shuffle<0, 1, 0, 1>(const vboolf8& v) { return _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(v))); }
155
156
template<int i> __forceinline vboolf8 insert4(const vboolf8& a, const vboolf4& b) { return _mm256_insertf128_ps(a, b, i); }
157
template<int i> __forceinline vboolf4 extract4 (const vboolf8& a) { return _mm256_extractf128_ps(a, i); }
158
template<> __forceinline vboolf4 extract4<0>(const vboolf8& a) { return _mm256_castps256_ps128(a); }
159
160
////////////////////////////////////////////////////////////////////////////////
161
/// Reduction Operations
162
////////////////////////////////////////////////////////////////////////////////
163
164
__forceinline bool reduce_and(const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }
165
__forceinline bool reduce_or (const vboolf8& a) { return !_mm256_testz_ps(a,a); }
166
167
__forceinline bool all (const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }
168
__forceinline bool any (const vboolf8& a) { return !_mm256_testz_ps(a,a); }
169
__forceinline bool none(const vboolf8& a) { return _mm256_testz_ps(a,a) != 0; }
170
171
__forceinline bool all (const vboolf8& valid, const vboolf8& b) { return all((!valid) | b); }
172
__forceinline bool any (const vboolf8& valid, const vboolf8& b) { return any(valid & b); }
173
__forceinline bool none(const vboolf8& valid, const vboolf8& b) { return none(valid & b); }
174
175
__forceinline unsigned int movemask(const vboolf8& a) { return _mm256_movemask_ps(a); }
176
__forceinline size_t popcnt (const vboolf8& a) { return popcnt((size_t)_mm256_movemask_ps(a)); }
177
178
////////////////////////////////////////////////////////////////////////////////
179
/// Get/Set Functions
180
////////////////////////////////////////////////////////////////////////////////
181
182
__forceinline bool get(const vboolf8& a, size_t index) { return a[index]; }
183
__forceinline void set(vboolf8& a, size_t index) { a[index] = -1; }
184
__forceinline void clear(vboolf8& a, size_t index) { a[index] = 0; }
185
186
////////////////////////////////////////////////////////////////////////////////
187
/// Output Operators
188
////////////////////////////////////////////////////////////////////////////////
189
190
__forceinline embree_ostream operator <<(embree_ostream cout, const vboolf8& a) {
191
return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", "
192
<< a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
193
}
194
}
195
196
#undef vboolf
197
#undef vboold
198
#undef vint
199
#undef vuint
200
#undef vllong
201
#undef vfloat
202
#undef vdouble
203
204