Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
std_algorithm.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <xvec/detail/config.hpp>
12
13#include <concepts>
14#include <utility> // std::pair, std::make_pair
15
16namespace _XVEC_NAMESPACE::simd
17{
18
26
32template<vec_type _Vp>
33requires std::totally_ordered<typename _Vp::value_type>
34constexpr _Vp min(const _Vp& lhs, const _Vp& rhs) noexcept {
35 if (std::is_constant_evaluated() || !detail::builtin_vectorizable<typename _Vp::value_type>)
36 return _Vp([=](auto i) { using std::min; return min(lhs[i], rhs[i]); });
37 else
38 return detail::minimum(target, lhs, rhs);
39}
40
46template<vec_type _Vp>
47requires std::totally_ordered<typename _Vp::value_type>
48constexpr _Vp max(const _Vp& lhs, const _Vp& rhs) noexcept {
49 if (std::is_constant_evaluated() || !detail::builtin_vectorizable<typename _Vp::value_type>)
50 return _Vp([=](auto i) { using std::max; return max(lhs[i], rhs[i]); });
51 else
52 return detail::maximum(target, lhs, rhs);
53}
54
61template<vec_totally_ordered _Vp>
62requires std::totally_ordered<typename _Vp::value_type>
63constexpr std::pair<_Vp, _Vp>
64minmax(const _Vp& lhs, const _Vp& rhs) noexcept { return std::make_pair(min(lhs, rhs), max(lhs, rhs)); }
65
74template<vec_totally_ordered _Vp>
75requires std::totally_ordered<typename _Vp::value_type>
76constexpr _Vp
77clamp(const _Vp& values, const _Vp& low, const _Vp& high) { return min(max(values, low), high); }
78
87template<mask_type _M, class _T, class _U>
88constexpr auto select(const _M& m, const _T& if_true, const _U& if_false) noexcept -> decltype(simd_select_impl(m, if_true, if_false))
89 { return simd_select_impl(m, if_true, if_false); }
90
98template<std::copyable _Tp, std::copyable _Up>
99constexpr auto select(bool c, const _Tp& a, const _Up& b) -> std::remove_cvref_t<decltype(c ? a : b)> { return c ? a : b; }
100
102
103} // namespace _XVEC_NAMESPACE::simd
constexpr _Vp clamp(const _Vp &values, const _Vp &low, const _Vp &high)
Clamp the input values to be contained within the range [low,high]. If a value is less than low it wi...
Definition std_algorithm.hpp:77
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 std::pair< _Vp, _Vp > minmax(const _Vp &lhs, const _Vp &rhs) noexcept
Compute a pair of vec values giving the smaller and larger respective of each corresponding vec eleme...
Definition std_algorithm.hpp:64
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