feat(tulip): introduce basic SIMD abstraction

This commit is contained in:
2026-11-22 14:19:26 +01:00
parent dea973ddd6
commit 3a27df500e
4 changed files with 144 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
#ifndef TULIP_SIMD_AVX_HPP
#define TULIP_SIMD_AVX_HPP
#include "./traits.hpp"
namespace tulip {
namespace simd {
namespace detail {
#ifdef TULIP_AVX_SUPPORTED
template <>
struct traits<float, avx_isa> {
using pack_type = __m128;
static constexpr std::size_t width = 4;
static pack_type load(const float* ptr) noexcept { return _mm128_loadu_ps(ptr); }
static void store(float* ptr, pack_type value) noexcept { _mm128_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm128_add_ps(lhs, rhs); }
};
#endif // TULIP_AVX_SUPPORTED
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_AVX_HPP
+40
View File
@@ -0,0 +1,40 @@
#ifndef TULIP_SIMD_AVX2_HPP
#define TULIP_SIMD_AVX2_HPP
#include "./traits.hpp"
namespace tulip {
namespace simd {
namespace detail {
#ifdef TULIP_AVX2_SUPPORTED
template <>
struct traits<float, avx2_isa> {
using pack_type = __m256;
static constexpr std::size_t width = 8;
static pack_type load(const float* ptr) noexcept { return _mm256_loadu_ps(ptr); }
static void store(float* ptr, pack_type value) noexcept { _mm256_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_ps(lhs, rhs); }
};
template <>
struct traits<double, avx2_isa> {
using pack_type = __m256d;
static constexpr std::size_t width = 4;
static pack_type load(const double* ptr) noexcept { return _mm256d_loadu_ps(ptr); }
static void store(double* ptr, pack_type value) noexcept { _mm256d_storeu_ps(ptr, value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256d_add_ps(lhs, rhs); }
};
#endif // TULIP_AVX2_SUPPORTED
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_AVX2_HPP
+24
View File
@@ -0,0 +1,24 @@
#ifndef TULIP_SIMD_CONFIG_HPP
#define TULIP_SIMD_CONFIG_HPP
namespace tulip {
namespace simd {
struct scalar_isa {};
struct avx_isa {};
struct avx2_isa {};
#ifdef __AVX__
#define TULIP_AVX_SUPPORTED
#include <immintrin.h>
#endif // __AVX__
#ifdef __AVX2__
#define TULIP_AVX2_SUPPORTED
#include <immintrin.h>
#endif // __AVX2__
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_CONFIG_HPP
+52
View File
@@ -0,0 +1,52 @@
#ifndef TULIP_SIMD_TRAITS_HPP
#define TULIP_SIMD_TRAITS_HPP
#include "./config.hpp"
#include <cstddef>
namespace tulip {
namespace simd {
namespace detail {
template <typename T, typename Isa>
struct traits {};
template <typename T>
struct traits<T, scalar_isa> {
using pack_type = T;
static constexpr std::size_t width = 1;
static pack_type load(const T* ptr) noexcept { return *ptr; }
static void store(T* ptr, pack_type value) noexcept { *ptr = value; }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return lhs + rhs; }
};
template <typename T, typename Isa>
using pack_t = typename traits<T, Isa>::pack_type;
template <typename T, typename Isa>
inline constexpr auto width_v = traits<T, Isa>::width;
template <typename T, typename Isa>
static auto load(const T* ptr) noexcept {
return traits<T, Isa>::load(ptr);
}
template <typename T, typename Isa>
static void store(const T* ptr, pack_t<T, Isa> value) noexcept {
traits<T, Isa>::store(ptr, value);
}
template <typename T, typename Isa>
static auto add(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
return traits<T, Isa>::add(lhs, rhs);
}
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_TRAITS_HPP