Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions include/xsimd/types/xsimd_batch_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ namespace xsimd
return {};
}

#if __cplusplus >= 202002L
template <std::array Arr, class A, std::size_t... Is>
XSIMD_INLINE constexpr batch_constant<typename decltype(Arr)::value_type, A, Arr[Is]...>
make_batch_constant(std::index_sequence<Is...>) noexcept
{
return {};
}
#endif

template <typename T, class G, class A, std::size_t... Is>
XSIMD_INLINE constexpr batch_bool_constant<T, A, G::get(Is, sizeof...(Is))...>
make_batch_bool_constant(std::index_sequence<Is...>) noexcept
Expand All @@ -422,6 +431,15 @@ namespace xsimd
return {};
}

#if __cplusplus >= 202002L
template <typename T, std::array Arr, class A, std::size_t... Is>
XSIMD_INLINE constexpr batch_bool_constant<T, A, Arr[Is]...>
make_batch_bool_constant(std::index_sequence<Is...>) noexcept
{
return {};
}
#endif

} // namespace detail

/**
Expand Down Expand Up @@ -479,6 +497,21 @@ namespace xsimd
return {};
}

#if __cplusplus >= 202002L
/**
* @brief Build a @c batch_constant from a std::array (C++20)
*
* @tparam Arr The std::array containing the values (non type template argument).
* @tparam A Architecture that will be used when converting to a regular batch.
*/
template <std::array Arr, class A = default_arch>
requires(Arr.size() == batch<typename decltype(Arr)::value_type, A>::size)
XSIMD_INLINE constexpr auto make_batch_constant() noexcept
{
return detail::make_batch_constant<Arr, A>(std::make_index_sequence<Arr.size()>());
}
#endif

/*
* @brief Build a @c batch_bool_constant with a single repeated value.
*
Expand All @@ -491,6 +524,23 @@ namespace xsimd
return {};
}

#if __cplusplus >= 202002L
/**
* @brief Build a @c batch_constant from a std::array of boolean (C++20)
*
* @tparam Arr The std::array containing the boolean values (non type template argument).
* @tparam A Architecture that will be used when converting to a regular batch.
*/
template <typename T, std::array Arr, class A = default_arch>
requires(
(Arr.size() == batch_bool<T, A>::size)
&& std::is_same_v<typename decltype(Arr)::value_type, bool>)
XSIMD_INLINE constexpr auto make_batch_bool_constant() noexcept
{
return detail::make_batch_bool_constant<T, Arr, A>(std::make_index_sequence<Arr.size()>());
}
#endif

#endif

namespace generator
Expand Down
41 changes: 41 additions & 0 deletions test/test_batch_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <numeric>

#include "xsimd/xsimd.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE

Expand Down Expand Up @@ -43,6 +45,22 @@ struct constant_batch_test
CHECK_BATCH_EQ((batch_type)b, expected);
}

void test_init_from_array() const
{
#if __cplusplus >= 202002L
constexpr array_type expected = []()
{
array_type out = {};
std::iota(out.begin(), out.end(), 0);
return out;
}();

constexpr auto b = xsimd::make_batch_constant<expected, arch_type>();
INFO("batch(value_type)");
CHECK_BATCH_EQ((batch_type)b, expected);
#endif
}

void test_init_from_generator() const
{
array_type expected;
Expand Down Expand Up @@ -217,6 +235,8 @@ TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
constant_batch_test<B> Test;
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }

SUBCASE("test_init_from_array") { Test.test_init_from_array(); }

SUBCASE("init_from_generator") { Test.test_init_from_generator(); }

SUBCASE("as_batch") { Test.test_cast(); }
Expand Down Expand Up @@ -263,6 +283,25 @@ struct constant_bool_batch_test
CHECK_BATCH_EQ((batch_bool_type)b, expected);
}

void test_init_from_array() const
{
#if __cplusplus >= 202002L
constexpr bool_array_type expected = []()
{
bool_array_type out = {};
for (std::size_t k = 0; k < out.size(); ++k)
{
out[k] = k % 2 == 0;
}
return out;
}();

constexpr auto b = xsimd::make_batch_bool_constant<value_type, expected, arch_type>();
INFO("batch_bool_constant(value_type)");
CHECK_BATCH_EQ((batch_bool_type)b, expected);
#endif
}

void test_init_from_generator() const
{
bool_array_type expected;
Expand Down Expand Up @@ -357,6 +396,8 @@ TEST_CASE_TEMPLATE("[constant bool batch]", B, BATCH_INT_TYPES)
constant_bool_batch_test<B> Test;
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }

SUBCASE("test_init_from_array") { Test.test_init_from_array(); }

SUBCASE("init_from_generator") { Test.test_init_from_generator(); }

SUBCASE("as_batch") { Test.test_cast(); }
Expand Down