From 98c4a59123ad1c22fe23aee1255d902ed7e6d230 Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Tue, 10 Feb 2026 17:19:28 +0100 Subject: [PATCH 1/2] Add make_batch_constant from std::array in C++20 --- include/xsimd/types/xsimd_batch_constant.hpp | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/include/xsimd/types/xsimd_batch_constant.hpp b/include/xsimd/types/xsimd_batch_constant.hpp index 4da2e3da6..0f5d4a978 100644 --- a/include/xsimd/types/xsimd_batch_constant.hpp +++ b/include/xsimd/types/xsimd_batch_constant.hpp @@ -408,6 +408,15 @@ namespace xsimd return {}; } +#if __cplusplus >= 202002L + template + XSIMD_INLINE constexpr batch_constant + make_batch_constant(std::index_sequence) noexcept + { + return {}; + } +#endif + template XSIMD_INLINE constexpr batch_bool_constant make_batch_bool_constant(std::index_sequence) noexcept @@ -422,6 +431,15 @@ namespace xsimd return {}; } +#if __cplusplus >= 202002L + template + XSIMD_INLINE constexpr batch_bool_constant + make_batch_bool_constant(std::index_sequence) noexcept + { + return {}; + } +#endif + } // namespace detail /** @@ -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 + requires(Arr.size() == batch::size) + XSIMD_INLINE constexpr auto make_batch_constant() noexcept + { + return detail::make_batch_constant(std::make_index_sequence()); + } +#endif + /* * @brief Build a @c batch_bool_constant with a single repeated value. * @@ -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 + requires( + (Arr.size() == batch_bool::size) + && std::is_same_v) + XSIMD_INLINE constexpr auto make_batch_bool_constant() noexcept + { + return detail::make_batch_bool_constant(std::make_index_sequence()); + } +#endif + #endif namespace generator From aad2337dcdbf2e0c754dbb97d2a64383e08b7c69 Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Thu, 19 Feb 2026 11:08:28 +0100 Subject: [PATCH 2/2] Test batch_constant from array --- test/test_batch_constant.cpp | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test_batch_constant.cpp b/test/test_batch_constant.cpp index 2b99fc05b..869df97ce 100644 --- a/test/test_batch_constant.cpp +++ b/test/test_batch_constant.cpp @@ -9,6 +9,8 @@ * The full license is in the file LICENSE, distributed with this software. * ****************************************************************************/ +#include + #include "xsimd/xsimd.hpp" #ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE @@ -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(); + INFO("batch(value_type)"); + CHECK_BATCH_EQ((batch_type)b, expected); +#endif + } + void test_init_from_generator() const { array_type expected; @@ -217,6 +235,8 @@ TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES) constant_batch_test 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(); } @@ -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(); + 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; @@ -357,6 +396,8 @@ TEST_CASE_TEMPLATE("[constant bool batch]", B, BATCH_INT_TYPES) constant_bool_batch_test 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(); }