Compare commits

...

3 Commits

7 changed files with 120 additions and 0 deletions

View File

@ -74,6 +74,11 @@ private:
return std::sqrt(dot3(v, v));
}
[[nodiscard]] inline float magnitude3_sq(vec4 v) noexcept
{
return dot3(v, v);
}
[[nodiscard]] inline vec4 norm3(vec4 v) noexcept
{
const float inv = 1.0f / magnitude3(v); // normalize xyz, keep w

View File

@ -101,6 +101,12 @@ vf4 magnitude3(vf4 v) noexcept
return _mm_sqrt_ps(dot3(v, v));
}
[[nodiscard]] TNVX_INLINE
vf4 magnitude3_sq(vf4 v) noexcept
{
return dot3(v, v);
}
[[nodiscard]] TNVX_INLINE
vf4 norm3(vf4 v) noexcept
{

View File

@ -0,0 +1,35 @@
#pragma once
#include "core.h"
#include "expression.h"
#include "traits.h"
#include "concepts.h"
namespace tnvx
{
template<vec_expr E>
struct Magn3Sq : Expr<Magn3Sq<E>>
{
TNVX_INLINE
explicit Magn3Sq(const E& e) noexcept;
[[nodiscard]] TNVX_INLINE
vf4 eval() const noexcept;
[[nodiscard]] TNVX_INLINE
operator float() const noexcept;
private:
tnvx_ref_or_value_t<E> _e;
};
template<vec_expr E> inline constexpr bool is_scalar_expr<Magn3Sq<E>> = true;
template<vec_expr E>
[[nodiscard]] TNVX_INLINE
Magn3Sq<E> magnitude3_sq(const E& e) noexcept;
}
#include "magnitude3_sq_impl.hpp"

View File

@ -0,0 +1,37 @@
#pragma once
#include "magnitude3_sq.h"
namespace tnvx
{
template<vec_expr E>
TNVX_INLINE
Magn3Sq<E>::Magn3Sq(const E& e) noexcept
: _e(e)
{
}
template<vec_expr E>
TNVX_INLINE
vf4 Magn3Sq<E>::eval() const noexcept
{
return detail::magnitude3_sq(_e.eval());
}
template<vec_expr E>
TNVX_INLINE
Magn3Sq<E>::operator float() const noexcept
{
return detail::to_float(eval());
}
template<vec_expr E>
TNVX_INLINE
Magn3Sq<E> magnitude3_sq(const E& e) noexcept
{
return Magn3Sq<E>(e);
}
}

View File

@ -9,6 +9,7 @@
#include "expressions/dot3.h"
#include "expressions/dot4.h"
#include "expressions/magnitude3.h"
#include "expressions/magnitude3_sq.h"
#include "expressions/norm3.h"
#include "expressions/cross3.h"
#include "expressions/vec4.h"

View File

@ -123,6 +123,24 @@ TEST(naive_vec4, magnitude_b)
EXPECT_FLOAT_EQ(check, result);
}
TEST(naive_vec4, magnitude3_sq_a)
{
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
float check = 1.0f;
float result = magnitude3_sq(a);
EXPECT_FLOAT_EQ(check, result);
}
TEST(naive_vec4, magnitude3_sq_b)
{
vec4 a = { 1.0f, 0.0f, 1.0f, 1.0f };
float check = 2.0f;
float result = magnitude3_sq(a);
EXPECT_FLOAT_EQ(check, result);
}
TEST(naive_vec4, expression_a)
{
vec4 a = { 1.0f, 1.0f, 1.0f, 1.0f };

View File

@ -121,6 +121,24 @@ TEST(vec4, magnitude_b)
EXPECT_FLOAT_EQ(check, result);
}
TEST(vec4, magnitude3_sq_a)
{
vec4 a = { 1.0f, 0.0f, 0.0f, 0.0f };
float check = 1.0f;
float result = magnitude3_sq(a);
EXPECT_FLOAT_EQ(check, result);
}
TEST(vec4, magnitude3_sq_b)
{
vec4 a = { 1.0f, 0.0f, 1.0f, 1.0f };
float check = 2.0f;
float result = magnitude3_sq(a);
EXPECT_FLOAT_EQ(check, result);
}
TEST(vec4, expression_a)
{
vec4 a = { 1.0f, 1.0f, 1.0f, 1.0f };