Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sseb.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f78299711e28ecad85a80206330cde335da8985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2013 Intel Corporation
 * Modifications Copyright 2014-2022 Blender Foundation. */

#ifndef __UTIL_SSEB_H__
#define __UTIL_SSEB_H__

CCL_NAMESPACE_BEGIN

#ifdef __KERNEL_SSE2__

struct ssei;
struct ssef;

/*! 4-wide SSE bool type. */
struct sseb {
  typedef sseb Mask;   // mask type
  typedef ssei Int;    // int type
  typedef ssef Float;  // float type

  enum { size = 4 };  // number of SIMD elements
  union {
    __m128 m128;
    int32_t v[4];
  };  // data

  ////////////////////////////////////////////////////////////////////////////////
  /// Constructors, Assignment & Cast Operators
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline sseb()
  {
  }
  __forceinline sseb(const sseb &other)
  {
    m128 = other.m128;
  }
  __forceinline sseb &operator=(const sseb &other)
  {
    m128 = other.m128;
    return *this;
  }

  __forceinline sseb(const __m128 input) : m128(input)
  {
  }
  __forceinline operator const __m128 &(void) const
  {
    return m128;
  }
  __forceinline operator const __m128i(void) const
  {
    return _mm_castps_si128(m128);
  }
  __forceinline operator const __m128d(void) const
  {
    return _mm_castps_pd(m128);
  }

  __forceinline sseb(bool a)
      : m128(_mm_lookupmask_ps[(size_t(a) << 3) | (size_t(a) << 2) | (size_t(a) << 1) | size_t(a)])
  {
  }
  __forceinline sseb(bool a, bool b)
      : m128(_mm_lookupmask_ps[(size_t(b) << 3) | (size_t(a) << 2) | (size_t(b) << 1) | size_t(a)])
  {
  }
  __forceinline sseb(bool a, bool b, bool c, bool d)
      : m128(_mm_lookupmask_ps[(size_t(d) << 3) | (size_t(c) << 2) | (size_t(b) << 1) | size_t(a)])
  {
  }
  __forceinline sseb(int mask)
  {
    assert(mask >= 0 && mask < 16);
    m128 = _mm_lookupmask_ps[mask];
  }

  ////////////////////////////////////////////////////////////////////////////////
  /// Constants
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline sseb(FalseTy) : m128(_mm_setzero_ps())
  {
  }
  __forceinline sseb(TrueTy)
      : m128(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())))
  {
  }

  ////////////////////////////////////////////////////////////////////////////////
  /// Array Access
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline bool operator[](const size_t i) const
  {
    assert(i < 4);
    return (_mm_movemask_ps(m128) >> i) & 1;
  }
  __forceinline int32_t &operator[](const size_t i)
  {
    assert(i < 4);
    return v[i];
  }
};

////////////////////////////////////////////////////////////////////////////////
/// Unary Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb operator!(const sseb &a)
{
  return _mm_xor_ps(a, sseb(True));
}

////////////////////////////////////////////////////////////////////////////////
/// Binary Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb operator&(const sseb &a, const sseb &b)
{
  return _mm_and_ps(a, b);
}
__forceinline const sseb operator|(const sseb &a, const sseb &b)
{
  return _mm_or_ps(a, b);
}
__forceinline const sseb operator^(const sseb &a, const sseb &b)
{
  return _mm_xor_ps(a, b);
}

////////////////////////////////////////////////////////////////////////////////
/// Assignment Operators
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb operator&=(sseb &a, const sseb &b)
{
  return a = a & b;
}
__forceinline const sseb operator|=(sseb &a, const sseb &b)
{
  return a = a | b;
}
__forceinline const sseb operator^=(sseb &a, const sseb &b)
{
  return a = a ^ b;
}

////////////////////////////////////////////////////////////////////////////////
/// Comparison Operators + Select
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb operator!=(const sseb &a, const sseb &b)
{
  return _mm_xor_ps(a, b);
}
__forceinline const sseb operator==(const sseb &a, const sseb &b)
{
  return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b));
}

__forceinline const sseb select(const sseb &m, const sseb &t, const sseb &f)
{
#  if defined(__KERNEL_SSE41__)
  return _mm_blendv_ps(f, t, m);
#  else
  return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
#  endif
}

////////////////////////////////////////////////////////////////////////////////
/// Movement/Shifting/Shuffling Functions
////////////////////////////////////////////////////////////////////////////////

__forceinline const sseb unpacklo(const sseb &a, const sseb &b)
{
  return _mm_unpacklo_ps(a, b);
}
__forceinline const sseb unpackhi(const sseb &a, const sseb &b)
{
  return _mm_unpackhi_ps(a, b);
}

template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const sseb shuffle(const sseb &a)
{
#  ifdef __KERNEL_NEON__
  return shuffle_neon<int32x4_t, i0, i1, i2, i3>(a);
#  else
  return _mm_castsi128_ps(_mm_shuffle_epi32(a, _MM_SHUFFLE(i3, i2, i1, i0)));
#  endif
}

