libstdc++
simd_reductions.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_REDUCTIONS_H
26 #define _GLIBCXX_SIMD_REDUCTIONS_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.reductions] ----------------------------------------------------------
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 namespace simd
45 {
46  template <typename _Tp, typename _Ap, __reduction_binary_operation<_Tp> _BinaryOperation = plus<>>
47  [[__gnu__::__always_inline__]]
48  constexpr _Tp
49  reduce(const basic_vec<_Tp, _Ap>& __x, _BinaryOperation __binary_op = {})
50  { return __x._M_reduce(__binary_op); }
51 
52  template <typename _Tp, typename _Ap, __reduction_binary_operation<_Tp> _BinaryOperation = plus<>>
53  [[__gnu__::__always_inline__]]
54  constexpr _Tp
55  reduce(const basic_vec<_Tp, _Ap>& __x, const typename basic_vec<_Tp, _Ap>::mask_type& __mask,
56  _BinaryOperation __binary_op = {}, type_identity_t<_Tp> __identity_element
57  = __default_identity_element<_Tp, _BinaryOperation>())
58  { return reduce(__select_impl(__mask, __x, __identity_element), __binary_op); }
59 
60  template <totally_ordered _Tp, typename _Ap>
61  [[__gnu__::__always_inline__]]
62  constexpr _Tp
63  reduce_min(const basic_vec<_Tp, _Ap>& __x) noexcept
64  {
65  return reduce(__x, []<typename _UV>(const _UV& __a, const _UV& __b) {
66  return __select_impl(__a < __b, __a, __b);
67  });
68  }
69 
70  template <totally_ordered _Tp, typename _Ap>
71  [[__gnu__::__always_inline__]]
72  constexpr _Tp
73  reduce_min(const basic_vec<_Tp, _Ap>& __x,
74  const typename basic_vec<_Tp, _Ap>::mask_type& __mask) noexcept
75  {
76  return reduce(__select_impl(__mask, __x, numeric_limits<_Tp>::max()),
77  []<typename _UV>(const _UV& __a, const _UV& __b) {
78  return __select_impl(__a < __b, __a, __b);
79  });
80  }
81 
82  template <totally_ordered _Tp, typename _Ap>
83  [[__gnu__::__always_inline__]]
84  constexpr _Tp
85  reduce_max(const basic_vec<_Tp, _Ap>& __x) noexcept
86  {
87  return reduce(__x, []<typename _UV>(const _UV& __a, const _UV& __b) {
88  return __select_impl(__a < __b, __b, __a);
89  });
90  }
91 
92  template <totally_ordered _Tp, typename _Ap>
93  [[__gnu__::__always_inline__]]
94  constexpr _Tp
95  reduce_max(const basic_vec<_Tp, _Ap>& __x,
96  const typename basic_vec<_Tp, _Ap>::mask_type& __mask) noexcept
97  {
98  return reduce(__select_impl(__mask, __x, numeric_limits<_Tp>::lowest()),
99  []<typename _UV>(const _UV& __a, const _UV& __b) {
100  return __select_impl(__a < __b, __b, __a);
101  });
102  }
103 } // namespace simd
104 _GLIBCXX_END_NAMESPACE_VERSION
105 } // namespace std
106 
107 #pragma GCC diagnostic pop
108 #endif // C++26
109 #endif // _GLIBCXX_SIMD_REDUCTIONS_H
constexpr _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Calculate reduction of values in a range.
Definition: numeric:294
ISO C++ entities toplevel namespace is std.
static constexpr _Tp max() noexcept
Definition: limits:328
static constexpr _Tp lowest() noexcept
Definition: limits:334