Add negative operator

This commit is contained in:
Artem Zyktin 2026-06-02 05:51:08 +08:00
parent aecca0d9a8
commit 6f7dbe7528
7 changed files with 91 additions and 4 deletions

View File

@ -89,4 +89,9 @@ inline bool approx_eq(vec4 a, vec4 b, float eps = 1e-6f) noexcept
&& std::fabs(a.w() - b.w()) <= eps;
}
[[nodiscard]] inline vec4 operator-(vec4 v) noexcept
{
return { -v.x(), -v.y(), -v.z(), -v.w() };
}
}

View File

@ -13,6 +13,12 @@
namespace tnvx::detail
{
[[nodiscard]] TNVX_INLINE
float to_float(vf4 v)
{
return _mm_cvtss_f32(v);
}
[[nodiscard]] TNVX_INLINE
bool eq(vf4 a, vf4 b)
{
@ -39,6 +45,12 @@ float get_lane(vf4 v) noexcept
return _mm_cvtss_f32(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, lane)));
}
[[nodiscard]] TNVX_INLINE
vf4 neg(vf4 v) noexcept
{
return _mm_xor_ps(v, _mm_set1_ps(-0.0f));
}
[[nodiscard]] TNVX_INLINE
vf4 add(vf4 l, vf4 r) noexcept
{

View File

@ -24,7 +24,7 @@ template<vec_expr L, vec_expr R>
TNVX_INLINE
Dot3<L, R>::operator float() const noexcept
{
return _mm_cvtss_f32(eval());
return detail::to_float(eval());
}
template<vec_expr L, vec_expr R>

View File

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

View File

@ -0,0 +1,36 @@
#pragma once
#include "neg.h"
namespace tnvx
{
template<expression E>
TNVX_INLINE
tnvx::Neg<E>::Neg(const E& TNVX_RESTRICT e) noexcept
: _e(e)
{
}
template<expression E>
TNVX_INLINE
vf4 Neg<E>::eval() const noexcept
{
return detail::neg(_e.eval());
}
template<expression E>
TNVX_INLINE
Neg<E>::operator float() const noexcept requires scalar_expr<E>
{
return detail::to_float(eval());
}
template<expression E>
TNVX_INLINE
Neg<E> operator-(const E& e) noexcept
{
return Neg<E>(e);
}
}

View File

@ -35,9 +35,6 @@ struct alignas(16) vec4 : Expr<vec4>
[[nodiscard]] TNVX_INLINE
vf4 eval() const noexcept;
[[nodiscard]] TNVX_INLINE
vec4 operator-() const noexcept;
private:
vf4 _data;

View File

@ -1,5 +1,6 @@
#pragma once
#include "expressions/neg.h"
#include "expressions/add.h"
#include "expressions/sub.h"
#include "expressions/scalar.h"