Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/xsimd/arch/generic/xsimd_generic_logical.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ namespace xsimd {
template<class A, class T> batch_bool<T, A> neq(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) {
return !(other == self);
}

// logical_and
template <class A, class T>
batch<T, A> logical_and(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) {
return detail::apply([](T x, T y) { return x && y;}, self, other);
}

// logical_or
template <class A, class T>
batch<T, A> logical_or(batch<T, A> const& self, batch<T, A> const& other, requires_arch<generic>) {
return detail::apply([](T x, T y) { return x || y;}, self, other);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions include/xsimd/types/xsimd_batch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ struct batch : types::simd_register<T, A> {
return batch(self) <<= other;
}

friend batch operator&&(batch const& self, batch const& other) {
return batch(self).logical_and(other);
}

friend batch operator||(batch const& self, batch const& other) {
return batch(self).logical_or(other);
}

// Update operators
batch& operator+=(batch const& other);
batch& operator-=(batch const& other);
Expand All @@ -147,6 +155,9 @@ struct batch : types::simd_register<T, A> {
private:
template<size_t... Is>
batch(T const* data, detail::index_sequence<Is...>);

batch logical_and(batch const& other) const;
batch logical_or(batch const& other) const;
};

template <class T, class A>
Expand Down Expand Up @@ -431,6 +442,16 @@ batch_bool<T, A> batch<T, A>::operator>(batch<T, A> const& other) const { return
template<class T, class A>
batch_bool<T, A> batch<T, A>::operator<(batch<T, A> const& other) const { return kernel::lt<A>(*this, other, A{}); }

template<class T, class A>
batch<T, A> batch<T, A>::logical_and(batch<T, A> const& other) const {
return kernel::logical_and<A>(*this, other, A());
}

template<class T, class A>
batch<T, A> batch<T, A>::logical_or(batch<T, A> const& other) const {
return kernel::logical_or<A>(*this, other, A());
}

template<class T, class A>
batch<T, A>& batch<T, A>::operator+=(batch<T, A> const& other) { return *this = kernel::add<A>(*this, other, A{}); }

Expand Down
40 changes: 40 additions & 0 deletions test/test_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,42 @@ class batch_test : public testing::Test
}
}

void test_logical() const
{
// batch && batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(), std::logical_and<value_type>());
batch_type res = batch_lhs() && batch_rhs();
EXPECT_BATCH_EQ(res, expected) << print_function_name("batch && batch");
}
// batch && scalar
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(), std::bind(std::logical_and<value_type>(), _1, scalar));
batch_type lres = batch_lhs() && scalar;
EXPECT_BATCH_EQ(lres, expected) << print_function_name("batch && scalar");
batch_type rres = scalar && batch_lhs();
EXPECT_BATCH_EQ(rres, expected) << print_function_name("scalar && batch");
}
// batch || batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(), std::logical_or<value_type>());
batch_type res = batch_lhs() || batch_rhs();
EXPECT_BATCH_EQ(res, expected) << print_function_name("batch && batch");
}
// batch || scalar
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(), std::bind(std::logical_or<value_type>(), _1, scalar));
batch_type lres = batch_lhs() || scalar;
EXPECT_BATCH_EQ(lres, expected) << print_function_name("batch || scalar");
batch_type rres = scalar || batch_lhs();
EXPECT_BATCH_EQ(rres, expected) << print_function_name("scalar || batch");
}
}

void test_min_max() const
{
// min
Expand Down Expand Up @@ -688,6 +724,10 @@ TYPED_TEST(batch_test, comparison)
{
this->test_comparison();
}
TYPED_TEST(batch_test, logical)
{
this->test_logical();
}

TYPED_TEST(batch_test, min_max)
{
Expand Down