libstdc++
simd_alg.h
1 // Implementation of <simd> -*- C++ -*-
2 
3 // Copyright The GNU Toolchain Authors.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 #ifndef _GLIBCXX_SIMD_ALG_H
26 #define _GLIBCXX_SIMD_ALG_H 1
27 
28 #ifdef _GLIBCXX_SYSHDR
29 #pragma GCC system_header
30 #endif
31 
32 #if __cplusplus >= 202400L
33 
34 #include "simd_vec.h"
35 
36 // psabi warnings are bogus because the ABI of the internal types never leaks into user code
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wpsabi"
39 
40 // [simd.alg] -----------------------------------------------------------------
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 namespace simd
45 {
46  template<typename _Tp, typename _Ap>
47  [[__gnu__::__always_inline__]]
48  constexpr basic_vec<_Tp, _Ap>
49  min(const basic_vec<_Tp, _Ap>& __a, const basic_vec<_Tp, _Ap>& __b) noexcept
50  { return __select_impl(__a < __b, __a, __b); }
51 
52  template<typename _Tp, typename _Ap>
53  [[__gnu__::__always_inline__]]
54  constexpr basic_vec<_Tp, _Ap>
55  max(const basic_vec<_Tp, _Ap>& __a, const basic_vec<_Tp, _Ap>& __b) noexcept
56  { return __select_impl(__a < __b, __b, __a); }
57 
58  template<typename _Tp, typename _Ap>
59  [[__gnu__::__always_inline__]]
60  constexpr pair<basic_vec<_Tp, _Ap>, basic_vec<_Tp, _Ap>>
61  minmax(const basic_vec<_Tp, _Ap>& __a, const basic_vec<_Tp, _Ap>& __b) noexcept
62  { return {min(__a, __b), max(__a, __b)}; }
63 
64  template<typename _Tp, typename _Ap>
65  [[__gnu__::__always_inline__]]
66  constexpr basic_vec<_Tp, _Ap>
67  clamp(const basic_vec<_Tp, _Ap>& __v, const basic_vec<_Tp, _Ap>& __lo,
68  const basic_vec<_Tp, _Ap>& __hi)
69  {
70  __glibcxx_simd_precondition(none_of(__lo > __hi), "lower bound is larger than upper bound");
71  return max(__lo, min(__hi, __v));
72  }
73 
74  template<typename _Tp, typename _Up>
75  constexpr auto
76  select(bool __c, const _Tp& __a, const _Up& __b)
77  -> remove_cvref_t<decltype(__c ? __a : __b)>
78  { return __c ? __a : __b; }
79 
80  template<size_t _Bytes, typename _Ap, typename _Tp, typename _Up>
81  [[__gnu__::__always_inline__]]
82  constexpr auto
83  select(const basic_mask<_Bytes, _Ap>& __c, const _Tp& __a, const _Up& __b)
84  noexcept -> decltype(__select_impl(__c, __a, __b))
85  { return __select_impl(__c, __a, __b); }
86 } // namespace simd
87 
88  using simd::min;
89  using simd::max;
90  using simd::minmax;
91  using simd::clamp;
92 
93 _GLIBCXX_END_NAMESPACE_VERSION
94 } // namespace std
95 
96 #pragma GCC diagnostic pop
97 #endif // C++26
98 #endif // _GLIBCXX_SIMD_ALG_H
constexpr const _Tp & clamp(const _Tp &, const _Tp &, const _Tp &)
Returns the value clamped between lo and hi.
Definition: stl_algo.h:3614
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:256
constexpr pair< const _Tp &, const _Tp & > minmax(const _Tp &, const _Tp &)
Determines min and max at once as an ordered pair.
Definition: stl_algo.h:3287
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:232
ISO C++ entities toplevel namespace is std.