Skip to content

Commit

Permalink
Fixed simd_traits implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Aug 27, 2021
1 parent c5e9d93 commit e87d936
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
17 changes: 15 additions & 2 deletions include/xsimd/types/xsimd_register.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef XSIMD_REGISTER_HPP
#define XSIMD_REGISTER_HPP

#include <type_traits>

namespace xsimd
{

Expand All @@ -16,14 +18,22 @@ namespace xsimd
template<class T, class Arch>
struct simd_register;

template <class T, class A>
struct has_simd_register : std::false_type
{
};

#define XSIMD_DECLARE_SIMD_REGISTER(SCALAR_TYPE, ISA, VECTOR_TYPE) \
template<> \
struct simd_register<SCALAR_TYPE, ISA>\
{\
using register_type = VECTOR_TYPE;\
register_type data;\
operator register_type() const { return data; }\
}
};\
template <>\
struct has_simd_register<SCALAR_TYPE, ISA> : std::true_type\
{}

#define XSIMD_DECLARE_SIMD_REGISTER_ALIAS(ISA, ISA_BASE)\
template<class T> \
Expand All @@ -32,7 +42,10 @@ namespace xsimd
using register_type = typename simd_register<T, ISA_BASE>::register_type;\
simd_register(register_type reg) : simd_register<T, ISA_BASE>{reg} {}\
simd_register() = default;\
}
};\
template<class T>\
struct has_simd_register<T, ISA> : has_simd_register<T, ISA_BASE>\
{}

template <class T, class Arch>
struct get_bool_simd_register
Expand Down
64 changes: 43 additions & 21 deletions include/xsimd/types/xsimd_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,72 @@

namespace xsimd
{

/**************************************
* simd_traits and revert_simd_traits *
**************************************/

template <class T, class A = default_arch>
struct has_simd_register : types::has_simd_register<T, A>
{
};

namespace detail
{
template <class T, class = void>
struct has_batch : std::false_type
template <class T, bool>
struct simd_traits_impl;

template <class T>
struct simd_traits_impl<T, false>
{
using type = T;
using bool_type = bool;
static constexpr size_t size = 1;
};

template <class T>
struct has_batch<T, check_size_t<sizeof(batch<T>)>> : std::true_type
constexpr size_t simd_traits_impl<T, false>::size;

template <class T>
struct simd_traits_impl<T, true>
{
using type = batch<T>;
using bool_type = typename type::batch_bool_type;
static constexpr size_t size = type::size;
};

template <class T>
constexpr size_t simd_traits_impl<T, true>::size;
}

template <class T, bool = detail::has_batch<T>::value>
struct simd_traits
template <class T>
struct simd_traits : detail::simd_traits_impl<T, has_simd_register<T>::value>
{
using type = T;
using bool_type = bool;
static constexpr size_t size = 1;
};

template <class T, bool B>
constexpr size_t simd_traits<T, B>::size;

template <class T>
struct revert_simd_traits
struct simd_traits<std::complex<T>>
: detail::simd_traits_impl<std::complex<T>, has_simd_register<T>::value>
{
using type = T;
static constexpr size_t size = simd_traits<type>::size;
};

template <class T>
constexpr size_t revert_simd_traits<T>::size;
#ifdef XSIMD_ENABLE_XTL_COMPLEX
template <class T, bool i3ec>
struct simd_traits<xtl::xcomplex<T, T, i3ec>>
: detail::simd_traits_impl<std::complex<T>, has_simd_register<T>::value>
{
};
#endif

template <class T>
struct simd_traits<T, true>
struct revert_simd_traits
{
using type = batch<T>;
using bool_type = typename type::batch_bool_type;
static constexpr size_t size = type::size;
using type = T;
static constexpr size_t size = simd_traits<type>::size;
};

template <class T>
constexpr size_t simd_traits<T, true>::size;
constexpr size_t revert_simd_traits<T>::size;

template <class T>
struct revert_simd_traits<batch<T>>
Expand Down
5 changes: 5 additions & 0 deletions test/test_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class traits_test : public testing::Test
using batch_bool_type = xsimd::batch_bool<value_type>;
constexpr bool same_bool_type = std::is_same<batch_bool_type, typename traits_type::bool_type>::value;
EXPECT_TRUE(same_bool_type);

using vector_traits_type = xsimd::simd_traits<std::vector<value_type>>;
EXPECT_EQ(vector_traits_type::size, 1);
constexpr bool vec_same_type = std::is_same<typename vector_traits_type::type, std::vector<value_type>>::value;
EXPECT_TRUE(vec_same_type);
}

void test_revert_simd_traits()
Expand Down

0 comments on commit e87d936

Please sign in to comment.