Skip to content

Commit 40ef070

Browse files
Added broadcast overload for bool (#1089)
* Added broadcast overload for bool * Improved documentation --------- Co-authored-by: serge-sans-paille <[email protected]>
1 parent b3c882c commit 40ef070

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

include/xsimd/arch/generic/xsimd_generic_memory.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ namespace xsimd
3232

3333
using namespace types;
3434

35+
// broadcast
36+
namespace detail
37+
{
38+
template <class T, class A>
39+
struct broadcaster
40+
{
41+
using return_type = batch<T, A>;
42+
43+
static XSIMD_INLINE return_type run(T v) noexcept
44+
{
45+
return return_type::broadcast(v);
46+
}
47+
};
48+
49+
template <class A>
50+
struct broadcaster<bool, A>
51+
{
52+
using return_type = batch_bool<xsimd::as_unsigned_integer_t<bool>, A>;
53+
54+
static XSIMD_INLINE return_type run(bool b) noexcept
55+
{
56+
return return_type(b);
57+
}
58+
};
59+
}
60+
3561
// compress
3662
namespace detail
3763
{

include/xsimd/types/xsimd_api.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,17 @@ namespace xsimd
486486
/**
487487
* @ingroup batch_data_transfer
488488
*
489-
* Creates a batch from the single value \c v.
489+
* Creates a batch from the single value \c v. If \c v is a boolean,
490+
* this function returns a batch_bool<uint8_t>. If you need another type
491+
* of batch_bool, please use \c broadcast_as instead.
490492
* @param v the value used to initialize the batch
491493
* @return a new batch instance
492494
*/
493495
template <class T, class A = default_arch>
494-
XSIMD_INLINE batch<T, A> broadcast(T v) noexcept
496+
XSIMD_INLINE typename kernel::detail::broadcaster<T, A>::return_type broadcast(T v) noexcept
495497
{
496498
detail::static_check_supported_config<T, A>();
497-
return batch<T, A>::broadcast(v);
499+
return kernel::detail::broadcaster<T, A>::run(v);
498500
}
499501

500502
/**

include/xsimd/types/xsimd_traits.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ namespace xsimd
7777
"usage of batch type with unsupported type");
7878
};
7979

80+
template <class A>
81+
struct static_check_supported_config_emitter<bool, A> : static_check_supported_config_emitter<xsimd::as_unsigned_integer_t<bool>, A>
82+
{
83+
};
84+
8085
template <class T, class A>
8186
struct static_check_supported_config_emitter<std::complex<T>, A> : static_check_supported_config_emitter<T, A>
8287
{

include/xsimd/types/xsimd_utils.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ namespace xsimd
7777
{
7878
};
7979

80+
template <>
81+
struct as_unsigned_integer<bool>
82+
{
83+
using type = uint8_t;
84+
};
85+
8086
template <>
8187
struct as_unsigned_integer<float>
8288
{

test/test_api.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct xsimd_api_test
2323
{
2424
using batch_type = B;
2525
using batch_bool_type = typename B::batch_bool_type;
26+
using arch_type = typename B::arch_type;
2627
using value_type = typename B::value_type;
2728
static constexpr size_t size = B::size;
2829
using array_type = std::array<value_type, size>;
@@ -100,6 +101,7 @@ struct xsimd_api_test
100101

101102
void test_set()
102103
{
104+
test_set_bool("set bool");
103105
test_set_impl<int8_t>("set int8_t");
104106
test_set_impl<uint8_t>("set uint8_t");
105107
test_set_impl<int16_t>("set int16_t");
@@ -171,6 +173,15 @@ struct xsimd_api_test
171173
CHECK_BATCH_EQ(res, expected);
172174
}
173175

176+
void test_set_bool(const std::string& name)
177+
{
178+
bool v = true;
179+
xsimd::batch_bool<uint8_t, arch_type> expected(v);
180+
xsimd::batch_bool<uint8_t, arch_type> res = xsimd::broadcast(v);
181+
INFO(name);
182+
CHECK_BATCH_EQ(res, expected);
183+
}
184+
174185
template <class V>
175186
void init_test_vector(V& vec)
176187
{

0 commit comments

Comments
 (0)