feat: Use memcpy to copy data into aligned array.

This commit is contained in:
Thorben Höhne 2025-05-01 19:50:17 +02:00
parent 5cc96939d4
commit 040c6b0e46
Signed by: thoehne
GPG Key ID: 60D202D915B81DEC
2 changed files with 36 additions and 30 deletions

View File

@ -1,30 +0,0 @@
#pragma once
#include "Core/Math/Boilerplate.h"
namespace Phanes::Core::Math::SIMD
{
// Structure to conveniently align arrays.
template<typename T, size_t L>
struct alignas(sizeof(T) * 4) AlignedVec
{
public:
T data[L];
AlignedVec(const T* n_aligned_data)
{
for (size_t i = 0; i < L; ++i)
{
data[i] = n_aligned_data[i];
}
}
const T* Get()
{
return data;
}
};
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <cstdlib>
#include <cstring>
#include <type_traits>
namespace Phanes::Core::Math::SIMD
{
// Structure to conveniently align arrays.
template <typename T, size_t L, size_t Align>
struct alignas(Align) AlignedVec
{
public:
static_assert(std::is_trivially_copyable_v<T>,
"Alignment.hpp: AlignedVecSSE must be trivially copyable.");
static_assert(Align >= alignof(T) && (Align & (Align - 1)) == 0,
"Alignment.hpp: Alignment must be a power of two and >= alignof(T).");
T data[L];
explicit AlignedVec(const T* n_aligned_data)
{
std::memcpy(data, n_aligned_data, sizeof(T) * L);
}
const T* Get() const
{
return data;
}
};
template <typename T, size_t L>
using AlignedVecSSE = AlignedVec<T, L, 16>;
template <typename T, size_t L>
using AlignedVecAVX = AlignedVec<T, L, 32>;
} // namespace Phanes::Core::Math::SIMD