diff --git a/include/tulip/simd/avx.hpp b/include/tulip/simd/avx.hpp new file mode 100644 index 0000000..bcfa117 --- /dev/null +++ b/include/tulip/simd/avx.hpp @@ -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 { + 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 diff --git a/include/tulip/simd/avx2.hpp b/include/tulip/simd/avx2.hpp new file mode 100644 index 0000000..34d6f2d --- /dev/null +++ b/include/tulip/simd/avx2.hpp @@ -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 { + 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 { + 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 diff --git a/include/tulip/simd/config.hpp b/include/tulip/simd/config.hpp new file mode 100644 index 0000000..f0010a0 --- /dev/null +++ b/include/tulip/simd/config.hpp @@ -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 +#endif // __AVX__ + +#ifdef __AVX2__ +#define TULIP_AVX2_SUPPORTED +#include +#endif // __AVX2__ + +} // namespace simd +} // namespace tulip + +#endif // TULIP_SIMD_CONFIG_HPP diff --git a/include/tulip/simd/traits.hpp b/include/tulip/simd/traits.hpp new file mode 100644 index 0000000..ee60216 --- /dev/null +++ b/include/tulip/simd/traits.hpp @@ -0,0 +1,52 @@ +#ifndef TULIP_SIMD_TRAITS_HPP +#define TULIP_SIMD_TRAITS_HPP + +#include "./config.hpp" + +#include + +namespace tulip { +namespace simd { +namespace detail { + +template +struct traits {}; + +template +struct traits { + 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 +using pack_t = typename traits::pack_type; + +template +inline constexpr auto width_v = traits::width; + +template +static auto load(const T* ptr) noexcept { + return traits::load(ptr); +} + +template +static void store(const T* ptr, pack_t value) noexcept { + traits::store(ptr, value); +} + +template +static auto add(pack_t lhs, pack_t rhs) noexcept { + return traits::add(lhs, rhs); +} + +} // namespace detail +} // namespace simd +} // namespace tulip + +#endif // TULIP_SIMD_TRAITS_HPP