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