11#include <xvec/detail/config.hpp>
18namespace _XVEC_NAMESPACE::simd
23inline auto minBinaryOperator = [](
auto lhs,
auto rhs) {
using std::min;
return min(lhs, rhs); };
24inline auto maxBinaryOperator = [](
auto lhs,
auto rhs) {
using std::max;
return max(lhs, rhs); };
26template<
typename _Op,
typename _Tp>
28 requires (
const _Op binary_op,
const vec<_Tp, 1> v) {{ binary_op(v, v) } -> std::same_as<vec<_Tp, 1>>; };
30template<
typename _Op,
typename _Tp>
31constexpr auto choose_identity_element() {
32 if constexpr (std::same_as<_Op, std::plus<>>)
34 else if constexpr (std::same_as<_Op, std::multiplies<>>)
36 else if constexpr (std::same_as<_Op, std::bit_and<>>)
38 else if constexpr (std::same_as<_Op, std::bit_or<>>)
40 else if constexpr (std::same_as<_Op, std::bit_xor<>>)
52template<simd_
size_type _Size,
typename _Tp,
typename _Abi, binary_reduction_fn<_Tp> _BinaryFn>
55 if constexpr (_Size == 1)
64 constexpr int halfSize = std::bit_ceil((std::uint64_t)_Size) / 2;
65 constexpr auto opSize = std::max(register_size<_Tp, 1>, halfSize);
70 auto r = binop(lower, upper);
75 constexpr auto remainderSize = _Size - halfSize;
76 if constexpr (halfSize != remainderSize)
79 r =
select(remainderMask, r, lower);
104template<vec_type _V, detail::binary_reduction_fn<
typename _V::value_type> _BinaryFn = std::plus<>>
105constexpr typename _V::value_type
reduce(
const _V& v, _BinaryFn binop = {})
106 {
return detail::reduce_helper<_V::size()>(v, binop); }
117template<
typename _Tp,
typename _Abi, detail::binary_reduction_fn<_Tp> _BinaryFn = std::plus<>>
120 _BinaryFn binop = {},
121 std::type_identity_t<_Tp> identity_element = detail::choose_identity_element<_BinaryFn, _Tp>())
122 {
return detail::reduce_helper<_Abi::num_elements>(select(m, v, identity_element), binop); }
130template<detail::vectorizable _Tp,
131 detail::binary_reduction_fn<_Tp> _BinaryFn = std::plus<>>
132constexpr _Tp
reduce(
const _Tp& v, [[maybe_unused]] _BinaryFn binop = {})
noexcept
143template<detail::vectorizable _Tp,
144 detail::binary_reduction_fn<_Tp> _BinaryFn = std::plus<>>
146 const _Tp& v, std::same_as<bool>
auto m, [[maybe_unused]] _BinaryFn binop = {},
147 std::type_identity_t<_Tp> identity_element =
148 detail::choose_identity_element<_BinaryFn, _Tp>())
noexcept
149 {
return m ? v : identity_element; }
155template<vec_type _V>
requires requires (_V x) { min(x, x); }
156constexpr typename _V::value_type
reduce_min(
const _V& v)
noexcept
157 {
return reduce(v, detail::minBinaryOperator); }
162template<vec_type _V>
requires requires (_V x) { reduce_min(x); }
163constexpr typename _V::value_type
164reduce_min(
const _V& v,
const typename _V::mask_type& m)
noexcept
170 if constexpr (std::numeric_limits<typename _V::value_type>::has_infinity)
173 return std::numeric_limits<typename _V::value_type>::max();
178 const auto r =
select(m, v, std::numeric_limits<_V>::max());
186template<vec_type _V>
requires requires (_V x) { max(x, x); }
187constexpr typename _V::value_type
reduce_max(
const _V& v)
noexcept {
return reduce(v, detail::maxBinaryOperator); }
192template<vec_type _V>
requires requires (_V x) { reduce_max(x); }
193constexpr typename _V::value_type
194reduce_max(
const _V& v,
const typename _V::mask_type& m)
noexcept
200 if constexpr (std::numeric_limits<typename _V::value_type>::has_infinity)
203 return std::numeric_limits<typename _V::value_type>::lowest();
208 const auto r =
select(m, v, std::numeric_limits<_V>::lowest());
217template<detail::vectorizable _Tp>
requires std::totally_ordered<_Tp>
225template<detail::vectorizable _Tp>
requires std::totally_ordered<_Tp>
226constexpr _Tp
reduce_min(
const _Tp& v, std::same_as<bool>
auto m)
noexcept
227 {
return m ? v : std::numeric_limits<_Tp>::max(); }
233template<detail::vectorizable _Tp>
requires std::totally_ordered<_Tp>
241template<detail::vectorizable _Tp>
requires std::totally_ordered<_Tp>
242constexpr _Tp
reduce_max(
const _Tp& v, std::same_as<bool>
auto m)
noexcept
243 {
return m ? v : std::numeric_limits<_Tp>::lowest(); }
static constexpr basic_mask __mask_from_count(simd_size_type _n)
Definition mask.hpp:223
Vector of elements sharing an element type and ABI.
Definition simd:161
basic_mask< sizeof(_Tp), _Abi > mask_type
Definition simd:184
constexpr _Vp min(const _Vp &lhs, const _Vp &rhs) noexcept
Compute the smaller of each respective vec element from the two input arguments.
Definition std_algorithm.hpp:34
constexpr _Vp max(const _Vp &lhs, const _Vp &rhs) noexcept
Compute the larger of each respective vec element from the two input arguments.
Definition std_algorithm.hpp:48
constexpr auto select(const _M &m, const _T &if_true, const _U &if_false) noexcept -> decltype(simd_select_impl(m, if_true, if_false))
Select between corresponding elements according to a mask.
Definition std_algorithm.hpp:88
constexpr bool none_of(const mask_type auto &mask) noexcept
Return true if no mask element is true.
Definition mask.hpp:489
basic_vec< _Tp, simd_fixed_size_abi< _Np > > vec
Definition core.hpp:379
constexpr _V::value_type reduce(const _V &v, _BinaryFn binop={})
Apply a reduction operation across all elements of the basic_vec value to reduce them to a single sca...
Definition reduce.hpp:105
constexpr _V::value_type reduce_max(const _V &v) noexcept
Return the maximum vector element.
Definition reduce.hpp:187
constexpr _V::value_type reduce_min(const _V &v) noexcept
Return the minimum vector element.
Definition reduce.hpp:156
constexpr auto drop(const vec_or_mask_type auto &in)
Discard the first _Np elements of the input, returning the remainder.
Definition named_permute.hpp:240
constexpr basic_vec< _Tp, _AbiIp > permute(VENDOR tag, const basic_vec< _Tp, _AbiTp > &v, const basic_vec< _Ip, _AbiIp > &indexes)
Definition permute.hpp:276
constexpr bool dependent_false
Definition utilities.hpp:103
_Tp reduce_helper(const basic_vec< _Tp, _Abi > &v, _BinaryFn binop)
Definition reduce.hpp:53