diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/Vector4Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/Vector4Decl.inl index 2352cea..4ed98b3 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/Vector4Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/Vector4Decl.inl @@ -6,6 +6,9 @@ namespace Phanes::Core::Math::Detail { + template + struct construct_vec4 {}; + template struct compute_vec4_add {}; @@ -31,6 +34,61 @@ namespace Phanes::Core::Math::Detail struct compute_vec4_dec {}; + + template + struct construct_vec4 + { + static constexpr void map(Phanes::Core::Math::TVector4& v1, const TVector4& v2) + { + v1.x = v2.x; + v1.y = v2.y; + v1.z = v2.z; + v1.w = v2.w; + } + + + static constexpr void map(Phanes::Core::Math::TVector4& v1, T s) + { + v1.x = s; + v1.y = s; + v1.z = s; + v1.w = s; + } + + static constexpr void map(Phanes::Core::Math::TVector4& v1, T x, T y, T z, T w) + { + v1.x = x; + v1.y = y; + v1.z = z; + v1.w = w; + } + + /*static constexpr void map(Phanes::Core::Math::TVector4& v1, const Phanes::Core::Math::TVector2& v2, const Phanes::Core::Math::TVector2& v3) + { + v1.x = v2.x; + v1.y = v2.y; + v1.z = v3.x; + v1.w = v3.y; + } + + static constexpr void map(Phanes::Core::Math::TVector4& v1, const Phanes::Core::Math::TVector2& v2, const Phanes::Core::Math::TVector2& v3) + { + v1.x = v2.x; + v1.y = v2.y; + v1.z = v3.x; + v1.w = v3.y; + }*/ + + static constexpr void map(Phanes::Core::Math::TVector4& v1, const T* comp) + { + v1.x = comp[0]; + v1.y = comp[1]; + v1.z = comp[2]; + v1.w = comp[3]; + } + }; + + template struct compute_vec4_add { diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/Alignment.h b/Engine/Source/Runtime/Core/public/Math/SIMD/Alignment.h new file mode 100644 index 0000000..61c7fdb --- /dev/null +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/Alignment.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Core/public/Math/Boilerplate.h" + + +namespace Phanes::Core::Math::SIMD +{ + + // Structure to conveniently align arrays. + template + struct alignas(sizeof(T) * 4) AlignedVec + { + public: + T data[L]; + + AlignedVec(const T* n_aligned_data) + { + for (size_t i = 0; i < L; ++i) + { + data[i] = n_aligned_data[i]; + } + } + + const T* Get() + { + return data; + } + }; + +} diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h index 6ff9116..d0ae2aa 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h @@ -31,7 +31,7 @@ namespace Phanes::Core::Math::SIMD template struct use_simd { - bool value = false; + static const bool value = false; }; @@ -40,37 +40,37 @@ namespace Phanes::Core::Math::SIMD template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; template<> struct use_simd { - bool value = true && (P_SSE__ || P_NEON__); + static const bool value = true && (P_SSE__ || P_NEON__); }; // SSE @@ -78,19 +78,19 @@ namespace Phanes::Core::Math::SIMD template<> struct use_simd { - bool value = true && P_SSE__; + static const bool value = true && P_SSE__; }; template<> struct use_simd { - bool value = true && P_SSE__; + static const bool value = true && P_SSE__; }; template<> struct use_simd { - bool value = true && P_SSE__; + static const bool value = true && P_SSE__; }; @@ -100,19 +100,19 @@ namespace Phanes::Core::Math::SIMD template<> struct use_simd { - bool value = true && P_AVX__; + static const bool value = true && P_AVX__; }; template<> struct use_simd { - bool value = true && P_AVX__; + static const bool value = true && P_AVX__; }; template<> struct use_simd { - bool value = true && P_AVX__; + static const bool value = true && P_AVX__; }; @@ -121,37 +121,37 @@ namespace Phanes::Core::Math::SIMD template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; template<> struct use_simd { - bool value = true && P_AVX2__; + static const bool value = true && P_AVX2__; }; } diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp index f0815c7..9e4294b 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp @@ -6,6 +6,7 @@ #include "Core/public/Math/Boilerplate.h" #include "Core/public/Math/MathCommon.hpp" + #include // -> For IntelliSense @@ -17,7 +18,7 @@ // ========== // -Phanes::Core::Types::Vec4f32Reg p_vec4_abs(const Phanes::Core::Types::Vec4f32Reg& v) +Phanes::Core::Types::Vec4f32Reg vec4_abs(const Phanes::Core::Types::Vec4f32Reg& v) { return _mm_and_ps(v, _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF))); } @@ -34,6 +35,47 @@ namespace Phanes::Core::Math::Detail { // Template class has already been defined and is included through: Storage.h -> Vector4.hpp -> SIMDIntrinsics.h -> PhanesVectorMathSEE.hpp + + template<> + struct construct_vec4 + { + static FORCEINLINE void map(Phanes::Core::Math::TVector4& v1, const TVector4& v2) + { + v1.comp = _mm_set_ps(v2.x, v2.y, v2.z, v2.w); + } + + + static FORCEINLINE void map(Phanes::Core::Math::TVector4& v1, float s) + { + v1.comp = _mm_set_ps1(s); + } + + static FORCEINLINE void map(Phanes::Core::Math::TVector4& v1, float x, float y, float z, float w) + { + v1.comp = _mm_set_ps(x, y, z, w); + } + + /*static constexpr void map(Phanes::Core::Math::TVector4& v1, const Phanes::Core::Math::TVector2& v2, const Phanes::Core::Math::TVector2& v3) + { + v1.comp = _mm_set_ps(v2.x, v2.y, v3.x, v3.y); + } + + static constexpr void map(Phanes::Core::Math::TVector4& v1, const Phanes::Core::Math::TVector2& v2, const Phanes::Core::Math::TVector2& v3) + { + v1.x = v2.x; + v1.y = v2.y; + v1.z = v3.x; + v1.w = v3.y; + }*/ + + static FORCEINLINE void map(Phanes::Core::Math::TVector4& v1, const float* s) + { + v1.comp = _mm_loadu_ps(s); + + } + }; + + template<> struct compute_vec4_add { diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h b/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h index f46ce25..1e2d7b5 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h @@ -5,7 +5,7 @@ #include "Core/public/Math/MathTypes.h" -namespace Phanes::Core::SIMD +namespace Phanes::Core::Math::SIMD { template struct Storage; diff --git a/Engine/Source/Runtime/Core/public/Math/Vector4.hpp b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp index 81f771d..1c4140b 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector4.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp @@ -24,22 +24,22 @@ namespace Phanes::Core::Math /// /// X component of vector /// - T x; + Real x; /// /// X component of vector /// - T y; + Real y; /// /// Z component of vector /// - T z; + Real z; /// /// W component of vector /// - T w; + Real w; }; /// @@ -47,8 +47,8 @@ namespace Phanes::Core::Math /// union { - typename Phanes::Core::SIMD::Storage<4, T, IsAlgined>::type comp; - typename Phanes::Core::SIMD::Storage<4, T, IsAlgined>::type data; + typename Phanes::Core::Math::SIMD::Storage<4, Real, SIMD::use_simd::value>::type comp; + typename Phanes::Core::Math::SIMD::Storage<4, Real, SIMD::use_simd::value>::type data; }; }; diff --git a/Engine/Source/Runtime/Core/public/Math/Vector4.inl b/Engine/Source/Runtime/Core/public/Math/Vector4.inl index f419752..faff256 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector4.inl +++ b/Engine/Source/Runtime/Core/public/Math/Vector4.inl @@ -15,44 +15,35 @@ namespace Phanes::Core::Math { template - TVector4::TVector4(const TVector4& v) : - x(v.x), - y(v.y), - z(v.z), - w(v.w) - {} + TVector4::TVector4(const TVector4& v) + { + Detail::construct_vec4::value>::map(*this, v); + } template - TVector4::TVector4(Real _x, Real _y, Real _z, Real _w) : - x(_x), - y(_y), - z(_z), - w(_w) - {} + TVector4::TVector4(Real _x, Real _y, Real _z, Real _w) + { + Detail::construct_vec4::value>::map(*this, _x, _y, _z, _w); + } template - Phanes::Core::Math::TVector4::TVector4(Real s) : - x(s), - y(s), - z(s), - w(s) - {} + Phanes::Core::Math::TVector4::TVector4(Real s) + { + Detail::construct_vec4::value>::map(*this, s); + } template - Phanes::Core::Math::TVector4::TVector4(const TVector2& v1, const TVector2& v2) : - x(v1.x), - y(v1.y), - z(v2.x), - w(v2.y) - {} + Phanes::Core::Math::TVector4::TVector4(const TVector2& v1, const TVector2& v2) + { + Detail::construct_vec4::value>::map(*this, v1, v2); + } template - Phanes::Core::Math::TVector4::TVector4(const Real* comp) : - x(comp[0]), - y(comp[1]), - z(comp[2]), - w(comp[3]) - {} + Phanes::Core::Math::TVector4::TVector4(const Real* comp) + { + Detail::construct_vec4::value>::map(*this, comp); + } + template TVector4 operator+=(TVector4& v1, const TVector4& v2) @@ -177,13 +168,13 @@ namespace Phanes::Core::Math // Comparision template - TVector4 operator==(const TVector4& v1, const TVector4& v2) + bool operator==(const TVector4& v1, const TVector4& v2) { return Detail::compute_vec4_eq::map(v1, v2); } template - TVector4 operator!=(const TVector4& v1, const TVector4& v2) + bool operator!=(const TVector4& v1, const TVector4& v2) { return Detail::compute_vec4_ieq::map(v1, v2); } @@ -226,49 +217,4 @@ namespace Phanes::Core::Math { return --v1; } - - - // SIMD constructor - - template<> - TVector4::TVector4(const TVector4& v) - { - this->comp = _mm_load_ps(reinterpret_cast(&v)); - } - - template<> - TVector4::TVector4(float _x, float _y, float _z, float _w) : - x(_x), - y(_y), - z(_z), - w(_w) - { - this->comp = _mm_load_ps(reinterpret_cast(&this->x)); - } - - template<> - TVector4::TVector4(float s) - { - this->comp = _mm_load_ps1(&s); - } - - template<> - TVector4::TVector4(const TVector2& v1, const TVector2& v2) : - x(v1.x), - y(v1.y), - z(v2.x), - w(v2.y) - { - this->comp = _mm_load_ps(reinterpret_cast(&this->x)); - } - - template<> - TVector4::TVector4(const float* comp) : - x(comp[0]), - y(comp[1]), - z(comp[2]), - w(comp[3]) - { - this->comp = _mm_load_ps(reinterpret_cast(&this->x)); - } } \ No newline at end of file