Debian Codegraph @

thirdparty/jolt_physics/Jolt/Math/UVec4.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Math/Vec4.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) UVec4
12 {
13 public:
14 JPH_OVERRIDE_NEW_DELETE
15
16 // Underlying vector type
17 #if defined(JPH_USE_SSE)
18 using Type = __m128i;
19 #elif defined(JPH_USE_NEON)
20 using Type = uint32x4_t;
21 #else
22 using Type = struct { uint32 mData[4]; };
23 #endif
24
25 /// Constructor
26 UVec4() = default; ///< Intentionally not initialized for performance reasons
27 UVec4(const UVec4 &inRHS) = default;
28 UVec4 & operator = (const UVec4 &inRHS) = default;
29 JPH_INLINE UVec4(Type inRHS) : mValue(inRHS) { }
30
31 /// Create a vector from 4 integer components
32 JPH_INLINE UVec4(uint32 inX, uint32 inY, uint32 inZ, uint32 inW);
33
34 /// Comparison
35 JPH_INLINE bool operator == (UVec4Arg inV2) const;
36 JPH_INLINE bool operator != (UVec4Arg inV2) const { return !(*this == inV2); }
37
38 /// Swizzle the elements in inV
39 template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
40 JPH_INLINE UVec4 Swizzle() const;
41
42 /// Vector with all zeros
43 static JPH_INLINE UVec4 sZero();
44
45 /// Replicate int inV across all components
46 static JPH_INLINE UVec4 sReplicate(uint32 inV);
47
48 /// Load 1 int from memory and place it in the X component, zeros Y, Z and W
49 static JPH_INLINE UVec4 sLoadInt(const uint32 *inV);
50
51 /// Load 4 ints from memory
52 static JPH_INLINE UVec4 sLoadInt4(const uint32 *inV);
53
54 /// Load 4 ints from memory, aligned to 16 bytes
55 static JPH_INLINE UVec4 sLoadInt4Aligned(const uint32 *inV);
56
57 /// Gather 4 ints from memory at inBase + inOffsets[i] * Scale
58 template <const int Scale>
59 static JPH_INLINE UVec4 sGatherInt4(const uint32 *inBase, UVec4Arg inOffsets);
60
61 /// Return the minimum value of each of the components
62 static JPH_INLINE UVec4 sMin(UVec4Arg inV1, UVec4Arg inV2);
63
64 /// Return the maximum of each of the components
65 static JPH_INLINE UVec4 sMax(UVec4Arg inV1, UVec4Arg inV2);
66
67 /// Equals (component wise)
68 static JPH_INLINE UVec4 sEquals(UVec4Arg inV1, UVec4Arg inV2);
69
70 /// Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit of inControl = 1
71 static JPH_INLINE UVec4 sSelect(UVec4Arg inNotSet, UVec4Arg inSet, UVec4Arg inControl);
72
73 /// Logical or (component wise)
74 static JPH_INLINE UVec4 sOr(UVec4Arg inV1, UVec4Arg inV2);
75
76 /// Logical xor (component wise)
77 static JPH_INLINE UVec4 sXor(UVec4Arg inV1, UVec4Arg inV2);
78
79 /// Logical and (component wise)
80 static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2);
81
82 /// Logical not (component wise)
83 static JPH_INLINE UVec4 sNot(UVec4Arg inV1);
84
85 /// Sorts the elements in inIndex so that the values that correspond to trues in inValue are the first elements.
86 /// The remaining elements will be set to inValue.w.
87 /// I.e. if inValue = (true, false, true, false) and inIndex = (1, 2, 3, 4) the function returns (1, 3, 4, 4).
88 static JPH_INLINE UVec4 sSort4True(UVec4Arg inValue, UVec4Arg inIndex);
89
90 /// Get individual components
91 #if defined(JPH_USE_SSE)
92 JPH_INLINE uint32 GetX() const { return uint32(_mm_cvtsi128_si32(mValue)); }
93 JPH_INLINE uint32 GetY() const { return mU32[1]; }
94 JPH_INLINE uint32 GetZ() const { return mU32[2]; }
95 JPH_INLINE uint32 GetW() const { return mU32[3]; }
96 #elif defined(JPH_USE_NEON)
97 JPH_INLINE uint32 GetX() const { return vgetq_lane_u32(mValue, 0); }
98 JPH_INLINE uint32 GetY() const { return vgetq_lane_u32(mValue, 1); }
99 JPH_INLINE uint32 GetZ() const { return vgetq_lane_u32(mValue, 2); }
100 JPH_INLINE uint32 GetW() const { return vgetq_lane_u32(mValue, 3); }
101 #else
102 JPH_INLINE uint32 GetX() const { return mU32[0]; }
103 JPH_INLINE uint32 GetY() const { return mU32[1]; }
104 JPH_INLINE uint32 GetZ() const { return mU32[2]; }
105 JPH_INLINE uint32 GetW() const { return mU32[3]; }
106 #endif
107
108 /// Set individual components
109 JPH_INLINE void SetX(uint32 inX) { mU32[0] = inX; }
110 JPH_INLINE void SetY(uint32 inY) { mU32[1] = inY; }
111 JPH_INLINE void SetZ(uint32 inZ) { mU32[2] = inZ; }
112 JPH_INLINE void SetW(uint32 inW) { mU32[3] = inW; }
113
114 /// Get component by index
115 JPH_INLINE uint32 operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mU32[inCoordinate]; }
116 JPH_INLINE uint32 & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mU32[inCoordinate]; }
117
118 /// Component wise multiplication of two integer vectors (stores low 32 bits of result only)
119 JPH_INLINE UVec4 operator * (UVec4Arg inV2) const;
120
121 /// Add two integer vectors (component wise)
122 JPH_INLINE UVec4 operator + (UVec4Arg inV2) const;
123
124 /// Add two integer vectors (component wise)
125 JPH_INLINE UVec4 & operator += (UVec4Arg inV2);
126
127 /// Subtract two integer vectors (component wise)
128 JPH_INLINE UVec4 operator - (UVec4Arg inV2) const;
129
130 /// Subtract two integer vectors (component wise)
131 JPH_INLINE UVec4 & operator -= (UVec4Arg inV2);
132
133 /// Replicate the X component to all components
134 JPH_INLINE UVec4 SplatX() const;
135
136 /// Replicate the Y component to all components
137 JPH_INLINE UVec4 SplatY() const;
138
139 /// Replicate the Z component to all components
140 JPH_INLINE UVec4 SplatZ() const;
141
142 /// Replicate the W component to all components
143 JPH_INLINE UVec4 SplatW() const;
144
145 /// Convert each component from an int to a float
146 JPH_INLINE Vec4 ToFloat() const;
147
148 /// Reinterpret UVec4 as a Vec4 (doesn't change the bits)
149 JPH_INLINE Vec4 ReinterpretAsFloat() const;
150
151 /// Dot product, returns the dot product in X, Y, Z and W components
152 JPH_INLINE UVec4 DotV(UVec4Arg inV2) const;
153
154 /// Dot product
155 JPH_INLINE uint32 Dot(UVec4Arg inV2) const;
156
157 /// Store 4 ints to memory
158 JPH_INLINE void StoreInt4(uint32 *outV) const;
159
160 /// Store 4 ints to memory, aligned to 16 bytes
161 JPH_INLINE void StoreInt4Aligned(uint32 *outV) const;
162
163 /// Test if any of the components are true (true is when highest bit of component is set)
164 JPH_INLINE bool TestAnyTrue() const;
165
166 /// Test if any of X, Y or Z components are true (true is when highest bit of component is set)
167 JPH_INLINE bool TestAnyXYZTrue() const;
168
169 /// Test if all components are true (true is when highest bit of component is set)
170 JPH_INLINE bool TestAllTrue() const;
171
172 /// Test if X, Y and Z components are true (true is when highest bit of component is set)
173 JPH_INLINE bool TestAllXYZTrue() const;
174
175 /// Count the number of components that are true (true is when highest bit of component is set)
176 JPH_INLINE int CountTrues() const;
177
178 /// Store if X is true in bit 0, Y in bit 1, Z in bit 2 and W in bit 3 (true is when highest bit of component is set)
179 JPH_INLINE int GetTrues() const;
180
181 /// Shift all components by Count bits to the left (filling with zeros from the left)
182 template <const uint Count>
183 JPH_INLINE UVec4 LogicalShiftLeft() const;
184
185 /// Shift all components by Count bits to the right (filling with zeros from the right)
186 template <const uint Count>
187 JPH_INLINE UVec4 LogicalShiftRight() const;
188
189 /// Shift all components by Count bits to the right (shifting in the value of the highest bit)
190 template <const uint Count>
191 JPH_INLINE UVec4 ArithmeticShiftRight() const;
192
193 /// Takes the lower 4 16 bits and expands them to X, Y, Z and W
194 JPH_INLINE UVec4 Expand4Uint16Lo() const;
195
196 /// Takes the upper 4 16 bits and expands them to X, Y, Z and W
197 JPH_INLINE UVec4 Expand4Uint16Hi() const;
198
199 /// Takes byte 0 .. 3 and expands them to X, Y, Z and W
200 JPH_INLINE UVec4 Expand4Byte0() const;
201
202 /// Takes byte 4 .. 7 and expands them to X, Y, Z and W
203 JPH_INLINE UVec4 Expand4Byte4() const;
204
205 /// Takes byte 8 .. 11 and expands them to X, Y, Z and W
206 JPH_INLINE UVec4 Expand4Byte8() const;
207
208 /// Takes byte 12 .. 15 and expands them to X, Y, Z and W
209 JPH_INLINE UVec4 Expand4Byte12() const;
210
211 /// Shift vector components by 4 - Count floats to the left, so if Count = 1 the resulting vector is (W, 0, 0, 0), when Count = 3 the resulting vector is (Y, Z, W, 0)
212 JPH_INLINE UVec4 ShiftComponents4Minus(int inCount) const;
213
214 /// To String
215 friend ostream & operator << (ostream &inStream, UVec4Arg inV)
216 {
217 inStream << inV.mU32[0] << ", " << inV.mU32[1] << ", " << inV.mU32[2] << ", " << inV.mU32[3];
218 return inStream;
219 }
220
221 union
222 {
223 Type mValue;
224 uint32 mU32[4];
225 };
226 };
227
228 static_assert(std::is_trivial<UVec4>(), "Is supposed to be a trivial type!");
229
230 JPH_NAMESPACE_END
231
232 #include "UVec4.inl"