Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Vec4.inl
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
6#include <Jolt/Math/Vec3.h>
7#include <Jolt/Math/UVec4.h>
8
10
11// Constructor
13 mValue(inRHS.mValue)
14{
15}
16
17Vec4::Vec4(Vec3Arg inRHS, float inW)
18{
19#if defined(JPH_USE_SSE4_1)
20 mValue = _mm_blend_ps(inRHS.mValue, _mm_set1_ps(inW), 8);
21#elif defined(JPH_USE_NEON)
22 mValue = vsetq_lane_f32(inW, inRHS.mValue, 3);
23#elif defined(JPH_USE_RVV)
24 const vfloat32m1_t v = __riscv_vle32_v_f32m1(inRHS.mF32, 4);
25 __riscv_vse32_v_f32m1(mF32, v, 4);
26 mF32[3] = inW;
27#else
28 for (int i = 0; i < 3; i++)
29 mF32[i] = inRHS.mF32[i];
30 mF32[3] = inW;
31#endif
32}
33
34Vec4::Vec4(float inX, float inY, float inZ, float inW)
35{
36#if defined(JPH_USE_SSE)
37 mValue = _mm_set_ps(inW, inZ, inY, inX);
38#elif defined(JPH_USE_NEON)
39 uint32x2_t xy = vcreate_u32(static_cast<uint64>(BitCast<uint32>(inX)) | (static_cast<uint64>(BitCast<uint32>(inY)) << 32));
40 uint32x2_t zw = vcreate_u32(static_cast<uint64>(BitCast<uint32>(inZ)) | (static_cast<uint64>(BitCast<uint32>(inW)) << 32));
41 mValue = vreinterpretq_f32_u32(vcombine_u32(xy, zw));
42#elif defined(JPH_USE_RVV)
43 vfloat32m1_t v = __riscv_vfmv_v_f_f32m1(inW, 4);
44 v = __riscv_vfslide1up_vf_f32m1(v, inZ, 4);
45 v = __riscv_vfslide1up_vf_f32m1(v, inY, 4);
46 v = __riscv_vfslide1up_vf_f32m1(v, inX, 4);
47 __riscv_vse32_v_f32m1(mF32, v, 4);
48#else
49 mF32[0] = inX;
50 mF32[1] = inY;
51 mF32[2] = inZ;
52 mF32[3] = inW;
53#endif
54}
55
56template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
58{
59 static_assert(SwizzleX <= 3, "SwizzleX template parameter out of range");
60 static_assert(SwizzleY <= 3, "SwizzleY template parameter out of range");
61 static_assert(SwizzleZ <= 3, "SwizzleZ template parameter out of range");
62 static_assert(SwizzleW <= 3, "SwizzleW template parameter out of range");
63
64#if defined(JPH_USE_SSE)
65 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(SwizzleW, SwizzleZ, SwizzleY, SwizzleX));
66#elif defined(JPH_USE_NEON)
67 return JPH_NEON_SHUFFLE_F32x4(mValue, mValue, SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
68#elif defined(JPH_USE_RVV)
69 Vec4 v;
70 const vfloat32m1_t data = __riscv_vle32_v_f32m1(mF32, 4);
71 const uint32 stored_indices[4] = { SwizzleX, SwizzleY, SwizzleZ, SwizzleW };
72 const vuint32m1_t index = __riscv_vle32_v_u32m1(stored_indices, 4);
73 const vfloat32m1_t swizzled = __riscv_vrgather_vv_f32m1(data, index, 4);
74 __riscv_vse32_v_f32m1(v.mF32, swizzled, 4);
75 return v;
76#else
77 return Vec4(mF32[SwizzleX], mF32[SwizzleY], mF32[SwizzleZ], mF32[SwizzleW]);
78#endif
79}
80
82{
83#if defined(JPH_USE_SSE)
84 return _mm_setzero_ps();
85#elif defined(JPH_USE_NEON)
86 return vdupq_n_f32(0);
87#elif defined(JPH_USE_RVV)
88 Vec4 v;
89 const vfloat32m1_t zero_vec = __riscv_vfmv_v_f_f32m1(0.0f, 4);
90 __riscv_vse32_v_f32m1(v.mF32, zero_vec, 4);
91 return v;
92#else
93 return Vec4(0, 0, 0, 0);
94#endif
95}
96
98{
99#if defined(JPH_USE_SSE)
100 return _mm_set1_ps(inV);
101#elif defined(JPH_USE_NEON)
102 return vdupq_n_f32(inV);
103#elif defined(JPH_USE_RVV)
104 Vec4 vec;
105 const vfloat32m1_t v = __riscv_vfmv_v_f_f32m1(inV, 4);
106 __riscv_vse32_v_f32m1(vec.mF32, v, 4);
107 return vec;
108#else
109 return Vec4(inV, inV, inV, inV);
110#endif
111}
112
114{
115 return sReplicate(1.0f);
116}
117
119{
120 return sReplicate(numeric_limits<float>::quiet_NaN());
121}
122
124{
125#if defined(JPH_USE_SSE)
126 return _mm_loadu_ps(&inV->x);
127#elif defined(JPH_USE_NEON)
128 return vld1q_f32(&inV->x);
129#elif defined(JPH_USE_RVV)
130 Vec4 vector;
131 const vfloat32m1_t v = __riscv_vle32_v_f32m1(&inV->x, 4);
132 __riscv_vse32_v_f32m1(vector.mF32, v, 4);
133 return vector;
134#else
135 return Vec4(inV->x, inV->y, inV->z, inV->w);
136#endif
137}
138
140{
141#if defined(JPH_USE_SSE)
142 return _mm_load_ps(&inV->x);
143#elif defined(JPH_USE_NEON)
144 return vld1q_f32(&inV->x);
145#elif defined(JPH_USE_RVV)
146 Vec4 vector;
147 vfloat32m1_t v = __riscv_vle32_v_f32m1(&inV->x, 4);
148 __riscv_vse32_v_f32m1(vector.mF32, v, 4);
149 return vector;
150#else
151 return Vec4(inV->x, inV->y, inV->z, inV->w);
152#endif
153}
154
155template <const int Scale>
156Vec4 Vec4::sGatherFloat4(const float *inBase, UVec4Arg inOffsets)
157{
158#if defined(JPH_USE_SSE)
159 #ifdef JPH_USE_AVX2
160 return _mm_i32gather_ps(inBase, inOffsets.mValue, Scale);
161 #else
162 const uint8 *base = reinterpret_cast<const uint8 *>(inBase);
163 Type x = _mm_load_ss(reinterpret_cast<const float *>(base + inOffsets.GetX() * Scale));
164 Type y = _mm_load_ss(reinterpret_cast<const float *>(base + inOffsets.GetY() * Scale));
165 Type xy = _mm_unpacklo_ps(x, y);
166 Type z = _mm_load_ss(reinterpret_cast<const float *>(base + inOffsets.GetZ() * Scale));
167 Type w = _mm_load_ss(reinterpret_cast<const float *>(base + inOffsets.GetW() * Scale));
168 Type zw = _mm_unpacklo_ps(z, w);
169 return _mm_movelh_ps(xy, zw);
170 #endif
171#elif defined(JPH_USE_RVV)
172 Vec4 v;
173 const vuint32m1_t offsets = __riscv_vle32_v_u32m1(inOffsets.mU32, 4);
174 const vuint32m1_t scaled_offsets = __riscv_vmul_vx_u32m1(offsets, Scale, 4);
175 const vfloat32m1_t gathered = __riscv_vluxei32_v_f32m1(inBase, scaled_offsets, 4);
176 __riscv_vse32_v_f32m1(v.mF32, gathered, 4);
177 return v;
178#else
179 const uint8 *base = reinterpret_cast<const uint8 *>(inBase);
180 float x = *reinterpret_cast<const float *>(base + inOffsets.GetX() * Scale);
181 float y = *reinterpret_cast<const float *>(base + inOffsets.GetY() * Scale);
182 float z = *reinterpret_cast<const float *>(base + inOffsets.GetZ() * Scale);
183 float w = *reinterpret_cast<const float *>(base + inOffsets.GetW() * Scale);
184 return Vec4(x, y, z, w);
185#endif
186}
187
189{
190#if defined(JPH_USE_SSE)
191 return _mm_min_ps(inV1.mValue, inV2.mValue);
192#elif defined(JPH_USE_NEON)
193 return vminq_f32(inV1.mValue, inV2.mValue);
194#elif defined(JPH_USE_RVV)
195 Vec4 res;
196 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
197 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
198 const vfloat32m1_t min = __riscv_vfmin_vv_f32m1(v1, v2, 4);
199 __riscv_vse32_v_f32m1(res.mF32, min, 4);
200 return res;
201#else
202 return Vec4(min(inV1.mF32[0], inV2.mF32[0]),
203 min(inV1.mF32[1], inV2.mF32[1]),
204 min(inV1.mF32[2], inV2.mF32[2]),
205 min(inV1.mF32[3], inV2.mF32[3]));
206#endif
207}
208
210{
211#if defined(JPH_USE_SSE)
212 return _mm_max_ps(inV1.mValue, inV2.mValue);
213#elif defined(JPH_USE_NEON)
214 return vmaxq_f32(inV1.mValue, inV2.mValue);
215#elif defined(JPH_USE_RVV)
216 Vec4 res;
217 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
218 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
219 const vfloat32m1_t max = __riscv_vfmax_vv_f32m1(v1, v2, 4);
220 __riscv_vse32_v_f32m1(res.mF32, max, 4);
221 return res;
222#else
223 return Vec4(max(inV1.mF32[0], inV2.mF32[0]),
224 max(inV1.mF32[1], inV2.mF32[1]),
225 max(inV1.mF32[2], inV2.mF32[2]),
226 max(inV1.mF32[3], inV2.mF32[3]));
227#endif
228}
229
231{
232 return sMax(sMin(inV, inMax), inMin);
233}
234
236{
237#if defined(JPH_USE_SSE)
238 return _mm_castps_si128(_mm_cmpeq_ps(inV1.mValue, inV2.mValue));
239#elif defined(JPH_USE_NEON)
240 return vceqq_f32(inV1.mValue, inV2.mValue);
241#elif defined(JPH_USE_RVV)
242 UVec4 res;
243 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
244 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
245 const vbool32_t mask = __riscv_vmfeq_vv_f32m1_b32(v1, v2, 4);
246 const vuint32m1_t zeros = __riscv_vmv_v_x_u32m1(0x0, 4);
247 const vuint32m1_t merged = __riscv_vmerge_vxm_u32m1(zeros, 0xFFFFFFFF, mask, 4);
248 __riscv_vse32_v_u32m1(res.mU32, merged, 4);
249 return res;
250#else
251 return UVec4(inV1.mF32[0] == inV2.mF32[0]? 0xffffffffu : 0,
252 inV1.mF32[1] == inV2.mF32[1]? 0xffffffffu : 0,
253 inV1.mF32[2] == inV2.mF32[2]? 0xffffffffu : 0,
254 inV1.mF32[3] == inV2.mF32[3]? 0xffffffffu : 0);
255#endif
256}
257
259{
260#if defined(JPH_USE_SSE)
261 return _mm_castps_si128(_mm_cmplt_ps(inV1.mValue, inV2.mValue));
262#elif defined(JPH_USE_NEON)
263 return vcltq_f32(inV1.mValue, inV2.mValue);
264#elif defined(JPH_USE_RVV)
265 UVec4 res;
266 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
267 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
268 const vbool32_t mask = __riscv_vmflt_vv_f32m1_b32(v1, v2, 4);
269 const vuint32m1_t zeros = __riscv_vmv_v_x_u32m1(0x0, 4);
270 const vuint32m1_t merged = __riscv_vmerge_vxm_u32m1(zeros, 0xFFFFFFFF, mask, 4);
271 __riscv_vse32_v_u32m1(res.mU32, merged, 4);
272 return res;
273#else
274 return UVec4(inV1.mF32[0] < inV2.mF32[0]? 0xffffffffu : 0,
275 inV1.mF32[1] < inV2.mF32[1]? 0xffffffffu : 0,
276 inV1.mF32[2] < inV2.mF32[2]? 0xffffffffu : 0,
277 inV1.mF32[3] < inV2.mF32[3]? 0xffffffffu : 0);
278#endif
279}
280
282{
283#if defined(JPH_USE_SSE)
284 return _mm_castps_si128(_mm_cmple_ps(inV1.mValue, inV2.mValue));
285#elif defined(JPH_USE_NEON)
286 return vcleq_f32(inV1.mValue, inV2.mValue);
287#elif defined(JPH_USE_RVV)
288 UVec4 res;
289 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
290 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
291 const vbool32_t mask = __riscv_vmfle_vv_f32m1_b32(v1, v2, 4);
292 const vuint32m1_t zeros = __riscv_vmv_v_x_u32m1(0x0, 4);
293 const vuint32m1_t merged = __riscv_vmerge_vxm_u32m1(zeros, 0xFFFFFFFF, mask, 4);
294 __riscv_vse32_v_u32m1(res.mU32, merged, 4);
295 return res;
296#else
297 return UVec4(inV1.mF32[0] <= inV2.mF32[0]? 0xffffffffu : 0,
298 inV1.mF32[1] <= inV2.mF32[1]? 0xffffffffu : 0,
299 inV1.mF32[2] <= inV2.mF32[2]? 0xffffffffu : 0,
300 inV1.mF32[3] <= inV2.mF32[3]? 0xffffffffu : 0);
301#endif
302}
303
305{
306#if defined(JPH_USE_SSE)
307 return _mm_castps_si128(_mm_cmpgt_ps(inV1.mValue, inV2.mValue));
308#elif defined(JPH_USE_NEON)
309 return vcgtq_f32(inV1.mValue, inV2.mValue);
310#elif defined(JPH_USE_RVV)
311 UVec4 res;
312 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
313 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
314 const vbool32_t mask = __riscv_vmfgt_vv_f32m1_b32(v1, v2, 4);
315 const vuint32m1_t zeros = __riscv_vmv_v_x_u32m1(0x0, 4);
316 const vuint32m1_t merged = __riscv_vmerge_vxm_u32m1(zeros, 0xFFFFFFFF, mask, 4);
317 __riscv_vse32_v_u32m1(res.mU32, merged, 4);
318 return res;
319#else
320 return UVec4(inV1.mF32[0] > inV2.mF32[0]? 0xffffffffu : 0,
321 inV1.mF32[1] > inV2.mF32[1]? 0xffffffffu : 0,
322 inV1.mF32[2] > inV2.mF32[2]? 0xffffffffu : 0,
323 inV1.mF32[3] > inV2.mF32[3]? 0xffffffffu : 0);
324#endif
325}
326
328{
329#if defined(JPH_USE_SSE)
330 return _mm_castps_si128(_mm_cmpge_ps(inV1.mValue, inV2.mValue));
331#elif defined(JPH_USE_NEON)
332 return vcgeq_f32(inV1.mValue, inV2.mValue);
333#elif defined(JPH_USE_RVV)
334 UVec4 res;
335 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV1.mF32, 4);
336 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
337 const vbool32_t mask = __riscv_vmfge_vv_f32m1_b32(v1, v2, 4);
338 const vuint32m1_t zeros = __riscv_vmv_v_x_u32m1(0x0, 4);
339 const vuint32m1_t merged = __riscv_vmerge_vxm_u32m1(zeros, 0xFFFFFFFF, mask, 4);
340 __riscv_vse32_v_u32m1(res.mU32, merged, 4);
341 return res;
342#else
343 return UVec4(inV1.mF32[0] >= inV2.mF32[0]? 0xffffffffu : 0,
344 inV1.mF32[1] >= inV2.mF32[1]? 0xffffffffu : 0,
345 inV1.mF32[2] >= inV2.mF32[2]? 0xffffffffu : 0,
346 inV1.mF32[3] >= inV2.mF32[3]? 0xffffffffu : 0);
347#endif
348}
349
351{
352#ifdef JPH_USE_FMADD
353 #ifdef JPH_USE_SSE
354 return _mm_fmadd_ps(inMul1.mValue, inMul2.mValue, inAdd.mValue);
355 #elif defined(JPH_USE_NEON)
356 return vmlaq_f32(inAdd.mValue, inMul1.mValue, inMul2.mValue);
357 #elif defined(JPH_USE_RVV)
358 Vec4 res;
359 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inMul1.mF32, 4);
360 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inMul2.mF32, 4);
361 const vfloat32m1_t rvv_add = __riscv_vle32_v_f32m1(inAdd.mF32, 4);
362 const vfloat32m1_t fmadd = __riscv_vfmacc_vv_f32m1(rvv_add, v1, v2, 4);
363 __riscv_vse32_v_f32m1(res.mF32, fmadd, 4);
364 return res;
365 #else
366 return inMul1 * inMul2 + inAdd;
367 #endif
368#else
369 return inMul1 * inMul2 + inAdd;
370#endif
371}
372
373Vec4 Vec4::sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
374{
375#if defined(JPH_USE_SSE4_1) && !defined(JPH_PLATFORM_WASM) // _mm_blendv_ps has problems on FireFox
376 return _mm_blendv_ps(inNotSet.mValue, inSet.mValue, _mm_castsi128_ps(inControl.mValue));
377#elif defined(JPH_USE_SSE)
378 __m128 is_set = _mm_castsi128_ps(_mm_srai_epi32(inControl.mValue, 31));
379 return _mm_or_ps(_mm_and_ps(is_set, inSet.mValue), _mm_andnot_ps(is_set, inNotSet.mValue));
380#elif defined(JPH_USE_NEON)
381 return vbslq_f32(vreinterpretq_u32_s32(vshrq_n_s32(vreinterpretq_s32_u32(inControl.mValue), 31)), inSet.mValue, inNotSet.mValue);
382#elif defined(JPH_USE_RVV)
383 Vec4 masked;
384 const vuint32m1_t control = __riscv_vle32_v_u32m1(inControl.mU32, 4);
385 const vfloat32m1_t not_set = __riscv_vle32_v_f32m1(inNotSet.mF32, 4);
386 const vfloat32m1_t set = __riscv_vle32_v_f32m1(inSet.mF32, 4);
387
388 // Generate RVV bool mask from UVec4
389 const vuint32m1_t r = __riscv_vand_vx_u32m1(control, 0x80000000u, 4);
390 const vbool32_t rvv_mask = __riscv_vmsne_vx_u32m1_b32(r, 0x0, 4);
391 const vfloat32m1_t merged = __riscv_vmerge_vvm_f32m1(not_set, set, rvv_mask, 4);
392 __riscv_vse32_v_f32m1(masked.mF32, merged, 4);
393 return masked;
394#else
395 Vec4 result;
396 for (int i = 0; i < 4; i++)
397 result.mF32[i] = (inControl.mU32[i] & 0x80000000u) ? inSet.mF32[i] : inNotSet.mF32[i];
398 return result;
399#endif
400}
401
403{
404#if defined(JPH_USE_SSE)
405 return _mm_or_ps(inV1.mValue, inV2.mValue);
406#elif defined(JPH_USE_NEON)
407 return vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
408#elif defined(JPH_USE_RVV)
409 Vec4 or_result;
410 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
411 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
412 const vuint32m1_t res = __riscv_vor_vv_u32m1(v1, v2, 4);
413 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(or_result.mF32), res, 4);
414 return or_result;
415#else
417#endif
418}
419
421{
422#if defined(JPH_USE_SSE)
423 return _mm_xor_ps(inV1.mValue, inV2.mValue);
424#elif defined(JPH_USE_NEON)
425 return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
426#elif defined(JPH_USE_RVV)
427 Vec4 xor_result;
428 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
429 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
430 const vuint32m1_t res = __riscv_vxor_vv_u32m1(v1, v2, 4);
431 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(xor_result.mF32), res, 4);
432 return xor_result;
433#else
435#endif
436}
437
439{
440#if defined(JPH_USE_SSE)
441 return _mm_and_ps(inV1.mValue, inV2.mValue);
442#elif defined(JPH_USE_NEON)
443 return vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
444#elif defined(JPH_USE_RVV)
445 Vec4 and_result;
446 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
447 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
448 const vuint32m1_t res = __riscv_vand_vv_u32m1(v1, v2, 4);
449 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(and_result.mF32), res, 4);
450 return and_result;
451#else
453#endif
454}
455
456void Vec4::sSort4(Vec4 &ioValue, UVec4 &ioIndex)
457{
458 // Pass 1, test 1st vs 3rd, 2nd vs 4th
461 UVec4 c1 = sLess(ioValue, v1).Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W>();
462 ioValue = sSelect(ioValue, v1, c1);
463 ioIndex = UVec4::sSelect(ioIndex, i1, c1);
464
465 // Pass 2, test 1st vs 2nd, 3rd vs 4th
468 UVec4 c2 = sLess(ioValue, v2).Swizzle<SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_W, SWIZZLE_W>();
469 ioValue = sSelect(ioValue, v2, c2);
470 ioIndex = UVec4::sSelect(ioIndex, i2, c2);
471
472 // Pass 3, test 2nd vs 3rd component
475 UVec4 c3 = sLess(ioValue, v3).Swizzle<SWIZZLE_X, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_W>();
476 ioValue = sSelect(ioValue, v3, c3);
477 ioIndex = UVec4::sSelect(ioIndex, i3, c3);
478}
479
480void Vec4::sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex)
481{
482 // Pass 1, test 1st vs 3rd, 2nd vs 4th
486 ioValue = sSelect(ioValue, v1, c1);
487 ioIndex = UVec4::sSelect(ioIndex, i1, c1);
488
489 // Pass 2, test 1st vs 2nd, 3rd vs 4th
493 ioValue = sSelect(ioValue, v2, c2);
494 ioIndex = UVec4::sSelect(ioIndex, i2, c2);
495
496 // Pass 3, test 2nd vs 3rd component
500 ioValue = sSelect(ioValue, v3, c3);
501 ioIndex = UVec4::sSelect(ioIndex, i3, c3);
502}
503
505{
506 return sEquals(*this, inV2).TestAllTrue();
507}
508
509bool Vec4::IsClose(Vec4Arg inV2, float inMaxDistSq) const
510{
511 return (inV2 - *this).LengthSq() <= inMaxDistSq;
512}
513
514bool Vec4::IsNearZero(float inMaxDistSq) const
515{
516 return LengthSq() <= inMaxDistSq;
517}
518
519bool Vec4::IsNormalized(float inTolerance) const
520{
521 return abs(LengthSq() - 1.0f) <= inTolerance;
522}
523
524bool Vec4::IsNaN() const
525{
526#if defined(JPH_USE_AVX512)
527 return _mm_fpclass_ps_mask(mValue, 0b10000001) != 0;
528#elif defined(JPH_USE_SSE)
529 return _mm_movemask_ps(_mm_cmpunord_ps(mValue, mValue)) != 0;
530#elif defined(JPH_USE_NEON)
531 uint32x4_t is_equal = vceqq_f32(mValue, mValue); // If a number is not equal to itself it's a NaN
532 return vaddvq_u32(vshrq_n_u32(is_equal, 31)) != 4;
533#elif defined(JPH_USE_RVV)
534 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
535 const vbool32_t mask = __riscv_vmfeq_vv_f32m1_b32(v, v, 4);
536 const uint32 eq = __riscv_vcpop_m_b32(mask, 4);
537 return eq != 4;
538#else
539 return isnan(mF32[0]) || isnan(mF32[1]) || isnan(mF32[2]) || isnan(mF32[3]);
540#endif
541}
542
544{
545#if defined(JPH_USE_SSE)
546 return _mm_mul_ps(mValue, inV2.mValue);
547#elif defined(JPH_USE_NEON)
548 return vmulq_f32(mValue, inV2.mValue);
549#elif defined(JPH_USE_RVV)
550 Vec4 res;
551 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
552 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
553 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v1, v2, 4);
554 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
555 return res;
556#else
557 return Vec4(mF32[0] * inV2.mF32[0],
558 mF32[1] * inV2.mF32[1],
559 mF32[2] * inV2.mF32[2],
560 mF32[3] * inV2.mF32[3]);
561#endif
562}
563
564Vec4 Vec4::operator * (float inV2) const
565{
566#if defined(JPH_USE_SSE)
567 return _mm_mul_ps(mValue, _mm_set1_ps(inV2));
568#elif defined(JPH_USE_NEON)
569 return vmulq_n_f32(mValue, inV2);
570#elif defined(JPH_USE_RVV)
571 Vec4 res;
572 const vfloat32m1_t src = __riscv_vle32_v_f32m1(mF32, 4);
573 const vfloat32m1_t mul = __riscv_vfmul_vf_f32m1(src, inV2, 4);
574 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
575 return res;
576#else
577 return Vec4(mF32[0] * inV2, mF32[1] * inV2, mF32[2] * inV2, mF32[3] * inV2);
578#endif
579}
580
582Vec4 operator * (float inV1, Vec4Arg inV2)
583{
584#if defined(JPH_USE_SSE)
585 return _mm_mul_ps(_mm_set1_ps(inV1), inV2.mValue);
586#elif defined(JPH_USE_NEON)
587 return vmulq_n_f32(inV2.mValue, inV1);
588#elif defined(JPH_USE_RVV)
589 Vec4 res;
590 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
591 const vfloat32m1_t mul = __riscv_vfmul_vf_f32m1(v1, inV1, 4);
592 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
593 return res;
594#else
595 return Vec4(inV1 * inV2.mF32[0],
596 inV1 * inV2.mF32[1],
597 inV1 * inV2.mF32[2],
598 inV1 * inV2.mF32[3]);
599#endif
600}
601
602Vec4 Vec4::operator / (float inV2) const
603{
604#if defined(JPH_USE_SSE)
605 return _mm_div_ps(mValue, _mm_set1_ps(inV2));
606#elif defined(JPH_USE_NEON)
607 return vdivq_f32(mValue, vdupq_n_f32(inV2));
608#elif defined(JPH_USE_RVV)
609 Vec4 res;
610 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
611 const vfloat32m1_t div = __riscv_vfdiv_vf_f32m1(v1, inV2, 4);
612 __riscv_vse32_v_f32m1(res.mF32, div, 4);
613 return res;
614#else
615 return Vec4(mF32[0] / inV2, mF32[1] / inV2, mF32[2] / inV2, mF32[3] / inV2);
616#endif
617}
618
620{
621#if defined(JPH_USE_SSE)
622 mValue = _mm_mul_ps(mValue, _mm_set1_ps(inV2));
623#elif defined(JPH_USE_NEON)
624 mValue = vmulq_n_f32(mValue, inV2);
625#elif defined(JPH_USE_RVV)
626 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
627 const vfloat32m1_t res = __riscv_vfmul_vf_f32m1(v1, inV2, 4);
628 __riscv_vse32_v_f32m1(mF32, res, 4);
629#else
630 for (int i = 0; i < 4; ++i)
631 mF32[i] *= inV2;
632#endif
633 return *this;
634}
635
637{
638#if defined(JPH_USE_SSE)
639 mValue = _mm_mul_ps(mValue, inV2.mValue);
640#elif defined(JPH_USE_NEON)
641 mValue = vmulq_f32(mValue, inV2.mValue);
642#elif defined(JPH_USE_RVV)
643 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
644 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
645 const vfloat32m1_t rvv_res = __riscv_vfmul_vv_f32m1(v1, v2, 4);
646 __riscv_vse32_v_f32m1(mF32, rvv_res, 4);
647#else
648 for (int i = 0; i < 4; ++i)
649 mF32[i] *= inV2.mF32[i];
650#endif
651 return *this;
652}
653
655{
656#if defined(JPH_USE_SSE)
657 mValue = _mm_div_ps(mValue, _mm_set1_ps(inV2));
658#elif defined(JPH_USE_NEON)
659 mValue = vdivq_f32(mValue, vdupq_n_f32(inV2));
660#elif defined(JPH_USE_RVV)
661 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
662 const vfloat32m1_t res = __riscv_vfdiv_vf_f32m1(v, inV2, 4);
663 __riscv_vse32_v_f32m1(mF32, res, 4);
664#else
665 for (int i = 0; i < 4; ++i)
666 mF32[i] /= inV2;
667#endif
668 return *this;
669}
670
672{
673#if defined(JPH_USE_SSE)
674 return _mm_add_ps(mValue, inV2.mValue);
675#elif defined(JPH_USE_NEON)
676 return vaddq_f32(mValue, inV2.mValue);
677#elif defined(JPH_USE_RVV)
678 Vec4 res;
679 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
680 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
681 const vfloat32m1_t rvv_add = __riscv_vfadd_vv_f32m1(v1, v2, 4);
682 __riscv_vse32_v_f32m1(res.mF32, rvv_add, 4);
683 return res;
684#else
685 return Vec4(mF32[0] + inV2.mF32[0],
686 mF32[1] + inV2.mF32[1],
687 mF32[2] + inV2.mF32[2],
688 mF32[3] + inV2.mF32[3]);
689#endif
690}
691
693{
694#if defined(JPH_USE_SSE)
695 mValue = _mm_add_ps(mValue, inV2.mValue);
696#elif defined(JPH_USE_NEON)
697 mValue = vaddq_f32(mValue, inV2.mValue);
698#elif defined(JPH_USE_RVV)
699 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
700 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
701 const vfloat32m1_t rvv_add = __riscv_vfadd_vv_f32m1(v1, v2, 4);
702 __riscv_vse32_v_f32m1(mF32, rvv_add, 4);
703#else
704 for (int i = 0; i < 4; ++i)
705 mF32[i] += inV2.mF32[i];
706#endif
707 return *this;
708}
709
711{
712#if defined(JPH_USE_SSE)
713 return _mm_sub_ps(_mm_setzero_ps(), mValue);
714#elif defined(JPH_USE_NEON)
715 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
716 return vsubq_f32(vdupq_n_f32(0), mValue);
717 #else
718 return vnegq_f32(mValue);
719 #endif
720#elif defined(JPH_USE_RVV)
721 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
722 Vec4 res;
723 const vfloat32m1_t rvv_zero = __riscv_vfmv_v_f_f32m1(0.0f, 4);
724 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
725 const vfloat32m1_t rvv_neg = __riscv_vfsub_vv_f32m1(rvv_zero, v, 4);
726 __riscv_vse32_v_f32m1(res.mF32, rvv_neg, 4);
727 return res;
728 #else
729 Vec4 res;
730 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
731 const vfloat32m1_t rvv_neg = __riscv_vfsgnjn_vv_f32m1(v, v, 4);
732 __riscv_vse32_v_f32m1(res.mF32, rvv_neg, 4);
733 return res;
734 #endif
735#else
736 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
737 return Vec4(0.0f - mF32[0], 0.0f - mF32[1], 0.0f - mF32[2], 0.0f - mF32[3]);
738 #else
739 return Vec4(-mF32[0], -mF32[1], -mF32[2], -mF32[3]);
740 #endif
741#endif
742}
743
745{
746#if defined(JPH_USE_SSE)
747 return _mm_sub_ps(mValue, inV2.mValue);
748#elif defined(JPH_USE_NEON)
749 return vsubq_f32(mValue, inV2.mValue);
750#elif defined(JPH_USE_RVV)
751 Vec4 res;
752 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
753 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
754 const vfloat32m1_t rvv_sub = __riscv_vfsub_vv_f32m1(v1, v2, 4);
755 __riscv_vse32_v_f32m1(res.mF32, rvv_sub, 4);
756 return res;
757#else
758 return Vec4(mF32[0] - inV2.mF32[0],
759 mF32[1] - inV2.mF32[1],
760 mF32[2] - inV2.mF32[2],
761 mF32[3] - inV2.mF32[3]);
762#endif
763}
764
766{
767#if defined(JPH_USE_SSE)
768 mValue = _mm_sub_ps(mValue, inV2.mValue);
769#elif defined(JPH_USE_NEON)
770 mValue = vsubq_f32(mValue, inV2.mValue);
771#elif defined(JPH_USE_RVV)
772 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
773 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
774 const vfloat32m1_t rvv_sub = __riscv_vfsub_vv_f32m1(v1, v2, 4);
775 __riscv_vse32_v_f32m1(mF32, rvv_sub, 4);
776#else
777 for (int i = 0; i < 4; ++i)
778 mF32[i] -= inV2.mF32[i];
779#endif
780 return *this;
781}
782
784{
785#if defined(JPH_USE_SSE)
786 return _mm_div_ps(mValue, inV2.mValue);
787#elif defined(JPH_USE_NEON)
788 return vdivq_f32(mValue, inV2.mValue);
789#elif defined(JPH_USE_RVV)
790 Vec4 res;
791 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
792 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
793 const vfloat32m1_t rvv_div = __riscv_vfdiv_vv_f32m1(v1, v2, 4);
794 __riscv_vse32_v_f32m1(res.mF32, rvv_div, 4);
795 return res;
796#else
797 return Vec4(mF32[0] / inV2.mF32[0],
798 mF32[1] / inV2.mF32[1],
799 mF32[2] / inV2.mF32[2],
800 mF32[3] / inV2.mF32[3]);
801#endif
802}
803
805{
806#if defined(JPH_USE_SSE)
807 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(0, 0, 0, 0));
808#elif defined(JPH_USE_NEON)
809 return vdupq_laneq_f32(mValue, 0);
810#elif defined(JPH_USE_RVV)
811 Vec4 vec;
812 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[0], 4);
813 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
814 return vec;
815#else
816 return Vec4(mF32[0], mF32[0], mF32[0], mF32[0]);
817#endif
818}
819
821{
822#if defined(JPH_USE_SSE)
823 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(1, 1, 1, 1));
824#elif defined(JPH_USE_NEON)
825 return vdupq_laneq_f32(mValue, 1);
826#elif defined(JPH_USE_RVV)
827 Vec4 vec;
828 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[1], 4);
829 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
830 return vec;
831#else
832 return Vec4(mF32[1], mF32[1], mF32[1], mF32[1]);
833#endif
834}
835
837{
838#if defined(JPH_USE_SSE)
839 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(2, 2, 2, 2));
840#elif defined(JPH_USE_NEON)
841 return vdupq_laneq_f32(mValue, 2);
842#elif defined(JPH_USE_RVV)
843 Vec4 vec;
844 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[2], 4);
845 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
846 return vec;
847#else
848 return Vec4(mF32[2], mF32[2], mF32[2], mF32[2]);
849#endif
850}
851
853{
854#if defined(JPH_USE_SSE)
855 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(3, 3, 3, 3));
856#elif defined(JPH_USE_NEON)
857 return vdupq_laneq_f32(mValue, 3);
858#elif defined(JPH_USE_RVV)
859 Vec4 vec;
860 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[3], 4);
861 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
862 return vec;
863#else
864 return Vec4(mF32[3], mF32[3], mF32[3], mF32[3]);
865#endif
866}
867
869{
870#if defined(JPH_USE_SSE)
871 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(0, 0, 0, 0));
872#elif defined(JPH_USE_NEON)
873 return vdupq_laneq_f32(mValue, 0);
874#elif defined(JPH_USE_RVV)
875 Vec3 vec;
876 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[0], 3);
877 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
878 return vec;
879#else
880 return Vec3(mF32[0], mF32[0], mF32[0]);
881#endif
882}
883
885{
886#if defined(JPH_USE_SSE)
887 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(1, 1, 1, 1));
888#elif defined(JPH_USE_NEON)
889 return vdupq_laneq_f32(mValue, 1);
890#elif defined(JPH_USE_RVV)
891 Vec3 vec;
892 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[1], 3);
893 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
894 return vec;
895#else
896 return Vec3(mF32[1], mF32[1], mF32[1]);
897#endif
898}
899
901{
902#if defined(JPH_USE_SSE)
903 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(2, 2, 2, 2));
904#elif defined(JPH_USE_NEON)
905 return vdupq_laneq_f32(mValue, 2);
906#elif defined(JPH_USE_RVV)
907 Vec3 vec;
908 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[2], 3);
909 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
910 return vec;
911#else
912 return Vec3(mF32[2], mF32[2], mF32[2]);
913#endif
914}
915
917{
918#if defined(JPH_USE_SSE)
919 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(3, 3, 3, 3));
920#elif defined(JPH_USE_NEON)
921 return vdupq_laneq_f32(mValue, 3);
922#elif defined(JPH_USE_RVV)
923 Vec3 vec;
924 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[3], 3);
925 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
926 return vec;
927#else
928 return Vec3(mF32[3], mF32[3], mF32[3]);
929#endif
930}
931
933{
934 // Get the minimum value in all 4 components
936 value = Vec4::sMin(value, value.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y>());
937
938 // Compare with the original vector to find which component is equal to the minimum value
939 return CountTrailingZeros(Vec4::sEquals(*this, value).GetTrues());
940}
941
943{
944 // Get the maximum value in all 4 components
946 value = Vec4::sMax(value, value.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y>());
947
948 // Compare with the original vector to find which component is equal to the maximum value
949 return CountTrailingZeros(Vec4::sEquals(*this, value).GetTrues());
950}
951
953{
954#if defined(JPH_USE_AVX512)
955 return _mm_range_ps(mValue, mValue, 0b1000);
956#elif defined(JPH_USE_SSE)
957 return _mm_max_ps(_mm_sub_ps(_mm_setzero_ps(), mValue), mValue);
958#elif defined(JPH_USE_NEON)
959 return vabsq_f32(mValue);
960#elif defined(JPH_USE_RVV)
961 Vec4 res;
962 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
963 const vfloat32m1_t rvv_abs = __riscv_vfsgnj_vf_f32m1(v, 1.0, 4);
964 __riscv_vse32_v_f32m1(res.mF32, rvv_abs, 4);
965 return res;
966#else
967 return Vec4(abs(mF32[0]), abs(mF32[1]), abs(mF32[2]), abs(mF32[3]));
968#endif
969}
970
972{
973 return sOne() / mValue;
974}
975
977{
978#ifdef JPH_USE_FMADD
979 Vec4 cd = inC * inD;
980 Vec4 err = Vec4::sFusedMultiplyAdd(-inC, inD, cd);
981 Vec4 dop = Vec4::sFusedMultiplyAdd(inA, inB, -cd);
982 return dop + err;
983#else
984 return inA * inB - inC * inD;
985#endif
986}
987
988float Vec4::ReduceSum() const
989{
990#if defined(JPH_USE_SSE4_1)
991 Type shuf = _mm_movehdup_ps(mValue); // [y, y, w, w]
992 Type sums = _mm_add_ps(mValue, shuf); // [x + y, y + y, z + w, w + w]
993 shuf = _mm_movehl_ps(sums, sums); // [z + w, w + w, z + w, w + w]
994 sums = _mm_add_ps(sums, shuf); // [(x + y) + (z + w), ...]
995 return _mm_cvtss_f32(sums);
996#elif defined(JPH_USE_NEON)
997 return vaddvq_f32(mValue);
998#elif defined(JPH_USE_RVV)
999 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1000 return __riscv_vfmv_f_s_f32m1_f32(RVVSumElementsFloat32x4(v));
1001#else
1002 // Brackets placed so that the order is consistent with the vectorized version
1003 return (mF32[0] + mF32[1]) + (mF32[2] + mF32[3]);
1004#endif
1005}
1006
1007float Vec4::Dot(Vec4Arg inV2) const
1008{
1009 return (*this * inV2).ReduceSum();
1010}
1011
1013{
1014 return Vec4::sReplicate(Dot(inV2));
1015}
1016
1017float Vec4::LengthSq() const
1018{
1019 return Dot(*this);
1020}
1021
1022float Vec4::Length() const
1023{
1024 return JPH::Sqrt(LengthSq());
1025}
1026
1028{
1029 return *this / Length();
1030}
1031
1033{
1034#if defined(JPH_USE_SSE)
1035 return _mm_sqrt_ps(mValue);
1036#elif defined(JPH_USE_NEON)
1037 return vsqrtq_f32(mValue);
1038#elif defined(JPH_USE_RVV)
1039 Vec4 res;
1040 const vfloat32m1_t rvv_v = __riscv_vle32_v_f32m1(mF32, 4);
1041 const vfloat32m1_t rvv_sqrt = __riscv_vfsqrt_v_f32m1(rvv_v, 4);
1042 __riscv_vse32_v_f32m1(res.mF32, rvv_sqrt, 4);
1043 return res;
1044#else
1045 return Vec4(JPH::Sqrt(mF32[0]), JPH::Sqrt(mF32[1]), JPH::Sqrt(mF32[2]), JPH::Sqrt(mF32[3]));
1046#endif
1047}
1048
1050{
1051#if defined(JPH_USE_AVX512)
1052 Type one = _mm_set1_ps(1.0f);
1053 return _mm_or_ps(_mm_fixupimm_ps(mValue, mValue, _mm_set1_epi32(0xA9A90100), 0), one);
1054#elif defined(JPH_USE_SSE)
1055 Type minus_one = _mm_set1_ps(-1.0f);
1056 Type one = _mm_set1_ps(1.0f);
1057 return _mm_or_ps(_mm_and_ps(mValue, minus_one), one);
1058#elif defined(JPH_USE_NEON)
1059 Type minus_one = vdupq_n_f32(-1.0f);
1060 Type one = vdupq_n_f32(1.0f);
1061 return vreinterpretq_f32_u32(vorrq_u32(vandq_u32(vreinterpretq_u32_f32(mValue), vreinterpretq_u32_f32(minus_one)), vreinterpretq_u32_f32(one)));
1062#elif defined(JPH_USE_RVV)
1063 Vec4 res;
1064 const vfloat32m1_t rvv_in = __riscv_vle32_v_f32m1(mF32, 4);
1065 const vfloat32m1_t rvv_one = __riscv_vfmv_v_f_f32m1(1.0, 4);
1066 const vfloat32m1_t rvv_signs = __riscv_vfsgnj_vv_f32m1(rvv_one, rvv_in, 4);
1067 __riscv_vse32_v_f32m1(res.mF32, rvv_signs, 4);
1068 return res;
1069#else
1070 return Vec4(std::signbit(mF32[0])? -1.0f : 1.0f,
1071 std::signbit(mF32[1])? -1.0f : 1.0f,
1072 std::signbit(mF32[2])? -1.0f : 1.0f,
1073 std::signbit(mF32[3])? -1.0f : 1.0f);
1074#endif
1075}
1076
1077template <int X, int Y, int Z, int W>
1078JPH_INLINE Vec4 Vec4::FlipSign() const
1079{
1080 static_assert(X == 1 || X == -1, "X must be 1 or -1");
1081 static_assert(Y == 1 || Y == -1, "Y must be 1 or -1");
1082 static_assert(Z == 1 || Z == -1, "Z must be 1 or -1");
1083 static_assert(W == 1 || W == -1, "W must be 1 or -1");
1084 return Vec4::sXor(*this, Vec4(X > 0? 0.0f : -0.0f, Y > 0? 0.0f : -0.0f, Z > 0? 0.0f : -0.0f, W > 0? 0.0f : -0.0f));
1085}
1086
1087void Vec4::StoreFloat4(Float4 *outV) const
1088{
1089#if defined(JPH_USE_SSE)
1090 _mm_storeu_ps(&outV->x, mValue);
1091#elif defined(JPH_USE_NEON)
1092 vst1q_f32(&outV->x, mValue);
1093#elif defined(JPH_USE_RVV)
1094 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1095 __riscv_vse32_v_f32m1(&outV->x, v, 4);
1096#else
1097 for (int i = 0; i < 4; ++i)
1098 (&outV->x)[i] = mF32[i];
1099#endif
1100}
1101
1103{
1104#if defined(JPH_USE_SSE)
1105 return _mm_cvttps_epi32(mValue);
1106#elif defined(JPH_USE_NEON)
1107 return vcvtq_u32_f32(mValue);
1108#elif defined(JPH_USE_RVV)
1109 UVec4 res;
1110 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1111 const vuint32m1_t cast = __riscv_vfcvt_rtz_xu_f_v_u32m1(v, 4);
1112 __riscv_vse32_v_u32m1(res.mU32, cast, 4);
1113 return res;
1114#else
1115 return UVec4(uint32(mF32[0]), uint32(mF32[1]), uint32(mF32[2]), uint32(mF32[3]));
1116#endif
1117}
1118
1120{
1121#if defined(JPH_USE_SSE)
1122 return UVec4(_mm_castps_si128(mValue));
1123#elif defined(JPH_USE_NEON)
1124 return vreinterpretq_u32_f32(mValue);
1125#else
1126 return *reinterpret_cast<const UVec4 *>(this);
1127#endif
1128}
1129
1131{
1132#if defined(JPH_USE_SSE)
1133 return _mm_movemask_ps(mValue);
1134#elif defined(JPH_USE_NEON)
1135 int32x4_t shift = JPH_NEON_INT32x4(0, 1, 2, 3);
1136 return vaddvq_u32(vshlq_u32(vshrq_n_u32(vreinterpretq_u32_f32(mValue), 31), shift));
1137#elif defined(JPH_USE_RVV)
1138 const vuint32m1_t v = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(mF32), 4);
1139 const vuint32m1_t shifted = __riscv_vsrl_vx_u32m1(v, 31, 4);
1140 const vbool32_t mask = __riscv_vmsne_vx_u32m1_b32(shifted, 0x0, 4);
1141 const vuint32m1_t as_int = __riscv_vreinterpret_v_b32_u32m1(mask);
1142 const uint32 result = __riscv_vmv_x_s_u32m1_u32(as_int) & 0xF;
1143 return result;
1144#else
1145 return (std::signbit(mF32[0])? 1 : 0) | (std::signbit(mF32[1])? 2 : 0) | (std::signbit(mF32[2])? 4 : 0) | (std::signbit(mF32[3])? 8 : 0);
1146#endif
1147}
1148
1149float Vec4::ReduceMin() const
1150{
1151#ifdef JPH_USE_NEON
1152 return vminvq_f32(mValue);
1153#else
1156 return v.GetX();
1157#endif
1158}
1159
1160float Vec4::ReduceMax() const
1161{
1162#ifdef JPH_USE_NEON
1163 return vmaxvq_f32(mValue);
1164#else
1167 return v.GetX();
1168#endif
1169}
1170
1171void Vec4::SinCos(Vec4 &outSin, Vec4 &outCos) const
1172{
1173 // Implementation based on sinf.c from the cephes library, combines sinf and cosf in a single function, changes octants to quadrants and vectorizes it
1174 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1175
1176 // Make argument positive and remember sign for sin only since cos is symmetric around x (highest bit of a float is the sign bit)
1177 UVec4 sin_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1178 Vec4 x = Vec4::sXor(*this, sin_sign.ReinterpretAsFloat());
1179
1180 // x / (PI / 2) rounded to nearest int gives us the quadrant closest to x
1181 UVec4 quadrant = (0.6366197723675814f * x + Vec4::sReplicate(0.5f)).ToInt();
1182
1183 // Make x relative to the closest quadrant.
1184 // This does x = x - quadrant * PI / 2 using a two step Cody-Waite argument reduction.
1185 // This improves the accuracy of the result by avoiding loss of significant bits in the subtraction.
1186 // We start with x = x - quadrant * PI / 2, PI / 2 in hexadecimal notation is 0x3fc90fdb, we remove the lowest 16 bits to
1187 // get 0x3fc90000 (= 1.5703125) this means we can now multiply with a number of up to 2^16 without losing any bits.
1188 // This leaves us with: x = (x - quadrant * 1.5703125) - quadrant * (PI / 2 - 1.5703125).
1189 // PI / 2 - 1.5703125 in hexadecimal is 0x39fdaa22, stripping the lowest 12 bits we get 0x39fda000 (= 0.0004837512969970703125)
1190 // This leaves uw with: x = ((x - quadrant * 1.5703125) - quadrant * 0.0004837512969970703125) - quadrant * (PI / 2 - 1.5703125 - 0.0004837512969970703125)
1191 // See: https://stackoverflow.com/questions/42455143/sine-cosine-modular-extended-precision-arithmetic
1192 // After this we have x in the range [-PI / 4, PI / 4].
1193 Vec4 float_quadrant = quadrant.ToFloat();
1194 x = ((x - float_quadrant * 1.5703125f) - float_quadrant * 0.0004837512969970703125f) - float_quadrant * 7.549789948768648e-8f;
1195
1196 // Calculate x2 = x^2
1197 Vec4 x2 = x * x;
1198
1199 // Taylor expansion:
1200 // Cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! + ... = (((x2/8!- 1/6!) * x2 + 1/4!) * x2 - 1/2!) * x2 + 1
1201 Vec4 taylor_cos = ((2.443315711809948e-5f * x2 - Vec4::sReplicate(1.388731625493765e-3f)) * x2 + Vec4::sReplicate(4.166664568298827e-2f)) * x2 * x2 - 0.5f * x2 + Vec4::sOne();
1202 // Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... = ((-x2/7! + 1/5!) * x2 - 1/3!) * x2 * x + x
1203 Vec4 taylor_sin = ((-1.9515295891e-4f * x2 + Vec4::sReplicate(8.3321608736e-3f)) * x2 - Vec4::sReplicate(1.6666654611e-1f)) * x2 * x + x;
1204
1205 // The lowest 2 bits of quadrant indicate the quadrant that we are in.
1206 // Let x be the original input value and x' our value that has been mapped to the range [-PI / 4, PI / 4].
1207 // since cos(x) = sin(x - PI / 2) and since we want to use the Taylor expansion as close as possible to 0,
1208 // we can alternate between using the Taylor expansion for sin and cos according to the following table:
1209 //
1210 // quadrant sin(x) cos(x)
1211 // XXX00b sin(x') cos(x')
1212 // XXX01b cos(x') -sin(x')
1213 // XXX10b -sin(x') -cos(x')
1214 // XXX11b -cos(x') sin(x')
1215 //
1216 // So: sin_sign = bit2, cos_sign = bit1 ^ bit2, bit1 determines if we use sin or cos Taylor expansion
1217 UVec4 bit1 = quadrant.LogicalShiftLeft<31>();
1218 UVec4 bit2 = UVec4::sAnd(quadrant.LogicalShiftLeft<30>(), UVec4::sReplicate(0x80000000U));
1219
1220 // Select which one of the results is sin and which one is cos
1221 Vec4 s = Vec4::sSelect(taylor_sin, taylor_cos, bit1);
1222 Vec4 c = Vec4::sSelect(taylor_cos, taylor_sin, bit1);
1223
1224 // Update the signs
1225 sin_sign = UVec4::sXor(sin_sign, bit2);
1226 UVec4 cos_sign = UVec4::sXor(bit1, bit2);
1227
1228 // Correct the signs
1229 outSin = Vec4::sXor(s, sin_sign.ReinterpretAsFloat());
1230 outCos = Vec4::sXor(c, cos_sign.ReinterpretAsFloat());
1231}
1232
1234{
1235 // Implementation based on tanf.c from the cephes library, see Vec4::SinCos for further details
1236 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1237
1238 // Make argument positive
1239 UVec4 tan_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1240 Vec4 x = Vec4::sXor(*this, tan_sign.ReinterpretAsFloat());
1241
1242 // x / (PI / 2) rounded to nearest int gives us the quadrant closest to x
1243 UVec4 quadrant = (0.6366197723675814f * x + Vec4::sReplicate(0.5f)).ToInt();
1244
1245 // Remap x to range [-PI / 4, PI / 4], see Vec4::SinCos
1246 Vec4 float_quadrant = quadrant.ToFloat();
1247 x = ((x - float_quadrant * 1.5703125f) - float_quadrant * 0.0004837512969970703125f) - float_quadrant * 7.549789948768648e-8f;
1248
1249 // Calculate x2 = x^2
1250 Vec4 x2 = x * x;
1251
1252 // Roughly equivalent to the Taylor expansion:
1253 // Tan(x) = x + x^3/3 + 2*x^5/15 + 17*x^7/315 + 62*x^9/2835 + ...
1254 Vec4 tan =
1255 (((((9.38540185543e-3f * x2 + Vec4::sReplicate(3.11992232697e-3f)) * x2 + Vec4::sReplicate(2.44301354525e-2f)) * x2
1256 + Vec4::sReplicate(5.34112807005e-2f)) * x2 + Vec4::sReplicate(1.33387994085e-1f)) * x2 + Vec4::sReplicate(3.33331568548e-1f)) * x2 * x + x;
1257
1258 // For the 2nd and 4th quadrant we need to invert the value
1259 UVec4 bit1 = quadrant.LogicalShiftLeft<31>();
1260 tan = Vec4::sSelect(tan, Vec4::sReplicate(-1.0f) / (tan JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(+ Vec4::sReplicate(FLT_MIN))), bit1); // Add small epsilon to prevent div by zero, works because tan is always positive
1261
1262 // Put the sign back
1263 return Vec4::sXor(tan, tan_sign.ReinterpretAsFloat());
1264}
1265
1267{
1268 // Implementation based on asinf.c from the cephes library
1269 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1270
1271 // Make argument positive
1272 UVec4 asin_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1273 Vec4 a = Vec4::sXor(*this, asin_sign.ReinterpretAsFloat());
1274
1275 // ASin is not defined outside the range [-1, 1] but it often happens that a value is slightly above 1 so we just clamp here
1276 a = Vec4::sMin(a, Vec4::sOne());
1277
1278 // When |x| <= 0.5 we use the asin approximation as is
1279 Vec4 z1 = a * a;
1280 Vec4 x1 = a;
1281
1282 // When |x| > 0.5 we use the identity asin(x) = PI / 2 - 2 * asin(sqrt((1 - x) / 2))
1283 Vec4 z2 = 0.5f * (Vec4::sOne() - a);
1284 Vec4 x2 = z2.Sqrt();
1285
1286 // Select which of the two situations we have
1287 UVec4 greater = Vec4::sGreater(a, Vec4::sReplicate(0.5f));
1288 Vec4 z = Vec4::sSelect(z1, z2, greater);
1289 Vec4 x = Vec4::sSelect(x1, x2, greater);
1290
1291 // Polynomial approximation of asin
1292 z = ((((4.2163199048e-2f * z + Vec4::sReplicate(2.4181311049e-2f)) * z + Vec4::sReplicate(4.5470025998e-2f)) * z + Vec4::sReplicate(7.4953002686e-2f)) * z + Vec4::sReplicate(1.6666752422e-1f)) * z * x + x;
1293
1294 // If |x| > 0.5 we need to apply the remainder of the identity above
1295 z = Vec4::sSelect(z, Vec4::sReplicate(0.5f * JPH_PI) - (z + z), greater);
1296
1297 // Put the sign back
1298 return Vec4::sXor(z, asin_sign.ReinterpretAsFloat());
1299}
1300
1302{
1303 // Not the most accurate, but simple
1304 return Vec4::sReplicate(0.5f * JPH_PI) - ASin();
1305}
1306
1308{
1309 // Implementation based on atanf.c from the cephes library
1310 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1311
1312 // Make argument positive
1313 UVec4 atan_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1314 Vec4 x = Vec4::sXor(*this, atan_sign.ReinterpretAsFloat());
1315 Vec4 y = Vec4::sZero();
1316
1317 // If x > Tan(PI / 8)
1318 UVec4 greater1 = Vec4::sGreater(x, Vec4::sReplicate(0.4142135623730950f));
1319 Vec4 x1 = (x - Vec4::sOne()) / (x + Vec4::sOne());
1320
1321 // If x > Tan(3 * PI / 8)
1322 UVec4 greater2 = Vec4::sGreater(x, Vec4::sReplicate(2.414213562373095f));
1323 Vec4 x2 = Vec4::sReplicate(-1.0f) / (x JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(+ Vec4::sReplicate(FLT_MIN))); // Add small epsilon to prevent div by zero, works because x is always positive
1324
1325 // Apply first if
1326 x = Vec4::sSelect(x, x1, greater1);
1327 y = Vec4::sSelect(y, Vec4::sReplicate(0.25f * JPH_PI), greater1);
1328
1329 // Apply second if
1330 x = Vec4::sSelect(x, x2, greater2);
1331 y = Vec4::sSelect(y, Vec4::sReplicate(0.5f * JPH_PI), greater2);
1332
1333 // Polynomial approximation
1334 Vec4 z = x * x;
1335 y += (((8.05374449538e-2f * z - Vec4::sReplicate(1.38776856032e-1f)) * z + Vec4::sReplicate(1.99777106478e-1f)) * z - Vec4::sReplicate(3.33329491539e-1f)) * z * x + x;
1336
1337 // Put the sign back
1338 return Vec4::sXor(y, atan_sign.ReinterpretAsFloat());
1339}
1340
1342{
1343 UVec4 sign_mask = UVec4::sReplicate(0x80000000U);
1344
1345 // Determine absolute value and sign of y
1346 UVec4 y_sign = UVec4::sAnd(inY.ReinterpretAsInt(), sign_mask);
1347 Vec4 y_abs = Vec4::sXor(inY, y_sign.ReinterpretAsFloat());
1348
1349 // Determine absolute value and sign of x
1350 UVec4 x_sign = UVec4::sAnd(inX.ReinterpretAsInt(), sign_mask);
1351 Vec4 x_abs = Vec4::sXor(inX, x_sign.ReinterpretAsFloat());
1352
1353 // Always divide smallest / largest to avoid dividing by zero
1354 UVec4 x_is_numerator = Vec4::sLess(x_abs, y_abs);
1355 Vec4 numerator = Vec4::sSelect(y_abs, x_abs, x_is_numerator);
1356 Vec4 denominator = Vec4::sSelect(x_abs, y_abs, x_is_numerator);
1357 Vec4 atan = (numerator / denominator).ATan();
1358
1359 // If we calculated x / y instead of y / x the result is PI / 2 - result (note that this is true because we know the result is positive because the input was positive)
1360 atan = Vec4::sSelect(atan, Vec4::sReplicate(0.5f * JPH_PI) - atan, x_is_numerator);
1361
1362 // Now we need to map to the correct quadrant
1363 // x_sign y_sign result
1364 // +1 +1 atan
1365 // -1 +1 -atan + PI
1366 // -1 -1 atan - PI
1367 // +1 -1 -atan
1368 // This can be written as: x_sign * y_sign * (atan - (x_sign < 0? PI : 0))
1369 atan -= Vec4::sAnd(x_sign.ArithmeticShiftRight<31>().ReinterpretAsFloat(), Vec4::sReplicate(JPH_PI));
1370 atan = Vec4::sXor(atan, UVec4::sXor(x_sign, y_sign).ReinterpretAsFloat());
1371 return atan;
1372}
1373
1375{
1376 constexpr float cOneOverSqrt2 = 0.70710678f;
1377 constexpr uint cNumBits = 9;
1378 constexpr uint cMask = (1 << cNumBits) - 1;
1379 constexpr uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
1380 constexpr float cScale = float(cMaxValue) / (2.0f * cOneOverSqrt2);
1381
1382 // Store sign bit
1383 Vec4 v = *this;
1384 uint32 max_element = v.Abs().GetHighestComponentIndex();
1385 uint32 value = 0;
1386 if (v[max_element] < 0.0f)
1387 {
1388 value = 0x80000000u;
1389 v = -v;
1390 }
1391
1392 // Store highest component
1393 value |= max_element << 29;
1394
1395 // Store the other three components in a compressed format
1396 UVec4 compressed = Vec4::sClamp((v + Vec4::sReplicate(cOneOverSqrt2)) * cScale + Vec4::sReplicate(0.5f), Vec4::sZero(), Vec4::sReplicate(cMaxValue)).ToInt();
1397 switch (max_element)
1398 {
1399 case 0:
1400 compressed = compressed.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>();
1401 break;
1402
1403 case 1:
1404 compressed = compressed.Swizzle<SWIZZLE_X, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>();
1405 break;
1406
1407 case 2:
1408 compressed = compressed.Swizzle<SWIZZLE_X, SWIZZLE_Y, SWIZZLE_W, SWIZZLE_UNUSED>();
1409 break;
1410 }
1411
1412 value |= compressed.GetX();
1413 value |= compressed.GetY() << cNumBits;
1414 value |= compressed.GetZ() << 2 * cNumBits;
1415 return value;
1416}
1417
1419{
1420 constexpr float cOneOverSqrt2 = 0.70710678f;
1421 constexpr uint cNumBits = 9;
1422 constexpr uint cMask = (1u << cNumBits) - 1;
1423 constexpr uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
1424 constexpr int cHalfMaxValue = int(cMaxValue >> 1);
1425 constexpr float cScale = 2.0f * cOneOverSqrt2 / float(cMaxValue);
1426
1427 // Restore three components
1428 Vec4 v = Vec4(float(int(inValue & cMask) - cHalfMaxValue), float(int((inValue >> cNumBits) & cMask) - cHalfMaxValue), float(int(inValue >> (2 * cNumBits)) & cMask) - cHalfMaxValue, 0) * cScale;
1429 JPH_ASSERT(v.GetW() == 0.0f);
1430
1431 // Restore the highest component
1432 v.SetW(JPH::Sqrt(max(1.0f - v.LengthSq(), 0.0f)));
1433
1434 // Extract sign
1435 if ((inValue & 0x80000000u) != 0)
1436 v = -v;
1437
1438 // Swizzle the components in place
1439 switch ((inValue >> 29) & 3)
1440 {
1441 case 0:
1443 break;
1444
1445 case 1:
1447 break;
1448
1449 case 2:
1451 break;
1452 }
1453
1454 return v;
1455}
1456
std::uint8_t uint8
Definition Core.h:511
std::uint64_t uint64
Definition Core.h:515
unsigned int uint
Definition Core.h:510
#define JPH_NAMESPACE_END
Definition Core.h:434
std::uint32_t uint32
Definition Core.h:513
#define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
Definition Core.h:585
#define JPH_NAMESPACE_BEGIN
Definition Core.h:428
#define xy
Definition HLSLToCPP.h:511
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr To BitCast(const From &inValue)
Simple implementation of C++20 std::bit_cast.
Definition Math.h:239
uint CountTrailingZeros(uint32 inValue)
Compute number of trailing zero bits (how many low bits are zero)
Definition Math.h:145
@ SWIZZLE_Z
Use the Z component.
Definition Swizzle.h:14
@ SWIZZLE_W
Use the W component.
Definition Swizzle.h:15
@ SWIZZLE_X
Use the X component.
Definition Swizzle.h:12
@ SWIZZLE_UNUSED
We always use the Z component when we don't specifically want to initialize a value,...
Definition Swizzle.h:16
@ SWIZZLE_Y
Use the Y component.
Definition Swizzle.h:13
Vec4 operator*(float inV1, Vec4Arg inV2)
Multiply vector with float.
Definition Vec4.inl:582
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition Float4.h:11
float x
Definition Float4.h:36
float y
Definition Float4.h:37
float z
Definition Float4.h:38
float w
Definition Float4.h:39
Definition UVec4.h:12
JPH_INLINE UVec4 Swizzle() const
Swizzle the elements in inV.
JPH_INLINE uint32 GetZ() const
Definition UVec4.h:104
JPH_INLINE UVec4 LogicalShiftLeft() const
Shift all components by Count bits to the left (filling with zeros from the left)
static JPH_INLINE UVec4 sSelect(UVec4Arg inNotSet, UVec4Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition UVec4.inl:221
JPH_INLINE uint32 GetY() const
Definition UVec4.h:103
static JPH_INLINE UVec4 sReplicate(uint32 inV)
Replicate int inV across all components.
Definition UVec4.inl:75
JPH_INLINE bool TestAllTrue() const
Test if all components are true (true is when highest bit of component is set)
Definition UVec4.inl:658
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition UVec4.inl:292
static JPH_INLINE UVec4 sOr(UVec4Arg inV1, UVec4Arg inV2)
Logical or (component wise)
Definition UVec4.inl:250
JPH_INLINE uint32 GetW() const
Definition UVec4.h:105
Type mValue
Definition UVec4.h:223
JPH_INLINE uint32 GetX() const
Get individual components.
Definition UVec4.h:102
static JPH_INLINE UVec4 sXor(UVec4Arg inV1, UVec4Arg inV2)
Logical xor (component wise)
Definition UVec4.inl:271
JPH_INLINE UVec4 ArithmeticShiftRight() const
Shift all components by Count bits to the right (shifting in the value of the highest bit)
JPH_INLINE Vec4 ToFloat() const
Convert each component from an int to a float.
Definition UVec4.inl:510
JPH_INLINE Vec4 ReinterpretAsFloat() const
Reinterpret UVec4 as a Vec4 (doesn't change the bits)
Definition UVec4.inl:527
uint32 mU32[4]
Definition UVec4.h:224
Definition Vec3.h:17
Type mValue
Definition Vec3.h:308
float mF32[4]
Definition Vec3.h:309
Definition Vec4.h:14
JPH_INLINE bool IsNearZero(float inMaxDistSq=1.0e-12f) const
Test if vector is near zero.
Definition Vec4.inl:514
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec4.inl:804
static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex)
Definition Vec4.inl:456
Vec4 ATan() const
Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2,...
Definition Vec4.inl:1307
static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2)
Greater than (component wise)
Definition Vec4.inl:304
float mF32[4]
Definition Vec4.h:318
JPH_INLINE Vec3 SplatW3() const
Replicate the W component to all components.
Definition Vec4.inl:916
JPH_INLINE Vec4 operator-() const
Negate.
Definition Vec4.inl:710
Vec4()=default
Constructor.
static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2)
Logical and (component wise)
Definition Vec4.inl:438
static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV)
Load 4 floats from memory, 16 bytes aligned.
Definition Vec4.inl:139
static Vec4 sATan2(Vec4Arg inY, Vec4Arg inX)
Calculate the arc tangent of y / x using the signs of the arguments to determine the correct quadrant...
Definition Vec4.inl:1341
JPH_INLINE void SetW(float inW)
Definition Vec4.h:129
JPH_INLINE Vec4 GetSign() const
Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
Definition Vec4.inl:1049
static JPH_INLINE Vec4 sDifferenceOfProducts(Vec4Arg inA, Vec4Arg inB, Vec4Arg inC, Vec4Arg inD)
Calculates inA * inB - inC * inD with more precision when FMA instructions are available....
Definition Vec4.inl:976
Vec4 ASin() const
Definition Vec4.inl:1266
JPH_INLINE Vec4 FlipSign() const
Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z c...
Definition Vec4.inl:1078
static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2)
Logical xor (component wise)
Definition Vec4.inl:420
JPH_INLINE Vec4 Abs() const
Return the absolute value of each of the components.
Definition Vec4.inl:952
JPH_INLINE Vec4 operator/(float inV2) const
Divide vector by float.
Definition Vec4.inl:602
Vec4 Tan() const
Calculate the tangent for each element of this vector (input in radians)
Definition Vec4.inl:1233
JPH_INLINE float GetW() const
Definition Vec4.h:122
JPH_INLINE UVec4 ToInt() const
Convert each component from a float to an int.
Definition Vec4.inl:1102
JPH_INLINE Vec4 & operator+=(Vec4Arg inV2)
Add two float vectors (component wise)
Definition Vec4.inl:692
static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Less than or equal (component wise)
Definition Vec4.inl:281
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2)
Less than (component wise)
Definition Vec4.inl:258
JPH_INLINE int GetLowestComponentIndex() const
Get index of component with lowest value.
Definition Vec4.inl:932
JPH_INLINE float Length() const
Length of vector.
Definition Vec4.inl:1022
static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex)
Definition Vec4.inl:480
static JPH_INLINE Vec4 sOne()
Vector with all ones.
Definition Vec4.inl:113
static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd)
Calculates inMul1 * inMul2 + inAdd.
Definition Vec4.inl:350
JPH_INLINE Vec4 Normalized() const
Normalize vector.
Definition Vec4.inl:1027
static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2)
Equals (component wise)
Definition Vec4.inl:235
JPH_INLINE float ReduceMax() const
Get the maximum of X, Y, Z and W.
Definition Vec4.inl:1160
JPH_INLINE Vec4 Reciprocal() const
Reciprocal vector (1 / value) for each of the components.
Definition Vec4.inl:971
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec4.inl:820
JPH_INLINE UVec4 ReinterpretAsInt() const
Reinterpret Vec4 as a UVec4 (doesn't change the bits)
Definition Vec4.inl:1119
static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Greater than or equal (component wise)
Definition Vec4.inl:327
JPH_INLINE float ReduceSum() const
Sum X, Y, Z and W.
Definition Vec4.inl:988
static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2)
Return the minimum value of each of the components.
Definition Vec4.inl:188
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec4.inl:836
JPH_INLINE Vec4 Sqrt() const
Component wise square root.
Definition Vec4.inl:1032
JPH_INLINE Vec4 & operator*=(float inV2)
Multiply vector with float.
Definition Vec4.inl:619
static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets)
Gather 4 floats from memory at inBase + inOffsets[i] * Scale.
JPH_INLINE Vec4 operator+(Vec4Arg inV2) const
Add two float vectors (component wise)
Definition Vec4.inl:671
JPH_INLINE Vec4 & operator/=(float inV2)
Divide vector by float.
Definition Vec4.inl:654
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec4.inl:519
JPH_INLINE bool operator==(Vec4Arg inV2) const
Comparison.
Definition Vec4.inl:504
JPH_INLINE Vec4 SplatW() const
Replicate the W component to all components.
Definition Vec4.inl:852
JPH_INLINE Vec4 DotV(Vec4Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition Vec4.inl:1012
JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq=1.0e-12f) const
Test if two vectors are close.
Definition Vec4.inl:509
JPH_INLINE float GetX() const
Get individual components.
Definition Vec4.h:119
static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV)
Load 4 floats from memory.
Definition Vec4.inl:123
static JPH_INLINE Vec4 sZero()
Vector with all zeros.
Definition Vec4.inl:81
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.
struct { float mData[4];} Type
Definition Vec4.h:24
static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2)
Logical or (component wise)
Definition Vec4.inl:402
JPH_INLINE float ReduceMin() const
Get the minimum of X, Y, Z and W.
Definition Vec4.inl:1149
Type mValue
Definition Vec4.h:317
static JPH_INLINE Vec4 sDecompressUnitVector(uint32 inValue)
Decompress a unit vector from a 32 bit value.
Definition Vec4.inl:1418
JPH_INLINE uint32 CompressUnitVector() const
Compress a unit vector to a 32 bit value, precision is around 0.5 * 10^-3.
Definition Vec4.inl:1374
JPH_INLINE Vec4 & operator-=(Vec4Arg inV2)
Subtract two float vectors (component wise)
Definition Vec4.inl:765
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec4.inl:1017
static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2)
Return the maximum of each of the components.
Definition Vec4.inl:209
JPH_INLINE float Dot(Vec4Arg inV2) const
Dot product.
Definition Vec4.inl:1007
JPH_INLINE Vec3 SplatZ3() const
Replicate the Z component to all components.
Definition Vec4.inl:900
JPH_INLINE bool IsNaN() const
Test if vector contains NaN elements.
Definition Vec4.inl:524
JPH_INLINE Vec3 SplatX3() const
Replicate the X component to all components.
Definition Vec4.inl:868
static JPH_INLINE Vec4 sNaN()
Vector with all NaN's.
Definition Vec4.inl:118
Vec4 ACos() const
Definition Vec4.inl:1301
static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec4.inl:373
JPH_INLINE int GetSignBits() const
Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3.
Definition Vec4.inl:1130
JPH_INLINE int GetHighestComponentIndex() const
Get index of component with highest value.
Definition Vec4.inl:942
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition Vec4.inl:97
JPH_INLINE Vec3 SplatY3() const
Replicate the Y component to all components.
Definition Vec4.inl:884
void SinCos(Vec4 &outSin, Vec4 &outCos) const
Calculate the sine and cosine for each element of this vector (input in radians)
Definition Vec4.inl:1171
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store 4 floats to memory.
Definition Vec4.inl:1087
static JPH_INLINE Vec4 sClamp(Vec4Arg inV, Vec4Arg inMin, Vec4Arg inMax)
Clamp a vector between min and max (component wise)
Definition Vec4.inl:230
friend JPH_INLINE Vec4 operator*(float inV1, Vec4Arg inV2)
Multiply vector with float.
Definition Vec4.inl:582