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

avxf.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03a13f3049059d5d910a44c9b364eae97e887b91 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2016 Intel Corporation */

#ifndef __UTIL_AVXF_H__
#define __UTIL_AVXF_H__

CCL_NAMESPACE_BEGIN

struct avxb;

struct avxf {
  typedef avxf Float;

  enum { size = 8 }; /* Number of SIMD elements. */

  union {
    __m256 m256;
    float f[8];
    int i[8];
  };

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

  __forceinline avxf(const __m256 a) : m256(a)
  {
  }
  __forceinline avxf(const __m256i a) : m256(_mm256_castsi256_ps(a))
  {
  }

  __forceinline operator const __m256 &() const
  {
    return m256;
  }
  __forceinline operator __m256 &()
  {
    return m256;
  }

  __forceinline avxf(float a) : m256(_mm256_set1_ps(a))
  {
  }

  __forceinline avxf(float high32x4, float low32x4)
      : m256(_mm256_set_ps(
            high32x4, high32x4, high32x4, high32x4, low32x4, low32x4, low32x4, low32x4))
  {
  }

  __forceinline avxf(float a3, float a2, float a1, float a0)
      : m256(_mm256_set_ps(a3, a2, a1, a0, a3, a2, a1, a0))
  {
  }

  __forceinline avxf(
      float a7, float a6, float a5, float a4, float a3, float a2, float a1, float a0)
      : m256(_mm256_set_ps(a7, a6, a5, a4, a3, a2, a1, a0))
  {
  }

  __forceinline avxf(float3 a) : m256(_mm256_set_ps(a.w, a.z, a.y, a.x, a.w, a.z, a.y, a.x))
  {
  }

  __forceinline avxf(int a3, int a2, int a1, int a0)
  {
    const __m256i foo = _mm256_set_epi32(a3, a2, a1, a0, a3, a2, a1, a0);
    m256 = _mm256_castsi256_ps(foo);
  }

  __forceinline avxf(int a7, int a6, int a5, int a4, int a3, int a2, int a1, int a0)
  {
    const __m256i foo = _mm256_set_epi32(a7, a6, a5, a4, a3, a2, a1, a0);
    m256 = _mm256_castsi256_ps(foo);
  }

  __forceinline avxf(__m128 a, __m128 b)
  {
    const __m256 foo = _mm256_castps128_ps256(a);
    m256 = _mm256_insertf128_ps(foo, b, 1);
  }

  __forceinline const float &operator[](const size_t i) const
  {
    assert(i < 8);
    return f[i];
  }
  __forceinline float &operator[](const size_t i)
  {
    assert(i < 8);
    return f[i];
  }
};

__forceinline avxf cross(const avxf &a, const avxf &b)
{
  avxf r(0.0,
         a[4] * b[5] - a[5] * b[4],
         a[6] * b[4] - a[4] * b[6],
         a[5] * b[6] - a[6] * b[5],
         0.0,
         a[0] * b[1] - a[1] * b[0],
         a[2] * b[0] - a[0] * b[2],
         a[1] * b[2] - a[2] * b[1]);
  return r;
}

__forceinline void dot3(const avxf &a, const avxf &b, float &den, float &den2)
{
  const avxf t = _mm256_mul_ps(a.m256, b.m256);
  den = ((float *)&t)[0] + ((float *)&t)[1] + ((float *)&t)[2];
  den2 = ((float *)&t)[4] + ((float *)&t)[5] + ((float *)&t)[6];
}

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

__forceinline const avxf cast(const __m256i &a)
{
  return _mm256_castsi256_ps(a);
}

__forceinline const avxf mm256_sqrt(const avxf &a)
{
  return _mm256_sqrt_ps(a.m256);
}

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

__forceinline const avxf operator+(const avxf &a, const avxf &b)
{
  return _mm256_add_ps(a.m256, b.m256);
}
__forceinline const avxf operator+(const avxf &a, const float &b)
{
  return a + avxf(b);
}
__forceinline const avxf operator+(const float &a, const avxf &b)
{
  return avxf(a) + b;
}

__forceinline const avxf operator-(const avxf &a, const avxf &b)
{
  return _mm256_sub_ps(a.m256, b.m256);
}
__forceinline const avxf operator-(const avxf &a, const float &b)
{
  return a - avxf(b);
}
__forceinline const avxf operator-(const float &a, const avxf &b)
{
  return avxf(a) - b;
}

__forceinline const avxf operator*(const avxf &a, const avxf &b)
{
  return _mm256_mul_ps(a.m256, b.m256);
}
__forceinline const avxf operator*(const avxf &a, const float &b)
{
  return a * avxf(b);
}
__forceinline const avxf operator*(const float &a, const avxf &b)
{
  return avxf(a) * b;
}

__forceinline const avxf operator/(const avxf &a, const avxf &b)
{
  return _mm256_div_ps(a.m256, b.m256);
}
__forceinline const avxf operator/(const avxf &a, const float &b)
{
  return a / avxf(b);
}
__forceinline const avxf operator/(const float &a, const avxf &b)
{
  return avxf(a) / b;
}

__forceinline const avxf operator|(const avxf &a, const avxf &b)
{
  return _mm256_or_ps(a.m256, b.m256);
}

__forceinline const avxf operator^(const avxf &a, const avxf &b)
{
  return _mm256_xor_ps(a.m256, b.m256);
}

__forceinline const avxf operator&(const avxf &a, const avxf &b)
{
  return _mm256_and_ps(a.m256, b.m256);
}

