Add get lane functions for vec4

This commit is contained in:
Artem Zyktin 2026-05-31 00:45:14 +08:00
parent d3a26c5989
commit 2af4bd7b06
3 changed files with 48 additions and 3 deletions

View File

@ -4,9 +4,10 @@
#include "defines.h"
#if defined(HAS_SSE42)
#include <smmintrin.h>
using vf4 = __m128;
#include <smmintrin.h>
using vf4 = __m128;
#else
#error "tenvex requires an SSE4.1 backend (define HAS_SSE42 and build with -msse4.1 on GCC/Clang)"
#endif
namespace tnvx::detail
@ -31,6 +32,13 @@ vf4 scalar(float value) noexcept
return _mm_set_ps1(value);
}
template<int lane>
[[nodiscard]] TNVX_INLINE
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 add(vf4 l, vf4 r) noexcept
{

View File

@ -20,10 +20,23 @@ struct alignas(16) vec4 : Expr<vec4>
TNVX_INLINE
vec4(const E& e) noexcept;
[[nodiscard]] TNVX_INLINE
float x() const noexcept;
[[nodiscard]] TNVX_INLINE
float y() const noexcept;
[[nodiscard]] TNVX_INLINE
float z() const noexcept;
[[nodiscard]] TNVX_INLINE
float w() const noexcept;
[[nodiscard]] TNVX_INLINE
vf4 eval() const noexcept;
private:
vf4 _data;
};

View File

@ -31,9 +31,33 @@ bool approx_eq(const vec4& lhs, const vec4& rhs, float eps) noexcept
}
TNVX_INLINE
bool tnvx::operator==(const vec4& lhs, const vec4& rhs) noexcept
bool operator==(const vec4& lhs, const vec4& rhs) noexcept
{
return detail::eq(lhs.eval(), rhs.eval());
}
TNVX_INLINE
float vec4::x() const noexcept
{
return detail::get_lane<0>(_data);
}
TNVX_INLINE
float vec4::y() const noexcept
{
return detail::get_lane<1>(_data);
}
TNVX_INLINE
float vec4::z() const noexcept
{
return detail::get_lane<2>(_data);
}
TNVX_INLINE
float vec4::w() const noexcept
{
return detail::get_lane<3>(_data);
}
}