Skip to content

Commit

Permalink
Added broadcast overload for bool
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Feb 24, 2025
1 parent b3c882c commit 9deba5b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
28 changes: 28 additions & 0 deletions include/xsimd/arch/generic/xsimd_generic_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ namespace xsimd

using namespace types;

// broadcast
namespace detail
{
template <class T, class A>
struct broadcaster
{
using value_type = T;
using return_type = batch<T, A>;

static return_type run(T v) noexcept
{
return return_type::broadcast(v);
}
};

template <class A>
struct broadcaster<bool, A>
{
using value_type = uint8_t;
using return_type = batch_bool<uint8_t, A>;

static return_type run(bool b) noexcept
{
return return_type(b);
}
};
}

// compress
namespace detail
{
Expand Down
7 changes: 4 additions & 3 deletions include/xsimd/types/xsimd_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,11 @@ namespace xsimd
* @return a new batch instance
*/
template <class T, class A = default_arch>
XSIMD_INLINE batch<T, A> broadcast(T v) noexcept
XSIMD_INLINE typename kernel::detail::broadcaster<T, A>::return_type broadcast(T v) noexcept
{
detail::static_check_supported_config<T, A>();
return batch<T, A>::broadcast(v);
using value_type = typename kernel::detail::broadcaster<T, A>::value_type;
detail::static_check_supported_config<value_type, A>();
return kernel::detail::broadcaster<T, A>::run(v);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/test_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct xsimd_api_test
{
using batch_type = B;
using batch_bool_type = typename B::batch_bool_type;
using arch_type = typename B::arch_type;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
using array_type = std::array<value_type, size>;
Expand Down Expand Up @@ -100,6 +101,7 @@ struct xsimd_api_test

void test_set()
{
test_set_bool("set bool");
test_set_impl<int8_t>("set int8_t");
test_set_impl<uint8_t>("set uint8_t");
test_set_impl<int16_t>("set int16_t");
Expand Down Expand Up @@ -171,6 +173,15 @@ struct xsimd_api_test
CHECK_BATCH_EQ(res, expected);
}

void test_set_bool(const std::string& name)
{
bool v = true;
xsimd::batch_bool<uint8_t, arch_type> expected(v);
xsimd::batch_bool<uint8_t, arch_type> res = xsimd::broadcast(v);
INFO(name);
CHECK_BATCH_EQ(res, expected);
}

template <class V>
void init_test_vector(V& vec)
{
Expand Down

0 comments on commit 9deba5b

Please sign in to comment.