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

avxb.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa3cb5653097328783942af09641467b943d9b32 (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2013 Intel Corporation
 * Modifications Copyright 2014-2022 Blender Foundation. */

#ifndef __UTIL_AVXB_H__
#define __UTIL_AVXB_H__

CCL_NAMESPACE_BEGIN

struct avxf;

/*! 4-wide SSE bool type. */
struct avxb {
  typedef avxb Mask;   // mask type
  typedef avxf Float;  // float type

  enum { size = 8 };  // number of SIMD elements
  union {
    __m256 m256;
    int32_t v[8];
  };  // data

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

  __forceinline avxb()
  {
  }
  __forceinline avxb(const avxb &other)
  {
    m256 = other.m256;
  }
  __forceinline avxb &operator=(const avxb &other)
  {
    m256 = other.m256;
    return *this;
  }

  __forceinline avxb(const __m256 input) : m256(input)
  {
  }
  __forceinline avxb(const __m128 &a, const __m128 &b)
      : m256(_mm256_insertf128_ps(_mm256_castps128_ps256(a), b, 1))
  {
  }
  __forceinline operator const __m256 &(void) const
  {
    return m256;
  }
  __forceinline operator const __m256i(void) const
  {
    return _mm256_castps_si256(m256);
  }
  __forceinline operator const __m256d(void) const
  {
    return _mm256_castps_pd(m256);
  }

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

  __forceinline avxb(FalseTy) : m256(_mm256_setzero_ps())
  {
  }
  __forceinline avxb(TrueTy) : m256(_mm256_castsi256_ps(_mm256_set1_epi32(-1)))
  {
  }

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

  __forceinline bool operator[](const size_t i) const
  {
    assert(i < 8);
    return (_mm256_movemask_ps(m256) >> i) & 1;
  }
  __forceinline int32_t &operator[](const size_t i)
  {
    assert(i < 8);
    return v[i];
  }
};

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

__forceinline const avxb operator!(const avxb &a)
{
  return _mm256_xor_ps(a, avxb(True));
}

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

__forceinline const avxb operator&(const avxb &a, const avxb &b)
{
  return _mm256_and_ps(a, b);
}
__forceinline const avxb operator|(const avxb &a, const avxb &b)
{
  return _mm256_or_ps(a, b);
}
__forceinline const avxb operator^(const avxb &a, const avxb &b)
{
  return _mm256_xor_ps(a, b);
}

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

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

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

__forceinline const avxb operator!=(const avxb &a, const avxb &b)
{
  return _mm256_xor_ps(a, b);
}
__forceinline const avxb operator==(const avxb &a, const avxb &b)
{
#ifdef __KERNEL_AVX2__
  return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b));
#else
  __m128i a_lo = _mm_castps_si128(_mm256_extractf128_ps(a, 0));
  __m128i a_hi = _mm_castps_si128(_mm256_extractf128_ps(a, 1));
  __m128i b_lo = _mm_castps_si128(_mm256_extractf128_ps(b, 0));
  __m128i b_hi = _mm_castps_si128(_mm256_extractf128_ps(b, 1));
  __m128i c_lo = _mm_cmpeq_epi32(a_lo, b_lo);
  __m128i c_hi = _mm_cmpeq_epi32(a_hi, b_hi);
  __m256i result = _mm256_insertf128_si256(_mm256_castsi128_si256(c_lo), c_hi, 1);
  return _mm256_castsi256_ps(result);
#endif
}

__forceinline const avxb select(const avxb &m, const avxb &t, const avxb &f)
{
#if defined(__KERNEL_SSE41__)
  return _mm256_blendv_ps(f, t, m);
#else
  return _mm256_or_ps(_mm256_and_ps(m, t), _mm256_andnot_ps(m, f));
#endif
}

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

__forceinline const avxb unpacklo(const avxb &a, const avxb &b)
{
  return _mm256_unpacklo_ps(a, b);
}
__forceinline const avxb unpackhi(const avxb &a, const avxb &b)
{
  return _mm256_unpackhi_ps(a, b);
}

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

#if defined(__KERNEL_SSE41__)
__forceinline uint32_t popcnt(const avxb &a)
{
  return _mm_popcnt_u32(_mm256_movemask_ps(a));
}
#else
__forceinline uint32_t popcnt(const avxb &a)
{
  return bool(a[0]) + bool(a[1]) + bool(a[2]) + bool(a[3]) + bool(a[4]) + bool(a[5]) + bool(a[6]) +
         bool(a[7]);
}
#endif

__forceinline bool reduce_and(const avxb &a)
{
  return _mm256_movemask_ps(a) == 0xf;
}
__forceinline bool reduce_or(const avxb &a)
{
  return _mm256_movemask_ps(a) != 0x0;
}
__forceinline bool all(const avxb &b)
{
  return _mm256_movemask_ps(b) == 0xf;
}
__forceinline bool any(const avxb &b)
{
  return _mm256_movemask_ps(b) != 0x0;
}
__forceinline bool none(const avxb &b)
{
  return _mm256_movemask_ps(b) == 0x0;
}

__forceinline uint32_t movemask(const avxb &a)
{
  return _mm256_movemask_ps(a);
}

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

ccl_device_inline void print_avxb(const char *label, const avxb &a)
{
  printf("%s: %d %d %d %d %d %d %d %d\n", label, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}

CCL_NAMESPACE_END

#endif