feat(tulip): implement some more SIMD

This commit is contained in:
2026-11-22 16:41:55 +01:00
parent f98615fea4
commit a8b00b32d2
15 changed files with 333 additions and 18 deletions
+5
View File
@@ -17,7 +17,12 @@ struct traits<float, avx_isa> {
static pack_type load(const float* ptr) noexcept { return _mm_loadu_ps(ptr); }
static void store(float* ptr, pack_type value) noexcept { _mm_storeu_ps(ptr, value); }
static pack_type broadcast(float value) noexcept { return _mm_set1_ps(value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm_add_ps(lhs, rhs); }
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm_sub_ps(lhs, rhs); }
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm_mul_ps(lhs, rhs); }
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm_div_ps(lhs, rhs); }
};
#endif // TULIP_AVX_SUPPORTED
+10
View File
@@ -17,7 +17,12 @@ struct traits<float, avx2_isa> {
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 broadcast(float value) noexcept { return _mm256_set1_ps(value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_ps(lhs, rhs); }
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm256_sub_ps(lhs, rhs); }
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm256_mul_ps(lhs, rhs); }
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm256_div_ps(lhs, rhs); }
};
template <>
@@ -29,7 +34,12 @@ struct traits<double, avx2_isa> {
static pack_type load(const double* ptr) noexcept { return _mm256_loadu_pd(ptr); }
static void store(double* ptr, pack_type value) noexcept { _mm256_storeu_pd(ptr, value); }
static pack_type broadcast(float value) noexcept { return _mm256_set1_pd(value); }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_pd(lhs, rhs); }
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm256_sub_pd(lhs, rhs); }
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm256_mul_pd(lhs, rhs); }
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm256_div_pd(lhs, rhs); }
};
#endif // TULIP_AVX2_SUPPORTED
+5 -5
View File
@@ -1,7 +1,7 @@
#ifndef TULIP_SIMD_CONFIG_HPP
#define TULIP_SIMD_CONFIG_HPP
#if defined(__AVX__) || defined(__AVX2__)
#if !defined(TULIP_DISABLE_SIMD) && (defined(__AVX__) || defined(__AVX2__))
#include <immintrin.h>
#endif
@@ -12,13 +12,13 @@ struct scalar_isa {};
struct avx_isa {};
struct avx2_isa {};
#ifdef __AVX__
#if defined(__AVX__) && !defined(TULIP_DISABLE_SIMD)
#define TULIP_AVX_SUPPORTED
#endif // __AVX__
#endif
#ifdef __AVX2__
#if defined(__AVX2__) && !defined(TULIP_DISABLE_SIMD)
#define TULIP_AVX2_SUPPORTED
#endif // __AVX2__
#endif
} // namespace simd
} // namespace tulip
+39
View File
@@ -0,0 +1,39 @@
#ifndef TULIP_SIMD_DISPATCH_HPP
#define TULIP_SIMD_DISPATCH_HPP
#include "./config.hpp"
namespace tulip {
namespace simd {
namespace detail {
template <typename T>
struct preferred_isa {
using type = simd::scalar_isa;
};
#ifdef TULIP_AVX2_SUPPORT
template <>
struct preferrpreferred_isa<float> {
using type = simd::avx2_isa;
};
template <>
struct preferrpreferred_isa<double> {
using type = simd::avx2_isa;
};
#elif defined(TULIP_AVX_SUPPORT)
template <>
struct preferrpreferred_isa<float> {
using type = simd::avx_isa;
};
#endif
template <typename T>
using preferred_isa_t = typename preferred_isa<T>::type;
} // namespace detail
} // namespace simd
} // namespace tulip
#endif // TULIP_SIMD_DISPATCH_HPP
+10
View File
@@ -0,0 +1,10 @@
#ifndef TULIP_SIMD_HPP
#define TULIP_SIMD_HPP
#include "./avx.hpp" // IWYU pragma: export
#include "./avx2.hpp" // IWYU pragma: export
#include "./config.hpp" // IWYU pragma: export
#include "./dispatch.hpp" // IWYU pragma: export
#include "./traits.hpp" // IWYU pragma: export
#endif // TULIP_SIMD_HPP
+25
View File
@@ -21,7 +21,12 @@ struct traits<T, scalar_isa> {
static pack_type load(const T* ptr) noexcept { return *ptr; }
static void store(T* ptr, pack_type value) noexcept { *ptr = value; }
static pack_type broadcast(T value) noexcept { return value; }
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return lhs + rhs; }
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return lhs - rhs; }
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return lhs * rhs; }
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return lhs / rhs; }
};
template <typename T, typename Isa>
@@ -40,11 +45,31 @@ static void store(T* ptr, pack_t<T, Isa> value) noexcept {
traits<T, Isa>::store(ptr, value);
}
template <typename T, typename Isa>
static auto broadcast(T value) noexcept {
return traits<T, Isa>::broadcast(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);
}
template <typename T, typename Isa>
static auto sub(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
return traits<T, Isa>::sub(lhs, rhs);
}
template <typename T, typename Isa>
static auto mul(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
return traits<T, Isa>::mul(lhs, rhs);
}
template <typename T, typename Isa>
static auto div(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
return traits<T, Isa>::div(lhs, rhs);
}
} // namespace detail
} // namespace simd
} // namespace tulip