Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/common/simd/vboolf4_sse2.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
/* 4-wide SSE bool type */
17
template<>
18
struct vboolf<4>
19
{
20
ALIGNED_STRUCT_(16);
21
22
typedef vboolf4 Bool;
23
typedef vint4 Int;
24
typedef vfloat4 Float;
25
26
enum { size = 4 }; // number of SIMD elements
27
union { __m128 v; int i[4]; }; // data
28
29
////////////////////////////////////////////////////////////////////////////////
30
/// Constructors, Assignment & Cast Operators
31
////////////////////////////////////////////////////////////////////////////////
32
33
__forceinline vboolf() {}
34
__forceinline vboolf(const vboolf4& other) { v = other.v; }
35
__forceinline vboolf4& operator =(const vboolf4& other) { v = other.v; return *this; }
36
37
__forceinline vboolf(__m128 input) : v(input) {}
38
__forceinline operator const __m128&() const { return v; }
39
#if !defined(__EMSCRIPTEN__)
40
__forceinline operator const __m128i() const { return _mm_castps_si128(v); }
41
__forceinline operator const __m128d() const { return _mm_castps_pd(v); }
42
#endif
43
44
__forceinline vboolf(bool a)
45
: v(mm_lookupmask_ps[(size_t(a) << 3) | (size_t(a) << 2) | (size_t(a) << 1) | size_t(a)]) {}
46
__forceinline vboolf(bool a, bool b)
47
: v(mm_lookupmask_ps[(size_t(b) << 3) | (size_t(a) << 2) | (size_t(b) << 1) | size_t(a)]) {}
48
__forceinline vboolf(bool a, bool b, bool c, bool d)
49
: v(mm_lookupmask_ps[(size_t(d) << 3) | (size_t(c) << 2) | (size_t(b) << 1) | size_t(a)]) {}
50
__forceinline vboolf(int mask) { assert(mask >= 0 && mask < 16); v = mm_lookupmask_ps[mask]; }
51
__forceinline vboolf(unsigned int mask) { assert(mask < 16); v = mm_lookupmask_ps[mask]; }
52
53
/* return int32 mask */
54
__forceinline __m128i mask32() const {
55
return _mm_castps_si128(v);
56
}
57
58
////////////////////////////////////////////////////////////////////////////////
59
/// Constants
60
////////////////////////////////////////////////////////////////////////////////
61
62
__forceinline vboolf(FalseTy) : v(_mm_setzero_ps()) {}
63
__forceinline vboolf(TrueTy) : v(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()))) {}
64
65
////////////////////////////////////////////////////////////////////////////////
66
/// Array Access
67
////////////////////////////////////////////////////////////////////////////////
68
69
__forceinline bool operator [](size_t index) const { assert(index < 4); return (_mm_movemask_ps(v) >> index) & 1; }
70
__forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }
71
};
72
73
////////////////////////////////////////////////////////////////////////////////
74
/// Unary Operators
75
////////////////////////////////////////////////////////////////////////////////
76
77
__forceinline vboolf4 operator !(const vboolf4& a) { return _mm_xor_ps(a, vboolf4(embree::True)); }
78
79
////////////////////////////////////////////////////////////////////////////////
80
/// Binary Operators
81
////////////////////////////////////////////////////////////////////////////////
82
83
__forceinline vboolf4 operator &(const vboolf4& a, const vboolf4& b) { return _mm_and_ps(a, b); }
84
__forceinline vboolf4 operator |(const vboolf4& a, const vboolf4& b) { return _mm_or_ps (a, b); }
85
__forceinline vboolf4 operator ^(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }
86
87
__forceinline vboolf4 andn(const vboolf4& a, const vboolf4& b) { return _mm_andnot_ps(b, a); }
88
89
////////////////////////////////////////////////////////////////////////////////
90
/// Assignment Operators
91
////////////////////////////////////////////////////////////////////////////////
92
93
__forceinline vboolf4& operator &=(vboolf4& a, const vboolf4& b) { return a = a & b; }
94
__forceinline vboolf4& operator |=(vboolf4& a, const vboolf4& b) { return a = a | b; }
95
__forceinline vboolf4& operator ^=(vboolf4& a, const vboolf4& b) { return a = a ^ b; }
96
97
////////////////////////////////////////////////////////////////////////////////
98
/// Comparison Operators + Select
99
////////////////////////////////////////////////////////////////////////////////
100
101
__forceinline vboolf4 operator !=(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }
102
__forceinline vboolf4 operator ==(const vboolf4& a, const vboolf4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }
103
104
__forceinline vboolf4 select(const vboolf4& m, const vboolf4& t, const vboolf4& f) {
105
#if defined(__aarch64__) || defined(__SSE4_1__)
106
return _mm_blendv_ps(f, t, m);
107
#else
108
return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
109
#endif
110
}
111
112
////////////////////////////////////////////////////////////////////////////////
113
/// Movement/Shifting/Shuffling Functions
114
////////////////////////////////////////////////////////////////////////////////
115
116
__forceinline vboolf4 unpacklo(const vboolf4& a, const vboolf4& b) { return _mm_unpacklo_ps(a, b); }
117
__forceinline vboolf4 unpackhi(const vboolf4& a, const vboolf4& b) { return _mm_unpackhi_ps(a, b); }
118
119
#if defined(__aarch64__)
120
template<int i0, int i1, int i2, int i3>
121
__forceinline vboolf4 shuffle(const vboolf4& v) {
122
return vreinterpretq_f32_u8(vqtbl1q_u8( vreinterpretq_u8_s32((int32x4_t)v.v), _MN_SHUFFLE(i0, i1, i2, i3)));
123
}
124
125
template<int i0, int i1, int i2, int i3>
126
__forceinline vboolf4 shuffle(const vboolf4& a, const vboolf4& b) {
127
return vreinterpretq_f32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));
128
}
129
#else
130
template<int i0, int i1, int i2, int i3>
131
__forceinline vboolf4 shuffle(const vboolf4& v) {
132
return _mm_castsi128_ps(_mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0)));
133
}
134
135
template<int i0, int i1, int i2, int i3>
136
__forceinline vboolf4 shuffle(const vboolf4& a, const vboolf4& b) {
137
return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
138
}
139
#endif
140
141
template<int i0>
142
__forceinline vboolf4 shuffle(const vboolf4& v) {
143
return shuffle<i0,i0,i0,i0>(v);
144
}
145
146
#if defined(__SSE3__)
147
template<> __forceinline vboolf4 shuffle<0, 0, 2, 2>(const vboolf4& v) { return _mm_moveldup_ps(v); }
148
template<> __forceinline vboolf4 shuffle<1, 1, 3, 3>(const vboolf4& v) { return _mm_movehdup_ps(v); }
149
template<> __forceinline vboolf4 shuffle<0, 1, 0, 1>(const vboolf4& v) { return _mm_castpd_ps(_mm_movedup_pd(v)); }
150
#endif
151
152
#if defined(__SSE4_1__) && !defined(__aarch64__)
153
template<int dst, int src, int clr> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr); }
154
template<int dst, int src> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return insert<dst, src, 0>(a, b); }
155
template<int dst> __forceinline vboolf4 insert(const vboolf4& a, const bool b) { return insert<dst, 0>(a, vboolf4(b)); }
156
#endif
157
158
////////////////////////////////////////////////////////////////////////////////
159
/// Reduction Operations
160
////////////////////////////////////////////////////////////////////////////////
161
162
__forceinline bool reduce_and(const vboolf4& a) { return _mm_movemask_ps(a) == 0xf; }
163
__forceinline bool reduce_or (const vboolf4& a) { return _mm_movemask_ps(a) != 0x0; }
164
165
__forceinline bool all (const vboolf4& b) { return _mm_movemask_ps(b) == 0xf; }
166
__forceinline bool any (const vboolf4& b) { return _mm_movemask_ps(b) != 0x0; }
167
__forceinline bool none(const vboolf4& b) { return _mm_movemask_ps(b) == 0x0; }
168
169
__forceinline bool all (const vboolf4& valid, const vboolf4& b) { return all((!valid) | b); }
170
__forceinline bool any (const vboolf4& valid, const vboolf4& b) { return any(valid & b); }
171
__forceinline bool none(const vboolf4& valid, const vboolf4& b) { return none(valid & b); }
172
173
__forceinline size_t movemask(const vboolf4& a) { return _mm_movemask_ps(a); }
174
#if defined(__aarch64__)
175
__forceinline size_t popcnt(const vboolf4& a) { return vaddvq_s32(vandq_u32(vreinterpretq_u32_f32(a.v),_mm_set1_epi32(1))); }
176
#elif defined(__SSE4_2__)
177
__forceinline size_t popcnt(const vboolf4& a) { return popcnt((size_t)_mm_movemask_ps(a)); }
178
#else
179
__forceinline size_t popcnt(const vboolf4& a) { return bool(a[0])+bool(a[1])+bool(a[2])+bool(a[3]); }
180
#endif
181
182
////////////////////////////////////////////////////////////////////////////////
183
/// Get/Set Functions
184
////////////////////////////////////////////////////////////////////////////////
185
186
__forceinline bool get(const vboolf4& a, size_t index) { return a[index]; }
187
__forceinline void set(vboolf4& a, size_t index) { a[index] = -1; }
188
__forceinline void clear(vboolf4& a, size_t index) { a[index] = 0; }
189
190
////////////////////////////////////////////////////////////////////////////////
191
/// Output Operators
192
////////////////////////////////////////////////////////////////////////////////
193
194
__forceinline embree_ostream operator <<(embree_ostream cout, const vboolf4& a) {
195
return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
196
}
197
}
198
199
#undef vboolf
200
#undef vboold
201
#undef vint
202
#undef vuint
203
#undef vllong
204
#undef vfloat
205
#undef vdouble
206
207