[libcxx-commits] [libcxx] [libc++] Implement P0493R5: Atomic minimum/maximum (PR #180333)

Connector Switch via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 8 03:37:50 PST 2026


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/180333

>From ab79abbd1c14a305de4c16b791bf5e22384d543a Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 17:10:11 +0800
Subject: [PATCH 1/8] implement atomic max/min

---
 libcxx/docs/FeatureTestMacroTable.rst         |  2 +-
 libcxx/docs/ReleaseNotes/23.rst               |  1 +
 libcxx/docs/Status/Cxx2cPapers.csv            |  2 +-
 libcxx/include/__atomic/atomic.h              | 68 ++++++++++++++++++
 libcxx/include/__atomic/atomic_ref.h          |  8 +++
 libcxx/include/__atomic/support.h             |  9 +++
 libcxx/include/__atomic/support/c11.h         | 26 +++++++
 libcxx/include/__atomic/support/gcc.h         | 24 +++++++
 libcxx/include/atomic                         | 24 +++++++
 libcxx/include/version                        |  2 +-
 libcxx/modules/std/atomic.inc                 |  4 ++
 .../atomics/atomics.ref/fetch_max.pass.cpp    | 70 +++++++++++++++++++
 .../atomics/atomics.ref/fetch_min.pass.cpp    | 70 +++++++++++++++++++
 .../atomic_fetch_max.pass.cpp                 | 55 +++++++++++++++
 .../atomic_fetch_max_explicit.pass.cpp        | 57 +++++++++++++++
 .../atomic_fetch_min.pass.cpp                 | 55 +++++++++++++++
 .../atomic_fetch_min_explicit.pass.cpp        | 57 +++++++++++++++
 .../atomic.version.compile.pass.cpp           | 16 ++---
 .../version.version.compile.pass.cpp          | 16 ++---
 .../generate_feature_test_macro_components.py |  1 -
 20 files changed, 541 insertions(+), 26 deletions(-)
 create mode 100644 libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
 create mode 100644 libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
 create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max.pass.cpp
 create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_explicit.pass.cpp
 create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min.pass.cpp
 create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_explicit.pass.cpp

diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index d2b76cb96e866..701775e051f9a 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -416,7 +416,7 @@ Status
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_associative_heterogeneous_insertion``          *unimplemented*
     ---------------------------------------------------------- -----------------
-    ``__cpp_lib_atomic_min_max``                               *unimplemented*
+    ``__cpp_lib_atomic_min_max``                               ``202403L``
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_atomic_ref``                                   ``202411L``
     ---------------------------------------------------------- -----------------
diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index a1d62a367f33c..ce2eee5ed852a 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -39,6 +39,7 @@ Implemented Papers
 ------------------
 
 - P2440R1 (partial): ``ranges::iota`` and ``ranges::shift_left`` are supported (`Github <https://llvm.org/PR105184>`__)
+- P0493R5: Atomic minimum/maximum are supported (`Github <https://llvm.org/PR105418>`__)
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 29642fc53cac6..50819f1b47a27 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -53,7 +53,7 @@
 "`P3107R5 <https://wg21.link/P3107R5>`__","Permit an efficient implementation of ``std::print``","2024-03 (Tokyo)","","","`#105414 <https://github.com/llvm/llvm-project/issues/105414>`__",""
 "`P3142R0 <https://wg21.link/P3142R0>`__","Printing Blank Lines with ``println``","2024-03 (Tokyo)","|Complete|","19","`#105415 <https://github.com/llvm/llvm-project/issues/105415>`__","Implemented as a DR against C++23. (MSVC STL and libstdc++ will do the same.)"
 "`P2845R8 <https://wg21.link/P2845R8>`__","Formatting of ``std::filesystem::path``","2024-03 (Tokyo)","","","`#105416 <https://github.com/llvm/llvm-project/issues/105416>`__",""
-"`P0493R5 <https://wg21.link/P0493R5>`__","Atomic minimum/maximum","2024-03 (Tokyo)","","","`#105418 <https://github.com/llvm/llvm-project/issues/105418>`__",""
+"`P0493R5 <https://wg21.link/P0493R5>`__","Atomic minimum/maximum","2024-03 (Tokyo)","|Complete|","23","`#105418 <https://github.com/llvm/llvm-project/issues/105418>`__",""
 "`P2542R8 <https://wg21.link/P2542R8>`__","``views::concat``","2024-03 (Tokyo)","","","`#105419 <https://github.com/llvm/llvm-project/issues/105419>`__",""
 "`P2591R5 <https://wg21.link/P2591R5>`__","Concatenation of strings and string views","2024-03 (Tokyo)","|Complete|","19","`#105420 <https://github.com/llvm/llvm-project/issues/105420>`__",""
 "`P2248R8 <https://wg21.link/P2248R8>`__","Enabling list-initialization for algorithms","2024-03 (Tokyo)","","","`#105421 <https://github.com/llvm/llvm-project/issues/105421>`__",""
diff --git a/libcxx/include/__atomic/atomic.h b/libcxx/include/__atomic/atomic.h
index ea1d28fbf850a..f1c7492590d61 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -178,6 +178,20 @@ struct __atomic_base<_Tp, true> : public __atomic_base<_Tp, false> {
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
     return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
   }
+#if _LIBCPP_STD_VER >= 26
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
+    return std::__cxx_atomic_fetch_max(std::addressof(this->__a_), __op, __m);
+  }
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
+    return std::__cxx_atomic_fetch_max(std::addressof(this->__a_), __op, __m);
+  }
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
+    return std::__cxx_atomic_fetch_min(std::addressof(this->__a_), __op, __m);
+  }
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
+    return std::__cxx_atomic_fetch_min(std::addressof(this->__a_), __op, __m);
+  }
+#endif
 
   _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) volatile _NOEXCEPT { return fetch_add(_Tp(1)); }
   _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) _NOEXCEPT { return fetch_add(_Tp(1)); }
