JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Vec3Pack.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include "JPLSpatial/Core.h"
25
26#include <type_traits>
27#include <ostream>
28#include <span>
29
30namespace JPL
31{
34 {
36 };
37
38 //======================================================================
40 struct Vec3Pack
41 {
42 simd X, Y, Z;
43
44 Vec3Pack() = default;
45 JPL_INLINE Vec3Pack(float x, float y, float z) noexcept : X(x), Y(y), Z(z) {}
46 JPL_INLINE Vec3Pack(const float* x, const float* y, const float* z) noexcept : X(x), Y(y), Z(z) {}
48
49 template<CVec3 Vec3Type>
50 explicit JPL_INLINE Vec3Pack(const Vec3Type& vec) noexcept : X(GetX(vec)), Y(GetY(vec)), Z(GetZ(vec)) {}
51
52 explicit JPL_INLINE Vec3Pack(const simd& axis) noexcept : X(axis), Y(axis), Z(axis) {}
53
54 template<CVec3 Vec3Type>
55 JPL_INLINE Vec3Pack(const Vec3Type& vec1,
56 const Vec3Type& vec2,
57 const Vec3Type& vec3,
58 const Vec3Type& vec4)
59 : X(GetX(vec1), GetX(vec2), GetX(vec3), GetX(vec4))
60 , Y(GetY(vec1), GetY(vec2), GetY(vec3), GetY(vec4))
61 , Z(GetZ(vec1), GetZ(vec2), GetZ(vec3), GetZ(vec4))
62 {
63 }
64
65 template<CVec3 Vec3Type>
66 JPL_INLINE void load(std::span<const Vec3Type> inVecs)
67 {
69
70 *this = Vec3Pack(inVecs[0], inVecs[1], inVecs[2], inVecs[3]);
71 }
72
74 JPL_INLINE void store(float* outX, float* outY, float* outZ) const
75 {
77 }
78
79 template<CVec3 Vec3Type>
80 JPL_INLINE void store(std::span<Vec3Type> outVecs) const
81 {
83
84 float xs[simd::size()]{}; X.store(xs);
85 float ys[simd::size()]{}; Y.store(ys);
86 float zs[simd::size()]{}; Z.store(zs);
87
88 for (uint32 i = 0; i < simd::size(); ++i)
89 {
90 Vec3Type& outVec = outVecs[i];
91 SetX(outVec, xs[i]); SetY(outVec, ys[i]); SetZ(outVec, zs[i]);
92 }
93 }
94
105
116
117 [[nodiscard]] JPL_INLINE static Vec3Pack Zero() noexcept { return Vec3Pack{ 0.0f, 0.0f, 0.0f }; }
118
119 [[nodiscard]] JPL_INLINE simd LengthSquared() const noexcept { return X * X + Y * Y + Z * Z; }
121
123 {
125 X *= invLength;
126 Y *= invLength;
127 Z *= invLength;
128 return *this;
129 }
130
132 [[nodiscard]] JPL_INLINE bool IsEqual(const Vec3Pack& other) const;
133
134 [[nodiscard]] JPL_INLINE Vec3Pack operator-() const { return Vec3Pack(-X, -Y, -Z); }
135 [[nodiscard]] JPL_INLINE Vec3Pack operator-(const Vec3Pack& V) const { return Vec3Pack(X - V.X, Y - V.Y, Z - V.Z); }
136 [[nodiscard]] JPL_INLINE Vec3Pack operator+(const Vec3Pack& V) const { return Vec3Pack(X + V.X, Y + V.Y, Z + V.Z); }
137
138 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
139 [[nodiscard]] JPL_INLINE Vec3Pack operator*(Arg Scale) const noexcept { return Vec3Pack(X * (float)Scale, Y * (float)Scale, Z * (float)Scale); }
140 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
141 [[nodiscard]] JPL_INLINE Vec3Pack operator/(Arg Scale) const noexcept { return Vec3Pack(X / (float)Scale, Y / (float)Scale, Z / (float)Scale); }
142
143 [[nodiscard]] JPL_INLINE Vec3Pack operator*(simd Scale) const noexcept { return Vec3Pack(X * Scale, Y * Scale, Z * Scale); }
144 [[nodiscard]] JPL_INLINE Vec3Pack operator/(simd Scale) const noexcept { return Vec3Pack(X / Scale, Y / Scale, Z / Scale); }
145
146
147 JPL_INLINE void operator+=(const Vec3Pack& V) noexcept { X += V.X; Y += V.Y; Z += V.Z; }
148 JPL_INLINE Vec3Pack& operator*=(const Vec3Pack Other) { X *= Other.X; Y *= Other.Y; Z *= Other.Z; return *this; }
149
150 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
151 JPL_INLINE Vec3Pack& operator/=(Arg Scale) noexcept { X /= (float)Scale; Y /= (float)Scale; Z /= (float)Scale; return *this; }
152
153 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
154 JPL_INLINE Vec3Pack& operator*=(Arg Scale) noexcept { X *= (float)Scale; Y *= (float)Scale; Z *= (float)Scale; return *this; }
155
156 JPL_INLINE Vec3Pack& operator/=(simd Scale) noexcept { X /= Scale; Y /= Scale; Z /= Scale; return *this; }
157 JPL_INLINE Vec3Pack& operator*=(simd Scale) noexcept { X *= Scale; Y *= Scale; Z *= Scale; return *this; }
158
159 };
160
161 [[nodiscard]] JPL_INLINE simd_mask operator==(const Vec3Pack& A, const Vec3Pack& B) noexcept { return (A.X == B.X) & (A.Y == B.Y) & (A.Z == B.Z); }
162 [[nodiscard]] JPL_INLINE bool Vec3Pack::IsEqual(const Vec3Pack& other) const { return (*this == other).all_of(); }
163
164 template<typename T> requires(std::is_arithmetic_v<T>)
165 [[nodiscard]] JPL_INLINE Vec3Pack operator*(T Scale, const JPL::Vec3Pack& V) noexcept { return V.operator*(Scale); }
166 template<typename T> requires(std::is_arithmetic_v<T>)
167 [[nodiscard]] JPL_INLINE Vec3Pack operator/(T Scale, const JPL::Vec3Pack& V) noexcept { return V.operator/(Scale); }
168
169 [[nodiscard]] JPL_INLINE Vec3Pack operator*(simd Scale, const JPL::Vec3Pack& V) noexcept { return V.operator*(Scale); }
170 [[nodiscard]] JPL_INLINE Vec3Pack operator/(simd Scale, const JPL::Vec3Pack& V) noexcept { return V.operator/(Scale); }
171
172
173 [[nodiscard]] JPL_INLINE Vec3Pack operator*(const JPL::Vec3Pack& A, const JPL::Vec3Pack& B) noexcept { return { A.X * B.X, A.Y * B.Y, A.Z * B.Z }; }
174 [[nodiscard]] JPL_INLINE Vec3Pack operator/(const JPL::Vec3Pack& A, const JPL::Vec3Pack& B) noexcept { return { A.X / B.X, A.Y / B.Y, A.Z / B.Z }; }
175
176 [[nodiscard]] static JPL_INLINE simd LengthSquared(const Vec3Pack& V) noexcept { return V.LengthSquared(); }
177 [[nodiscard]] static JPL_INLINE simd Length(const Vec3Pack& V) noexcept { return V.Length(); }
178 static JPL_INLINE Vec3Pack& Normalize(Vec3Pack& V) noexcept { return V.Normalize(); }
179 [[nodiscard]] static JPL_INLINE Vec3Pack Normalized(const Vec3Pack& V) noexcept { return Vec3Pack(V).Normalize(); }
180 [[nodiscard]] static JPL_INLINE simd DotProduct(const Vec3Pack& A, const Vec3Pack& B) noexcept { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; }
181 [[nodiscard]] static JPL_INLINE Vec3Pack CrossProduct(const Vec3Pack& A, const Vec3Pack& B) noexcept
182 {
183 return Vec3Pack{
184 A.Y * B.Z - A.Z * B.Y,
185 A.Z * B.X - A.X * B.Z,
186 A.X * B.Y - A.Y * B.X
187 };
188 }
189
190 [[nodiscard]] static JPL_INLINE Vec3Pack Abs(const Vec3Pack& V) noexcept { return { abs(V.X), abs(V.Y), abs(V.Z) }; }
191
192 inline std::ostream& operator<<(std::ostream& os, const Vec3Pack& v)
193 {
194 os << "{ " << v.X << ", " << v.Y << ", " << v.Z << " }"; return os;
195 }
196
197 namespace Math
198 {
199 [[nodiscard]] static JPL_INLINE Vec3Pack FMA(const Vec3Pack& mulA, const Vec3Pack& mulB, const Vec3Pack& addC) noexcept
200 {
201 return Vec3Pack{
202 fma(mulA.X, mulB.X, addC.X),
203 fma(mulA.Y, mulB.Y, addC.Y),
204 fma(mulA.Z, mulB.Z, addC.Z),
205 };
206 }
207
208 [[nodiscard]] static JPL_INLINE Vec3Pack FMA(const Vec3Pack& mulA, const simd& mulB, const Vec3Pack& addC) noexcept
209 {
210 return Vec3Pack{
211 fma(mulA.X, mulB, addC.X),
212 fma(mulA.Y, mulB, addC.Y),
213 fma(mulA.Z, mulB, addC.Z),
214 };
215 }
216
217 [[nodiscard]] static JPL_INLINE Vec3Pack FMA(const simd& mulA, const Vec3Pack& mulB, const Vec3Pack& addC) noexcept
218 {
219 return Vec3Pack{
220 fma(mulA, mulB.X, addC.X),
221 fma(mulA, mulB.Y, addC.Y),
222 fma(mulA, mulB.Z, addC.Z),
223 };
224 }
225 }
226} // namespace JPL
#define JPL_ASSERT(inExpression,...)
Main assert macro, usage: JPL_ASSERT(condition, message) or JPL_ASSERT(condition)
Definition ErrorReporting.h:76
JPL_INLINE constexpr T FMA(T a, T b, T c) noexcept
Inlined fuse multiply-add. Compiler in some circumstances is more eager to optimize this than std::fm...
Definition Math.h:186
JPL_INLINE constexpr T Sqrt(T x) noexcept
Definition Math.h:269
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
JPL_INLINE simd InvSqrtFast(const simd &vec) noexcept
Definition SIMD.h:1783
Definition AcousticMaterial.h:36
JPL_INLINE void SetZ(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:41
JPL_INLINE auto GetX(const Vec3Type &v) noexcept
Definition Vec3Traits.h:35
JPL_INLINE void SetY(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:40
JPL_INLINE auto GetZ(const Vec3Type &v) noexcept
Definition Vec3Traits.h:37
JPL_INLINE constexpr Vec2 operator/(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:64
std::uint32_t uint32
Definition Core.h:311
JPL_INLINE constexpr bool operator==(const Vec2 &A, const Vec2 &B) noexcept
Definition MinimalVec2.h:59
JPL_INLINE auto GetY(const Vec3Type &v) noexcept
Definition Vec3Traits.h:36
JPL_INLINE simd fma(const simd &mul1, const simd &mul2, const simd &addV) noexcept
Element-wise fused multiply-add.
Definition SIMD.h:1843
JPL_INLINE constexpr Vec2 operator*(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:62
JPL_INLINE void SetX(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:39
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
JPL_INLINE simd abs(const simd &vec) noexcept
Definition SIMD.h:1827
std::ostream & operator<<(std::ostream &os, const AcousticMaterial &v)
Definition AcousticMaterial.h:104
Structure to pass around per axis mask parameters.
Definition Vec3Pack.h:34
simd_mask X
Definition Vec3Pack.h:35
simd_mask Y
Definition Vec3Pack.h:35
simd_mask Z
Definition Vec3Pack.h:35
Minimal Vec3 class for SIMD operations on 4 Vec3 at a time.
Definition Vec3Pack.h:41
JPL_INLINE bool IsEqual(const Vec3Pack &other) const
Check if all components of all lanes match.
Definition Vec3Pack.h:162
JPL_INLINE void store(std::span< Vec3Type > outVecs) const
Definition Vec3Pack.h:80
JPL_INLINE Vec3Pack & operator/=(simd Scale) noexcept
Definition Vec3Pack.h:156
JPL_INLINE Vec3Pack operator*(simd Scale) const noexcept
Definition Vec3Pack.h:143
static JPL_INLINE Vec3Pack select(const Vec3MaskPack &conditionMasks, const Vec3Pack &ifTrue, const Vec3Pack &ifFalse)
Definition Vec3Pack.h:108
JPL_INLINE Vec3Pack(const Vec3Type &vec) noexcept
Definition Vec3Pack.h:50
JPL_INLINE Vec3Pack operator-(const Vec3Pack &V) const
Definition Vec3Pack.h:135
JPL_INLINE Vec3Pack & operator*=(simd Scale) noexcept
Definition Vec3Pack.h:157
JPL_INLINE Vec3Pack operator-() const
Definition Vec3Pack.h:134
static JPL_INLINE Vec3Pack select(const simd_mask &conditionMask, const Vec3Pack &ifTrue, const Vec3Pack &ifFalse)
Definition Vec3Pack.h:97
JPL_INLINE void load(std::span< const Vec3Type > inVecs)
Definition Vec3Pack.h:66
JPL_INLINE Vec3Pack(const Vec3Type &vec1, const Vec3Type &vec2, const Vec3Type &vec3, const Vec3Type &vec4)
Definition Vec3Pack.h:55
JPL_INLINE Vec3Pack(float x, float y, float z) noexcept
Definition Vec3Pack.h:45
JPL_INLINE Vec3Pack & Normalize() noexcept
Definition Vec3Pack.h:122
JPL_INLINE Vec3Pack(simd x, simd y, simd z) noexcept
Definition Vec3Pack.h:47
JPL_INLINE Vec3Pack operator+(const Vec3Pack &V) const
Definition Vec3Pack.h:136
JPL_INLINE void store(float *outX, float *outY, float *outZ) const
Pointers must point to storate allocated for at least simd::size() num of floats each.
Definition Vec3Pack.h:74
simd Z
Definition Vec3Pack.h:42
JPL_INLINE void operator+=(const Vec3Pack &V) noexcept
Definition Vec3Pack.h:147
simd X
Definition Vec3Pack.h:42
Vec3Pack()=default
JPL_INLINE Vec3Pack(const float *x, const float *y, const float *z) noexcept
Definition Vec3Pack.h:46
JPL_INLINE Vec3Pack & operator*=(const Vec3Pack Other)
Definition Vec3Pack.h:148
static JPL_INLINE Vec3Pack Zero() noexcept
Definition Vec3Pack.h:117
JPL_INLINE simd LengthSquared() const noexcept
Definition Vec3Pack.h:119
JPL_INLINE Vec3Pack(const simd &axis) noexcept
Definition Vec3Pack.h:52
simd Y
Definition Vec3Pack.h:42
JPL_INLINE Vec3Pack operator/(simd Scale) const noexcept
Definition Vec3Pack.h:144
JPL_INLINE simd Length() const noexcept
Definition Vec3Pack.h:120
Definition SIMD.h:207
Minimal 4-wide 32-bit float vector implementation for SIMD.
Definition SIMD.h:60
static JPL_INLINE simd select(const simd_mask &mask, const simd &a, const simd &b) noexcept
Component-wise select, returns 'a' if mask is true, 'b' otherwise.
Definition SIMD.h:1034
JPL_INLINE void store(float *mem) const
Store values from simd to provided memory location.
Definition SIMD.h:573
static constexpr std::size_t size() noexcept
Get number of element of the vector.
Definition SIMD.h:97