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
43 changes: 43 additions & 0 deletions include/xsimd/types/xsimd_batch_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,28 @@ namespace xsimd
{
return {};
}

template <typename T, T Val, class A, std::size_t... Is>
XSIMD_INLINE constexpr batch_constant<T, A, (static_cast<T>(0 * Is) + Val)...>
make_batch_constant(detail::index_sequence<Is...>) noexcept
{
return {};
}

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(detail::index_sequence<Is...>) noexcept
{
return {};
}

template <typename T, bool Val, class A, std::size_t... Is>
XSIMD_INLINE constexpr batch_bool_constant<T, A, ((static_cast<bool>(Is) | true) & Val)...>
make_batch_bool_constant(detail::index_sequence<Is...>) noexcept
{
return {};
}

} // namespace detail

/**
Expand Down Expand Up @@ -287,13 +302,41 @@ namespace xsimd
return {};
}

/**
Copy link
Contributor

@serge-sans-paille serge-sans-paille Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a user-facing API, but kit lacks the proper doxygen group and doc integration. I'll fix this.

* @brief Build a @c batch_constant with a single repeated value.
*
* @tparam T type of the data held in the batch.
* @tparam Val The value to repeat.
* @tparam A Architecture that will be used when converting to a regular batch.
*/
template <typename T, T Val, class A = default_arch>
XSIMD_INLINE constexpr decltype(detail::make_batch_constant<T, Val, A>(detail::make_index_sequence<batch<T, A>::size>()))
make_batch_constant() noexcept
{
return {};
}

template <typename T, class G, class A = default_arch>
XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant<T, G, A>(detail::make_index_sequence<batch<T, A>::size>()))
make_batch_bool_constant() noexcept
{
return {};
}

/**
* @brief Build a @c batch_bool_constant with a single repeated value.
*
* @tparam T type of the data held in the batch.
* @tparam Val The value to repeat.
* @tparam A Architecture that will be used when converting to a regular batch.
*/
template <typename T, bool Val, class A = default_arch>
XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant<T, Val, A>(detail::make_index_sequence<batch<T, A>::size>()))
make_batch_bool_constant() noexcept
{
return {};
}

} // namespace xsimd

#endif
28 changes: 26 additions & 2 deletions test/test_batch_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ struct constant_batch_test
}
};

void test_init_from_constant() const
{
array_type expected;
std::generate(expected.begin(), expected.end(), []()
{ return 1; });
constexpr auto b = xsimd::make_batch_constant<value_type, 1, arch_type>();
INFO("batch(value_type)");
CHECK_BATCH_EQ((batch_type)b, expected);
}

void test_init_from_generator() const
{
array_type expected;
Expand Down Expand Up @@ -83,7 +93,7 @@ struct constant_batch_test
}
};

void test_init_from_constant() const
void test_init_from_constant_generator() const
{
array_type expected;
std::fill(expected.begin(), expected.end(), constant<3>::get(0, 0));
Expand Down Expand Up @@ -142,6 +152,8 @@ struct constant_batch_test
TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
{
constant_batch_test<B> Test;
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }

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

SUBCASE("as_batch") { Test.test_cast(); }
Expand All @@ -151,7 +163,7 @@ TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
Test.test_init_from_generator_arange();
}

SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
SUBCASE("init_from_constant_generator") { Test.test_init_from_constant_generator(); }

SUBCASE("operators")
{
Expand All @@ -178,6 +190,16 @@ struct constant_bool_batch_test
}
};

void test_init_from_constant() const
{
bool_array_type expected;
std::generate(expected.begin(), expected.end(), []()
{ return false; });
constexpr auto b = xsimd::make_batch_bool_constant<value_type, false, arch_type>();
INFO("batch_bool_constant(value_type)");
CHECK_BATCH_EQ((batch_bool_type)b, expected);
}

void test_init_from_generator() const
{
bool_array_type expected;
Expand Down Expand Up @@ -270,6 +292,8 @@ struct constant_bool_batch_test
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("init_from_generator") { Test.test_init_from_generator(); }

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