@@ -799,6 +813,60 @@ atomic_fetch_xor_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __o
   return __o->fetch_xor(__op, __m);
 }
 
+#if _LIBCPP_STD_VER >= 26
+// atomic_fetch_max
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_max(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+  return __o->fetch_max(__op);
+}
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_max(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+  return __o->fetch_max(__op);
+}
+
+// atomic_fetch_max_explicit
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_max_explicit(
+    volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+  return __o->fetch_max(__op, __m);
+}
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp
+atomic_fetch_max_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+  return __o->fetch_max(__op, __m);
+}
+
+// atomic_fetch_min
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_min(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+  return __o->fetch_min(__op);
+}
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_min(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+  return __o->fetch_min(__op);
+}
+
+// atomic_fetch_min_explicit
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp atomic_fetch_min_explicit(
+    volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+  return __o->fetch_min(__op, __m);
+}
+
+template <class _Tp, __enable_if_t<is_integral<_Tp>::value && !is_same<_Tp, bool>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _Tp
+atomic_fetch_min_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+  return __o->fetch_min(__op, __m);
+}
+#endif
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___ATOMIC_ATOMIC_H
diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 69edbfe6ecadd..cedd4248eb200 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -299,6 +299,14 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
     return __atomic_fetch_xor(this->__ptr_, __arg, std::__to_gcc_order(__order));
   }
+#  if _LIBCPP_STD_VER >= 26
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+    return __atomic_fetch_max(this->__ptr_, __arg, std::__to_gcc_order(__order));
+  }
+  _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+    return __atomic_fetch_min(this->__ptr_, __arg, std::__to_gcc_order(__order));
+  }
+#  endif
 
   _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) const noexcept { return fetch_add(_Tp(1)); }
   _LIBCPP_HIDE_FROM_ABI _Tp operator--(int) const noexcept { return fetch_sub(_Tp(1)); }
diff --git a/libcxx/include/__atomic/support.h b/libcxx/include/__atomic/support.h
index 99d0f6aa543ca..eda38705acd32 100644
--- a/libcxx/include/__atomic/support.h
+++ b/libcxx/include/__atomic/support.h
@@ -99,6 +99,15 @@
 // template <class _Tp>
 // _Tp __cxx_atomic_fetch_xor(_Atmc<_Tp>* __a, _Tp __pattern, memory_order __order) noexcept;
 //