#  ifndef __KERNEL_NEON__
template<> __forceinline const sseb shuffle<0, 1, 0, 1>(const sseb &a)
{
  return _mm_movelh_ps(a, a);
}

template<> __forceinline const sseb shuffle<2, 3, 2, 3>(const sseb &a)
{
  return _mm_movehl_ps(a, a);
}
#  endif

template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const sseb shuffle(const sseb &a, const sseb &b)
{
#  ifdef __KERNEL_NEON__
  return shuffle_neon<int32x4_t, i0, i1, i2, i3>(a, b);
#  else
  return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
#  endif
}

#  ifndef __KERNEL_NEON__
template<> __forceinline const sseb shuffle<0, 1, 0, 1>(const sseb &a, const sseb &b)
{
  return _mm_movelh_ps(a, b);
}

template<> __forceinline const sseb shuffle<2, 3, 2, 3>(const sseb &a, const sseb &b)
{
  return _mm_movehl_ps(b, a);
}
#  endif

#  if defined(__KERNEL_SSE3__) && !defined(__KERNEL_NEON__)
template<> __forceinline const sseb shuffle<0, 0, 2, 2>(const sseb &a)
{
  return _mm_moveldup_ps(a);
}
template<> __forceinline const sseb shuffle<1, 1, 3, 3>(const sseb &a)
{
  return _mm_movehdup_ps(a);
}
#  endif

#  if defined(__KERNEL_SSE41__)
template<size_t dst, size_t src, size_t clr>
__forceinline const sseb insert(const sseb &a, const sseb &b)
{
#    ifdef __KERNEL_NEON__
  sseb res = a;
  if (clr)
    res[dst] = 0;
  else
    res[dst] = b[src];
  return res;
#    else
  return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr);
#    endif
}
template<size_t dst, size_t src> __forceinline const sseb insert(const sseb &a, const sseb &b)
{
  return insert<dst, src, 0>(a, b);
}
template<size_t dst> __forceinline const sseb insert(const sseb &a, const bool b)
{
  return insert<dst, 0>(a, sseb(b));
}
#  endif

////////////////////////////////////////////////////////////////////////////////
/// Reduction Operations
////////////////////////////////////////////////////////////////////////////////

#  if defined(__KERNEL_SSE41__)
__forceinline uint32_t popcnt(const sseb &a)
{
#    if defined(__KERNEL_NEON__)
  const int32x4_t mask = {1, 1, 1, 1};
  int32x4_t t = vandq_s32(vreinterpretq_s32_m128(a.m128), mask);
  return vaddvq_s32(t);
#    else
  return _mm_popcnt_u32(_mm_movemask_ps(a));
#    endif
}
#  else
__forceinline uint32_t popcnt(const sseb &a)
{
  return bool(a[0]) + bool(a[1]) + bool(a[2]) + bool(a[3]);
}
#  endif

__forceinline bool reduce_and(const sseb &a)
{
#  if defined(__KERNEL_NEON__)
  return vaddvq_s32(vreinterpretq_s32_m128(a.m128)) == -4;
#  else
  return _mm_movemask_ps(a) == 0xf;
#  endif
}
__forceinline bool reduce_or(const sseb &a)
{
#  if defined(__KERNEL_NEON__)
  return vaddvq_s32(vreinterpretq_s32_m128(a.m128)) != 0x0;
#  else
  return _mm_movemask_ps(a) != 0x0;
#  endif
}
__forceinline bool all(const sseb &b)
{
#  if defined(__KERNEL_NEON__)
  return vaddvq_s32(vreinterpretq_s32_m128(b.m128)) == -4;
#  else
  return _mm_movemask_ps(b) == 0xf;
#  endif
}
__forceinline bool any(const sseb &b)
{
#  if defined(__KERNEL_NEON__)
  return vaddvq_s32(vreinterpretq_s32_m128(b.m128)) != 0x0;
#  else
  return _mm_movemask_ps(b) != 0x0;
#  endif
}
__forceinline bool none(const sseb &b)
{
#  if defined(__KERNEL_NEON__)
  return vaddvq_s32(vreinterpretq_s32_m128(b.m128)) == 0x0;
#  else
  return _mm_movemask_ps(b) == 0x0;
#  endif
}

__forceinline uint32_t movemask(const sseb &a)
{
  return _mm_movemask_ps(a);
}

////////////////////////////////////////////////////////////////////////////////
/// Debug Functions
////////////////////////////////////////////////////////////////////////////////

ccl_device_inline void print_sseb(const char *label, const sseb &a)
{
  printf("%s: %d %d %d %d\n", label, a[0], a[1], a[2], a[3]);
}

#endif

CCL_NAMESPACE_END

#endif