Skip to content

Commit 548b05f

Browse files
authored
Merge pull request #1256 from AntoinePrv/batch-constant-nttp
Add make_batch_constant from std::array in C++20
2 parents e284e0b + aad2337 commit 548b05f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

include/xsimd/types/xsimd_batch_constant.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ namespace xsimd
408408
return {};
409409
}
410410

411+
#if __cplusplus >= 202002L
412+
template <std::array Arr, class A, std::size_t... Is>
413+
XSIMD_INLINE constexpr batch_constant<typename decltype(Arr)::value_type, A, Arr[Is]...>
414+
make_batch_constant(std::index_sequence<Is...>) noexcept
415+
{
416+
return {};
417+
}
418+
#endif
419+
411420
template <typename T, class G, class A, std::size_t... Is>
412421
XSIMD_INLINE constexpr batch_bool_constant<T, A, G::get(Is, sizeof...(Is))...>
413422
make_batch_bool_constant(std::index_sequence<Is...>) noexcept
@@ -422,6 +431,15 @@ namespace xsimd
422431
return {};
423432
}
424433

434+
#if __cplusplus >= 202002L
435+
template <typename T, std::array Arr, class A, std::size_t... Is>
436+
XSIMD_INLINE constexpr batch_bool_constant<T, A, Arr[Is]...>
437+
make_batch_bool_constant(std::index_sequence<Is...>) noexcept
438+
{
439+
return {};
440+
}
441+
#endif
442+
425443
} // namespace detail
426444

427445
/**
@@ -479,6 +497,21 @@ namespace xsimd
479497
return {};
480498
}
481499

500+
#if __cplusplus >= 202002L
501+
/**
502+
* @brief Build a @c batch_constant from a std::array (C++20)
503+
*
504+
* @tparam Arr The std::array containing the values (non type template argument).
505+
* @tparam A Architecture that will be used when converting to a regular batch.
506+
*/
507+
template <std::array Arr, class A = default_arch>
508+
requires(Arr.size() == batch<typename decltype(Arr)::value_type, A>::size)
509+
XSIMD_INLINE constexpr auto make_batch_constant() noexcept
510+
{
511+
return detail::make_batch_constant<Arr, A>(std::make_index_sequence<Arr.size()>());
512+
}
513+
#endif
514+
482515
/*
483516
* @brief Build a @c batch_bool_constant with a single repeated value.
484517
*
@@ -491,6 +524,23 @@ namespace xsimd
491524
return {};
492525
}
493526

527+
#if __cplusplus >= 202002L
528+
/**
529+
* @brief Build a @c batch_constant from a std::array of boolean (C++20)
530+
*
531+
* @tparam Arr The std::array containing the boolean values (non type template argument).
532+
* @tparam A Architecture that will be used when converting to a regular batch.
533+
*/
534+
template <typename T, std::array Arr, class A = default_arch>
535+
requires(
536+
(Arr.size() == batch_bool<T, A>::size)
537+
&& std::is_same_v<typename decltype(Arr)::value_type, bool>)
538+
XSIMD_INLINE constexpr auto make_batch_bool_constant() noexcept
539+
{
540+
return detail::make_batch_bool_constant<T, Arr, A>(std::make_index_sequence<Arr.size()>());
541+
}
542+
#endif
543+
494544
#endif
495545

496546
namespace generator

test/test_batch_constant.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* The full license is in the file LICENSE, distributed with this software. *
1010
****************************************************************************/
1111

12+
#include <numeric>
13+
1214
#include "xsimd/xsimd.hpp"
1315
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
1416

@@ -43,6 +45,22 @@ struct constant_batch_test
4345
CHECK_BATCH_EQ((batch_type)b, expected);
4446
}
4547

48+
void test_init_from_array() const
49+
{
50+
#if __cplusplus >= 202002L
51+
constexpr array_type expected = []()
52+
{
53+
array_type out = {};
54+
std::iota(out.begin(), out.end(), 0);
55+
return out;
56+
}();
57+
58+
constexpr auto b = xsimd::make_batch_constant<expected, arch_type>();
59+
INFO("batch(value_type)");
60+
CHECK_BATCH_EQ((batch_type)b, expected);
61+
#endif
62+
}
63+
4664
void test_init_from_generator() const
4765
{
4866
array_type expected;
@@ -217,6 +235,8 @@ TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
217235
constant_batch_test<B> Test;
218236
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
219237

238+
SUBCASE("test_init_from_array") { Test.test_init_from_array(); }
239+
220240
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
221241

222242
SUBCASE("as_batch") { Test.test_cast(); }
@@ -263,6 +283,25 @@ struct constant_bool_batch_test
263283
CHECK_BATCH_EQ((batch_bool_type)b, expected);
264284
}
265285

286+
void test_init_from_array() const
287+
{
288+
#if __cplusplus >= 202002L
289+
constexpr bool_array_type expected = []()
290+
{
291+
bool_array_type out = {};
292+
for (std::size_t k = 0; k < out.size(); ++k)
293+
{
294+
out[k] = k % 2 == 0;
295+
}
296+
return out;
297+
}();
298+
299+
constexpr auto b = xsimd::make_batch_bool_constant<value_type, expected, arch_type>();
300+
INFO("batch_bool_constant(value_type)");
301+
CHECK_BATCH_EQ((batch_bool_type)b, expected);
302+
#endif
303+
}
304+
266305
void test_init_from_generator() const
267306
{
268307
bool_array_type expected;
@@ -357,6 +396,8 @@ TEST_CASE_TEMPLATE("[constant bool batch]", B, BATCH_INT_TYPES)
357396
constant_bool_batch_test<B> Test;
358397
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
359398

399+
SUBCASE("test_init_from_array") { Test.test_init_from_array(); }
400+
360401
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
361402

362403
SUBCASE("as_batch") { Test.test_cast(); }

0 commit comments

Comments
 (0)