diff --git a/include/xsimd/types/xsimd_batch_constant.hpp b/include/xsimd/types/xsimd_batch_constant.hpp index ea69c2aae..60e27493d 100644 --- a/include/xsimd/types/xsimd_batch_constant.hpp +++ b/include/xsimd/types/xsimd_batch_constant.hpp @@ -251,6 +251,14 @@ namespace xsimd { return {}; } + + template + XSIMD_INLINE constexpr batch_constant(0 * Is) + Val)...> + make_batch_constant(detail::index_sequence) noexcept + { + return {}; + } + template XSIMD_INLINE constexpr batch_bool_constant make_batch_bool_constant(detail::index_sequence) noexcept @@ -258,6 +266,13 @@ namespace xsimd return {}; } + template + XSIMD_INLINE constexpr batch_bool_constant(Is) | true) & Val)...> + make_batch_bool_constant(detail::index_sequence) noexcept + { + return {}; + } + } // namespace detail /** @@ -287,6 +302,20 @@ namespace xsimd return {}; } + /** + * @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 + XSIMD_INLINE constexpr decltype(detail::make_batch_constant(detail::make_index_sequence::size>())) + make_batch_constant() noexcept + { + return {}; + } + template XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant(detail::make_index_sequence::size>())) make_batch_bool_constant() noexcept @@ -294,6 +323,20 @@ namespace xsimd 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 + XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant(detail::make_index_sequence::size>())) + make_batch_bool_constant() noexcept + { + return {}; + } + } // namespace xsimd #endif diff --git a/test/test_batch_constant.cpp b/test/test_batch_constant.cpp index 7cc036020..f5e1a09b4 100644 --- a/test/test_batch_constant.cpp +++ b/test/test_batch_constant.cpp @@ -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(); + INFO("batch(value_type)"); + CHECK_BATCH_EQ((batch_type)b, expected); + } + void test_init_from_generator() const { array_type expected; @@ -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)); @@ -142,6 +152,8 @@ struct constant_batch_test TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES) { constant_batch_test 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(); } @@ -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") { @@ -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(); + INFO("batch_bool_constant(value_type)"); + CHECK_BATCH_EQ((batch_bool_type)b, expected); + } + void test_init_from_generator() const { bool_array_type expected; @@ -270,6 +292,8 @@ struct constant_bool_batch_test 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("init_from_generator") { Test.test_init_from_generator(); } SUBCASE("as_batch") { Test.test_cast(); }