__forceinline const avxf max(const avxf &a, const avxf &b)
{
  return _mm256_max_ps(a.m256, b.m256);
}
__forceinline const avxf min(const avxf &a, const avxf &b)
{
  return _mm256_min_ps(a.m256, b.m256);
}

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

__forceinline const avxf shuffle(const avxf &a, const __m256i &shuf)
{
  return _mm256_permutevar_ps(a, shuf);
}

template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
__forceinline const avxf shuffle(const avxf &a)
{
  return _mm256_permutevar_ps(a, _mm256_set_epi32(i7, i6, i5, i4, i3, i2, i1, i0));
}

template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const avxf shuffle(const avxf &a, const avxf &b)
{
  return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
}
template<size_t i0, size_t i1, size_t i2, size_t i3>
__forceinline const avxf shuffle(const avxf &a)
{
  return shuffle<i0, i1, i2, i3>(a, a);
}
template<size_t i0> __forceinline const avxf shuffle(const avxf &a, const avxf &b)
{
  return shuffle<i0, i0, i0, i0>(a, b);
}
template<size_t i0> __forceinline const avxf shuffle(const avxf &a)
{
  return shuffle<i0>(a, a);
}

template<size_t i> __forceinline float extract(const avxf &a)
{
  __m256 b = shuffle<i, i, i, i>(a).m256;
  return _mm256_cvtss_f32(b);
}
template<> __forceinline float extract<0>(const avxf &a)
{
  return _mm256_cvtss_f32(a.m256);
}

__forceinline ssef low(const avxf &a)
{
  return _mm256_extractf128_ps(a.m256, 0);
}
__forceinline ssef high(const avxf &a)
{
  return _mm256_extractf128_ps(a.m256, 1);
}

template<int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7>
__forceinline const avxf permute(const avxf &a)
{
#ifdef __KERNEL_AVX2__
  return _mm256_permutevar8x32_ps(a, _mm256_set_epi32(i7, i6, i5, i4, i3, i2, i1, i0));
#else
  float temp[8];
  _mm256_storeu_ps((float *)&temp, a);
  return avxf(temp[i7], temp[i6], temp[i5], temp[i4], temp[i3], temp[i2], temp[i1], temp[i0]);
#endif
}

template<int S0, int S1, int S2, int S3, int S4, int S5, int S6, int S7>
ccl_device_inline const avxf set_sign_bit(const avxf &a)
{
  return a ^ avxf(S7 << 31, S6 << 31, S5 << 31, S4 << 31, S3 << 31, S2 << 31, S1 << 31, S0 << 31);
}

template<size_t S0, size_t S1, size_t S2, size_t S3, size_t S4, size_t S5, size_t S6, size_t S7>
ccl_device_inline const avxf blend(const avxf &a, const avxf &b)
{
  return _mm256_blend_ps(
      a, b, S7 << 0 | S6 << 1 | S5 << 2 | S4 << 3 | S3 << 4 | S2 << 5 | S1 << 6 | S0 << 7);
}

template<size_t S0, size_t S1, size_t S2, size_t S3>
ccl_device_inline const avxf blend(const avxf &a, const avxf &b)
{
  return blend<S0, S1, S2, S3, S0, S1, S2, S3>(a, b);
}

//#if defined(__KERNEL_SSE41__)
__forceinline avxf maxi(const avxf &a, const avxf &b)
{
  const avxf ci = _mm256_max_ps(a, b);
  return ci;
}

__forceinline avxf mini(const avxf &a, const avxf &b)
{
  const avxf ci = _mm256_min_ps(a, b);
  return ci;
}
//#endif

////////////////////////////////////////////////////////////////////////////////
/// Ternary Operators
////////////////////////////////////////////////////////////////////////////////
__forceinline const avxf madd(const avxf &a, const avxf &b, const avxf &c)
{
#ifdef __KERNEL_AVX2__
  return _mm256_fmadd_ps(a, b, c);
#else
  return c + (a * b);
#endif
}

__forceinline const avxf nmadd(const avxf &a, const avxf &b, const avxf &c)
{
#ifdef __KERNEL_AVX2__
  return _mm256_fnmadd_ps(a, b, c);
#else
  return c - (a * b);
#endif
}
__forceinline const avxf msub(const avxf &a, const avxf &b, const avxf &c)
{
#ifdef __KERNEL_AVX2__
  return _mm256_fmsub_ps(a, b, c);
#else
  return (a * b) - c;
#endif
}

////////////////////////////////////////////////////////////////////////////////
/// Comparison Operators + Select
////////////////////////////////////////////////////////////////////////////////
__forceinline const avxb operator<=(const avxf &a, const avxf &b)
{
  return _mm256_cmp_ps(a.m256, b.m256, _CMP_LE_OS);
}

__forceinline const avxf select(const avxb &m, const avxf &t, const avxf &f)
{
  return _mm256_blendv_ps(f, t, m);
}

////////////////////////////////////////////////////////////////////////////////
/// Common Functions
////////////////////////////////////////////////////////////////////////////////

__forceinline avxf mix(const avxf &a, const avxf &b, const avxf &t)
{
  return madd(t, b, (avxf(1.0f) - t) * a);
}

#ifndef _mm256_set_m128
#  define _mm256_set_m128(/* __m128 */ hi, /* __m128 */ lo) \
    _mm256_insertf128_ps(_mm256_castps128_ps256(lo), (hi), 0x1)
#endif

#define _mm256_loadu2_m128(/* float const* */ hiaddr, /* float const* */ loaddr) \
  _mm256_set_m128(_mm_loadu_ps(hiaddr), _mm_loadu_ps(loaddr))

CCL_NAMESPACE_END

#endif