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