+// template <class _Tp>
+// _Tp __cxx_atomic_fetch_max(_Atmc<_Tp> volatile* __a, _Tp __pattern, memory_order __order) noexcept;
+// template <class _Tp>
+// _Tp __cxx_atomic_fetch_max(_Atmc<_Tp>* __a, _Tp __pattern, memory_order __order) noexcept;
+// template <class _Tp>
+// _Tp __cxx_atomic_fetch_min(_Atmc<_Tp> volatile* __a, _Tp __pattern, memory_order __order) noexcept;
+// template <class _Tp>
+// _Tp __cxx_atomic_fetch_min(_Atmc<_Tp>* __a, _Tp __pattern, memory_order __order) noexcept;
+//
 // clang-format on
 //
 
diff --git a/libcxx/include/__atomic/support/c11.h b/libcxx/include/__atomic/support/c11.h
index 1ad299882a12a..f28b5e461c18a 100644
--- a/libcxx/include/__atomic/support/c11.h
+++ b/libcxx/include/__atomic/support/c11.h
@@ -259,6 +259,32 @@ __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
       std::addressof(__a->__a_value), __pattern, static_cast<__memory_order_underlying_t>(__order));
 }
 
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
+  return __c11_atomic_fetch_max(
+      std::addressof(__a->__a_value), __pattern, static_cast<__memory_order_underlying_t>(__order));
+}
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
+  return __c11_atomic_fetch_max(
+      std::addressof(__a->__a_value), __pattern, static_cast<__memory_order_underlying_t>(__order));
+}
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
+  return __c11_atomic_fetch_min(
+      std::addressof(__a->__a_value), __pattern, static_cast<__memory_order_underlying_t>(__order));
+}
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
+  return __c11_atomic_fetch_min(
+      std::addressof(__a->__a_value), __pattern, static_cast<__memory_order_underlying_t>(__order));
+}
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___ATOMIC_SUPPORT_C11_H
diff --git a/libcxx/include/__atomic/support/gcc.h b/libcxx/include/__atomic/support/gcc.h
index 73c1b1c8070a4..dfaebb68ced74 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -258,6 +258,30 @@ __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
   return __atomic_fetch_xor(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
 }
 
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
+  return __atomic_fetch_max(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
+  return __atomic_fetch_max(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
+  return __atomic_fetch_min(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
+  return __atomic_fetch_min(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+}
+
 #define __cxx_atomic_is_lock_free(__s) __atomic_is_lock_free(__s, 0)
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/atomic b/libcxx/include/atomic
index 23a3db5d35029..4cf93b6bfbd85 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -164,6 +164,10 @@ struct atomic<integral>
     integral fetch_or(integral op, memory_order m = memory_order_seq_cst) noexcept;
     integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
     integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) noexcept;
+    integral fetch_max(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
+    integral fetch_max(integral op, memory_order m = memory_order_seq_cst) noexcept;
+    integral fetch_min(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
+    integral fetch_min(integral op, memory_order m = memory_order_seq_cst) noexcept;
 
     integral operator++(int) volatile noexcept;
     integral operator++(int) noexcept;
@@ -441,6 +445,26 @@ template<class T>
 template<class T>
   T atomic_fetch_xor_explicit(atomic<T>*, atomic<T>::value_type,
                               memory_order) noexcept;
+template<class T>
+  T atomic_fetch_max(volatile atomic<T>*, atomic<T>::value_type) noexcept;
+template<class T>
+  T atomic_fetch_max(atomic<T>*, atomic<T>::value_type) noexcept;
+template<class T>
+  T atomic_fetch_max_explicit(volatile atomic<T>*, atomic<T>::value_type,
+                              memory_order) noexcept;
+template<class T>
+  T atomic_fetch_max_explicit(atomic<T>*, atomic<T>::value_type,
+                              memory_order) noexcept;
+template<class T>
+  T atomic_fetch_min(volatile atomic<T>*, atomic<T>::value_type) noexcept;
+template<class T>
+  T atomic_fetch_min(atomic<T>*, atomic<T>::value_type) noexcept;
+template<class T>
+  T atomic_fetch_min_explicit(volatile atomic<T>*, atomic<T>::value_type,
+                              memory_order) noexcept;
+template<class T>
+  T atomic_fetch_min_explicit(atomic<T>*, atomic<T>::value_type,
+                              memory_order) noexcept;
 
 template<class T>
   void atomic_wait(const volatile atomic<T>*, atomic<T>::value_type) noexcept; // since C++20
diff --git a/libcxx/include/version b/libcxx/include/version
index 7d77677a012ce..a00e9441202bd 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -546,7 +546,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 #if _LIBCPP_STD_VER >= 26
 # define __cpp_lib_aligned_accessor                     202411L
 // # define __cpp_lib_associative_heterogeneous_insertion  202306L
-// # define __cpp_lib_atomic_min_max                       202403L
+# define __cpp_lib_atomic_min_max                       202403L
 # undef  __cpp_lib_atomic_ref
 # define __cpp_lib_atomic_ref                           202411L
 # undef  __cpp_lib_bind_front
diff --git a/libcxx/modules/std/atomic.inc b/libcxx/modules/std/atomic.inc
index 9a30fb7affe71..11f77991068c1 100644
--- a/libcxx/modules/std/atomic.inc
+++ b/libcxx/modules/std/atomic.inc
@@ -50,6 +50,10 @@ export namespace std {
   using std::atomic_fetch_sub_explicit _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_xor _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_xor_explicit _LIBCPP_USING_IF_EXISTS;
+  using std::atomic_fetch_max _LIBCPP_USING_IF_EXISTS;
+  using std::atomic_fetch_max_explicit _LIBCPP_USING_IF_EXISTS;
+  using std::atomic_fetch_min _LIBCPP_USING_IF_EXISTS;
+  using std::atomic_fetch_min_explicit _LIBCPP_USING_IF_EXISTS;
   using std::atomic_notify_all _LIBCPP_USING_IF_EXISTS;
   using std::atomic_notify_one _LIBCPP_USING_IF_EXISTS;
   using std::atomic_wait _LIBCPP_USING_IF_EXISTS;
diff --git a/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp b/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
new file mode 100644
index 0000000000000..6c1b69dce1595
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// integral-type fetch_max(integral-type, memory_order = memory_order::seq_cst) const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "atomic_helpers.h"
+#include "test_macros.h"
+
+template <typename T>
+concept has_fetch_max = requires {
+  std::declval<T const>().fetch_max(std::declval<T>());
+  std::declval<T const>().fetch_max(std::declval<T>(), std::declval<std::memory_order>());
+};
+
+template <typename T>
+struct TestDoesNotHaveFetchMax {
+  void operator()() const { static_assert(!has_fetch_max<std::atomic_ref<T>>); }
+};
+
+template <typename T>
+struct TestFetchMax {
+  void operator()() const {
+    static_assert(std::is_integral_v<T>);
+
+    alignas(std::atomic_ref<T>::required_alignment) T x(T(1));
+    std::atomic_ref<T> const a(x);
+
+    {
+      std::same_as<T> decltype(auto) y = a.fetch_max(T(2));
+      assert(y == T(1));
+      assert(x == T(2));
+      ASSERT_NOEXCEPT(a.fetch_max(T(0)));
+    }
+
+    {
+      std::same_as<T> decltype(auto) y = a.fetch_max(T(1), std::memory_order_relaxed);
+      assert(y == T(2));
+      assert(x == T(2));
+      ASSERT_NOEXCEPT(a.fetch_max(T(0), std::memory_order_relaxed));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFetchMax>()();
+
+  TestEachFloatingPointType<TestDoesNotHaveFetchMax>()();
+
+  TestEachPointerType<TestDoesNotHaveFetchMax>()();
+
+  TestDoesNotHaveFetchMax<bool>()();
+  TestDoesNotHaveFetchMax<UserAtomicType>()();
+  TestDoesNotHaveFetchMax<LargeUserAtomicType>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp b/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
new file mode 100644
index 0000000000000..1fa2d32c4d7d8
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// integral-type fetch_min(integral-type, memory_order = memory_order::seq_cst) const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "atomic_helpers.h"
+#include "test_macros.h"
+
+template <typename T>
+concept has_fetch_min = requires {
+  std::declval<T const>().fetch_min(std::declval<T>());
+  std::declval<T const>().fetch_min(std::declval<T>(), std::declval<std::memory_order>());
+};
+
+template <typename T>
+struct TestDoesNotHaveFetchMin {
+  void operator()() const { static_assert(!has_fetch_min<std::atomic_ref<T>>); }
+};
+
+template <typename T>
+struct TestFetchMin {
+  void operator()() const {
+    static_assert(std::is_integral_v<T>);
+
+    alignas(std::atomic_ref<T>::required_alignment) T x(T(3));
+    std::atomic_ref<T> const a(x);
+
+    {
+      std::same_as<T> decltype(auto) y = a.fetch_min(T(2));
+      assert(y == T(3));
+      assert(x == T(2));
+      ASSERT_NOEXCEPT(a.fetch_min(T(0)));
+    }
+
+    {
+      std::same_as<T> decltype(auto) y = a.fetch_min(T(1), std::memory_order_relaxed);
+      assert(y == T(2));
+      assert(x == T(1));
+      ASSERT_NOEXCEPT(a.fetch_min(T(0), std::memory_order_relaxed));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFetchMin>()();
+
+  TestEachFloatingPointType<TestDoesNotHaveFetchMin>()();
+
+  TestEachPointerType<TestDoesNotHaveFetchMin>()();
+
+  TestDoesNotHaveFetchMin<bool>()();
+  TestDoesNotHaveFetchMin<UserAtomicType>()();
+  TestDoesNotHaveFetchMin<LargeUserAtomicType>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max.pass.cpp
new file mode 100644
index 0000000000000..25411767ca8ad
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max.pass.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// <atomic>
+
+// template<class T>
+//     T
+//     atomic_fetch_max(volatile atomic<T>*, atomic<T>::value_type) noexcept;
+//
+// template<class T>
+//     T
+//     atomic_fetch_max(atomic<T>*, atomic<T>::value_type) noexcept;
+
+#include <atomic>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+
+template <class T>
+struct TestFn {
+  void operator()() const {
+    {
+      typedef std::atomic<T> A;
+      A t(T(1));
+      assert(std::atomic_fetch_max(&t, T(2)) == T(1));
+      assert(t == T(2));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_max(&t, T(2)));
+    }
+    {
+      typedef std::atomic<T> A;
+      volatile A t(T(3));
+      assert(std::atomic_fetch_max(&t, T(2)) == T(3));
+      assert(t == T(3));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_max(&t, T(2)));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFn>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_explicit.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_explicit.pass.cpp
new file mode 100644
index 0000000000000..a670f1c10ce60
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_explicit.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// <atomic>
+
+// template<class T>
+//     T
+//     atomic_fetch_max_explicit(volatile atomic<T>*, atomic<T>::value_type,
+//                               memory_order) noexcept;
+//
+// template<class T>
+//     T
+//     atomic_fetch_max_explicit(atomic<T>*, atomic<T>::value_type,
+//                               memory_order) noexcept;
+
+#include <atomic>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+
+template <class T>
+struct TestFn {
+  void operator()() const {
+    {
+      typedef std::atomic<T> A;
+      A t(T(1));
+      assert(std::atomic_fetch_max_explicit(&t, T(2), std::memory_order_seq_cst) == T(1));
+      assert(t == T(2));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&t, T(2), std::memory_order_seq_cst));
+    }
+    {
+      typedef std::atomic<T> A;
+      volatile A t(T(3));
+      assert(std::atomic_fetch_max_explicit(&t, T(2), std::memory_order_seq_cst) == T(3));
+      assert(t == T(3));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&t, T(2), std::memory_order_seq_cst));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFn>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min.pass.cpp
new file mode 100644
index 0000000000000..242355120c28d
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min.pass.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// <atomic>
+
+// template<class T>
+//     T
+//     atomic_fetch_min(volatile atomic<T>*, atomic<T>::value_type) noexcept;
+//
+// template<class T>
+//     T
+//     atomic_fetch_min(atomic<T>*, atomic<T>::value_type) noexcept;
+
+#include <atomic>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+
+template <class T>
+struct TestFn {
+  void operator()() const {
+    {
+      typedef std::atomic<T> A;
+      A t(T(1));
+      assert(std::atomic_fetch_min(&t, T(2)) == T(1));
+      assert(t == T(1));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_min(&t, T(2)));
+    }
+    {
+      typedef std::atomic<T> A;
+      volatile A t(T(3));
+      assert(std::atomic_fetch_min(&t, T(2)) == T(3));
+      assert(t == T(2));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_min(&t, T(2)));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFn>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_explicit.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_explicit.pass.cpp
new file mode 100644
index 0000000000000..46b7531b1f727
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_explicit.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// <atomic>
+
+// template<class T>
+//     T
+//     atomic_fetch_min_explicit(volatile atomic<T>*, atomic<T>::value_type,
+//                               memory_order) noexcept;
+//
+// template<class T>
+//     T
+//     atomic_fetch_min_explicit(atomic<T>*, atomic<T>::value_type,
+//                               memory_order) noexcept;
+
+#include <atomic>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+
+template <class T>
+struct TestFn {
+  void operator()() const {
+    {
+      typedef std::atomic<T> A;
+      A t(T(1));
+      assert(std::atomic_fetch_min_explicit(&t, T(2), std::memory_order_seq_cst) == T(1));
+      assert(t == T(1));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&t, T(2), std::memory_order_seq_cst));
+    }
+    {
+      typedef std::atomic<T> A;
+      volatile A t(T(3));
+      assert(std::atomic_fetch_min_explicit(&t, T(2), std::memory_order_seq_cst) == T(3));
+      assert(t == T(2));
+
+      ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&t, T(2), std::memory_order_seq_cst));
+    }
+  }
+};
+
+int main(int, char**) {
+  TestEachIntegralType<TestFn>()();
+
+  return 0;
+}
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/atomic.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/atomic.version.compile.pass.cpp
index 8bd027445c85b..9e620317c35c8 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/atomic.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/atomic.version.compile.pass.cpp
@@ -339,17 +339,11 @@
 #    error "__cpp_lib_atomic_lock_free_type_aliases should have the value 201907L in c++26"
 #  endif
 
-#  if !defined(_LIBCPP_VERSION)
-#    ifndef __cpp_lib_atomic_min_max
-#      error "__cpp_lib_atomic_min_max should be defined in c++26"
-#    endif
-#    if __cpp_lib_atomic_min_max != 202403L
-#      error "__cpp_lib_atomic_min_max should have the value 202403L in c++26"
-#    endif
-#  else
-#    ifdef __cpp_lib_atomic_min_max
-#      error "__cpp_lib_atomic_min_max should not be defined because it is unimplemented in libc++!"
-#    endif
+#  ifndef __cpp_lib_atomic_min_max
+#    error "__cpp_lib_atomic_min_max should be defined in c++26"
+#  endif
+#  if __cpp_lib_atomic_min_max != 202403L
+#    error "__cpp_lib_atomic_min_max should have the value 202403L in c++26"
 #  endif
 
 #  ifndef __cpp_lib_atomic_ref
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index f442b22c6d752..33f402b6422f6 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -6325,17 +6325,11 @@
 #    error "__cpp_lib_atomic_lock_free_type_aliases should have the value 201907L in c++26"
 #  endif
 
-#  if !defined(_LIBCPP_VERSION)
-#    ifndef __cpp_lib_atomic_min_max
-#      error "__cpp_lib_atomic_min_max should be defined in c++26"
-#    endif
-#    if __cpp_lib_atomic_min_max != 202403L
-#      error "__cpp_lib_atomic_min_max should have the value 202403L in c++26"
-#    endif
-#  else
-#    ifdef __cpp_lib_atomic_min_max
-#      error "__cpp_lib_atomic_min_max should not be defined because it is unimplemented in libc++!"
-#    endif
+#  ifndef __cpp_lib_atomic_min_max
+#    error "__cpp_lib_atomic_min_max should be defined in c++26"
+#  endif
+#  if __cpp_lib_atomic_min_max != 202403L
+#    error "__cpp_lib_atomic_min_max should have the value 202403L in c++26"
 #  endif
 
 #  ifndef __cpp_lib_atomic_ref
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 88431c76a00f5..799bb088f061e 100644
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -191,7 +191,6 @@ def add_version_header(tc):
             "name": "__cpp_lib_atomic_min_max",
             "values": {"c++26": 202403}, # P0493R5: Atomic minimum/maximum
             "headers": ["atomic"],
-            "unimplemented": True,
         },
         {
             "name": "__cpp_lib_atomic_ref",

>From fb39fd535b1714f6d422d9b0f1810102a763a1b3 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 19:20:50 +0800
Subject: [PATCH 2/8] fix gcc build

---
 libcxx/include/__atomic/support/gcc.h | 28 +++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__atomic/support/gcc.h b/libcxx/include/__atomic/support/gcc.h
index dfaebb68ced74..e50143bb2bfa9 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -261,25 +261,45 @@ __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  return __atomic_fetch_max(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __value;
+  do {
+    __value = __ret > __pattern ? __ret : __pattern;
+  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  return __ret;
 }
 
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  return __atomic_fetch_max(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __value;
+  do {
+    __value = __ret > __pattern ? __ret : __pattern;
+  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  return __ret;
 }
 
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  return __atomic_fetch_min(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __value;
+  do {
+    __value = __ret < __pattern ? __ret : __pattern;
+  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  return __ret;
 }
 
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  return __atomic_fetch_min(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __value;
+  do {
+    __value = __ret < __pattern ? __ret : __pattern;
+  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  return __ret;
 }
 
 #define __cxx_atomic_is_lock_free(__s) __atomic_is_lock_free(__s, 0)

>From 906a95e15e4bdd1136be7bfc1f14731e6a3a3135 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 19:36:17 +0800
Subject: [PATCH 3/8] fix gcc atomic_ref

---
 libcxx/include/__atomic/atomic_ref.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index cedd4248eb200..6ea270dc3b902 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -23,6 +23,7 @@
 #include <__atomic/check_memory_order.h>
 #include <__atomic/floating_point_helper.h>
 #include <__atomic/memory_order.h>
+#include <__atomic/support.h>
 #include <__atomic/to_gcc_order.h>
 #include <__concepts/arithmetic.h>
 #include <__concepts/same_as.h>
@@ -301,10 +302,10 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
   }
 #  if _LIBCPP_STD_VER >= 26
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
-    return __atomic_fetch_max(this->__ptr_, __arg, std::__to_gcc_order(__order));
+    return std::__cxx_atomic_fetch_max(this->__ptr_, __arg, __m);
   }
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
-    return __atomic_fetch_min(this->__ptr_, __arg, std::__to_gcc_order(__order));
+    return std::__cxx_atomic_fetch_min(this->__ptr_, __arg, __m);
   }
 #  endif
 

>From a64208d9c117ccbd5a4cc5677a662b6e4f972f59 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 19:55:43 +0800
Subject: [PATCH 4/8] fix gcc

---
 libcxx/include/__atomic/atomic_ref.h  | 14 ++++++++++++--
 libcxx/include/__atomic/support/gcc.h |  8 ++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 6ea270dc3b902..11d74f71e6de1 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -302,10 +302,20 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
   }
 #  if _LIBCPP_STD_VER >= 26
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
-    return std::__cxx_atomic_fetch_max(this->__ptr_, __arg, __m);
+    _Tp __old = this->load(memory_order_relaxed);
+    _Tp __new;
+    do {
+      __new = __old > __arg ? __old : __arg;
+    } while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed));
+    return __old;
   }
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
-    return std::__cxx_atomic_fetch_min(this->__ptr_, __arg, __m);
+    _Tp __old = this->load(memory_order_relaxed);
+    _Tp __new;
+    do {
+      __new = __old < __arg ? __old : __arg;
+    } while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed));
+    return __old;
   }
 #  endif
 
diff --git a/libcxx/include/__atomic/support/gcc.h b/libcxx/include/__atomic/support/gcc.h
index e50143bb2bfa9..c224bb6c95391 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -261,7 +261,7 @@ __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __ret = __cxx_atomic_load(__a, memory_order_relaxed);
   _Tp __value;
   do {
     __value = __ret > __pattern ? __ret : __pattern;
@@ -272,7 +272,7 @@ __cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __ret = __cxx_atomic_load(__a, memory_order_relaxed);
   _Tp __value;
   do {
     __value = __ret > __pattern ? __ret : __pattern;
@@ -283,7 +283,7 @@ __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __ret = __cxx_atomic_load(__a, memory_order_relaxed);
   _Tp __value;
   do {
     __value = __ret < __pattern ? __ret : __pattern;
@@ -294,7 +294,7 @@ __cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
 template <typename _Tp>
 _LIBCPP_HIDE_FROM_ABI _Tp
 __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_order __order) {
-  _Tp __ret = __cxx_atomic_load(__a, __order);
+  _Tp __ret = __cxx_atomic_load(__a, memory_order_relaxed);
   _Tp __value;
   do {
     __value = __ret < __pattern ? __ret : __pattern;

>From 5eeeeea250032dd5ae65ec4a1868158cefe3c650 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 20:12:55 +0800
Subject: [PATCH 5/8] fix gcc

---
 libcxx/include/__atomic/support/gcc.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__atomic/support/gcc.h b/libcxx/include/__atomic/support/gcc.h
index c224bb6c95391..3eaccf95e19d8 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -265,7 +265,7 @@ __cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
   _Tp __value;
   do {
     __value = __ret > __pattern ? __ret : __pattern;
-  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
   return __ret;
 }
 
@@ -276,7 +276,7 @@ __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
   _Tp __value;
   do {
     __value = __ret > __pattern ? __ret : __pattern;
-  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
   return __ret;
 }
 
@@ -287,7 +287,7 @@ __cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
   _Tp __value;
   do {
     __value = __ret < __pattern ? __ret : __pattern;
-  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
   return __ret;
 }
 
@@ -298,7 +298,7 @@ __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern, memory_o
   _Tp __value;
   do {
     __value = __ret < __pattern ? __ret : __pattern;
-  } while (__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
+  } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
   return __ret;
 }
 

>From 46257efd512cc8bd741cc9dd3157b59ab1ce1ca1 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 20:39:31 +0800
Subject: [PATCH 6/8] remove unused include

---
 libcxx/include/__atomic/atomic_ref.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 11d74f71e6de1..84b9bf211b4b9 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -23,7 +23,6 @@
 #include <__atomic/check_memory_order.h>
 #include <__atomic/floating_point_helper.h>
 #include <__atomic/memory_order.h>
-#include <__atomic/support.h>
 #include <__atomic/to_gcc_order.h>
 #include <__concepts/arithmetic.h>
 #include <__concepts/same_as.h>

>From dc1cb331ddcb980cce4a8deeef8d1c21b08b0eb7 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 7 Feb 2026 21:29:39 +0800
Subject: [PATCH 7/8] guard modules

---
 libcxx/modules/std/atomic.inc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/modules/std/atomic.inc b/libcxx/modules/std/atomic.inc
index 11f77991068c1..38321e1a1bb64 100644
--- a/libcxx/modules/std/atomic.inc
+++ b/libcxx/modules/std/atomic.inc
@@ -50,10 +50,12 @@ export namespace std {
   using std::atomic_fetch_sub_explicit _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_xor _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_xor_explicit _LIBCPP_USING_IF_EXISTS;
+#if _LIBCPP_STD_VER >= 26
   using std::atomic_fetch_max _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_max_explicit _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_min _LIBCPP_USING_IF_EXISTS;
   using std::atomic_fetch_min_explicit _LIBCPP_USING_IF_EXISTS;
+#endif
   using std::atomic_notify_all _LIBCPP_USING_IF_EXISTS;
   using std::atomic_notify_one _LIBCPP_USING_IF_EXISTS;
   using std::atomic_wait _LIBCPP_USING_IF_EXISTS;

>From dfce3c7d45c1dca9e1ac85b293321ef19e007b23 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 8 Feb 2026 19:37:33 +0800
Subject: [PATCH 8/8] address review comments: use builtin if possible

---
 libcxx/include/__atomic/atomic_ref.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 84b9bf211b4b9..88c7d7dbc5dfb 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -301,20 +301,28 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
   }
 #  if _LIBCPP_STD_VER >= 26
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_max(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+#    if __has_builtin(__atomic_fetch_max)
+    return __atomic_fetch_max(this->__ptr_, __arg, std::__to_gcc_order(__order));
+#    else
     _Tp __old = this->load(memory_order_relaxed);
     _Tp __new;
     do {
       __new = __old > __arg ? __old : __arg;
     } while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed));
     return __old;
+#    endif
   }
   _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+#    if __has_builtin(__atomic_fetch_min)
+    return __atomic_fetch_min(this->__ptr_, __arg, std::__to_gcc_order(__order));
+#    else
     _Tp __old = this->load(memory_order_relaxed);
     _Tp __new;
     do {
       __new = __old < __arg ? __old : __arg;
     } while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed));
     return __old;
+#    endif
   }
 #  endif
 



More information about the libcxx-commits mailing list