diff --git a/src/tenvex/expressions/add.h b/src/tenvex/expressions/add.h index 0dbb6a5..e9eac64 100644 --- a/src/tenvex/expressions/add.h +++ b/src/tenvex/expressions/add.h @@ -1,12 +1,10 @@ #pragma once -#include "defines.h" +#include "core.h" #include "expression.h" #include "concepts.h" #include "traits.h" -#include - namespace tnvx { @@ -17,7 +15,7 @@ struct Add : Expr> Add(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; [[nodiscard]] TNVX_INLINE - __m128 eval() const noexcept; + vf4 eval() const noexcept; private: tnvx_ref_or_value_t _l; @@ -26,7 +24,7 @@ private: template [[nodiscard]] TNVX_INLINE -Add operator+(const L& l, const R& r) noexcept; +Add operator+(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; template inline constexpr bool is_vec_expr> = true; diff --git a/src/tenvex/expressions/add_impl.hpp b/src/tenvex/expressions/add_impl.hpp index 1df6b07..f5b894f 100644 --- a/src/tenvex/expressions/add_impl.hpp +++ b/src/tenvex/expressions/add_impl.hpp @@ -1,6 +1,5 @@ #pragma once -#include "defines.h" #include "add.h" namespace tnvx @@ -16,14 +15,14 @@ Add::Add(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept template [[nodiscard]] TNVX_INLINE -__m128 Add::eval() const noexcept +vf4 Add::eval() const noexcept { - return _mm_add_ps(_l.eval(), _r.eval()); + return detail::add(_l.eval(), _r.eval()); } template [[nodiscard]] TNVX_INLINE -Add operator+(const L& l, const R& r) noexcept +Add operator+(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept { return { l, r }; } diff --git a/src/tenvex/expressions/concepts.h b/src/tenvex/expressions/concepts.h index 59db405..f15b4ea 100644 --- a/src/tenvex/expressions/concepts.h +++ b/src/tenvex/expressions/concepts.h @@ -11,7 +11,7 @@ namespace tnvx template concept expression = requires (T t) { - { t.eval() } -> std::same_as<__m128>; + { t.eval() } -> std::same_as; { t.self() } -> std::same_as; } && std::is_base_of_v, T>; diff --git a/src/tenvex/expressions/config.h b/src/tenvex/expressions/config.h new file mode 100644 index 0000000..0f866cf --- /dev/null +++ b/src/tenvex/expressions/config.h @@ -0,0 +1,3 @@ +#pragma once + +#define HAS_SSE42 1 diff --git a/src/tenvex/expressions/core.h b/src/tenvex/expressions/core.h new file mode 100644 index 0000000..c95f5f2 --- /dev/null +++ b/src/tenvex/expressions/core.h @@ -0,0 +1,88 @@ +#pragma once + +#include "config.h" +#include "defines.h" + +#if defined(HAS_SSE42) +#include +using vf4 = __m128; +#else +#endif + +namespace tnvx::detail +{ + +[[nodiscard]] TNVX_INLINE +bool eq(vf4 a, vf4 b) +{ + return _mm_movemask_ps(_mm_cmpeq_ps(a, b)) == 0b1111; +} + +[[nodiscard]] TNVX_INLINE +bool approx_eq(vf4 a, vf4 b, float eps) noexcept +{ + vf4 abs_diff = _mm_andnot_ps(_mm_set1_ps(-0.0f), _mm_sub_ps(a, b)); + return _mm_movemask_ps(_mm_cmple_ps(abs_diff, _mm_set1_ps(eps))) == 0b1111; +} + +[[nodiscard]] TNVX_INLINE +vf4 scalar(float value) noexcept +{ + return _mm_set_ps1(value); +} + +[[nodiscard]] TNVX_INLINE +vf4 add(vf4 l, vf4 r) noexcept +{ + return _mm_add_ps(l, r); +} + +[[nodiscard]] TNVX_INLINE +vf4 sub(vf4 l, vf4 r) noexcept +{ + return _mm_sub_ps(l, r); +} + +[[nodiscard]] TNVX_INLINE +vf4 mul(vf4 l, vf4 r) noexcept +{ + return _mm_mul_ps(l, r); +} + +[[nodiscard]] TNVX_INLINE +vf4 div(vf4 l, vf4 r) noexcept +{ + return _mm_div_ps(l, r); +} + +[[nodiscard]] TNVX_INLINE +vf4 dot3(vf4 l, vf4 r) noexcept +{ + return _mm_dp_ps(l, r, 0b01111111); +} + +[[nodiscard]] TNVX_INLINE +vf4 magnitude3(vf4 v) noexcept +{ + return _mm_sqrt_ps(_mm_dp_ps(v, v, 0b01111111)); +} + +[[nodiscard]] TNVX_INLINE +vf4 norm3(vf4 v) noexcept +{ + return _mm_blend_ps(_mm_div_ps(v, magnitude3(v)), v, 0b1000); +} + +[[nodiscard]] TNVX_INLINE +vf4 cross3(vf4 l, vf4 r) noexcept +{ + vf4 a_yzx = _mm_shuffle_ps(l, l, _MM_SHUFFLE(3, 0, 2, 1)); + vf4 b_yzx = _mm_shuffle_ps(r, r, _MM_SHUFFLE(3, 0, 2, 1)); + + vf4 t = _mm_sub_ps(_mm_mul_ps(l, b_yzx), + _mm_mul_ps(a_yzx, r)); + + return _mm_shuffle_ps(t, t, _MM_SHUFFLE(3, 0, 2, 1)); +} + +} diff --git a/src/tenvex/expressions/cross3.h b/src/tenvex/expressions/cross3.h new file mode 100644 index 0000000..7834f00 --- /dev/null +++ b/src/tenvex/expressions/cross3.h @@ -0,0 +1,33 @@ +#pragma once + +#include "core.h" +#include "expression.h" +#include "traits.h" +#include "concepts.h" + +namespace tnvx +{ + +template +struct Cross3 : Expr> +{ + TNVX_INLINE + Cross3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; + + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; + +private: + tnvx_ref_or_value_t _l; + tnvx_ref_or_value_t _r; +}; + +template +[[nodiscard]] TNVX_INLINE +Cross3 cross3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; + +template inline constexpr bool is_vec_expr> = true; + +} + +#include "cross3_impl.hpp" diff --git a/src/tenvex/expressions/cross3_impl.hpp b/src/tenvex/expressions/cross3_impl.hpp new file mode 100644 index 0000000..cd13a74 --- /dev/null +++ b/src/tenvex/expressions/cross3_impl.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "cross3.h" + +namespace tnvx +{ + +template +TNVX_INLINE +Cross3::Cross3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept + : _l(l) + , _r(r) +{ +} + +template +TNVX_INLINE +vf4 Cross3::eval() const noexcept +{ + return detail::cross3(_l.eval(), _r.eval()); +} + +template +TNVX_INLINE +Cross3 cross3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept +{ + return { l, r }; +} + +} diff --git a/src/tenvex/expressions/defines.h b/src/tenvex/expressions/defines.h index 4eb4064..ec5d154 100644 --- a/src/tenvex/expressions/defines.h +++ b/src/tenvex/expressions/defines.h @@ -9,4 +9,4 @@ #else #define TNVX_INLINE inline #define TNVX_RESTRICT -#endif \ No newline at end of file +#endif diff --git a/src/tenvex/expressions/div.h b/src/tenvex/expressions/div.h new file mode 100644 index 0000000..eabaf0d --- /dev/null +++ b/src/tenvex/expressions/div.h @@ -0,0 +1,35 @@ +#pragma once + +#include "core.h" +#include "expression.h" +#include "traits.h" +#include "concepts.h" + +#include "scalar.h" + +namespace tnvx +{ + +template +struct Div : Expr> +{ + TNVX_INLINE + Div(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; + + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; + +private: + tnvx_ref_or_value_t _l; + tnvx_ref_or_value_t _r; +}; + +template +[[nodiscard]] TNVX_INLINE +Div operator/(const E& TNVX_RESTRICT l, float r) noexcept; + +template inline constexpr bool is_vec_expr> = true; + +} + +#include "div_impl.hpp" diff --git a/src/tenvex/expressions/div_impl.hpp b/src/tenvex/expressions/div_impl.hpp new file mode 100644 index 0000000..a32acf9 --- /dev/null +++ b/src/tenvex/expressions/div_impl.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "div.h" + +namespace tnvx +{ + +template +TNVX_INLINE +tnvx::Div::Div(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept + : _l(l) + , _r(r) +{ +} + +template +TNVX_INLINE +vf4 Div::eval() const noexcept +{ + return detail::div(_l.eval(), _r.eval()); +} + +template +TNVX_INLINE +Div operator/(const E& TNVX_RESTRICT l, float r) noexcept +{ + return { l, Scalar(r) }; +} + +} diff --git a/src/tenvex/expressions/dot3.h b/src/tenvex/expressions/dot3.h new file mode 100644 index 0000000..f2984d2 --- /dev/null +++ b/src/tenvex/expressions/dot3.h @@ -0,0 +1,36 @@ +#pragma once + +#include "core.h" +#include "expression.h" +#include "traits.h" +#include "concepts.h" + +namespace tnvx +{ + +template +struct Dot3 : Expr> +{ + TNVX_INLINE + Dot3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; + + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; + + [[nodiscard]] TNVX_INLINE + operator float() const noexcept; + +private: + tnvx_ref_or_value_t _l; + tnvx_ref_or_value_t _r; +}; + +template +[[nodiscard]] TNVX_INLINE +Dot3 dot3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; + +template inline constexpr bool is_scalar_expr> = true; + +} + +#include "dot3_impl.h" diff --git a/src/tenvex/expressions/dot3_impl.h b/src/tenvex/expressions/dot3_impl.h new file mode 100644 index 0000000..ba55fdc --- /dev/null +++ b/src/tenvex/expressions/dot3_impl.h @@ -0,0 +1,37 @@ +#pragma once + +#include "dot3.h" + +namespace tnvx +{ + +template +TNVX_INLINE +Dot3::Dot3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept + : _l(l) + , _r(r) +{ +} + +template +TNVX_INLINE +vf4 Dot3::eval() const noexcept +{ + return detail::dot3(_l.eval(), _r.eval()); +} + +template +TNVX_INLINE +Dot3::operator float() const noexcept +{ + return _mm_cvtss_f32(eval()); +} + +template +TNVX_INLINE +Dot3 dot3(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept +{ + return { l, r }; +} + +} diff --git a/src/tenvex/expressions/expression.h b/src/tenvex/expressions/expression.h index f309e5e..14e2514 100644 --- a/src/tenvex/expressions/expression.h +++ b/src/tenvex/expressions/expression.h @@ -1,8 +1,6 @@ #pragma once -#include "defines.h" - -#include +#include "core.h" namespace tnvx { diff --git a/src/tenvex/expressions/magnitude3.h b/src/tenvex/expressions/magnitude3.h new file mode 100644 index 0000000..cc28bf2 --- /dev/null +++ b/src/tenvex/expressions/magnitude3.h @@ -0,0 +1,35 @@ +#pragma once + +#include "core.h" +#include "expression.h" +#include "traits.h" +#include "concepts.h" + +namespace tnvx +{ + +template +struct Magn3 : Expr> +{ + TNVX_INLINE + explicit Magn3(const E& TNVX_RESTRICT e) noexcept; + + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; + + [[nodiscard]] TNVX_INLINE + operator float() const noexcept; + +private: + tnvx_ref_or_value_t _e; +}; + +template +TNVX_INLINE +Magn3 magnitude3(const E& TNVX_RESTRICT e) noexcept; + +template inline constexpr bool is_scalar_expr> = true; + +} + +#include "magnitude3_impl.hpp" diff --git a/src/tenvex/expressions/magnitude3_impl.hpp b/src/tenvex/expressions/magnitude3_impl.hpp new file mode 100644 index 0000000..099951f --- /dev/null +++ b/src/tenvex/expressions/magnitude3_impl.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "magnitude3.h" +#include "dot3.h" + +namespace tnvx +{ + +template +TNVX_INLINE +Magn3::Magn3(const E& TNVX_RESTRICT e) noexcept + : _e(e) +{ +} + +template +TNVX_INLINE +vf4 Magn3::eval() const noexcept +{ + return detail::magnitude3(_e.eval()); +} + +template +TNVX_INLINE +Magn3::operator float() const noexcept +{ + return _mm_cvtss_f32(eval()); +} + +template +TNVX_INLINE +Magn3 magnitude3(const E& TNVX_RESTRICT e) noexcept +{ + return Magn3(e); +} + +} diff --git a/src/tenvex/expressions/mul.h b/src/tenvex/expressions/mul.h index b8e14b0..be8478e 100644 --- a/src/tenvex/expressions/mul.h +++ b/src/tenvex/expressions/mul.h @@ -1,14 +1,12 @@ #pragma once -#include "defines.h" -#include "traits.h" +#include "core.h" #include "expression.h" +#include "traits.h" #include "concepts.h" #include "scalar.h" -#include - namespace tnvx { @@ -16,10 +14,10 @@ template struct Mul : Expr> { TNVX_INLINE - Mul(const L& l, const R& r) noexcept; + Mul(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; [[nodiscard]] TNVX_INLINE - __m128 eval() const noexcept; + vf4 eval() const noexcept; private: tnvx_ref_or_value_t _l; @@ -28,15 +26,15 @@ private: template [[nodiscard]] TNVX_INLINE -Mul operator*(const E& l, float r) noexcept; +Mul operator*(const E& TNVX_RESTRICT l, float r) noexcept; template [[nodiscard]] TNVX_INLINE -Mul operator*(float l, const E& r) noexcept; +Mul operator*(float l, const E& TNVX_RESTRICT r) noexcept; template [[nodiscard]] TNVX_INLINE -Mul operator*(const L& l, const R& r) noexcept; +Mul operator*(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; template inline constexpr bool is_vec_expr> = true; template inline constexpr bool is_vec_expr> = true; diff --git a/src/tenvex/expressions/mul_impl.hpp b/src/tenvex/expressions/mul_impl.hpp index 80278d7..efa8464 100644 --- a/src/tenvex/expressions/mul_impl.hpp +++ b/src/tenvex/expressions/mul_impl.hpp @@ -7,7 +7,7 @@ namespace tnvx template TNVX_INLINE -Mul::Mul(const L& l, const R& r) noexcept +Mul::Mul(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept : _l(l) , _r(r) { @@ -15,28 +15,28 @@ Mul::Mul(const L& l, const R& r) noexcept template TNVX_INLINE -__m128 Mul::eval() const noexcept +vf4 Mul::eval() const noexcept { return _mm_mul_ps(_l.eval(), _r.eval()); } template TNVX_INLINE -Mul operator*(const E& l, float r) noexcept +Mul operator*(const E& TNVX_RESTRICT l, float r) noexcept { return { l, Scalar(r) }; } template TNVX_INLINE -Mul operator*(float l, const E& r) noexcept +Mul operator*(float l, const E& TNVX_RESTRICT r) noexcept { return { Scalar(l), r }; } template TNVX_INLINE -Mul operator*(const L& l, const R& r) noexcept +Mul operator*(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept { return { l, r }; } diff --git a/src/tenvex/expressions/norm3.h b/src/tenvex/expressions/norm3.h new file mode 100644 index 0000000..f7a650a --- /dev/null +++ b/src/tenvex/expressions/norm3.h @@ -0,0 +1,31 @@ +#pragma once + +#include "core.h" +#include "expression.h" +#include "traits.h" +#include "concepts.h" + +namespace tnvx +{ + +template +struct Norm3 : Expr> +{ + TNVX_INLINE + explicit Norm3(const E& TNVX_RESTRICT e) noexcept; + + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; + +private: + tnvx_ref_or_value_t _e; +}; + +template +[[nodiscard]] TNVX_INLINE +Norm3 norm3(const E& TNVX_RESTRICT e) noexcept; + +template inline constexpr bool is_vec_expr> = true; + +} +#include "norm3_impl.hpp" diff --git a/src/tenvex/expressions/norm3_impl.hpp b/src/tenvex/expressions/norm3_impl.hpp new file mode 100644 index 0000000..cbf6f6a --- /dev/null +++ b/src/tenvex/expressions/norm3_impl.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "norm3.h" + +namespace tnvx +{ + +template +TNVX_INLINE +Norm3::Norm3(const E& TNVX_RESTRICT e) noexcept + : _e(e) +{ +} + +template +TNVX_INLINE +vf4 Norm3::eval() const noexcept +{ + return detail::norm3(_e.eval()); +} + +template +TNVX_INLINE +Norm3 norm3(const E& TNVX_RESTRICT e) noexcept +{ + return Norm3(e); +} + +} diff --git a/src/tenvex/expressions/scalar.h b/src/tenvex/expressions/scalar.h index a58a36c..53c97e6 100644 --- a/src/tenvex/expressions/scalar.h +++ b/src/tenvex/expressions/scalar.h @@ -1,12 +1,10 @@ #pragma once -#include "defines.h" +#include "core.h" #include "expression.h" #include "traits.h" #include "concepts.h" -#include - namespace tnvx { @@ -16,10 +14,10 @@ struct Scalar : Expr explicit Scalar(float value) noexcept; [[nodiscard]] TNVX_INLINE - __m128 eval() const noexcept; + vf4 eval() const noexcept; private: - __m128 _value; + vf4 _value; }; template<> diff --git a/src/tenvex/expressions/scalar_impl.hpp b/src/tenvex/expressions/scalar_impl.hpp index 4db581f..fe1ee34 100644 --- a/src/tenvex/expressions/scalar_impl.hpp +++ b/src/tenvex/expressions/scalar_impl.hpp @@ -8,11 +8,11 @@ namespace tnvx TNVX_INLINE Scalar::Scalar(float value) noexcept { - _value = _mm_set1_ps(value); + _value = detail::scalar(value); } TNVX_INLINE -__m128 Scalar::eval() const noexcept +vf4 Scalar::eval() const noexcept { return _value; } diff --git a/src/tenvex/expressions/sub.h b/src/tenvex/expressions/sub.h index 65d317f..d5cd754 100644 --- a/src/tenvex/expressions/sub.h +++ b/src/tenvex/expressions/sub.h @@ -1,12 +1,10 @@ #pragma once -#include "defines.h" +#include "core.h" #include "expression.h" #include "concepts.h" #include "traits.h" -#include - namespace tnvx { @@ -17,7 +15,7 @@ struct Sub : Expr> Sub(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; [[nodiscard]] TNVX_INLINE - __m128 eval() const noexcept; + vf4 eval() const noexcept; private: tnvx_ref_or_value_t _l; @@ -26,10 +24,9 @@ private: template [[nodiscard]] TNVX_INLINE -Sub operator-(const L& l, const R& r) noexcept; +Sub operator-(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept; -template -inline constexpr bool is_vec_expr> = true; +template inline constexpr bool is_vec_expr> = true; } diff --git a/src/tenvex/expressions/sub_impl.hpp b/src/tenvex/expressions/sub_impl.hpp index eeebe9c..2864019 100644 --- a/src/tenvex/expressions/sub_impl.hpp +++ b/src/tenvex/expressions/sub_impl.hpp @@ -15,14 +15,14 @@ Sub::Sub(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept template TNVX_INLINE -__m128 Sub::eval() const noexcept +vf4 Sub::eval() const noexcept { - return _mm_sub_ps(_l.eval(), _r.eval()); + return detail::sub(_l.eval(), _r.eval()); } template TNVX_INLINE -Sub operator-(const L& l, const R& r) noexcept +Sub operator-(const L& TNVX_RESTRICT l, const R& TNVX_RESTRICT r) noexcept { return { l, r }; } diff --git a/src/tenvex/expressions/vec4.h b/src/tenvex/expressions/vec4.h index b4b52ec..d82f317 100644 --- a/src/tenvex/expressions/vec4.h +++ b/src/tenvex/expressions/vec4.h @@ -1,12 +1,10 @@ #pragma once -#include "defines.h" +#include "core.h" #include "expression.h" #include "traits.h" #include "concepts.h" -#include - namespace tnvx { @@ -20,17 +18,16 @@ struct alignas(16) vec4 : Expr template TNVX_INLINE - vec4(const E& e) noexcept; + vec4(const E& TNVX_RESTRICT e) noexcept; - [[nodiscaed]] TNVX_INLINE - __m128 eval() const noexcept; + [[nodiscard]] TNVX_INLINE + vf4 eval() const noexcept; private: - __m128 _data; + vf4 _data; }; -template<> -inline constexpr bool is_vec_expr = true; +template<> inline constexpr bool is_vec_expr = true; TNVX_INLINE bool approx_eq(const vec4& lhs, const vec4& rhs, float eps = 1e-6f) noexcept; @@ -39,3 +36,5 @@ TNVX_INLINE bool operator==(const vec4& lhs, const vec4& rhs) noexcept; } + +#include "vec4_impl.hpp" diff --git a/src/tenvex/expressions/vec4_impl.hpp b/src/tenvex/expressions/vec4_impl.hpp index 5f28270..73b3659 100644 --- a/src/tenvex/expressions/vec4_impl.hpp +++ b/src/tenvex/expressions/vec4_impl.hpp @@ -1 +1,39 @@ - \ No newline at end of file +#pragma once + +#include "vec4.h" + +namespace tnvx +{ + +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; +} + +template +TNVX_INLINE +vec4::vec4(const E& TNVX_RESTRICT e) noexcept + : _data(e.eval()) +{ +} + +TNVX_INLINE +bool approx_eq(const vec4& lhs, const vec4& rhs, float eps) noexcept +{ + return detail::approx_eq(lhs.eval(), rhs.eval(), eps); +} + +TNVX_INLINE +bool tnvx::operator==(const vec4& lhs, const vec4& rhs) noexcept +{ + return detail::eq(lhs.eval(), rhs.eval()); +} + +} diff --git a/src/tenvex/tenvex.h b/src/tenvex/tenvex.h index b44d4ea..f87fc6b 100644 --- a/src/tenvex/tenvex.h +++ b/src/tenvex/tenvex.h @@ -1,11 +1,12 @@ #pragma once -#include "expressions/defines.h" -#include "expressions/traits.h" -#include "expressions/expression.h" -#include "expressions/concepts.h" #include "expressions/add.h" #include "expressions/sub.h" #include "expressions/scalar.h" #include "expressions/mul.h" +#include "expressions/div.h" +#include "expressions/dot3.h" +#include "expressions/magnitude3.h" +#include "expressions/norm3.h" +#include "expressions/cross3.h" #include "expressions/vec4.h" diff --git a/src/tests/vec4_tests.cpp b/src/tests/vec4_tests.cpp index e817aef..b3e5965 100644 --- a/src/tests/vec4_tests.cpp +++ b/src/tests/vec4_tests.cpp @@ -45,12 +45,21 @@ TEST(vec4, mult_b) EXPECT_TRUE(approx_eq(check, result)); } -TEST(vec4, dot) +TEST(vec4, div) { - vec4 a = { 1.0f, 0.0f, 3.0f, 0.0f }; - vec4 b = { 1.0f, 2.0f, 3.0f, 1.0f }; + vec4 a = { 3.0f, 6.0f, 0.0f, 0.0f }; + float b = 3.0f; + vec4 check = { 1.0f, 2.0f, 0.0f, 0.0f }; + vec4 result = a / b; + EXPECT_TRUE(approx_eq(check, result)); +} + +TEST(vec4, dot3) +{ + vec4 a = { 1.0f, 0.0f, 3.0f, 1.0f }; + vec4 b = { 1.0f, 2.0f, 3.0f, 4.0f }; float check = 10.0f; - float result = dot(a, b); + float result = dot3(a, b); EXPECT_FLOAT_EQ(check, result); } @@ -60,25 +69,25 @@ 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); + vec4 result = cross3(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); + vec4 a = { 1.0f, 0.0f, 0.0f, 1.0f }; + vec4 check = { 1.0f, 0.0f, 0.0f, 1.0f }; + vec4 result = norm3(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); + vec4 a = { 1.0f, 0.0f, 1.0f, 1.0f }; + vec4 check = { 0.707106f, 0.0f, 0.707106f, 1.0f }; + vec4 result = norm3(a); EXPECT_TRUE(approx_eq(check, result)); } @@ -87,7 +96,7 @@ TEST(vec4, magnitude_a) { vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f }; float check = 1.0f; - float result = magnitude(a); + float result = magnitude3(a); EXPECT_FLOAT_EQ(check, result); } @@ -96,20 +105,20 @@ TEST(vec4, magnitude_b) { vec4 a = { 1.0f, 0.0f, 1.0f, 0.0f }; float check = 1.4142135f; - float result = magnitude(a); + float result = magnitude3(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 a = { 1.0f, 1.0f, 1.0f, 1.0f }; + vec4 b = { 0.0f, 1.0f, 0.0f, 3.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; + vec4 result = dot3(a, b) * 3.0f * c; EXPECT_TRUE(approx_eq(check, result)); } @@ -122,7 +131,7 @@ TEST(vec4, expression_b) vec4 check = { 33.0f, 0.0f, -3.45f, 0.0f }; - vec4 result = dot(a, b) * 3.0f * cross(b, c); + vec4 result = dot3(a, b) * 3.0f * cross3(b, c); EXPECT_TRUE(approx_eq(check, result)); } @@ -133,22 +142,12 @@ TEST(vec4, expression_c) 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 check = { 7.24264050f, 5.65685463f, 10.0710678f, 0.0f }; - vec4 result = norm(a + b * 2.0f) * dot(b, c) + c * 3.0f; + vec4 tmp1 = norm3(a + b * 2.0f) * dot3(b, c); + vec4 tmp2 = c * 3.0f; - EXPECT_TRUE(approx_eq(check, result)); -} - -TEST(vec4, expression_d) -{ - 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 = magnitude(a) * dot(b, c) + c * 3.0f; + vec4 result = tmp1 + tmp2; EXPECT_TRUE(approx_eq(check, result)); } @@ -157,27 +156,27 @@ 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)); + EXPECT_TRUE(approx_eq(cross3(a, b), cross3(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)); + EXPECT_FLOAT_EQ(dot3(a, b), dot3(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); + EXPECT_NEAR(magnitude3(norm3(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); + EXPECT_FLOAT_EQ(dot3(a, b), 0.0f); } TEST(vec4, cross_parallel) @@ -185,7 +184,7 @@ 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)); + EXPECT_TRUE(approx_eq(cross3(a, b), check)); } }