Skip to content

Commit 37e5d9f

Browse files
authored
Merge pull request #1203 from AntoinePrv/batch_constant
Add constant batch_constant generator
2 parents aaec742 + c49fa4c commit 37e5d9f

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

include/xsimd/types/xsimd_batch_constant.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,28 @@ namespace xsimd
251251
{
252252
return {};
253253
}
254+
255+
template <typename T, T Val, class A, std::size_t... Is>
256+
XSIMD_INLINE constexpr batch_constant<T, A, (static_cast<T>(0 * Is) + Val)...>
257+
make_batch_constant(detail::index_sequence<Is...>) noexcept
258+
{
259+
return {};
260+
}
261+
254262
template <typename T, class G, class A, std::size_t... Is>
255263
XSIMD_INLINE constexpr batch_bool_constant<T, A, G::get(Is, sizeof...(Is))...>
256264
make_batch_bool_constant(detail::index_sequence<Is...>) noexcept
257265
{
258266
return {};
259267
}
260268

269+
template <typename T, bool Val, class A, std::size_t... Is>
270+
XSIMD_INLINE constexpr batch_bool_constant<T, A, ((static_cast<bool>(Is) | true) & Val)...>
271+
make_batch_bool_constant(detail::index_sequence<Is...>) noexcept
272+
{
273+
return {};
274+
}
275+
261276
} // namespace detail
262277

263278
/**
@@ -287,13 +302,41 @@ namespace xsimd
287302
return {};
288303
}
289304

305+
/**
306+
* @brief Build a @c batch_constant with a single repeated value.
307+
*
308+
* @tparam T type of the data held in the batch.
309+
* @tparam Val The value to repeat.
310+
* @tparam A Architecture that will be used when converting to a regular batch.
311+
*/
312+
template <typename T, T Val, class A = default_arch>
313+
XSIMD_INLINE constexpr decltype(detail::make_batch_constant<T, Val, A>(detail::make_index_sequence<batch<T, A>::size>()))
314+
make_batch_constant() noexcept
315+
{
316+
return {};
317+
}
318+
290319
template <typename T, class G, class A = default_arch>
291320
XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant<T, G, A>(detail::make_index_sequence<batch<T, A>::size>()))
292321
make_batch_bool_constant() noexcept
293322
{
294323
return {};
295324
}
296325

326+
/**
327+
* @brief Build a @c batch_bool_constant with a single repeated value.
328+
*
329+
* @tparam T type of the data held in the batch.
330+
* @tparam Val The value to repeat.
331+
* @tparam A Architecture that will be used when converting to a regular batch.
332+
*/
333+
template <typename T, bool Val, class A = default_arch>
334+
XSIMD_INLINE constexpr decltype(detail::make_batch_bool_constant<T, Val, A>(detail::make_index_sequence<batch<T, A>::size>()))
335+
make_batch_bool_constant() noexcept
336+
{
337+
return {};
338+
}
339+
297340
} // namespace xsimd
298341

299342
#endif

test/test_batch_constant.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ struct constant_batch_test
3333
}
3434
};
3535

36+
void test_init_from_constant() const
37+
{
38+
array_type expected;
39+
std::generate(expected.begin(), expected.end(), []()
40+
{ return 1; });
41+
constexpr auto b = xsimd::make_batch_constant<value_type, 1, arch_type>();
42+
INFO("batch(value_type)");
43+
CHECK_BATCH_EQ((batch_type)b, expected);
44+
}
45+
3646
void test_init_from_generator() const
3747
{
3848
array_type expected;
@@ -83,7 +93,7 @@ struct constant_batch_test
8393
}
8494
};
8595

86-
void test_init_from_constant() const
96+
void test_init_from_constant_generator() const
8797
{
8898
array_type expected;
8999
std::fill(expected.begin(), expected.end(), constant<3>::get(0, 0));
@@ -142,6 +152,8 @@ struct constant_batch_test
142152
TEST_CASE_TEMPLATE("[constant batch]", B, BATCH_INT_TYPES)
143153
{
144154
constant_batch_test<B> Test;
155+
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
156+
145157
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
146158

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

154-
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
166+
SUBCASE("init_from_constant_generator") { Test.test_init_from_constant_generator(); }
155167

156168
SUBCASE("operators")
157169
{
@@ -178,6 +190,16 @@ struct constant_bool_batch_test
178190
}
179191
};
180192

193+
void test_init_from_constant() const
194+
{
195+
bool_array_type expected;
196+
std::generate(expected.begin(), expected.end(), []()
197+
{ return false; });
198+
constexpr auto b = xsimd::make_batch_bool_constant<value_type, false, arch_type>();
199+
INFO("batch_bool_constant(value_type)");
200+
CHECK_BATCH_EQ((batch_bool_type)b, expected);
201+
}
202+
181203
void test_init_from_generator() const
182204
{
183205
bool_array_type expected;
@@ -270,6 +292,8 @@ struct constant_bool_batch_test
270292
TEST_CASE_TEMPLATE("[constant bool batch]", B, BATCH_INT_TYPES)
271293
{
272294
constant_bool_batch_test<B> Test;
295+
SUBCASE("init_from_constant") { Test.test_init_from_constant(); }
296+
273297
SUBCASE("init_from_generator") { Test.test_init_from_generator(); }
274298

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

0 commit comments

Comments
 (0)