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#if defined(JPH_USE_SSE)
353 #ifdef JPH_USE_FMADD
354 return _mm_fmadd_ps(inMul1.mValue, inMul2.mValue, inAdd.mValue);
355 #else
356 return _mm_add_ps(_mm_mul_ps(inMul1.mValue, inMul2.mValue), inAdd.mValue);
357 #endif
358#elif defined(JPH_USE_NEON)
359 return vmlaq_f32(inAdd.mValue, inMul1.mValue, inMul2.mValue);
360#elif defined(JPH_USE_RVV)
361 Vec4 res;
362 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inMul1.mF32, 4);
363 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inMul2.mF32, 4);
364 const vfloat32m1_t rvv_add = __riscv_vle32_v_f32m1(inAdd.mF32, 4);
365 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v1, v2, 4);
366 const vfloat32m1_t fmadd = __riscv_vfadd_vv_f32m1(rvv_add, mul, 4);
367 __riscv_vse32_v_f32m1(res.mF32, fmadd, 4);
368 return res;
369#else
370 return Vec4(inMul1.mF32[0] * inMul2.mF32[0] + inAdd.mF32[0],
371 inMul1.mF32[1] * inMul2.mF32[1] + inAdd.mF32[1],
372 inMul1.mF32[2] * inMul2.mF32[2] + inAdd.mF32[2],
373 inMul1.mF32[3] * inMul2.mF32[3] + inAdd.mF32[3]);
374#endif
375}
376
377Vec4 Vec4::sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
378{
379#if defined(JPH_USE_SSE4_1) && !defined(JPH_PLATFORM_WASM) // _mm_blendv_ps has problems on FireFox
380 return _mm_blendv_ps(inNotSet.mValue, inSet.mValue, _mm_castsi128_ps(inControl.mValue));
381#elif defined(JPH_USE_SSE)
382 __m128 is_set = _mm_castsi128_ps(_mm_srai_epi32(inControl.mValue, 31));
383 return _mm_or_ps(_mm_and_ps(is_set, inSet.mValue), _mm_andnot_ps(is_set, inNotSet.mValue));
384#elif defined(JPH_USE_NEON)
385 return vbslq_f32(vreinterpretq_u32_s32(vshrq_n_s32(vreinterpretq_s32_u32(inControl.mValue), 31)), inSet.mValue, inNotSet.mValue);
386#elif defined(JPH_USE_RVV)
387 Vec4 masked;
388 const vuint32m1_t control = __riscv_vle32_v_u32m1(inControl.mU32, 4);
389 const vfloat32m1_t not_set = __riscv_vle32_v_f32m1(inNotSet.mF32, 4);
390 const vfloat32m1_t set = __riscv_vle32_v_f32m1(inSet.mF32, 4);
391
392 // Generate RVV bool mask from UVec4
393 const vuint32m1_t r = __riscv_vand_vx_u32m1(control, 0x80000000u, 4);
394 const vbool32_t rvv_mask = __riscv_vmsne_vx_u32m1_b32(r, 0x0, 4);
395 const vfloat32m1_t merged = __riscv_vmerge_vvm_f32m1(not_set, set, rvv_mask, 4);
396 __riscv_vse32_v_f32m1(masked.mF32, merged, 4);
397 return masked;
398#else
399 Vec4 result;
400 for (int i = 0; i < 4; i++)
401 result.mF32[i] = (inControl.mU32[i] & 0x80000000u) ? inSet.mF32[i] : inNotSet.mF32[i];
402 return result;
403#endif
404}
405
407{
408#if defined(JPH_USE_SSE)
409 return _mm_or_ps(inV1.mValue, inV2.mValue);
410#elif defined(JPH_USE_NEON)
411 return vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
412#elif defined(JPH_USE_RVV)
413 Vec4 or_result;
414 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
415 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
416 const vuint32m1_t res = __riscv_vor_vv_u32m1(v1, v2, 4);
417 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(or_result.mF32), res, 4);
418 return or_result;
419#else
421#endif
422}
423
425{
426#if defined(JPH_USE_SSE)
427 return _mm_xor_ps(inV1.mValue, inV2.mValue);
428#elif defined(JPH_USE_NEON)
429 return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
430#elif defined(JPH_USE_RVV)
431 Vec4 xor_result;
432 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
433 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
434 const vuint32m1_t res = __riscv_vxor_vv_u32m1(v1, v2, 4);
435 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(xor_result.mF32), res, 4);
436 return xor_result;
437#else
439#endif
440}
441
443{
444#if defined(JPH_USE_SSE)
445 return _mm_and_ps(inV1.mValue, inV2.mValue);
446#elif defined(JPH_USE_NEON)
447 return vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(inV1.mValue), vreinterpretq_u32_f32(inV2.mValue)));
448#elif defined(JPH_USE_RVV)
449 Vec4 and_result;
450 const vuint32m1_t v1 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV1.mF32), 4);
451 const vuint32m1_t v2 = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(inV2.mF32), 4);
452 const vuint32m1_t res = __riscv_vand_vv_u32m1(v1, v2, 4);
453 __riscv_vse32_v_u32m1(reinterpret_cast<uint32 *>(and_result.mF32), res, 4);
454 return and_result;
455#else
457#endif
458}
459
460void Vec4::sSort4(Vec4 &ioValue, UVec4 &ioIndex)
461{
462 // Pass 1, test 1st vs 3rd, 2nd vs 4th
465 UVec4 c1 = sLess(ioValue, v1).Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W>();
466 ioValue = sSelect(ioValue, v1, c1);
467 ioIndex = UVec4::sSelect(ioIndex, i1, c1);
468
469 // Pass 2, test 1st vs 2nd, 3rd vs 4th
472 UVec4 c2 = sLess(ioValue, v2).Swizzle<SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_W, SWIZZLE_W>();
473 ioValue = sSelect(ioValue, v2, c2);
474 ioIndex = UVec4::sSelect(ioIndex, i2, c2);
475
476 // Pass 3, test 2nd vs 3rd component
479 UVec4 c3 = sLess(ioValue, v3).Swizzle<SWIZZLE_X, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_W>();
480 ioValue = sSelect(ioValue, v3, c3);
481 ioIndex = UVec4::sSelect(ioIndex, i3, c3);
482}
483
484void Vec4::sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex)
485{
486 // Pass 1, test 1st vs 3rd, 2nd vs 4th
490 ioValue = sSelect(ioValue, v1, c1);
491 ioIndex = UVec4::sSelect(ioIndex, i1, c1);
492
493 // Pass 2, test 1st vs 2nd, 3rd vs 4th
497 ioValue = sSelect(ioValue, v2, c2);
498 ioIndex = UVec4::sSelect(ioIndex, i2, c2);
499
500 // Pass 3, test 2nd vs 3rd component
504 ioValue = sSelect(ioValue, v3, c3);
505 ioIndex = UVec4::sSelect(ioIndex, i3, c3);
506}
507
509{
510 return sEquals(*this, inV2).TestAllTrue();
511}
512
513bool Vec4::IsClose(Vec4Arg inV2, float inMaxDistSq) const
514{
515 return (inV2 - *this).LengthSq() <= inMaxDistSq;
516}
517
518bool Vec4::IsNearZero(float inMaxDistSq) const
519{
520 return LengthSq() <= inMaxDistSq;
521}
522
523bool Vec4::IsNormalized(float inTolerance) const
524{
525 return abs(LengthSq() - 1.0f) <= inTolerance;
526}
527
528bool Vec4::IsNaN() const
529{
530#if defined(JPH_USE_AVX512)
531 return _mm_fpclass_ps_mask(mValue, 0b10000001) != 0;
532#elif defined(JPH_USE_SSE)
533 return _mm_movemask_ps(_mm_cmpunord_ps(mValue, mValue)) != 0;
534#elif defined(JPH_USE_NEON)
535 uint32x4_t is_equal = vceqq_f32(mValue, mValue); // If a number is not equal to itself it's a NaN
536 return vaddvq_u32(vshrq_n_u32(is_equal, 31)) != 4;
537#elif defined(JPH_USE_RVV)
538 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
539 const vbool32_t mask = __riscv_vmfeq_vv_f32m1_b32(v, v, 4);
540 const uint32 eq = __riscv_vcpop_m_b32(mask, 4);
541 return eq != 4;
542#else
543 return isnan(mF32[0]) || isnan(mF32[1]) || isnan(mF32[2]) || isnan(mF32[3]);
544#endif
545}
546
548{
549#if defined(JPH_USE_SSE)
550 return _mm_mul_ps(mValue, inV2.mValue);
551#elif defined(JPH_USE_NEON)
552 return vmulq_f32(mValue, inV2.mValue);
553#elif defined(JPH_USE_RVV)
554 Vec4 res;
555 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
556 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
557 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v1, v2, 4);
558 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
559 return res;
560#else
561 return Vec4(mF32[0] * inV2.mF32[0],
562 mF32[1] * inV2.mF32[1],
563 mF32[2] * inV2.mF32[2],
564 mF32[3] * inV2.mF32[3]);
565#endif
566}
567
568Vec4 Vec4::operator * (float inV2) const
569{
570#if defined(JPH_USE_SSE)
571 return _mm_mul_ps(mValue, _mm_set1_ps(inV2));
572#elif defined(JPH_USE_NEON)
573 return vmulq_n_f32(mValue, inV2);
574#elif defined(JPH_USE_RVV)
575 Vec4 res;
576 const vfloat32m1_t src = __riscv_vle32_v_f32m1(mF32, 4);
577 const vfloat32m1_t mul = __riscv_vfmul_vf_f32m1(src, inV2, 4);
578 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
579 return res;
580#else
581 return Vec4(mF32[0] * inV2, mF32[1] * inV2, mF32[2] * inV2, mF32[3] * inV2);
582#endif
583}
584
586Vec4 operator * (float inV1, Vec4Arg inV2)
587{
588#if defined(JPH_USE_SSE)
589 return _mm_mul_ps(_mm_set1_ps(inV1), inV2.mValue);
590#elif defined(JPH_USE_NEON)
591 return vmulq_n_f32(inV2.mValue, inV1);
592#elif defined(JPH_USE_RVV)
593 Vec4 res;
594 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
595 const vfloat32m1_t mul = __riscv_vfmul_vf_f32m1(v1, inV1, 4);
596 __riscv_vse32_v_f32m1(res.mF32, mul, 4);
597 return res;
598#else
599 return Vec4(inV1 * inV2.mF32[0],
600 inV1 * inV2.mF32[1],
601 inV1 * inV2.mF32[2],
602 inV1 * inV2.mF32[3]);
603#endif
604}
605
606Vec4 Vec4::operator / (float inV2) const
607{
608#if defined(JPH_USE_SSE)
609 return _mm_div_ps(mValue, _mm_set1_ps(inV2));
610#elif defined(JPH_USE_NEON)
611 return vdivq_f32(mValue, vdupq_n_f32(inV2));
612#elif defined(JPH_USE_RVV)
613 Vec4 res;
614 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
615 const vfloat32m1_t div = __riscv_vfdiv_vf_f32m1(v1, inV2, 4);
616 __riscv_vse32_v_f32m1(res.mF32, div, 4);
617 return res;
618#else
619 return Vec4(mF32[0] / inV2, mF32[1] / inV2, mF32[2] / inV2, mF32[3] / inV2);
620#endif
621}
622
624{
625#if defined(JPH_USE_SSE)
626 mValue = _mm_mul_ps(mValue, _mm_set1_ps(inV2));
627#elif defined(JPH_USE_NEON)
628 mValue = vmulq_n_f32(mValue, inV2);
629#elif defined(JPH_USE_RVV)
630 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
631 const vfloat32m1_t res = __riscv_vfmul_vf_f32m1(v1, inV2, 4);
632 __riscv_vse32_v_f32m1(mF32, res, 4);
633#else
634 for (int i = 0; i < 4; ++i)
635 mF32[i] *= inV2;
636#endif
637 return *this;
638}
639
641{
642#if defined(JPH_USE_SSE)
643 mValue = _mm_mul_ps(mValue, inV2.mValue);
644#elif defined(JPH_USE_NEON)
645 mValue = vmulq_f32(mValue, inV2.mValue);
646#elif defined(JPH_USE_RVV)
647 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
648 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
649 const vfloat32m1_t rvv_res = __riscv_vfmul_vv_f32m1(v1, v2, 4);
650 __riscv_vse32_v_f32m1(mF32, rvv_res, 4);
651#else
652 for (int i = 0; i < 4; ++i)
653 mF32[i] *= inV2.mF32[i];
654#endif
655 return *this;
656}
657
659{
660#if defined(JPH_USE_SSE)
661 mValue = _mm_div_ps(mValue, _mm_set1_ps(inV2));
662#elif defined(JPH_USE_NEON)
663 mValue = vdivq_f32(mValue, vdupq_n_f32(inV2));
664#elif defined(JPH_USE_RVV)
665 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
666 const vfloat32m1_t res = __riscv_vfdiv_vf_f32m1(v, inV2, 4);
667 __riscv_vse32_v_f32m1(mF32, res, 4);
668#else
669 for (int i = 0; i < 4; ++i)
670 mF32[i] /= inV2;
671#endif
672 return *this;
673}
674
676{
677#if defined(JPH_USE_SSE)
678 return _mm_add_ps(mValue, inV2.mValue);
679#elif defined(JPH_USE_NEON)
680 return vaddq_f32(mValue, inV2.mValue);
681#elif defined(JPH_USE_RVV)
682 Vec4 res;
683 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
684 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
685 const vfloat32m1_t rvv_add = __riscv_vfadd_vv_f32m1(v1, v2, 4);
686 __riscv_vse32_v_f32m1(res.mF32, rvv_add, 4);
687 return res;
688#else
689 return Vec4(mF32[0] + inV2.mF32[0],
690 mF32[1] + inV2.mF32[1],
691 mF32[2] + inV2.mF32[2],
692 mF32[3] + inV2.mF32[3]);
693#endif
694}
695
697{
698#if defined(JPH_USE_SSE)
699 mValue = _mm_add_ps(mValue, inV2.mValue);
700#elif defined(JPH_USE_NEON)
701 mValue = vaddq_f32(mValue, inV2.mValue);
702#elif defined(JPH_USE_RVV)
703 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
704 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
705 const vfloat32m1_t rvv_add = __riscv_vfadd_vv_f32m1(v1, v2, 4);
706 __riscv_vse32_v_f32m1(mF32, rvv_add, 4);
707#else
708 for (int i = 0; i < 4; ++i)
709 mF32[i] += inV2.mF32[i];
710#endif
711 return *this;
712}
713
715{
716#if defined(JPH_USE_SSE)
717 return _mm_sub_ps(_mm_setzero_ps(), mValue);
718#elif defined(JPH_USE_NEON)
719 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
720 return vsubq_f32(vdupq_n_f32(0), mValue);
721 #else
722 return vnegq_f32(mValue);
723 #endif
724#elif defined(JPH_USE_RVV)
725 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
726 Vec4 res;
727 const vfloat32m1_t rvv_zero = __riscv_vfmv_v_f_f32m1(0.0f, 4);
728 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
729 const vfloat32m1_t rvv_neg = __riscv_vfsub_vv_f32m1(rvv_zero, v, 4);
730 __riscv_vse32_v_f32m1(res.mF32, rvv_neg, 4);
731 return res;
732 #else
733 Vec4 res;
734 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
735 const vfloat32m1_t rvv_neg = __riscv_vfsgnjn_vv_f32m1(v, v, 4);
736 __riscv_vse32_v_f32m1(res.mF32, rvv_neg, 4);
737 return res;
738 #endif
739#else
740 #ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
741 return Vec4(0.0f - mF32[0], 0.0f - mF32[1], 0.0f - mF32[2], 0.0f - mF32[3]);
742 #else
743 return Vec4(-mF32[0], -mF32[1], -mF32[2], -mF32[3]);
744 #endif
745#endif
746}
747
749{
750#if defined(JPH_USE_SSE)
751 return _mm_sub_ps(mValue, inV2.mValue);
752#elif defined(JPH_USE_NEON)
753 return vsubq_f32(mValue, inV2.mValue);
754#elif defined(JPH_USE_RVV)
755 Vec4 res;
756 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
757 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
758 const vfloat32m1_t rvv_sub = __riscv_vfsub_vv_f32m1(v1, v2, 4);
759 __riscv_vse32_v_f32m1(res.mF32, rvv_sub, 4);
760 return res;
761#else
762 return Vec4(mF32[0] - inV2.mF32[0],
763 mF32[1] - inV2.mF32[1],
764 mF32[2] - inV2.mF32[2],
765 mF32[3] - inV2.mF32[3]);
766#endif
767}
768
770{
771#if defined(JPH_USE_SSE)
772 mValue = _mm_sub_ps(mValue, inV2.mValue);
773#elif defined(JPH_USE_NEON)
774 mValue = vsubq_f32(mValue, inV2.mValue);
775#elif defined(JPH_USE_RVV)
776 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
777 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
778 const vfloat32m1_t rvv_sub = __riscv_vfsub_vv_f32m1(v1, v2, 4);
779 __riscv_vse32_v_f32m1(mF32, rvv_sub, 4);
780#else
781 for (int i = 0; i < 4; ++i)
782 mF32[i] -= inV2.mF32[i];
783#endif
784 return *this;
785}
786
788{
789#if defined(JPH_USE_SSE)
790 return _mm_div_ps(mValue, inV2.mValue);
791#elif defined(JPH_USE_NEON)
792 return vdivq_f32(mValue, inV2.mValue);
793#elif defined(JPH_USE_RVV)
794 Vec4 res;
795 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
796 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
797 const vfloat32m1_t rvv_div = __riscv_vfdiv_vv_f32m1(v1, v2, 4);
798 __riscv_vse32_v_f32m1(res.mF32, rvv_div, 4);
799 return res;
800#else
801 return Vec4(mF32[0] / inV2.mF32[0],
802 mF32[1] / inV2.mF32[1],
803 mF32[2] / inV2.mF32[2],
804 mF32[3] / inV2.mF32[3]);
805#endif
806}
807
809{
810#if defined(JPH_USE_SSE)
811 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(0, 0, 0, 0));
812#elif defined(JPH_USE_NEON)
813 return vdupq_laneq_f32(mValue, 0);
814#elif defined(JPH_USE_RVV)
815 Vec4 vec;
816 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[0], 4);
817 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
818 return vec;
819#else
820 return Vec4(mF32[0], mF32[0], mF32[0], mF32[0]);
821#endif
822}
823
825{
826#if defined(JPH_USE_SSE)
827 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(1, 1, 1, 1));
828#elif defined(JPH_USE_NEON)
829 return vdupq_laneq_f32(mValue, 1);
830#elif defined(JPH_USE_RVV)
831 Vec4 vec;
832 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[1], 4);
833 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
834 return vec;
835#else
836 return Vec4(mF32[1], mF32[1], mF32[1], mF32[1]);
837#endif
838}
839
841{
842#if defined(JPH_USE_SSE)
843 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(2, 2, 2, 2));
844#elif defined(JPH_USE_NEON)
845 return vdupq_laneq_f32(mValue, 2);
846#elif defined(JPH_USE_RVV)
847 Vec4 vec;
848 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[2], 4);
849 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
850 return vec;
851#else
852 return Vec4(mF32[2], mF32[2], mF32[2], mF32[2]);
853#endif
854}
855
857{
858#if defined(JPH_USE_SSE)
859 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(3, 3, 3, 3));
860#elif defined(JPH_USE_NEON)
861 return vdupq_laneq_f32(mValue, 3);
862#elif defined(JPH_USE_RVV)
863 Vec4 vec;
864 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[3], 4);
865 __riscv_vse32_v_f32m1(vec.mF32, splat, 4);
866 return vec;
867#else
868 return Vec4(mF32[3], mF32[3], mF32[3], mF32[3]);
869#endif
870}
871
873{
874#if defined(JPH_USE_SSE)
875 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(0, 0, 0, 0));
876#elif defined(JPH_USE_NEON)
877 return vdupq_laneq_f32(mValue, 0);
878#elif defined(JPH_USE_RVV)
879 Vec3 vec;
880 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[0], 3);
881 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
882 return vec;
883#else
884 return Vec3(mF32[0], mF32[0], mF32[0]);
885#endif
886}
887
889{
890#if defined(JPH_USE_SSE)
891 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(1, 1, 1, 1));
892#elif defined(JPH_USE_NEON)
893 return vdupq_laneq_f32(mValue, 1);
894#elif defined(JPH_USE_RVV)
895 Vec3 vec;
896 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[1], 3);
897 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
898 return vec;
899#else
900 return Vec3(mF32[1], mF32[1], mF32[1]);
901#endif
902}
903
905{
906#if defined(JPH_USE_SSE)
907 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(2, 2, 2, 2));
908#elif defined(JPH_USE_NEON)
909 return vdupq_laneq_f32(mValue, 2);
910#elif defined(JPH_USE_RVV)
911 Vec3 vec;
912 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[2], 3);
913 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
914 return vec;
915#else
916 return Vec3(mF32[2], mF32[2], mF32[2]);
917#endif
918}
919
921{
922#if defined(JPH_USE_SSE)
923 return _mm_shuffle_ps(mValue, mValue, _MM_SHUFFLE(3, 3, 3, 3));
924#elif defined(JPH_USE_NEON)
925 return vdupq_laneq_f32(mValue, 3);
926#elif defined(JPH_USE_RVV)
927 Vec3 vec;
928 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(mF32[3], 3);
929 __riscv_vse32_v_f32m1(vec.mF32, splat, 3);
930 return vec;
931#else
932 return Vec3(mF32[3], mF32[3], mF32[3]);
933#endif
934}
935
937{
938 // Get the minimum value in all 4 components
940 value = Vec4::sMin(value, value.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y>());
941
942 // Compare with the original vector to find which component is equal to the minimum value
943 return CountTrailingZeros(Vec4::sEquals(*this, value).GetTrues());
944}
945
947{
948 // Get the maximum value in all 4 components
950 value = Vec4::sMax(value, value.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_X, SWIZZLE_Y>());
951
952 // Compare with the original vector to find which component is equal to the maximum value
953 return CountTrailingZeros(Vec4::sEquals(*this, value).GetTrues());
954}
955
957{
958#if defined(JPH_USE_AVX512)
959 return _mm_range_ps(mValue, mValue, 0b1000);
960#elif defined(JPH_USE_SSE)
961 return _mm_max_ps(_mm_sub_ps(_mm_setzero_ps(), mValue), mValue);
962#elif defined(JPH_USE_NEON)
963 return vabsq_f32(mValue);
964#elif defined(JPH_USE_RVV)
965 Vec4 res;
966 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
967 const vfloat32m1_t rvv_abs = __riscv_vfsgnj_vf_f32m1(v, 1.0, 4);
968 __riscv_vse32_v_f32m1(res.mF32, rvv_abs, 4);
969 return res;
970#else
971 return Vec4(abs(mF32[0]), abs(mF32[1]), abs(mF32[2]), abs(mF32[3]));
972#endif
973}
974
976{
977 return sOne() / mValue;
978}
979
981{
982#if defined(JPH_USE_SSE4_1)
983 return _mm_dp_ps(mValue, inV2.mValue, 0xff);
984#elif defined(JPH_USE_NEON)
985 float32x4_t mul = vmulq_f32(mValue, inV2.mValue);
986 return vdupq_n_f32(vaddvq_f32(mul));
987#elif defined(JPH_USE_RVV)
988 Vec4 res;
989 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
990 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
991 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v1, v2, 4);
992 float dot = RVVSumElementsFloat32x4(mul);
993 const vfloat32m1_t splat = __riscv_vfmv_v_f_f32m1(dot, 4);
994 __riscv_vse32_v_f32m1(res.mF32, splat, 4);
995 return res;
996#else
997 // Brackets placed so that the order is consistent with the vectorized version
998 return Vec4::sReplicate((mF32[0] * inV2.mF32[0] + mF32[1] * inV2.mF32[1]) + (mF32[2] * inV2.mF32[2] + mF32[3] * inV2.mF32[3]));
999#endif
1000}
1001
1002float Vec4::Dot(Vec4Arg inV2) const
1003{
1004#if defined(JPH_USE_SSE4_1)
1005 return _mm_cvtss_f32(_mm_dp_ps(mValue, inV2.mValue, 0xff));
1006#elif defined(JPH_USE_NEON)
1007 float32x4_t mul = vmulq_f32(mValue, inV2.mValue);
1008 return vaddvq_f32(mul);
1009#elif defined(JPH_USE_RVV)
1010 const vfloat32m1_t v1 = __riscv_vle32_v_f32m1(mF32, 4);
1011 const vfloat32m1_t v2 = __riscv_vle32_v_f32m1(inV2.mF32, 4);
1012 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v1, v2, 4);
1013 return RVVSumElementsFloat32x4(mul);
1014#else
1015 // Brackets placed so that the order is consistent with the vectorized version
1016 return (mF32[0] * inV2.mF32[0] + mF32[1] * inV2.mF32[1]) + (mF32[2] * inV2.mF32[2] + mF32[3] * inV2.mF32[3]);
1017#endif
1018}
1019
1020float Vec4::LengthSq() const
1021{
1022#if defined(JPH_USE_SSE4_1)
1023 return _mm_cvtss_f32(_mm_dp_ps(mValue, mValue, 0xff));
1024#elif defined(JPH_USE_NEON)
1025 float32x4_t mul = vmulq_f32(mValue, mValue);
1026 return vaddvq_f32(mul);
1027#elif defined(JPH_USE_RVV)
1028 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1029 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v, v, 4);
1030 return RVVSumElementsFloat32x4(mul);
1031#else
1032 // Brackets placed so that the order is consistent with the vectorized version
1033 return (mF32[0] * mF32[0] + mF32[1] * mF32[1]) + (mF32[2] * mF32[2] + mF32[3] * mF32[3]);
1034#endif
1035}
1036
1037float Vec4::Length() const
1038{
1039#if defined(JPH_USE_SSE4_1)
1040 return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(mValue, mValue, 0xff)));
1041#elif defined(JPH_USE_NEON)
1042 float32x4_t mul = vmulq_f32(mValue, mValue);
1043 float32x2_t sum = vdup_n_f32(vaddvq_f32(mul));
1044 return vget_lane_f32(vsqrt_f32(sum), 0);
1045#elif defined(JPH_USE_RVV)
1046 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1047 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v, v, 4);
1048 return sqrt(RVVSumElementsFloat32x4(mul));
1049#else
1050 // Brackets placed so that the order is consistent with the vectorized version
1051 return sqrt((mF32[0] * mF32[0] + mF32[1] * mF32[1]) + (mF32[2] * mF32[2] + mF32[3] * mF32[3]));
1052#endif
1053}
1054
1056{
1057#if defined(JPH_USE_SSE)
1058 return _mm_sqrt_ps(mValue);
1059#elif defined(JPH_USE_NEON)
1060 return vsqrtq_f32(mValue);
1061#elif defined(JPH_USE_RVV)
1062 Vec4 res;
1063 const vfloat32m1_t rvv_v = __riscv_vle32_v_f32m1(mF32, 4);
1064 const vfloat32m1_t rvv_sqrt = __riscv_vfsqrt_v_f32m1(rvv_v, 4);
1065 __riscv_vse32_v_f32m1(res.mF32, rvv_sqrt, 4);
1066 return res;
1067#else
1068 return Vec4(sqrt(mF32[0]), sqrt(mF32[1]), sqrt(mF32[2]), sqrt(mF32[3]));
1069#endif
1070}
1071
1072
1074{
1075#if defined(JPH_USE_AVX512)
1076 return _mm_fixupimm_ps(mValue, mValue, _mm_set1_epi32(0xA9A90A00), 0);
1077#elif defined(JPH_USE_SSE)
1078 Type minus_one = _mm_set1_ps(-1.0f);
1079 Type one = _mm_set1_ps(1.0f);
1080 return _mm_or_ps(_mm_and_ps(mValue, minus_one), one);
1081#elif defined(JPH_USE_NEON)
1082 Type minus_one = vdupq_n_f32(-1.0f);
1083 Type one = vdupq_n_f32(1.0f);
1084 return vreinterpretq_f32_u32(vorrq_u32(vandq_u32(vreinterpretq_u32_f32(mValue), vreinterpretq_u32_f32(minus_one)), vreinterpretq_u32_f32(one)));
1085#elif defined(JPH_USE_RVV)
1086 Vec4 res;
1087 const vfloat32m1_t rvv_in = __riscv_vle32_v_f32m1(mF32, 4);
1088 const vfloat32m1_t rvv_one = __riscv_vfmv_v_f_f32m1(1.0, 4);
1089 const vfloat32m1_t rvv_signs = __riscv_vfsgnj_vv_f32m1(rvv_one, rvv_in, 4);
1090 __riscv_vse32_v_f32m1(res.mF32, rvv_signs, 4);
1091 return res;
1092#else
1093 return Vec4(std::signbit(mF32[0])? -1.0f : 1.0f,
1094 std::signbit(mF32[1])? -1.0f : 1.0f,
1095 std::signbit(mF32[2])? -1.0f : 1.0f,
1096 std::signbit(mF32[3])? -1.0f : 1.0f);
1097#endif
1098}
1099
1100template <int X, int Y, int Z, int W>
1101JPH_INLINE Vec4 Vec4::FlipSign() const
1102{
1103 static_assert(X == 1 || X == -1, "X must be 1 or -1");
1104 static_assert(Y == 1 || Y == -1, "Y must be 1 or -1");
1105 static_assert(Z == 1 || Z == -1, "Z must be 1 or -1");
1106 static_assert(W == 1 || W == -1, "W must be 1 or -1");
1107 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));
1108}
1109
1111{
1112#if defined(JPH_USE_SSE4_1)
1113 return _mm_div_ps(mValue, _mm_sqrt_ps(_mm_dp_ps(mValue, mValue, 0xff)));
1114#elif defined(JPH_USE_NEON)
1115 float32x4_t mul = vmulq_f32(mValue, mValue);
1116 float32x4_t sum = vdupq_n_f32(vaddvq_f32(mul));
1117 return vdivq_f32(mValue, vsqrtq_f32(sum));
1118#elif defined(JPH_USE_RVV)
1119 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1120 const vfloat32m1_t mul = __riscv_vfmul_vv_f32m1(v, v, 4);
1121 const float length = sqrt(RVVSumElementsFloat32x4(mul));
1122 const vfloat32m1_t norm_v = __riscv_vfdiv_vf_f32m1(v, length, 4);
1123
1124 Vec4 vec;
1125 __riscv_vse32_v_f32m1(vec.mF32, norm_v, 4);
1126 return vec;
1127#else
1128 return *this / Length();
1129#endif
1130}
1131
1132void Vec4::StoreFloat4(Float4 *outV) const
1133{
1134#if defined(JPH_USE_SSE)
1135 _mm_storeu_ps(&outV->x, mValue);
1136#elif defined(JPH_USE_NEON)
1137 vst1q_f32(&outV->x, mValue);
1138#elif defined(JPH_USE_RVV)
1139 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1140 __riscv_vse32_v_f32m1(&outV->x, v, 4);
1141#else
1142 for (int i = 0; i < 4; ++i)
1143 (&outV->x)[i] = mF32[i];
1144#endif
1145}
1146
1148{
1149#if defined(JPH_USE_SSE)
1150 return _mm_cvttps_epi32(mValue);
1151#elif defined(JPH_USE_NEON)
1152 return vcvtq_u32_f32(mValue);
1153#elif defined(JPH_USE_RVV)
1154 UVec4 res;
1155 const vfloat32m1_t v = __riscv_vle32_v_f32m1(mF32, 4);
1156 const vuint32m1_t cast = __riscv_vfcvt_rtz_xu_f_v_u32m1(v, 4);
1157 __riscv_vse32_v_u32m1(res.mU32, cast, 4);
1158 return res;
1159#else
1160 return UVec4(uint32(mF32[0]), uint32(mF32[1]), uint32(mF32[2]), uint32(mF32[3]));
1161#endif
1162}
1163
1165{
1166#if defined(JPH_USE_SSE)
1167 return UVec4(_mm_castps_si128(mValue));
1168#elif defined(JPH_USE_NEON)
1169 return vreinterpretq_u32_f32(mValue);
1170#else
1171 return *reinterpret_cast<const UVec4 *>(this);
1172#endif
1173}
1174
1176{
1177#if defined(JPH_USE_SSE)
1178 return _mm_movemask_ps(mValue);
1179#elif defined(JPH_USE_NEON)
1180 int32x4_t shift = JPH_NEON_INT32x4(0, 1, 2, 3);
1181 return vaddvq_u32(vshlq_u32(vshrq_n_u32(vreinterpretq_u32_f32(mValue), 31), shift));
1182#elif defined(JPH_USE_RVV)
1183 const vuint32m1_t v = __riscv_vle32_v_u32m1(reinterpret_cast<const uint32 *>(mF32), 4);
1184 const vuint32m1_t shifted = __riscv_vsrl_vx_u32m1(v, 31, 4);
1185 const vbool32_t mask = __riscv_vmsne_vx_u32m1_b32(shifted, 0x0, 4);
1186 const vuint32m1_t as_int = __riscv_vreinterpret_v_b32_u32m1(mask);
1187 const uint32 result = __riscv_vmv_x_s_u32m1_u32(as_int) & 0xF;
1188 return result;
1189#else
1190 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);
1191#endif
1192}
1193
1200
1207
1208void Vec4::SinCos(Vec4 &outSin, Vec4 &outCos) const
1209{
1210 // Implementation based on sinf.c from the cephes library, combines sinf and cosf in a single function, changes octants to quadrants and vectorizes it
1211 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1212
1213 // Make argument positive and remember sign for sin only since cos is symmetric around x (highest bit of a float is the sign bit)
1214 UVec4 sin_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1215 Vec4 x = Vec4::sXor(*this, sin_sign.ReinterpretAsFloat());
1216
1217 // x / (PI / 2) rounded to nearest int gives us the quadrant closest to x
1218 UVec4 quadrant = (0.6366197723675814f * x + Vec4::sReplicate(0.5f)).ToInt();
1219
1220 // Make x relative to the closest quadrant.
1221 // This does x = x - quadrant * PI / 2 using a two step Cody-Waite argument reduction.
1222 // This improves the accuracy of the result by avoiding loss of significant bits in the subtraction.
1223 // We start with x = x - quadrant * PI / 2, PI / 2 in hexadecimal notation is 0x3fc90fdb, we remove the lowest 16 bits to
1224 // get 0x3fc90000 (= 1.5703125) this means we can now multiply with a number of up to 2^16 without losing any bits.
1225 // This leaves us with: x = (x - quadrant * 1.5703125) - quadrant * (PI / 2 - 1.5703125).
1226 // PI / 2 - 1.5703125 in hexadecimal is 0x39fdaa22, stripping the lowest 12 bits we get 0x39fda000 (= 0.0004837512969970703125)
1227 // This leaves uw with: x = ((x - quadrant * 1.5703125) - quadrant * 0.0004837512969970703125) - quadrant * (PI / 2 - 1.5703125 - 0.0004837512969970703125)
1228 // See: https://stackoverflow.com/questions/42455143/sine-cosine-modular-extended-precision-arithmetic
1229 // After this we have x in the range [-PI / 4, PI / 4].
1230 Vec4 float_quadrant = quadrant.ToFloat();
1231 x = ((x - float_quadrant * 1.5703125f) - float_quadrant * 0.0004837512969970703125f) - float_quadrant * 7.549789948768648e-8f;
1232
1233 // Calculate x2 = x^2
1234 Vec4 x2 = x * x;
1235
1236 // Taylor expansion:
1237 // 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
1238 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();
1239 // Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... = ((-x2/7! + 1/5!) * x2 - 1/3!) * x2 * x + x
1240 Vec4 taylor_sin = ((-1.9515295891e-4f * x2 + Vec4::sReplicate(8.3321608736e-3f)) * x2 - Vec4::sReplicate(1.6666654611e-1f)) * x2 * x + x;
1241
1242 // The lowest 2 bits of quadrant indicate the quadrant that we are in.
1243 // Let x be the original input value and x' our value that has been mapped to the range [-PI / 4, PI / 4].
1244 // since cos(x) = sin(x - PI / 2) and since we want to use the Taylor expansion as close as possible to 0,
1245 // we can alternate between using the Taylor expansion for sin and cos according to the following table:
1246 //
1247 // quadrant sin(x) cos(x)
1248 // XXX00b sin(x') cos(x')
1249 // XXX01b cos(x') -sin(x')
1250 // XXX10b -sin(x') -cos(x')
1251 // XXX11b -cos(x') sin(x')
1252 //
1253 // So: sin_sign = bit2, cos_sign = bit1 ^ bit2, bit1 determines if we use sin or cos Taylor expansion
1254 UVec4 bit1 = quadrant.LogicalShiftLeft<31>();
1255 UVec4 bit2 = UVec4::sAnd(quadrant.LogicalShiftLeft<30>(), UVec4::sReplicate(0x80000000U));
1256
1257 // Select which one of the results is sin and which one is cos
1258 Vec4 s = Vec4::sSelect(taylor_sin, taylor_cos, bit1);
1259 Vec4 c = Vec4::sSelect(taylor_cos, taylor_sin, bit1);
1260
1261 // Update the signs
1262 sin_sign = UVec4::sXor(sin_sign, bit2);
1263 UVec4 cos_sign = UVec4::sXor(bit1, bit2);
1264
1265 // Correct the signs
1266 outSin = Vec4::sXor(s, sin_sign.ReinterpretAsFloat());
1267 outCos = Vec4::sXor(c, cos_sign.ReinterpretAsFloat());
1268}
1269
1271{
1272 // Implementation based on tanf.c from the cephes library, see Vec4::SinCos for further details
1273 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1274
1275 // Make argument positive
1276 UVec4 tan_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1277 Vec4 x = Vec4::sXor(*this, tan_sign.ReinterpretAsFloat());
1278
1279 // x / (PI / 2) rounded to nearest int gives us the quadrant closest to x
1280 UVec4 quadrant = (0.6366197723675814f * x + Vec4::sReplicate(0.5f)).ToInt();
1281
1282 // Remap x to range [-PI / 4, PI / 4], see Vec4::SinCos
1283 Vec4 float_quadrant = quadrant.ToFloat();
1284 x = ((x - float_quadrant * 1.5703125f) - float_quadrant * 0.0004837512969970703125f) - float_quadrant * 7.549789948768648e-8f;
1285
1286 // Calculate x2 = x^2
1287 Vec4 x2 = x * x;
1288
1289 // Roughly equivalent to the Taylor expansion:
1290 // Tan(x) = x + x^3/3 + 2*x^5/15 + 17*x^7/315 + 62*x^9/2835 + ...
1291 Vec4 tan =
1292 (((((9.38540185543e-3f * x2 + Vec4::sReplicate(3.11992232697e-3f)) * x2 + Vec4::sReplicate(2.44301354525e-2f)) * x2
1293 + Vec4::sReplicate(5.34112807005e-2f)) * x2 + Vec4::sReplicate(1.33387994085e-1f)) * x2 + Vec4::sReplicate(3.33331568548e-1f)) * x2 * x + x;
1294
1295 // For the 2nd and 4th quadrant we need to invert the value
1296 UVec4 bit1 = quadrant.LogicalShiftLeft<31>();
1297 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
1298
1299 // Put the sign back
1300 return Vec4::sXor(tan, tan_sign.ReinterpretAsFloat());
1301}
1302
1304{
1305 // Implementation based on asinf.c from the cephes library
1306 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1307
1308 // Make argument positive
1309 UVec4 asin_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1310 Vec4 a = Vec4::sXor(*this, asin_sign.ReinterpretAsFloat());
1311
1312 // 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
1313 a = Vec4::sMin(a, Vec4::sOne());
1314
1315 // When |x| <= 0.5 we use the asin approximation as is
1316 Vec4 z1 = a * a;
1317 Vec4 x1 = a;
1318
1319 // When |x| > 0.5 we use the identity asin(x) = PI / 2 - 2 * asin(sqrt((1 - x) / 2))
1320 Vec4 z2 = 0.5f * (Vec4::sOne() - a);
1321 Vec4 x2 = z2.Sqrt();
1322
1323 // Select which of the two situations we have
1324 UVec4 greater = Vec4::sGreater(a, Vec4::sReplicate(0.5f));
1325 Vec4 z = Vec4::sSelect(z1, z2, greater);
1326 Vec4 x = Vec4::sSelect(x1, x2, greater);
1327
1328 // Polynomial approximation of asin
1329 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;
1330
1331 // If |x| > 0.5 we need to apply the remainder of the identity above
1332 z = Vec4::sSelect(z, Vec4::sReplicate(0.5f * JPH_PI) - (z + z), greater);
1333
1334 // Put the sign back
1335 return Vec4::sXor(z, asin_sign.ReinterpretAsFloat());
1336}
1337
1339{
1340 // Not the most accurate, but simple
1341 return Vec4::sReplicate(0.5f * JPH_PI) - ASin();
1342}
1343
1345{
1346 // Implementation based on atanf.c from the cephes library
1347 // Original implementation by Stephen L. Moshier (See: http://www.moshier.net/)
1348
1349 // Make argument positive
1350 UVec4 atan_sign = UVec4::sAnd(ReinterpretAsInt(), UVec4::sReplicate(0x80000000U));
1351 Vec4 x = Vec4::sXor(*this, atan_sign.ReinterpretAsFloat());
1352 Vec4 y = Vec4::sZero();
1353
1354 // If x > Tan(PI / 8)
1355 UVec4 greater1 = Vec4::sGreater(x, Vec4::sReplicate(0.4142135623730950f));
1356 Vec4 x1 = (x - Vec4::sOne()) / (x + Vec4::sOne());
1357
1358 // If x > Tan(3 * PI / 8)
1359 UVec4 greater2 = Vec4::sGreater(x, Vec4::sReplicate(2.414213562373095f));
1360 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
1361
1362 // Apply first if
1363 x = Vec4::sSelect(x, x1, greater1);
1364 y = Vec4::sSelect(y, Vec4::sReplicate(0.25f * JPH_PI), greater1);
1365
1366 // Apply second if
1367 x = Vec4::sSelect(x, x2, greater2);
1368 y = Vec4::sSelect(y, Vec4::sReplicate(0.5f * JPH_PI), greater2);
1369
1370 // Polynomial approximation
1371 Vec4 z = x * x;
1372 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;
1373
1374 // Put the sign back
1375 return Vec4::sXor(y, atan_sign.ReinterpretAsFloat());
1376}
1377
1379{
1380 UVec4 sign_mask = UVec4::sReplicate(0x80000000U);
1381
1382 // Determine absolute value and sign of y
1383 UVec4 y_sign = UVec4::sAnd(inY.ReinterpretAsInt(), sign_mask);
1384 Vec4 y_abs = Vec4::sXor(inY, y_sign.ReinterpretAsFloat());
1385
1386 // Determine absolute value and sign of x
1387 UVec4 x_sign = UVec4::sAnd(inX.ReinterpretAsInt(), sign_mask);
1388 Vec4 x_abs = Vec4::sXor(inX, x_sign.ReinterpretAsFloat());
1389
1390 // Always divide smallest / largest to avoid dividing by zero
1391 UVec4 x_is_numerator = Vec4::sLess(x_abs, y_abs);
1392 Vec4 numerator = Vec4::sSelect(y_abs, x_abs, x_is_numerator);
1393 Vec4 denominator = Vec4::sSelect(x_abs, y_abs, x_is_numerator);
1394 Vec4 atan = (numerator / denominator).ATan();
1395
1396 // 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)
1397 atan = Vec4::sSelect(atan, Vec4::sReplicate(0.5f * JPH_PI) - atan, x_is_numerator);
1398
1399 // Now we need to map to the correct quadrant
1400 // x_sign y_sign result
1401 // +1 +1 atan
1402 // -1 +1 -atan + PI
1403 // -1 -1 atan - PI
1404 // +1 -1 -atan
1405 // This can be written as: x_sign * y_sign * (atan - (x_sign < 0? PI : 0))
1406 atan -= Vec4::sAnd(x_sign.ArithmeticShiftRight<31>().ReinterpretAsFloat(), Vec4::sReplicate(JPH_PI));
1407 atan = Vec4::sXor(atan, UVec4::sXor(x_sign, y_sign).ReinterpretAsFloat());
1408 return atan;
1409}
1410
1412{
1413 constexpr float cOneOverSqrt2 = 0.70710678f;
1414 constexpr uint cNumBits = 9;
1415 constexpr uint cMask = (1 << cNumBits) - 1;
1416 constexpr uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
1417 constexpr float cScale = float(cMaxValue) / (2.0f * cOneOverSqrt2);
1418
1419 // Store sign bit
1420 Vec4 v = *this;
1421 uint32 max_element = v.Abs().GetHighestComponentIndex();
1422 uint32 value = 0;
1423 if (v[max_element] < 0.0f)
1424 {
1425 value = 0x80000000u;
1426 v = -v;
1427 }
1428
1429 // Store highest component
1430 value |= max_element << 29;
1431
1432 // Store the other three components in a compressed format
1433 UVec4 compressed = Vec4::sClamp((v + Vec4::sReplicate(cOneOverSqrt2)) * cScale + Vec4::sReplicate(0.5f), Vec4::sZero(), Vec4::sReplicate(cMaxValue)).ToInt();
1434 switch (max_element)
1435 {
1436 case 0:
1437 compressed = compressed.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>();
1438 break;
1439
1440 case 1:
1441 compressed = compressed.Swizzle<SWIZZLE_X, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>();
1442 break;
1443
1444 case 2:
1445 compressed = compressed.Swizzle<SWIZZLE_X, SWIZZLE_Y, SWIZZLE_W, SWIZZLE_UNUSED>();
1446 break;
1447 }
1448
1449 value |= compressed.GetX();
1450 value |= compressed.GetY() << cNumBits;
1451 value |= compressed.GetZ() << 2 * cNumBits;
1452 return value;
1453}
1454
1456{
1457 constexpr float cOneOverSqrt2 = 0.70710678f;
1458 constexpr uint cNumBits = 9;
1459 constexpr uint cMask = (1u << cNumBits) - 1;
1460 constexpr uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
1461 constexpr float cScale = 2.0f * cOneOverSqrt2 / float(cMaxValue);
1462
1463 // Restore three components
1464 Vec4 v = Vec4(UVec4(inValue & cMask, (inValue >> cNumBits) & cMask, (inValue >> (2 * cNumBits)) & cMask, 0).ToFloat()) * cScale - Vec4(cOneOverSqrt2, cOneOverSqrt2, cOneOverSqrt2, 0.0f);
1465 JPH_ASSERT(v.GetW() == 0.0f);
1466
1467 // Restore the highest component
1468 v.SetW(sqrt(max(1.0f - v.LengthSq(), 0.0f)));
1469
1470 // Extract sign
1471 if ((inValue & 0x80000000u) != 0)
1472 v = -v;
1473
1474 // Swizzle the components in place
1475 switch ((inValue >> 29) & 3)
1476 {
1477 case 0:
1479 break;
1480
1481 case 1:
1483 break;
1484
1485 case 2:
1487 break;
1488 }
1489
1490 return v;
1491}
1492
std::uint8_t uint8
Definition Core.h:506
std::uint64_t uint64
Definition Core.h:510
unsigned int uint
Definition Core.h:505
#define JPH_NAMESPACE_END
Definition Core.h:428
std::uint32_t uint32
Definition Core.h:508
#define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
Definition Core.h:580
#define JPH_NAMESPACE_BEGIN
Definition Core.h:422
#define xy
Definition HLSLToCPP.h:511
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
uint CountTrailingZeros(uint32 inValue)
Compute number of trailing zero bits (how many low bits are zero)
Definition Math.h:98
JPH_INLINE To BitCast(const From &inValue)
Definition Math.h:192
@ 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:586
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:299
float mF32[4]
Definition Vec3.h:300
Definition Vec4.h:14
JPH_INLINE bool IsNearZero(float inMaxDistSq=1.0e-12f) const
Test if vector is near zero.
Definition Vec4.inl:518
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec4.inl:808
static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex)
Definition Vec4.inl:460
Vec4 ATan() const
Calculate the arc tangent for each element of this vector (returns value in the range [-PI / 2,...
Definition Vec4.inl:1344
static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2)
Greater than (component wise)
Definition Vec4.inl:304
float mF32[4]
Definition Vec4.h:312
JPH_INLINE Vec3 SplatW3() const
Replicate the W component to all components.
Definition Vec4.inl:920
JPH_INLINE Vec4 operator-() const
Negate.
Definition Vec4.inl:714
Vec4()=default
Constructor.
static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2)
Logical and (component wise)
Definition Vec4.inl:442
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:1378
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:1073
Vec4 ASin() const
Definition Vec4.inl:1303
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:1101
static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2)
Logical xor (component wise)
Definition Vec4.inl:424
JPH_INLINE Vec4 Abs() const
Return the absolute value of each of the components.
Definition Vec4.inl:956
JPH_INLINE Vec4 operator/(float inV2) const
Divide vector by float.
Definition Vec4.inl:606
Vec4 Tan() const
Calculate the tangent for each element of this vector (input in radians)
Definition Vec4.inl:1270
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:1147
JPH_INLINE Vec4 & operator+=(Vec4Arg inV2)
Add two float vectors (component wise)
Definition Vec4.inl:696
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:936
JPH_INLINE float Length() const
Length of vector.
Definition Vec4.inl:1037
static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex)
Definition Vec4.inl:484
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:1110
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:1201
JPH_INLINE Vec4 Reciprocal() const
Reciprocal vector (1 / value) for each of the components.
Definition Vec4.inl:975
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec4.inl:824
JPH_INLINE UVec4 ReinterpretAsInt() const
Reinterpret Vec4 as a UVec4 (doesn't change the bits)
Definition Vec4.inl:1164
static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Greater than or equal (component wise)
Definition Vec4.inl:327
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:840
JPH_INLINE Vec4 Sqrt() const
Component wise square root.
Definition Vec4.inl:1055
JPH_INLINE Vec4 & operator*=(float inV2)
Multiply vector with float.
Definition Vec4.inl:623
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:675
JPH_INLINE Vec4 & operator/=(float inV2)
Divide vector by float.
Definition Vec4.inl:658
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec4.inl:523
JPH_INLINE bool operator==(Vec4Arg inV2) const
Comparison.
Definition Vec4.inl:508
JPH_INLINE Vec4 SplatW() const
Replicate the W component to all components.
Definition Vec4.inl:856
JPH_INLINE Vec4 DotV(Vec4Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition Vec4.inl:980
JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq=1.0e-12f) const
Test if two vectors are close.
Definition Vec4.inl:513
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:406
JPH_INLINE float ReduceMin() const
Get the minimum of X, Y, Z and W.
Definition Vec4.inl:1194
Type mValue
Definition Vec4.h:311
static JPH_INLINE Vec4 sDecompressUnitVector(uint32 inValue)
Decompress a unit vector from a 32 bit value.
Definition Vec4.inl:1455
JPH_INLINE uint32 CompressUnitVector() const
Compress a unit vector to a 32 bit value, precision is around 0.5 * 10^-3.
Definition Vec4.inl:1411
JPH_INLINE Vec4 & operator-=(Vec4Arg inV2)
Subtract two float vectors (component wise)
Definition Vec4.inl:769
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec4.inl:1020
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:1002
JPH_INLINE Vec3 SplatZ3() const
Replicate the Z component to all components.
Definition Vec4.inl:904
JPH_INLINE bool IsNaN() const
Test if vector contains NaN elements.
Definition Vec4.inl:528
JPH_INLINE Vec3 SplatX3() const
Replicate the X component to all components.
Definition Vec4.inl:872
static JPH_INLINE Vec4 sNaN()
Vector with all NaN's.
Definition Vec4.inl:118
Vec4 ACos() const
Definition Vec4.inl:1338
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:377
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:1175
JPH_INLINE int GetHighestComponentIndex() const
Get index of component with highest value.
Definition Vec4.inl:946
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:888
void SinCos(Vec4 &outSin, Vec4 &outCos) const
Calculate the sine and cosine for each element of this vector (input in radians)
Definition Vec4.inl:1208
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store 4 floats to memory.
Definition Vec4.inl:1132
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:586