Merge branch 'feature/add-expressions'
This commit is contained in:
commit
23b5a2aa0e
80
premake5.lua
80
premake5.lua
@ -6,46 +6,6 @@ workspace "tenvex"
|
||||
|
||||
outputdir = "%{cfg.system}/x64/%{cfg.buildcfg}"
|
||||
|
||||
project "tenvex"
|
||||
location "build"
|
||||
kind "None"
|
||||
language "C++"
|
||||
cppdialect "C++20"
|
||||
staticruntime "on"
|
||||
|
||||
targetdir ("bin/" .. outputdir .. "/output")
|
||||
objdir ("bin/" .. outputdir .. "/intermediate")
|
||||
|
||||
files {
|
||||
"src/tenvex/**.h",
|
||||
"src/tenvex/**.hpp",
|
||||
}
|
||||
|
||||
includedirs {
|
||||
"src/tenvex",
|
||||
}
|
||||
|
||||
filter "configurations:debug"
|
||||
runtime "Debug"
|
||||
symbols "on"
|
||||
optimize "off"
|
||||
defines { "TENVEX_DEBUG" }
|
||||
|
||||
filter "configurations:release"
|
||||
runtime "Release"
|
||||
symbols "on"
|
||||
optimize "Speed"
|
||||
defines { "TENVEX_RELEASE" }
|
||||
|
||||
filter "system:windows"
|
||||
systemversion "latest"
|
||||
|
||||
filter "system:linux"
|
||||
pic "on"
|
||||
|
||||
filter "system:macosx"
|
||||
pic "on"
|
||||
|
||||
project "gtest"
|
||||
location "build"
|
||||
kind "StaticLib"
|
||||
@ -116,6 +76,46 @@ project "gtest_main"
|
||||
filter "system:windows"
|
||||
systemversion "latest"
|
||||
|
||||
project "tenvex"
|
||||
location "build"
|
||||
kind "None"
|
||||
language "C++"
|
||||
cppdialect "C++20"
|
||||
staticruntime "on"
|
||||
|
||||
targetdir ("bin/" .. outputdir .. "/output")
|
||||
objdir ("bin/" .. outputdir .. "/intermediate")
|
||||
|
||||
files {
|
||||
"src/tenvex/**.h",
|
||||
"src/tenvex/**.hpp",
|
||||
}
|
||||
|
||||
includedirs {
|
||||
"src/tenvex",
|
||||
}
|
||||
|
||||
filter "configurations:debug"
|
||||
runtime "Debug"
|
||||
symbols "on"
|
||||
optimize "off"
|
||||
defines { "TENVEX_DEBUG" }
|
||||
|
||||
filter "configurations:release"
|
||||
runtime "Release"
|
||||
symbols "on"
|
||||
optimize "Speed"
|
||||
defines { "TENVEX_RELEASE" }
|
||||
|
||||
filter "system:windows"
|
||||
systemversion "latest"
|
||||
|
||||
filter "system:linux"
|
||||
pic "on"
|
||||
|
||||
filter "system:macosx"
|
||||
pic "on"
|
||||
|
||||
project "tenvex_tests"
|
||||
location "build"
|
||||
kind "ConsoleApp"
|
||||
|
||||
28
src/tenvex/internal/add.h
Normal file
28
src/tenvex/internal/add.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
struct Add : Expr<Add<L, R>>
|
||||
{
|
||||
inline
|
||||
Add(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<L> _l;
|
||||
tnvx_type_storage<R> _r;
|
||||
};
|
||||
|
||||
template<expression L, expression R>
|
||||
[[nodiscard]] inline
|
||||
Add<L, R> operator+(const Expr<L>& l, const Expr<R>& r) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "add_impl.hpp"
|
||||
30
src/tenvex/internal/add_impl.hpp
Normal file
30
src/tenvex/internal/add_impl.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "add.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
Add<L, R>::Add(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept
|
||||
: _l(l)
|
||||
, _r(r)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
__m128 Add<L, R>::eval() const noexcept
|
||||
{
|
||||
return _mm_add_ps(_l.eval(), _r.eval());
|
||||
}
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
Add<L, R> operator+(const Expr<L>& l, const Expr<R>& r) noexcept
|
||||
{
|
||||
return { l.self(), r.self() };
|
||||
}
|
||||
|
||||
} // namespace tnvx
|
||||
28
src/tenvex/internal/cross.h
Normal file
28
src/tenvex/internal/cross.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
struct Cross : Expr<Cross<L, R>>
|
||||
{
|
||||
inline
|
||||
Cross(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<L> _l;
|
||||
tnvx_type_storage<R> _r;
|
||||
};
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
[[nodiscard]] inline
|
||||
Cross<L, R> cross(const Expr<L>& l, const Expr<L>& r) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "cross_impl.hpp"
|
||||
47
src/tenvex/internal/cross_impl.hpp
Normal file
47
src/tenvex/internal/cross_impl.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "cross.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
Cross<L, R>::Cross(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept
|
||||
: _l(l)
|
||||
, _r(r)
|
||||
{
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
__m128 Cross<L, R>::eval() const noexcept
|
||||
{
|
||||
__m128 a = _l.eval();
|
||||
__m128 b = _r.eval();
|
||||
|
||||
// [ay, az, ax, 0]
|
||||
__m128 a_yzx = _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 0, 2, 1));
|
||||
// [by, bz, bx, 0]
|
||||
__m128 b_yzx = _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 0, 2, 1));
|
||||
|
||||
// [ay*bz, az*bx, ax*by, 0]
|
||||
__m128 mul0 = _mm_mul_ps(a, b_yzx);
|
||||
// [az*by, ax*bz, ay*bx, 0]
|
||||
__m128 mul1 = _mm_mul_ps(a_yzx, b);
|
||||
|
||||
// [ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx, 0]
|
||||
__m128 sub = _mm_sub_ps(mul0, mul1);
|
||||
|
||||
// shuffle обратно в [x, y, z, 0]
|
||||
return _mm_shuffle_ps(sub, sub, _MM_SHUFFLE(3, 0, 2, 1));
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
Cross<L, R> cross(const Expr<L>& l, const Expr<R>& r) noexcept
|
||||
{
|
||||
return { l.self(), r.self() };
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
12
src/tenvex/internal/defines.h
Normal file
12
src/tenvex/internal/defines.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define TNVX_INLINE __forceinline
|
||||
#define TNVX_RESTRICT __restrict
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#define TNVX_INLINE __attribute__((always_inline)) inline
|
||||
#define TNVX_RESTRICT __restrict__
|
||||
#else
|
||||
#define TNVX_INLINE inline
|
||||
#define TNVX_RESTRICT
|
||||
#endif
|
||||
39
src/tenvex/internal/dot.h
Normal file
39
src/tenvex/internal/dot.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
struct Dot : Expr<Dot<L, R>>
|
||||
{
|
||||
inline
|
||||
Dot(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
operator float() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<L> _l;
|
||||
tnvx_type_storage<R> _r;
|
||||
};
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
[[nodiscard]] inline
|
||||
Dot<L, R> dot(const Expr<L>& l, const Expr<R>& r) noexcept;
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
[[nodiscard]] inline
|
||||
float operator*(const Dot<L, R>& l, float r) noexcept;
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
[[nodiscard]] inline
|
||||
float operator*(float l, const Dot<L, R>& r) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "dot_impl.hpp"
|
||||
60
src/tenvex/internal/dot_impl.hpp
Normal file
60
src/tenvex/internal/dot_impl.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include "dot.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
Dot<L, R>::Dot(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept
|
||||
: _l(l)
|
||||
, _r(r)
|
||||
{
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
__m128 Dot<L, R>::eval() const noexcept
|
||||
{
|
||||
__m128 a = _l.eval();
|
||||
__m128 b = _r.eval();
|
||||
|
||||
__m128 mul = _mm_mul_ps(a, b);
|
||||
__m128 shuf = _mm_shuffle_ps(mul, mul, _MM_SHUFFLE(2, 3, 0, 1));
|
||||
__m128 sum = _mm_add_ps(mul, shuf);
|
||||
shuf = _mm_movehl_ps(shuf, sum);
|
||||
sum = _mm_add_ss(sum, shuf);
|
||||
return _mm_shuffle_ps(sum, sum, 0);
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
Dot<L, R>::operator float() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(eval());
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
Dot<L, R> dot(const Expr<L>& l, const Expr<R>& r) noexcept
|
||||
{
|
||||
return { l.self(), r.self() };
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
TNVX_INLINE
|
||||
float operator*(const Dot<L, R>& l, float r) noexcept
|
||||
{
|
||||
return static_cast<float>(l) * r;
|
||||
}
|
||||
|
||||
template<vector_expression L, vector_expression R>
|
||||
float operator*(float l, const Dot<L, R>& r) noexcept
|
||||
{
|
||||
return l * static_cast<float>(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
47
src/tenvex/internal/expression.h
Normal file
47
src/tenvex/internal/expression.h
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
#include <immintrin.h>
|
||||
#include <cstdint>
|
||||
#include <concepts>
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<typename Derived>
|
||||
struct Expr
|
||||
{
|
||||
[[nodiscard]] inline
|
||||
const Derived& self() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float operator[](size_t i) const noexcept;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept expression = requires(T a)
|
||||
{
|
||||
{ a.eval() } -> std::same_as<__m128>;
|
||||
}
|
||||
&& std::derived_from<T, Expr<T>>;
|
||||
|
||||
struct Scalar;
|
||||
|
||||
template<typename T>
|
||||
concept vector_expression = expression<T> && !std::same_as<T, Scalar>;
|
||||
|
||||
template<typename T>
|
||||
constexpr bool tnvx_store_by_value = std::is_trivially_copyable_v<T>
|
||||
&& std::is_trivially_destructible_v<T>
|
||||
&& sizeof(T) <= 16;
|
||||
|
||||
template<typename T>
|
||||
using tnvx_type_storage = std::conditional_t<tnvx_store_by_value<T>, T, const T&>;
|
||||
|
||||
} // namespace tnvx
|
||||
|
||||
#include "expression_impl.hpp"
|
||||
31
src/tenvex/internal/expression_impl.hpp
Normal file
31
src/tenvex/internal/expression_impl.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<typename Derived>
|
||||
TNVX_INLINE
|
||||
const Derived& Expr<Derived>::self() const noexcept
|
||||
{
|
||||
return static_cast<const Derived&>(*this);
|
||||
}
|
||||
|
||||
template<typename Derived>
|
||||
TNVX_INLINE
|
||||
__m128 Expr<Derived>::eval() const noexcept
|
||||
{
|
||||
return self().eval();
|
||||
}
|
||||
|
||||
template<typename Derived>
|
||||
TNVX_INLINE
|
||||
float Expr<Derived>::operator[](size_t i) const noexcept
|
||||
{
|
||||
alignas(16) float tmp[4];
|
||||
_mm_store_ps(tmp, eval());
|
||||
return tmp[i];
|
||||
}
|
||||
|
||||
} // namespace tnvx
|
||||
30
src/tenvex/internal/magnitude.h
Normal file
30
src/tenvex/internal/magnitude.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression E>
|
||||
struct Magnitude : Expr<Magnitude<E>>
|
||||
{
|
||||
inline
|
||||
Magnitude(const E& TNVX_RESTRICT e) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
operator float() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<E> _e;
|
||||
};
|
||||
|
||||
template<vector_expression E>
|
||||
[[nodiscard]] inline
|
||||
Magnitude<E> magnitude(const Expr<E>& e) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "magnitude_impl.hpp"
|
||||
37
src/tenvex/internal/magnitude_impl.hpp
Normal file
37
src/tenvex/internal/magnitude_impl.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "magnitude.h"
|
||||
#include "dot.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
Magnitude<E>::Magnitude(const E& TNVX_RESTRICT e) noexcept
|
||||
: _e(e)
|
||||
{
|
||||
}
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
__m128 Magnitude<E>::eval() const noexcept
|
||||
{
|
||||
return _mm_sqrt_ss(Dot<E, E>(_e, _e).eval());
|
||||
}
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
Magnitude<E>::operator float() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(eval());
|
||||
}
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
Magnitude<E> magnitude(const Expr<E>& e) noexcept
|
||||
{
|
||||
return { e.self() };
|
||||
}
|
||||
|
||||
}
|
||||
37
src/tenvex/internal/mul.h
Normal file
37
src/tenvex/internal/mul.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
#include "scalar.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
struct Mul : Expr<Mul<L, R>>
|
||||
{
|
||||
inline
|
||||
Mul(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<L> _l;
|
||||
tnvx_type_storage<R> _r;
|
||||
};
|
||||
|
||||
//template<vector_expression L, vector_expression R>
|
||||
//[[nodiscard]] inline
|
||||
//Mul<L, R> operator*(const Expr<L>& l, const Expr<R>& r) noexcept;
|
||||
|
||||
template<expression E>
|
||||
[[nodiscard]] inline
|
||||
Mul<E, Scalar> operator*(const Expr<E>& l, float r) noexcept;
|
||||
|
||||
template<expression E>
|
||||
[[nodiscard]] inline
|
||||
Mul<Scalar, E> operator*(float l, const Expr<E>& r) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "mul_impl.hpp"
|
||||
44
src/tenvex/internal/mul_impl.hpp
Normal file
44
src/tenvex/internal/mul_impl.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "mul.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
inline Mul<L, R>::Mul(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept
|
||||
: _l(l)
|
||||
, _r(r)
|
||||
{
|
||||
}
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
__m128 Mul<L, R>::eval() const noexcept
|
||||
{
|
||||
return _mm_mul_ps(_l.eval(), _r.eval());
|
||||
}
|
||||
|
||||
//template<vector_expression L, vector_expression R>
|
||||
//TNVX_INLINE
|
||||
//Mul<L, R> operator*(const Expr<L>& l, const Expr<R>& r) noexcept
|
||||
//{
|
||||
// return { l.self(), r.self() };
|
||||
//}
|
||||
|
||||
template<expression E>
|
||||
TNVX_INLINE
|
||||
Mul<E, Scalar> operator*(const Expr<E>& l, float r) noexcept
|
||||
{
|
||||
return { l.self(), Scalar(r) };
|
||||
}
|
||||
|
||||
template<expression E>
|
||||
TNVX_INLINE
|
||||
Mul<Scalar, E> operator*(float l, const Expr<E>& r) noexcept
|
||||
{
|
||||
return { Scalar(l), r.self() };
|
||||
}
|
||||
|
||||
}
|
||||
27
src/tenvex/internal/norm.h
Normal file
27
src/tenvex/internal/norm.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression E>
|
||||
struct Norm : Expr<Norm<E>>
|
||||
{
|
||||
inline
|
||||
Norm(const E& TNVX_RESTRICT e) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<E> _e;
|
||||
};
|
||||
|
||||
template<vector_expression E>
|
||||
[[nodiscard]] inline
|
||||
Norm<E> norm(const Expr<E>& e) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "norm_impl.hpp"
|
||||
37
src/tenvex/internal/norm_impl.hpp
Normal file
37
src/tenvex/internal/norm_impl.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "norm.h"
|
||||
#include "dot.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
Norm<E>::Norm(const E& TNVX_RESTRICT e) noexcept
|
||||
: _e(e)
|
||||
{
|
||||
}
|
||||
|
||||
template<vector_expression E>
|
||||
TNVX_INLINE
|
||||
__m128 Norm<E>::eval() const noexcept
|
||||
{
|
||||
__m128 data = _e.eval();
|
||||
__m128 mul = _mm_mul_ps(data, data);
|
||||
__m128 shuf = _mm_shuffle_ps(mul, mul, _MM_SHUFFLE(2, 3, 0, 1));
|
||||
__m128 sum = _mm_add_ps(mul, shuf);
|
||||
shuf = _mm_movehl_ps(shuf, sum);
|
||||
sum = _mm_add_ss(sum, shuf);
|
||||
__m128 len = _mm_shuffle_ps(sum, sum, 0);
|
||||
__m128 sqrt_len = _mm_sqrt_ps(len);
|
||||
return _mm_div_ps(data, sqrt_len);
|
||||
}
|
||||
|
||||
template<vector_expression E>
|
||||
Norm<E> norm(const Expr<E>& e) noexcept
|
||||
{
|
||||
return { e.self() };
|
||||
}
|
||||
|
||||
}
|
||||
25
src/tenvex/internal/scalar.h
Normal file
25
src/tenvex/internal/scalar.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
struct Scalar : Expr<Scalar>
|
||||
{
|
||||
inline
|
||||
Scalar(float value) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float operator[](size_t) const noexcept;
|
||||
|
||||
private:
|
||||
float _value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "scalar_impl.hpp"
|
||||
26
src/tenvex/internal/scalar_impl.hpp
Normal file
26
src/tenvex/internal/scalar_impl.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "scalar.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
TNVX_INLINE
|
||||
Scalar::Scalar(float value) noexcept
|
||||
: _value(value)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] TNVX_INLINE
|
||||
__m128 Scalar::eval() const noexcept
|
||||
{
|
||||
return _mm_set1_ps(_value);
|
||||
}
|
||||
|
||||
[[nodiscard]] TNVX_INLINE
|
||||
float Scalar::operator[](size_t) const noexcept
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
}
|
||||
28
src/tenvex/internal/sub.h
Normal file
28
src/tenvex/internal/sub.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "expression.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
struct Sub : Expr<Sub<L, R>>
|
||||
{
|
||||
inline
|
||||
Sub(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
private:
|
||||
tnvx_type_storage<L> _l;
|
||||
tnvx_type_storage<R> _r;
|
||||
};
|
||||
|
||||
template<expression L, expression R>
|
||||
[[nodiscard]] inline
|
||||
Sub<L, R> operator-(const Expr<L>& l, const Expr<R>& r) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "sub_impl.hpp"
|
||||
28
src/tenvex/internal/sub_impl.hpp
Normal file
28
src/tenvex/internal/sub_impl.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "sub.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<expression L, expression R>
|
||||
tnvx::Sub<L, R>::Sub(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept
|
||||
: _l(l), _r(r)
|
||||
{
|
||||
}
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
__m128 Sub<L, R>::eval() const noexcept
|
||||
{
|
||||
return _mm_sub_ps(_l.eval(), _r.eval());
|
||||
}
|
||||
|
||||
template<expression L, expression R>
|
||||
TNVX_INLINE
|
||||
Sub<L, R> operator-(const Expr<L>& l, const Expr<R>& r) noexcept
|
||||
{
|
||||
return { l.self(), r.self() };
|
||||
}
|
||||
|
||||
}
|
||||
12
src/tenvex/tenvex.h
Normal file
12
src/tenvex/tenvex.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "vec4.h"
|
||||
|
||||
#include "internal/add.h"
|
||||
#include "internal/sub.h"
|
||||
#include "internal/mul.h"
|
||||
#include "internal/dot.h"
|
||||
#include "internal/cross.h"
|
||||
#include "internal/norm.h"
|
||||
#include "internal/magnitude.h"
|
||||
#include "internal/scalar.h"
|
||||
63
src/tenvex/vec4.h
Normal file
63
src/tenvex/vec4.h
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "internal/expression.h"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
struct alignas(16) vec4 : Expr<vec4>
|
||||
{
|
||||
vec4() = default;
|
||||
|
||||
inline
|
||||
vec4(float x, float y, float z, float w) noexcept;
|
||||
|
||||
template<typename E>
|
||||
inline
|
||||
vec4(const Expr<E>& expr) noexcept;
|
||||
|
||||
template<typename E>
|
||||
inline
|
||||
vec4& operator=(const Expr<E>& expr) noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
__m128 eval() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float magnitude() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
vec4 norm() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float x() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float y() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float z() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
float w() const noexcept;
|
||||
|
||||
[[nodiscard]] inline
|
||||
std::array<float, 4> data() const noexcept;
|
||||
|
||||
private:
|
||||
__m128 _data;
|
||||
};
|
||||
|
||||
inline
|
||||
bool operator==(const vec4& a, const vec4& b) noexcept;
|
||||
|
||||
inline
|
||||
bool approx_eq(const vec4& a, const vec4& b, float eps = 1e-3f) noexcept;
|
||||
|
||||
}
|
||||
|
||||
#include "vec4_impl.hpp"
|
||||
96
src/tenvex/vec4_impl.hpp
Normal file
96
src/tenvex/vec4_impl.hpp
Normal file
@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include "vec4.h"
|
||||
#include "internal/magnitude.h"
|
||||
#include "internal/norm.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
template<typename E>
|
||||
TNVX_INLINE
|
||||
vec4::vec4(const Expr<E>& expr) noexcept
|
||||
: _data(expr.eval())
|
||||
{
|
||||
}
|
||||
|
||||
template<typename E>
|
||||
TNVX_INLINE
|
||||
vec4& vec4::operator=(const Expr<E>& expr) noexcept
|
||||
{
|
||||
_data = expr.eval();
|
||||
return *this;
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
vec4::vec4(float x, float y, float z, float w) noexcept
|
||||
{
|
||||
_data = _mm_set_ps(w, z, y, x);
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
__m128 vec4::eval() const noexcept
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
float vec4::magnitude() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(Magnitude<vec4>(*this).eval());
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
vec4 vec4::norm() const noexcept
|
||||
{
|
||||
return { Norm<vec4>(*this) };
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
float vec4::x() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(_data);
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
float vec4::y() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(_mm_shuffle_ps(_data, _data, _MM_SHUFFLE(1, 1, 1, 1)));
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
float vec4::z() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(_mm_shuffle_ps(_data, _data, _MM_SHUFFLE(2, 2, 2, 2)));
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
float vec4::w() const noexcept
|
||||
{
|
||||
return _mm_cvtss_f32(_mm_shuffle_ps(_data, _data, _MM_SHUFFLE(3, 3, 3, 3)));
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
std::array<float, 4> vec4::data() const noexcept
|
||||
{
|
||||
std::array<float, 4> result;
|
||||
_mm_storeu_ps(result.data(), _data);
|
||||
return result;
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
bool operator==(const vec4& a, const vec4& b) noexcept
|
||||
{
|
||||
return _mm_movemask_ps(_mm_cmpeq_ps(a.eval(), b.eval())) == 0xF;
|
||||
}
|
||||
|
||||
TNVX_INLINE
|
||||
bool approx_eq(const vec4& a, const vec4& b, float eps) noexcept
|
||||
{
|
||||
__m128 epsilon = _mm_set1_ps(eps);
|
||||
__m128 diff = _mm_sub_ps(a.eval(), b.eval());
|
||||
__m128 abs_diff = _mm_andnot_ps(_mm_set1_ps(-0.0f), diff);
|
||||
return _mm_movemask_ps(_mm_cmplt_ps(abs_diff, epsilon)) == 0xF;
|
||||
}
|
||||
|
||||
}
|
||||
178
src/tests/vec4_tests.cpp
Normal file
178
src/tests/vec4_tests.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
#include "tenvex.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tnvx
|
||||
{
|
||||
|
||||
TEST(vec4, add)
|
||||
{
|
||||
vec4 a = { 1.0f, 2.0f, 3.0f, 4.0f };
|
||||
vec4 b = { 4.0f, 3.0f, 2.0f, 1.0f };
|
||||
vec4 check{ 5.0f, 5.0f, 5.0f, 5.0f };
|
||||
vec4 result = a + b;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, sub)
|
||||
{
|
||||
vec4 a = { 5.0f, 5.0f, 5.0f, 5.0f };
|
||||
vec4 b = { 4.0f, 3.0f, 2.0f, 1.0f };
|
||||
vec4 check = { 1.0f, 2.0f, 3.0f, 4.0f };
|
||||
vec4 result = a - b;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, mult_a)
|
||||
{
|
||||
vec4 a = { 1.0f, 2.0f, 0.0f, 0.0f };
|
||||
float b = 3.0f;
|
||||
vec4 check = { 3.0f, 6.0f, 0.0f, 0.0f };
|
||||
vec4 result = a * b;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, mult_b)
|
||||
{
|
||||
float a = 2.0f;
|
||||
vec4 b = { 1.0f, 0.0f, 2.0f, 0.0f };
|
||||
vec4 check = { 2.0f, 0.0f, 4.0f, 0.0f };
|
||||
vec4 result = a * b;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, dot)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 3.0f, 0.0f };
|
||||
vec4 b = { 1.0f, 2.0f, 3.0f, 1.0f };
|
||||
float check = 10.0f;
|
||||
float result = dot(a, b);
|
||||
|
||||
EXPECT_FLOAT_EQ(check, result);
|
||||
}
|
||||
|
||||
TEST(vec4, cross)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 b = { 0.0f, 1.0f, 0.0f, 0.0f };
|
||||
vec4 check = { 0.0f, 0.0f, 1.0f, 0.0f };
|
||||
vec4 result = cross(a, b);
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, norm_a)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 check = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 result = norm(a);
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, norm_b)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 1.0f, 0.0f };
|
||||
vec4 check = { 0.707106f, 0.0f, 0.707106f, 0.0f };
|
||||
vec4 result = norm(a);
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, magnitude_a)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
float check = 1.0f;
|
||||
float result = magnitude(a);
|
||||
|
||||
EXPECT_FLOAT_EQ(check, result);
|
||||
}
|
||||
|
||||
TEST(vec4, magnitude_b)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 1.0f, 0.0f };
|
||||
float check = 1.4142135f;
|
||||
float result = magnitude(a);
|
||||
|
||||
EXPECT_FLOAT_EQ(check, result);
|
||||
}
|
||||
|
||||
TEST(vec4, expression_a)
|
||||
{
|
||||
vec4 a = { 1.0f, 1.0f, 1.0f, 0.0f };
|
||||
vec4 b = { 0.0f, 1.0f, 0.0f, 0.0f };
|
||||
vec4 c = { 1.15f, 0.0f, 11.0f, 0.0f };
|
||||
|
||||
vec4 check = { 3.45f, 0.0f, 33.0f, 0.0f };
|
||||
|
||||
vec4 result = dot(a, b) * 3.0f * c;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, expression_b)
|
||||
{
|
||||
vec4 a = { 1.0f, 1.0f, 1.0f, 0.0f };
|
||||
vec4 b = { 0.0f, 1.0f, 0.0f, 0.0f };
|
||||
vec4 c = { 1.15f, 0.0f, 11.0f, 0.0f };
|
||||
|
||||
vec4 check = { 33.0f, 0.0f, -3.45f, 0.0f };
|
||||
|
||||
vec4 result = dot(a, b) * 3.0f * cross(b, c);
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, expression_c)
|
||||
{
|
||||
vec4 a = { 1.0f, 2.0f, 3.0f, 0.0f };
|
||||
vec4 b = { 4.0f, 5.0f, 6.0f, 0.0f };
|
||||
vec4 c = { 1.0f, 0.0f, 1.0f, 0.0f };
|
||||
|
||||
vec4 check = { 7.243f, 5.657f, 10.071f, 0.0f };
|
||||
|
||||
vec4 result = norm(a + b * 2.0f) * dot(b, c) + c * 3.0f;
|
||||
|
||||
EXPECT_TRUE(approx_eq(check, result));
|
||||
}
|
||||
|
||||
TEST(vec4, cross_anticommutative)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 b = { 0.0f, 1.0f, 0.0f, 0.0f };
|
||||
EXPECT_TRUE(approx_eq(cross(a, b), cross(b, a) * -1.0f));
|
||||
}
|
||||
|
||||
TEST(vec4, dot_commutative)
|
||||
{
|
||||
vec4 a = { 1.0f, 2.0f, 3.0f, 0.0f };
|
||||
vec4 b = { 4.0f, 5.0f, 6.0f, 0.0f };
|
||||
EXPECT_FLOAT_EQ(dot(a, b), dot(b, a));
|
||||
}
|
||||
|
||||
TEST(vec4, norm_magnitude)
|
||||
{
|
||||
vec4 a = { 3.0f, 1.0f, 4.0f, 0.0f };
|
||||
EXPECT_NEAR(magnitude(norm(a)), 1.0f, 1e-5f);
|
||||
}
|
||||
|
||||
TEST(vec4, dot_perpendicular)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 b = { 0.0f, 1.0f, 0.0f, 0.0f };
|
||||
EXPECT_FLOAT_EQ(dot(a, b), 0.0f);
|
||||
}
|
||||
|
||||
TEST(vec4, cross_parallel)
|
||||
{
|
||||
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 b = { 2.0f, 0.0f, 0.0f, 0.0f };
|
||||
vec4 check = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
EXPECT_TRUE(approx_eq(cross(a, b), check));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user