Blender V4.3
math_int8.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#ifndef __UTIL_MATH_INT8_H__
7#define __UTIL_MATH_INT8_H__
8
9#ifndef __UTIL_MATH_H__
10# error "Do not include this file directly, include util/types.h instead."
11#endif
12
14
15#ifndef __KERNEL_GPU__
16ccl_device_inline vint8 operator+(const vint8 a, const vint8 b)
17{
18# ifdef __KERNEL_AVX__
19 return vint8(_mm256_add_epi32(a.m256, b.m256));
20# else
21 return make_vint8(
22 a.a + b.a, a.b + b.b, a.c + b.c, a.d + b.d, a.e + b.e, a.f + b.f, a.g + b.g, a.h + b.h);
23# endif
24}
25
26ccl_device_inline vint8 operator+=(vint8 &a, const vint8 b)
27{
28 return a = a + b;
29}
30
31ccl_device_inline vint8 operator-(const vint8 a, const vint8 b)
32{
33# ifdef __KERNEL_AVX__
34 return vint8(_mm256_sub_epi32(a.m256, b.m256));
35# else
36 return make_vint8(
37 a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d, a.e - b.e, a.f - b.f, a.g - b.g, a.h - b.h);
38# endif
39}
40
41ccl_device_inline vint8 operator-=(vint8 &a, const vint8 b)
42{
43 return a = a - b;
44}
45
46ccl_device_inline vint8 operator>>(const vint8 a, int i)
47{
48# ifdef __KERNEL_AVX__
49 return vint8(_mm256_srai_epi32(a.m256, i));
50# else
51 return make_vint8(
52 a.a >> i, a.b >> i, a.c >> i, a.d >> i, a.e >> i, a.f >> i, a.g >> i, a.h >> i);
53# endif
54}
55
56ccl_device_inline vint8 operator<<(const vint8 a, int i)
57{
58# ifdef __KERNEL_AVX__
59 return vint8(_mm256_slli_epi32(a.m256, i));
60# else
61 return make_vint8(
62 a.a << i, a.b << i, a.c << i, a.d << i, a.e << i, a.f << i, a.g << i, a.h << i);
63# endif
64}
65
66ccl_device_inline vint8 operator<(const vint8 a, const vint8 b)
67{
68# ifdef __KERNEL_AVX__
69 return vint8(_mm256_cmpgt_epi32(b.m256, a.m256));
70# else
71 return make_vint8(
72 a.a < b.a, a.b < b.b, a.c < b.c, a.d < b.d, a.e < b.e, a.f < b.f, a.g < b.g, a.h < b.h);
73# endif
74}
75
76ccl_device_inline vint8 operator<(const vint8 a, const int b)
77{
78 return a < make_vint8(b);
79}
80
81ccl_device_inline vint8 operator==(const vint8 a, const vint8 b)
82{
83# ifdef __KERNEL_AVX__
84 return vint8(_mm256_cmpeq_epi32(a.m256, b.m256));
85# else
86 return make_vint8(a.a == b.a,
87 a.b == b.b,
88 a.c == b.c,
89 a.d == b.d,
90 a.e == b.e,
91 a.f == b.f,
92 a.g == b.g,
93 a.h == b.h);
94# endif
95}
96
97ccl_device_inline vint8 operator==(const vint8 a, const int b)
98{
99 return a == make_vint8(b);
100}
101
102ccl_device_inline vint8 operator>=(const vint8 a, const vint8 b)
103{
104# ifdef __KERNEL_AVX__
105 return vint8(
106 _mm256_xor_si256(_mm256_set1_epi32(0xffffffff), _mm256_cmpgt_epi32(b.m256, a.m256)));
107# else
108 return make_vint8(a.a >= b.a,
109 a.b >= b.b,
110 a.c >= b.c,
111 a.d >= b.d,
112 a.e >= b.e,
113 a.f >= b.f,
114 a.g >= b.g,
115 a.h >= b.h);
116# endif
117}
118
119ccl_device_inline vint8 operator>=(const vint8 a, const int b)
120{
121 return a >= make_vint8(b);
122}
123
124ccl_device_inline vint8 operator&(const vint8 a, const vint8 b)
125{
126# ifdef __KERNEL_AVX__
127 return vint8(_mm256_and_si256(a.m256, b.m256));
128# else
129 return make_vint8(
130 a.a & b.a, a.b & b.b, a.c & b.c, a.d & b.d, a.e & b.e, a.f & b.f, a.g & b.g, a.h & b.h);
131# endif
132}
133
134ccl_device_inline vint8 operator|(const vint8 a, const vint8 b)
135{
136# ifdef __KERNEL_AVX__
137 return vint8(_mm256_or_si256(a.m256, b.m256));
138# else
139 return make_vint8(
140 a.a | b.a, a.b | b.b, a.c | b.c, a.d | b.d, a.e | b.e, a.f | b.f, a.g | b.g, a.h | b.h);
141# endif
142}
143
144ccl_device_inline vint8 operator^(const vint8 a, const vint8 b)
145{
146# ifdef __KERNEL_AVX__
147 return vint8(_mm256_xor_si256(a.m256, b.m256));
148# else
149 return make_vint8(
150 a.a ^ b.a, a.b ^ b.b, a.c ^ b.c, a.d ^ b.d, a.e ^ b.e, a.f ^ b.f, a.g ^ b.g, a.h ^ b.h);
151# endif
152}
153
154ccl_device_inline vint8 operator&(const int32_t a, const vint8 b)
155{
156 return make_vint8(a) & b;
157}
158
159ccl_device_inline vint8 operator&(const vint8 a, const int32_t b)
160{
161 return a & make_vint8(b);
162}
163
164ccl_device_inline vint8 operator|(const int32_t a, const vint8 b)
165{
166 return make_vint8(a) | b;
167}
168
169ccl_device_inline vint8 operator|(const vint8 a, const int32_t b)
170{
171 return a | make_vint8(b);
172}
173
174ccl_device_inline vint8 operator^(const int32_t a, const vint8 b)
175{
176 return make_vint8(a) ^ b;
177}
178
179ccl_device_inline vint8 operator^(const vint8 a, const int32_t b)
180{
181 return a ^ make_vint8(b);
182}
183
184ccl_device_inline vint8 &operator&=(vint8 &a, const vint8 b)
185{
186 return a = a & b;
187}
188ccl_device_inline vint8 &operator&=(vint8 &a, const int32_t b)
189{
190 return a = a & b;
191}
192
193ccl_device_inline vint8 &operator|=(vint8 &a, const vint8 b)
194{
195 return a = a | b;
196}
197ccl_device_inline vint8 &operator|=(vint8 &a, const int32_t b)
198{
199 return a = a | b;
200}
201
202ccl_device_inline vint8 &operator^=(vint8 &a, const vint8 b)
203{
204 return a = a ^ b;
205}
206ccl_device_inline vint8 &operator^=(vint8 &a, const int32_t b)
207{
208 return a = a ^ b;
209}
210
211ccl_device_inline vint8 &operator<<=(vint8 &a, const int32_t b)
212{
213 return a = a << b;
214}
215ccl_device_inline vint8 &operator>>=(vint8 &a, const int32_t b)
216{
217 return a = a >> b;
218}
219
220# ifdef __KERNEL_AVX__
221ccl_device_forceinline const vint8 srl(const vint8 a, const int32_t b)
222{
223 return vint8(_mm256_srli_epi32(a.m256, b));
224}
225# endif
226
227ccl_device_inline vint8 min(vint8 a, vint8 b)
228{
229# if defined(__KERNEL_AVX__) && defined(__KERNEL_AVX41__)
230 return vint8(_mm256_min_epi32(a.m256, b.m256));
231# else
232 return make_vint8(min(a.a, b.a),
233 min(a.b, b.b),
234 min(a.c, b.c),
235 min(a.d, b.d),
236 min(a.e, b.e),
237 min(a.f, b.f),
238 min(a.g, b.g),
239 min(a.h, b.h));
240# endif
241}
242
243ccl_device_inline vint8 max(vint8 a, vint8 b)
244{
245# if defined(__KERNEL_AVX__) && defined(__KERNEL_AVX41__)
246 return vint8(_mm256_max_epi32(a.m256, b.m256));
247# else
248 return make_vint8(max(a.a, b.a),
249 max(a.b, b.b),
250 max(a.c, b.c),
251 max(a.d, b.d),
252 max(a.e, b.e),
253 max(a.f, b.f),
254 max(a.g, b.g),
255 max(a.h, b.h));
256# endif
257}
258
259ccl_device_inline vint8 clamp(const vint8 a, const vint8 mn, const vint8 mx)
260{
261 return min(max(a, mn), mx);
262}
263
264ccl_device_inline vint8 select(const vint8 mask, const vint8 a, const vint8 b)
265{
266# ifdef __KERNEL_AVX__
267 return vint8(_mm256_castps_si256(_mm256_blendv_ps(
268 _mm256_castsi256_ps(b), _mm256_castsi256_ps(a), _mm256_castsi256_ps(mask))));
269# else
270 return make_vint8((mask.a) ? a.a : b.a,
271 (mask.b) ? a.b : b.b,
272 (mask.c) ? a.c : b.c,
273 (mask.d) ? a.d : b.d,
274 (mask.e) ? a.e : b.e,
275 (mask.f) ? a.f : b.f,
276 (mask.g) ? a.g : b.g,
277 (mask.h) ? a.h : b.h);
278# endif
279}
280
282{
283# ifdef __KERNEL_AVX__
284 return vint8(_mm256_loadu_si256((__m256i *)v));
285# else
286 return make_vint8(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
287# endif
288}
289#endif /* __KERNEL_GPU__ */
290
291ccl_device_inline vfloat8 cast(const vint8 a)
292{
293#ifdef __KERNEL_AVX__
294 return vfloat8(_mm256_castsi256_ps(a));
295#else
296 return make_vfloat8(__int_as_float(a.a),
297 __int_as_float(a.b),
298 __int_as_float(a.c),
299 __int_as_float(a.d),
300 __int_as_float(a.e),
301 __int_as_float(a.f),
302 __int_as_float(a.g),
303 __int_as_float(a.h));
304#endif
305}
306
307#ifdef __KERNEL_AVX__
308template<size_t i> ccl_device_forceinline const vint8 shuffle(const vint8 a)
309{
310 return vint8(
311 _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i, i, i, i))));
312}
313
314template<size_t i0, size_t i1> ccl_device_forceinline const vint8 shuffle(const vint8 a)
315{
316 return vint8(_mm256_permute2f128_si256(a, a, (i1 << 4) | (i0 << 0)));
317}
318
319template<size_t i0, size_t i1>
320ccl_device_forceinline const vint8 shuffle(const vint8 a, const vint8 b)
321{
322 return vint8(_mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0)));
323}
324
325template<size_t i0, size_t i1, size_t i2, size_t i3>
326ccl_device_forceinline const vint8 shuffle(const vint8 a)
327{
328 return vint8(
329 _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(a), _MM_SHUFFLE(i3, i2, i1, i0))));
330}
331
332template<size_t i0, size_t i1, size_t i2, size_t i3>
333ccl_device_forceinline const vint8 shuffle(const vint8 a, const vint8 b)
334{
335 return vint8(_mm256_castps_si256(_mm256_shuffle_ps(
336 _mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0))));
337}
338
339template<> __forceinline const vint8 shuffle<0, 0, 2, 2>(const vint8 b)
340{
341 return vint8(_mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(b))));
342}
343template<> __forceinline const vint8 shuffle<1, 1, 3, 3>(const vint8 b)
344{
345 return vint8(_mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(b))));
346}
347template<> __forceinline const vint8 shuffle<0, 1, 0, 1>(const vint8 b)
348{
349 return vint8(_mm256_castps_si256(
350 _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(b))))));
351}
352#endif
353
355
356#endif /* __UTIL_MATH_INT8_H__ */
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
#define ccl_device_forceinline
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define __int_as_float(x)
#define __forceinline
ccl_device_inline vint8 operator==(const vint8 a, const vint8 b)
Definition math_int8.h:81
ccl_device_inline vint8 operator<<(const vint8 a, int i)
Definition math_int8.h:56
ccl_device_inline vint8 & operator&=(vint8 &a, const vint8 b)
Definition math_int8.h:184
ccl_device_inline vint8 operator-(const vint8 a, const vint8 b)
Definition math_int8.h:31
ccl_device_inline vint8 operator^(const vint8 a, const vint8 b)
Definition math_int8.h:144
ccl_device_inline vint8 clamp(const vint8 a, const vint8 mn, const vint8 mx)
Definition math_int8.h:259
ccl_device_inline vint8 operator>=(const vint8 a, const vint8 b)
Definition math_int8.h:102
ccl_device_inline vint8 operator-=(vint8 &a, const vint8 b)
Definition math_int8.h:41
ccl_device_inline vint8 operator>>(const vint8 a, int i)
Definition math_int8.h:46
ccl_device_inline vint8 load_vint8(const int *v)
Definition math_int8.h:281
ccl_device_inline vint8 operator|(const vint8 a, const vint8 b)
Definition math_int8.h:134
ccl_device_inline vint8 operator&(const vint8 a, const vint8 b)
Definition math_int8.h:124
ccl_device_inline vint8 & operator>>=(vint8 &a, const int32_t b)
Definition math_int8.h:215
ccl_device_inline vint8 operator<(const vint8 a, const vint8 b)
Definition math_int8.h:66
ccl_device_inline vint8 & operator^=(vint8 &a, const vint8 b)
Definition math_int8.h:202
CCL_NAMESPACE_BEGIN ccl_device_inline vint8 operator+(const vint8 a, const vint8 b)
Definition math_int8.h:16
ccl_device_inline vfloat8 cast(const vint8 a)
Definition math_int8.h:291
ccl_device_inline vint8 select(const vint8 mask, const vint8 a, const vint8 b)
Definition math_int8.h:264
ccl_device_inline vint8 & operator<<=(vint8 &a, const int32_t b)
Definition math_int8.h:211
ccl_device_inline vint8 operator+=(vint8 &a, const vint8 b)
Definition math_int8.h:26
ccl_device_inline vint8 & operator|=(vint8 &a, const vint8 b)
Definition math_int8.h:193
#define min(a, b)
Definition sort.c:32
signed int int32_t
Definition stdint.h:77
float max
ccl_device_inline vfloat8 make_vfloat8(float f)
ccl_device_inline vint8 make_vint8(int a, int b, int c, int d, int e, int f, int g, int h)