libstdc++-v3/ChangeLog:

        * include/bits/simd_reductions.h: New file.

Signed-off-by: Matthias Kretz <[email protected]>
---
 libstdc++-v3/include/bits/simd_reductions.h | 112 ++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 libstdc++-v3/include/bits/simd_reductions.h


--
──────────────────────────────────────────────────────────────────────────
 Dr. Matthias Kretz                           https://mattkretz.github.io
 GSI Helmholtz Center for Heavy Ion Research               https://gsi.de
 std::simd
──────────────────────────────────────────────────────────────────────────
diff --git a/libstdc++-v3/include/bits/simd_reductions.h b/libstdc++-v3/include/bits/simd_reductions.h
new file mode 100644
index 00000000000..127cde01dee
--- /dev/null
+++ b/libstdc++-v3/include/bits/simd_reductions.h
@@ -0,0 +1,112 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later WITH GCC-exception-3.1 */
+/* Copyright © 2025      GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
+ *                       Matthias Kretz <[email protected]>
+ */
+
+#ifndef _GLIBCXX_SIMD_REDUCTIONS_H
+#define _GLIBCXX_SIMD_REDUCTIONS_H 1
+
+#ifdef _GLIBCXX_SYSHDR
+#pragma GCC system_header
+#endif
+
+#if __cplusplus >= 202400L
+
+#include "simd_vec.h"
+
+// psabi warnings are bogus because the ABI of the internal types never leaks into user code
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpsabi"
+
+// [simd.reductions] ----------------------------------------------------------
+namespace std::simd
+{
+  template <typename _Tp, typename _BinaryOperation>
+    inline constexpr _Tp __identity_element_for = nullptr;
+
+  template <typename _Tp>
+    inline constexpr _Tp __identity_element_for<_Tp, plus<>> = _Tp(0);
+
+  template <typename _Tp>
+    inline constexpr _Tp __identity_element_for<_Tp, multiplies<>> = _Tp(1);
+
+  template <typename _Tp>
+    inline constexpr _Tp __identity_element_for<_Tp, bit_and<>> = _Tp(~_Tp());
+
+  template <typename _Tp>
+    inline constexpr _Tp __identity_element_for<_Tp, bit_or<>> = _Tp(0);
+
+  template <typename _Tp>
+    inline constexpr _Tp __identity_element_for<_Tp, bit_xor<>> = _Tp(0);
+
+  template <typename _Tp, typename _BinaryOperation>
+    requires same_as<_BinaryOperation, plus<>>
+      or same_as<_BinaryOperation, multiplies<>>
+      or same_as<_BinaryOperation, bit_and<>>
+      or same_as<_BinaryOperation, bit_or<>>
+      or same_as<_BinaryOperation, bit_xor<>>
+    consteval _Tp __default_identity_element()
+    { return __identity_element_for<_Tp, _BinaryOperation>; }
+
+  template <typename _Tp, typename _Ap, __reduction_binary_operation<_Tp> _BinaryOperation = plus<>>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce(const basic_vec<_Tp, _Ap>& __x, _BinaryOperation __binary_op = {})
+    { return __x._M_reduce(__binary_op); }
+
+  template <typename _Tp, typename _Ap, __reduction_binary_operation<_Tp> _BinaryOperation = plus<>>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce(const basic_vec<_Tp, _Ap>& __x, const typename basic_vec<_Tp, _Ap>::mask_type& __mask,
+           _BinaryOperation __binary_op = {}, type_identity_t<_Tp> __identity_element
+             = __default_identity_element<_Tp, _BinaryOperation>())
+    { return reduce(select(__mask, __x, __identity_element), __binary_op); }
+
+  template <totally_ordered _Tp, typename _Ap>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce_min(const basic_vec<_Tp, _Ap>& __x) noexcept
+    {
+      return reduce(__x, []<typename _UV>(const _UV& __a, const _UV& __b) {
+               return select(__a < __b, __a, __b);
+             });
+    }
+
+  template <totally_ordered _Tp, typename _Ap>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce_min(const basic_vec<_Tp, _Ap>& __x,
+               const typename basic_vec<_Tp, _Ap>::mask_type& __mask) noexcept
+    {
+      return reduce(select(__mask, __x, numeric_limits<_Tp>::max()),
+                    []<typename _UV>(const _UV& __a, const _UV& __b) {
+                      return select(__a < __b, __a, __b);
+                    });
+    }
+
+  template <totally_ordered _Tp, typename _Ap>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce_max(const basic_vec<_Tp, _Ap>& __x) noexcept
+    {
+      return reduce(__x, []<typename _UV>(const _UV& __a, const _UV& __b) {
+               return select(__a < __b, __b, __a);
+             });
+    }
+
+  template <totally_ordered _Tp, typename _Ap>
+    [[__gnu__::__always_inline__]]
+    constexpr _Tp
+    reduce_max(const basic_vec<_Tp, _Ap>& __x,
+               const typename basic_vec<_Tp, _Ap>::mask_type& __mask) noexcept
+    {
+      return reduce(select(__mask, __x, numeric_limits<_Tp>::lowest()),
+                    []<typename _UV>(const _UV& __a, const _UV& __b) {
+                      return select(__a < __b, __b, __a);
+                    });
+    }
+}
+
+#pragma GCC diagnostic pop
+#endif // C++26
+#endif // _GLIBCXX_SIMD_REDUCTIONS_H

Reply via email to