Blender V5.0
types_int3.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/types_base.h"
8
10
11#ifndef __KERNEL_NATIVE_VECTOR_TYPES__
12# ifdef __KERNEL_ONEAPI__
13/* Keep structure packed for oneAPI. */
14struct int3
15# else
16struct ccl_try_align(16) int3
17# endif
18{
19# ifdef __KERNEL_GPU__
20 /* Compact structure on the GPU. */
21 int x, y, z;
22# else
23 /* SIMD aligned structure for CPU. */
24# ifdef __KERNEL_SSE__
25 union {
26 __m128i m128;
27 struct {
28 int x, y, z, w;
29 };
30 };
31
32 __forceinline int3() = default;
33 __forceinline int3(const int3 &a) = default;
34 __forceinline explicit int3(const __m128i &a) : m128(a) {}
35
36 __forceinline operator const __m128i &() const
37 {
38 return m128;
39 }
40
41 __forceinline operator __m128i &()
42 {
43 return m128;
44 }
45
47 {
48 m128 = a.m128;
49 return *this;
50 }
51# else /* __KERNEL_SSE__ */
52 int x, y, z, w;
53# endif /* __KERNEL_SSE__ */
54# endif
55
56# ifndef __KERNEL_GPU__
57 __forceinline int operator[](int i) const
58 {
59 util_assert(i >= 0);
60 util_assert(i < 3);
61 return *(&x + i);
62 }
63
64 __forceinline int &operator[](int i)
65 {
66 util_assert(i >= 0);
67 util_assert(i < 3);
68 return *(&x + i);
69 }
70# endif
71};
72
73ccl_device_inline int3 make_int3(const int x, const int y, int z)
74{
75# if defined(__KERNEL_GPU__)
76 return {x, y, z};
77# elif defined(__KERNEL_SSE__)
78 return int3(_mm_set_epi32(0, z, y, x));
79# else
80 return {x, y, z, 0};
81# endif
82}
83
84#endif /* __KERNEL_NATIVE_VECTOR_TYPES__ */
85
87{
88#if defined(__KERNEL_GPU__)
89 return make_int3(i, i, i);
90#elif defined(__KERNEL_SSE__)
91 return int3(_mm_set1_epi32(i));
92#else
93 return {i, i, i, i};
94#endif
95}
96
97ccl_device_inline void print_int3(const ccl_private char *label, const int3 a)
98{
99#ifdef __KERNEL_PRINTF__
100 printf("%s: %d %d %d\n", label, a.x, a.y, a.z);
101#endif
102}
103
104#if defined(__KERNEL_METAL__)
105/* Metal has native packed_int3. */
106#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_ONEAPI__)
107/* CUDA/oneAPI int3 is already packed. */
108typedef int3 packed_int3;
109#else
110/* HIP int3 is not packed (https://github.com/ROCm-Developer-Tools/HIP/issues/706). */
112 int x, y, z;
113
115
116 ccl_device_inline_method packed_int3(const int px, const int py, const int pz)
117 : x(px), y(py), z(pz) {};
118
119 ccl_device_inline_method packed_int3(const int3 &a) : x(a.x), y(a.y), z(a.z) {}
120
122 {
123 return make_int3(x, y, z);
124 }
125
127 {
128 x = a.x;
129 y = a.y;
130 z = a.z;
131 return *this;
132 }
133
134# ifndef __KERNEL_GPU__
136 {
137 util_assert(i < 3);
138 return *(&x + i);
139 }
140
142 {
143 util_assert(i < 3);
144 return *(&x + i);
145 }
146# endif
147};
148#endif
149
150static_assert(sizeof(packed_int3) == 12, "packed_int3 expected to be exactly 12 bytes");
151
153{
154 packed_int3 a = {x, y, z};
155 return a;
156}
157
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define util_assert(statement)
#define ccl_private
#define ccl_device_inline
#define ccl_try_align(...)
#define ccl_device_inline_method
#define __forceinline
#define CCL_NAMESPACE_END
#define printf(...)
VecBase< int, 3 > int3
const int & operator[](int i) const
ccl_device_inline_method packed_int3 & operator=(const int3 &a)
Definition types_int3.h:126
ccl_device_inline_method packed_int3(const int3 &a)
Definition types_int3.h:119
ccl_device_inline_method packed_int3(const int px, const int py, const int pz)
Definition types_int3.h:116
__forceinline int & operator[](int i)
Definition types_int3.h:141
__forceinline int operator[](int i) const
Definition types_int3.h:135
ccl_device_inline_method packed_int3()=default
i
Definition text_draw.cc:230
ccl_device_inline int3 make_int3(const int x, const int y, int z)
Definition types_int3.h:73
ccl_device_inline void print_int3(const ccl_private char *label, const int3 a)
Definition types_int3.h:97
ccl_device_inline packed_int3 make_packed_int3(const int x, const int y, int z)
Definition types_int3.h:152