Skip to content

Commit 96a24ae

Browse files
authored
Merge pull request #874 from xtensor-stack/feature/generic-reducer
Provide a generic reducer
2 parents 23a95d8 + ade0acc commit 96a24ae

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/xsimd/types/xsimd_api.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,21 @@ namespace xsimd
877877
return x > y;
878878
}
879879

880+
/**
881+
* @ingroup batch_reducers
882+
*
883+
* Generic reducer using only batch operations
884+
* @param f reducing function, accepting `batch ()(batch, batch)`
885+
* @param x batch involved in the reduction
886+
* @return the result of the reduction, as a scalar.
887+
*/
888+
template <class T, class A, class F>
889+
inline T reduce(F&& r, batch<T, A> const& x) noexcept
890+
{
891+
detail::static_check_supported_config<T, A>();
892+
return kernel::detail::reduce(std::forward<F>(r), x, std::integral_constant<unsigned, batch<T, A>::size>());
893+
}
894+
880895
/**
881896
* @ingroup batch_reducers
882897
*

test/test_batch.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,19 @@ struct batch_test
718718
}
719719
}
720720

721+
template <size_t N>
722+
typename std::enable_if<4 <= N, void>::type test_generic_horizontal_operations(std::integral_constant<size_t, N>) const
723+
{
724+
// reduce generic
725+
{
726+
value_type expected = std::accumulate(lhs.cbegin(), lhs.cend(), value_type(1), std::multiplies<value_type>());
727+
value_type res = reduce(xsimd::mul<typename B::value_type, typename B::arch_type>, batch_lhs());
728+
INFO("generic reduce");
729+
CHECK_SCALAR_EQ(res, expected);
730+
}
731+
}
732+
void test_generic_horizontal_operations(...) const { }
733+
721734
void test_boolean_conversions() const
722735
{
723736
using batch_bool_type = typename batch_type::batch_bool_type;
@@ -879,6 +892,7 @@ TEST_CASE_TEMPLATE("[batch]", B, BATCH_TYPES)
879892
SUBCASE("horizontal_operations")
880893
{
881894
Test.test_horizontal_operations();
895+
Test.test_generic_horizontal_operations(std::integral_constant<size_t, sizeof(typename B::value_type)>());
882896
}
883897

884898
SUBCASE("boolean_conversions")

0 commit comments

Comments
 (0)