[libcxx-commits] [libcxx] [libcxx] atomic min/max (PR #186694)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Mar 23 07:08:01 PDT 2026
https://github.com/gonzalobg updated https://github.com/llvm/llvm-project/pull/186694
>From 48727eb88375128358288ef5f8a19340596ed2d5 Mon Sep 17 00:00:00 2001
From: Gonzalo Brito Gadeschi <gonzalob at nvidia.com>
Date: Sun, 15 Mar 2026 11:35:12 -0700
Subject: [PATCH 1/3] [libcxx] atomic min/max
---
libcxx/docs/FeatureTestMacroTable.rst | 2 +-
libcxx/docs/ReleaseNotes/23.rst | 1 +
libcxx/docs/Status/Cxx2cPapers.csv | 2 +-
libcxx/include/__atomic/atomic.h | 95 +++++++++++
libcxx/include/__atomic/atomic_ref.h | 39 +++++
libcxx/include/__atomic/support/c11.h | 44 ++++++
libcxx/include/__atomic/support/gcc.h | 44 ++++++
libcxx/include/version | 2 +-
libcxx/modules/std/atomic.inc | 6 +
.../atomics/atomics.ref/fetch_max.pass.cpp | 78 ++++++++++
.../atomics/atomics.ref/fetch_min.pass.cpp | 78 ++++++++++
.../atomic_fetch_max.pass.cpp | 85 ++++++++++
.../atomic_fetch_max_explicit.pass.cpp | 87 +++++++++++
.../atomic_fetch_max_helper.h | 147 ++++++++++++++++++
.../atomic_fetch_min.pass.cpp | 85 ++++++++++
.../atomic_fetch_min_explicit.pass.cpp | 87 +++++++++++
.../atomic_fetch_min_helper.h | 147 ++++++++++++++++++
.../atomic.version.compile.pass.cpp | 16 +-
.../version.version.compile.pass.cpp | 16 +-
.../generate_feature_test_macro_components.py | 1 -
20 files changed, 1036 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_max_helper.h
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
create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h
diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 0f65770a4fa14..90e7c014ca899 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -418,7 +418,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 4441a8aed198c..f312576ce23c7 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -39,6 +39,7 @@ Implemented Papers
------------------
- P2440R1: ``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right`` (`Github <https://llvm.org/PR105184>`__)
+- P0493R5: Atomic minimum/maximum (`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 3d0ed9cc47396..58324ffc44ef1 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -179,6 +179,21 @@ struct __atomic_base<_Tp, true> : public __atomic_base<_Tp, false> {
return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
}
+#if _LIBCPP_STD_VER >= 26
+ _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);
+ }
+ _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);
+ }
+#endif // _LIBCPP_STD_VER >= 26
+
_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)); }
_LIBCPP_HIDE_FROM_ABI _Tp operator--(int) volatile _NOEXCEPT { return fetch_sub(_Tp(1)); }
@@ -306,6 +321,26 @@ struct atomic<_Tp*> : public __atomic_base<_Tp*> {
return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m);
}
+#if _LIBCPP_STD_VER >= 26
+ _LIBCPP_HIDE_FROM_ABI _Tp* fetch_min(_Tp* __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
+ static_assert(!is_function<__remove_pointer_t<_Tp> >::value, "Pointer to function isn't allowed");
+ 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 {
+ static_assert(!is_function<__remove_pointer_t<_Tp> >::value, "Pointer to function isn't allowed");
+ return std::__cxx_atomic_fetch_min(std::addressof(this->__a_), __op, __m);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI _Tp* fetch_max(_Tp* __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
+ static_assert(!is_function<__remove_pointer_t<_Tp> >::value, "Pointer to function isn't allowed");
+ 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 {
+ static_assert(!is_function<__remove_pointer_t<_Tp> >::value, "Pointer to function isn't allowed");
+ return std::__cxx_atomic_fetch_max(std::addressof(this->__a_), __op, __m);
+ }
+#endif // _LIBCPP_STD_VER >= 26
+
_LIBCPP_HIDE_FROM_ABI _Tp* operator++(int) volatile _NOEXCEPT { return fetch_add(1); }
_LIBCPP_HIDE_FROM_ABI _Tp* operator++(int) _NOEXCEPT { return fetch_add(1); }
_LIBCPP_HIDE_FROM_ABI _Tp* operator--(int) volatile _NOEXCEPT { return fetch_sub(1); }
@@ -799,6 +834,66 @@ 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_min
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+atomic_fetch_min(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+ return __o->fetch_min(__op);
+}
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+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>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type 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>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+atomic_fetch_min_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+ return __o->fetch_min(__op, __m);
+}
+
+// atomic_fetch_max
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+atomic_fetch_max(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT {
+ return __o->fetch_max(__op);
+}
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+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>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type 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>
+_LIBCPP_HIDE_FROM_ABI typename atomic<_Tp>::value_type
+atomic_fetch_max_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT {
+ return __o->fetch_max(__op, __m);
+}
+
+#endif // _LIBCPP_STD_VER >= 26
+
_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..984c8594f90c8 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -1,3 +1,4 @@
+
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
@@ -300,6 +301,25 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
return __atomic_fetch_xor(this->__ptr_, __arg, std::__to_gcc_order(__order));
}
+# if _LIBCPP_STD_VER >= 26
+ _LIBCPP_HIDE_FROM_ABI _Tp fetch_min(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+ _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_max(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+ _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_STD_VER >= 26
+
_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)); }
_LIBCPP_HIDE_FROM_ABI _Tp operator++() const noexcept { return fetch_add(_Tp(1)) + _Tp(1); }
@@ -378,6 +398,25 @@ struct atomic_ref<_Tp*> : public __atomic_ref_base<_Tp*> {
return __atomic_fetch_sub(this->__ptr_, __arg * sizeof(_Tp), std::__to_gcc_order(__order));
}
+# if _LIBCPP_STD_VER >= 26
+ _LIBCPP_HIDE_FROM_ABI _Tp* fetch_min(_Tp* __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+ _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_max(_Tp* __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+ _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_STD_VER >= 26
+
_LIBCPP_HIDE_FROM_ABI _Tp* operator++(int) const noexcept { return fetch_add(1); }
_LIBCPP_HIDE_FROM_ABI _Tp* operator--(int) const noexcept { return fetch_sub(1); }
_LIBCPP_HIDE_FROM_ABI _Tp* operator++() const noexcept { return fetch_add(1) + 1; }
diff --git a/libcxx/include/__atomic/support/c11.h b/libcxx/include/__atomic/support/c11.h
index 1ad299882a12a..5c744617b1281 100644
--- a/libcxx/include/__atomic/support/c11.h
+++ b/libcxx/include/__atomic/support/c11.h
@@ -13,7 +13,9 @@
#include <__config>
#include <__cstddef/ptrdiff_t.h>
#include <__memory/addressof.h>
+#include <__type_traits/is_pointer.h>
#include <__type_traits/remove_const.h>
+#include <cstdint>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -259,6 +261,48 @@ __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_min(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old < __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old < __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old > __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old > __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
_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..16f56e5ee86c6 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -258,6 +258,50 @@ __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_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old < __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old < __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old > __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
+template <typename _Tp>
+_LIBCPP_HIDE_FROM_ABI _Tp
+__cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order __order) _NOEXCEPT {
+ _Tp __old = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __new;
+ do {
+ __new = __old > __val ? __old : __val;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ return __old;
+}
+
#define __cxx_atomic_is_lock_free(__s) __atomic_is_lock_free(__s, 0)
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/version b/libcxx/include/version
index 562538c5393cc..027250a28b0b2 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -549,7 +549,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..a22f2e7b81837 100644
--- a/libcxx/modules/std/atomic.inc
+++ b/libcxx/modules/std/atomic.inc
@@ -50,6 +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_min _LIBCPP_USING_IF_EXISTS;
+ using std::atomic_fetch_min_explicit _LIBCPP_USING_IF_EXISTS;
+ using std::atomic_fetch_max _LIBCPP_USING_IF_EXISTS;
+ using std::atomic_fetch_max_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;
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..1ce4d5601e376
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// integral-type fetch_max(integral-type, memory_order = memory_order::seq_cst) const noexcept;
+// T* fetch_max(T*, memory_order = memory_order::seq_cst) const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "../atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_helper.h"
+
+template <typename T>
+concept has_fetch_max = requires(T t) {
+ std::declval<T const>().fetch_max(std::declval<typename T::value_type>());
+ std::declval<T const>().fetch_max(std::declval<typename T::value_type>(), 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 {
+ if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
+ alignas(std::atomic_ref<T>::required_alignment) T x;
+ std::atomic_ref<T> const a(x);
+
+ auto load = [&]() { return x; };
+ auto store = [&](T val) { x = val; };
+ auto max = [&](T val, auto order) { return a.fetch_max(val, order); };
+
+ ASSERT_NOEXCEPT(a.fetch_max(T(0), std::memory_order_seq_cst));
+ test_fetch_max_integral<T>(load, store, max);
+
+ } else if constexpr (std::is_pointer_v<T>) {
+ using U = std::remove_pointer_t<T>;
+ U t[9] = {};
+ alignas(std::atomic_ref<T>::required_alignment) T p;
+ std::atomic_ref<T> const a(p);
+
+ auto load = [&]() { return p; };
+ auto store = [&](T val) { p = val; };
+ auto max = [&](T val, auto order) { return a.fetch_max(val, order); };
+
+ ASSERT_NOEXCEPT(a.fetch_max(&t[0], std::memory_order_seq_cst));
+ test_fetch_max_pointer<U>(&t[0], &t[2], &t[4], load, store, max);
+
+ } else {
+ static_assert(std::is_void_v<T>);
+ }
+ }
+};
+
+int main(int, char**) {
+ TestEachIntegralType<TestFetchMax>()();
+ TestEachPointerType<TestFetchMax>()();
+
+ TestDoesNotHaveFetchMax<bool>{}();
+ TestDoesNotHaveFetchMax<float>{}();
+ TestDoesNotHaveFetchMax<double>{}();
+
+ 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..2bcbc31c99387
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// integral-type fetch_min(integral-type, memory_order = memory_order::seq_cst) const noexcept;
+// T* fetch_min(T*, memory_order = memory_order::seq_cst) const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "../atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h"
+
+template <typename T>
+concept has_fetch_min = requires(T t) {
+ std::declval<T const>().fetch_min(std::declval<typename T::value_type>());
+ std::declval<T const>().fetch_min(std::declval<typename T::value_type>(), 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 {
+ if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
+ alignas(std::atomic_ref<T>::required_alignment) T x;
+ std::atomic_ref<T> const a(x);
+
+ auto load = [&]() { return x; };
+ auto store = [&](T val) { x = val; };
+ auto min = [&](T val, auto order) { return a.fetch_min(val, order); };
+
+ ASSERT_NOEXCEPT(a.fetch_min(T(0), std::memory_order_seq_cst));
+ test_fetch_min_integral<T>(load, store, min);
+
+ } else if constexpr (std::is_pointer_v<T>) {
+ using U = std::remove_pointer_t<T>;
+ U t[9] = {};
+ alignas(std::atomic_ref<T>::required_alignment) T p;
+ std::atomic_ref<T> const a(p);
+
+ auto load = [&]() { return p; };
+ auto store = [&](T val) { p = val; };
+ auto min = [&](T val, auto order) { return a.fetch_min(val, order); };
+
+ ASSERT_NOEXCEPT(a.fetch_min(&t[0], std::memory_order_seq_cst));
+ test_fetch_min_pointer<U>(&t[0], &t[2], &t[4], load, store, min);
+
+ } else {
+ static_assert(std::is_void_v<T>);
+ }
+ }
+};
+
+int main(int, char**) {
+ TestEachIntegralType<TestFetchMin>()();
+ TestEachPointerType<TestFetchMin>()();
+
+ TestDoesNotHaveFetchMin<bool>{}();
+ TestDoesNotHaveFetchMin<float>{}();
+ TestDoesNotHaveFetchMin<double>{}();
+
+ 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..f497af73deb42
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// template<class T>
+// T atomic_fetch_max(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
+//
+// template<class T>
+// T atomic_fetch_max(atomic<T>*, typename atomic<T>::value_type) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "atomic_fetch_max_helper.h"
+
+template <class T>
+struct TestFn {
+ void operator()() const {
+ // Test non-volatile
+ {
+ std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto max = [&](T val, auto) { return std::atomic_fetch_max(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max(&a, T(0)));
+ test_fetch_max_integral<T>(load, store, max);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto max = [&](T val, auto) { return std::atomic_fetch_max(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max(&a, T(0)));
+ test_fetch_max_integral<T>(load, store, max);
+ }
+ }
+};
+
+template <class T>
+void test_pointer() {
+ T arr[5];
+ T* p0 = &arr[0];
+ T* p2 = &arr[2];
+ T* p4 = &arr[4];
+
+ // Test non-volatile
+ {
+ std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto max = [&](T* val, auto) { return std::atomic_fetch_max(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max(&a, p0));
+ test_fetch_max_pointer<T>(p0, p2, p4, load, store, max);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto max = [&](T* val, auto) { return std::atomic_fetch_max(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max(&a, p0));
+ test_fetch_max_pointer<T>(p0, p2, p4, load, store, max);
+ }
+}
+
+int main(int, char**) {
+ TestEachIntegralType<TestFn>()();
+ test_pointer<int>();
+ test_pointer<char>();
+
+ 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..66d1cac452737
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_explicit.pass.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// template<class T>
+// T atomic_fetch_max_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
+// memory_order) noexcept;
+//
+// template<class T>
+// T atomic_fetch_max_explicit(atomic<T>*, typename atomic<T>::value_type,
+// memory_order) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "atomic_fetch_max_helper.h"
+
+template <class T>
+struct TestFn {
+ void operator()() const {
+ // Test non-volatile
+ {
+ std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto max = [&](T val, auto order) { return std::atomic_fetch_max_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&a, T(0), std::memory_order_seq_cst));
+ test_fetch_max_integral<T>(load, store, max);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto max = [&](T val, auto order) { return std::atomic_fetch_max_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&a, T(0), std::memory_order_seq_cst));
+ test_fetch_max_integral<T>(load, store, max);
+ }
+ }
+};
+
+template <class T>
+void test_pointer() {
+ T arr[5];
+ T* p0 = &arr[0];
+ T* p2 = &arr[2];
+ T* p4 = &arr[4];
+
+ // Test non-volatile
+ {
+ std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto max = [&](T* val, auto order) { return std::atomic_fetch_max_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&a, p0, std::memory_order_seq_cst));
+ test_fetch_max_pointer<T>(p0, p2, p4, load, store, max);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto max = [&](T* val, auto order) { return std::atomic_fetch_max_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_max_explicit(&a, p0, std::memory_order_seq_cst));
+ test_fetch_max_pointer<T>(p0, p2, p4, load, store, max);
+ }
+}
+
+int main(int, char**) {
+ TestEachIntegralType<TestFn>()();
+ test_pointer<int>();
+ test_pointer<char>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_helper.h b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_helper.h
new file mode 100644
index 0000000000000..ff4bd7aa38912
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_helper.h
@@ -0,0 +1,147 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_STD_ATOMICS_ATOMIC_FETCH_MAX_HELPER_H
+#define TEST_STD_ATOMICS_ATOMIC_FETCH_MAX_HELPER_H
+
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+// Test fetch_max for integral types
+// LoadOp: () -> T - reads current value
+// StoreOp: (T) -> void - stores a value
+// MaxOp: (T, memory_order) -> T - performs fetch_max operation
+template <class T, class LoadOp, class StoreOp, class MaxOp>
+void test_fetch_max_integral(LoadOp load, StoreOp store, MaxOp max) {
+ // Test basic fetch_max (update to larger value)
+ {
+ store(T(10));
+ T old = max(T(20), std::memory_order_seq_cst);
+ assert(old == T(10));
+ assert(load() == T(20));
+ }
+
+ // Test with smaller value (no change)
+ {
+ store(T(10));
+ T old = max(T(5), std::memory_order_seq_cst);
+ assert(old == T(10));
+ assert(load() == T(10));
+ }
+
+ // Test different memory orderings
+ {
+ store(T(15));
+ T old = max(T(25), std::memory_order_seq_cst);
+ assert(old == T(15));
+ assert(load() == T(25));
+ }
+
+ {
+ store(T(10));
+ T old = max(T(25), std::memory_order_relaxed);
+ assert(old == T(10));
+ assert(load() == T(25));
+ }
+
+ {
+ store(T(10));
+ T old = max(T(30), std::memory_order_acquire);
+ assert(old == T(10));
+ assert(load() == T(30));
+ }
+
+ {
+ store(T(10));
+ T old = max(T(15), std::memory_order_release);
+ assert(old == T(10));
+ assert(load() == T(15));
+ }
+
+ {
+ store(T(10));
+ T old = max(T(22), std::memory_order_acq_rel);
+ assert(old == T(10));
+ assert(load() == T(22));
+ }
+
+ {
+ store(T(15));
+ T old = max(T(35), std::memory_order_relaxed);
+ assert(old == T(15));
+ assert(load() == T(35));
+ }
+
+ // Test return type
+ {
+ store(T(10));
+ static_assert(std::is_same_v<decltype(max(T(20), std::memory_order_seq_cst)), T>);
+ }
+}
+
+// Test fetch_max for pointer types
+// LoadOp: () -> T* - reads current pointer value
+// StoreOp: (T*) -> void - stores a pointer value
+// MaxOp: (T*, memory_order) -> T* - performs fetch_max operation
+template <class T, class LoadOp, class StoreOp, class MaxOp>
+void test_fetch_max_pointer(T* p0, T* p2, T* p4, LoadOp load, StoreOp store, MaxOp max) {
+ // Test basic fetch_max (update to larger pointer)
+ {
+ store(p2);
+ T* old = max(p4, std::memory_order_seq_cst);
+ assert(old == p2);
+ assert(load() == p4);
+ }
+
+ // Test with smaller pointer (no change)
+ {
+ store(p2);
+ T* old = max(p0, std::memory_order_seq_cst);
+ assert(old == p2);
+ assert(load() == p2);
+ }
+
+ // Test different memory orderings
+ {
+ store(p0);
+ T* old = max(p2, std::memory_order_seq_cst);
+ assert(old == p0);
+ assert(load() == p2);
+ }
+
+ {
+ store(p0);
+ T* old = max(p2, std::memory_order_relaxed);
+ assert(old == p0);
+ assert(load() == p2);
+ }
+
+ {
+ store(p0);
+ T* old = max(p4, std::memory_order_acquire);
+ assert(old == p0);
+ assert(load() == p4);
+ }
+
+ {
+ store(p0);
+ T* old = max(p4, std::memory_order_release);
+ assert(old == p0);
+ assert(load() == p4);
+ }
+
+ // Test return type
+ {
+ store(p2);
+ static_assert(std::is_same_v<decltype(max(p4, std::memory_order_seq_cst)), T*>);
+ }
+}
+
+#endif // TEST_STD_ATOMICS_ATOMIC_FETCH_MAX_HELPER_H
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..21b1cd5a017af
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// template<class T>
+// T atomic_fetch_min(volatile atomic<T>*, typename atomic<T>::value_type) noexcept;
+//
+// template<class T>
+// T atomic_fetch_min(atomic<T>*, typename atomic<T>::value_type) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "atomic_fetch_min_helper.h"
+
+template <class T>
+struct TestFn {
+ void operator()() const {
+ // Test non-volatile
+ {
+ std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto min = [&](T val, auto) { return std::atomic_fetch_min(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min(&a, T(0)));
+ test_fetch_min_integral<T>(load, store, min);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto min = [&](T val, auto) { return std::atomic_fetch_min(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min(&a, T(0)));
+ test_fetch_min_integral<T>(load, store, min);
+ }
+ }
+};
+
+template <class T>
+void test_pointer() {
+ T arr[5];
+ T* p0 = &arr[0];
+ T* p2 = &arr[2];
+ T* p4 = &arr[4];
+
+ // Test non-volatile
+ {
+ std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto min = [&](T* val, auto) { return std::atomic_fetch_min(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min(&a, p0));
+ test_fetch_min_pointer<T>(p0, p2, p4, load, store, min);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto min = [&](T* val, auto) { return std::atomic_fetch_min(&a, val); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min(&a, p0));
+ test_fetch_min_pointer<T>(p0, p2, p4, load, store, min);
+ }
+}
+
+int main(int, char**) {
+ TestEachIntegralType<TestFn>()();
+ test_pointer<int>();
+ test_pointer<char>();
+
+ 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..3236556334ee4
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_explicit.pass.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// XFAIL: !has-64-bit-atomics
+
+// template<class T>
+// T atomic_fetch_min_explicit(volatile atomic<T>*, typename atomic<T>::value_type,
+// memory_order) noexcept;
+//
+// template<class T>
+// T atomic_fetch_min_explicit(atomic<T>*, typename atomic<T>::value_type,
+// memory_order) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "atomic_helpers.h"
+#include "atomic_fetch_min_helper.h"
+
+template <class T>
+struct TestFn {
+ void operator()() const {
+ // Test non-volatile
+ {
+ std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto min = [&](T val, auto order) { return std::atomic_fetch_min_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&a, T(0), std::memory_order_seq_cst));
+ test_fetch_min_integral<T>(load, store, min);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T val) { a.store(val); };
+ auto min = [&](T val, auto order) { return std::atomic_fetch_min_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&a, T(0), std::memory_order_seq_cst));
+ test_fetch_min_integral<T>(load, store, min);
+ }
+ }
+};
+
+template <class T>
+void test_pointer() {
+ T arr[5];
+ T* p0 = &arr[0];
+ T* p2 = &arr[2];
+ T* p4 = &arr[4];
+
+ // Test non-volatile
+ {
+ std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto min = [&](T* val, auto order) { return std::atomic_fetch_min_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&a, p0, std::memory_order_seq_cst));
+ test_fetch_min_pointer<T>(p0, p2, p4, load, store, min);
+ }
+
+ // Test volatile
+ {
+ volatile std::atomic<T*> a;
+ auto load = [&]() { return a.load(); };
+ auto store = [&](T* val) { a.store(val); };
+ auto min = [&](T* val, auto order) { return std::atomic_fetch_min_explicit(&a, val, order); };
+ ASSERT_NOEXCEPT(std::atomic_fetch_min_explicit(&a, p0, std::memory_order_seq_cst));
+ test_fetch_min_pointer<T>(p0, p2, p4, load, store, min);
+ }
+}
+
+int main(int, char**) {
+ TestEachIntegralType<TestFn>()();
+ test_pointer<int>();
+ test_pointer<char>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h
new file mode 100644
index 0000000000000..20a3558296118
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h
@@ -0,0 +1,147 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_STD_ATOMICS_ATOMIC_FETCH_MIN_HELPER_H
+#define TEST_STD_ATOMICS_ATOMIC_FETCH_MIN_HELPER_H
+
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+// Test fetch_min for integral types
+// LoadOp: () -> T - reads current value
+// StoreOp: (T) -> void - stores a value
+// MinOp: (T, memory_order) -> T - performs fetch_min operation
+template <class T, class LoadOp, class StoreOp, class MinOp>
+void test_fetch_min_integral(LoadOp load, StoreOp store, MinOp min) {
+ // Test basic fetch_min (update to smaller value)
+ {
+ store(T(10));
+ T old = min(T(5), std::memory_order_seq_cst);
+ assert(old == T(10));
+ assert(load() == T(5));
+ }
+
+ // Test with larger value (no change)
+ {
+ store(T(10));
+ T old = min(T(20), std::memory_order_seq_cst);
+ assert(old == T(10));
+ assert(load() == T(10));
+ }
+
+ // Test different memory orderings
+ {
+ store(T(15));
+ T old = min(T(8), std::memory_order_seq_cst);
+ assert(old == T(15));
+ assert(load() == T(8));
+ }
+
+ {
+ store(T(10));
+ T old = min(T(3), std::memory_order_relaxed);
+ assert(old == T(10));
+ assert(load() == T(3));
+ }
+
+ {
+ store(T(10));
+ T old = min(T(2), std::memory_order_acquire);
+ assert(old == T(10));
+ assert(load() == T(2));
+ }
+
+ {
+ store(T(10));
+ T old = min(T(7), std::memory_order_release);
+ assert(old == T(10));
+ assert(load() == T(7));
+ }
+
+ {
+ store(T(10));
+ T old = min(T(4), std::memory_order_acq_rel);
+ assert(old == T(10));
+ assert(load() == T(4));
+ }
+
+ {
+ store(T(15));
+ T old = min(T(6), std::memory_order_relaxed);
+ assert(old == T(15));
+ assert(load() == T(6));
+ }
+
+ // Test return type
+ {
+ store(T(10));
+ static_assert(std::is_same_v<decltype(min(T(5), std::memory_order_seq_cst)), T>);
+ }
+}
+
+// Test fetch_min for pointer types
+// LoadOp: () -> T* - reads current pointer value
+// StoreOp: (T*) -> void - stores a pointer value
+// MinOp: (T*, memory_order) -> T* - performs fetch_min operation
+template <class T, class LoadOp, class StoreOp, class MinOp>
+void test_fetch_min_pointer(T* p0, T* p2, T* p4, LoadOp load, StoreOp store, MinOp min) {
+ // Test basic fetch_min (update to smaller pointer)
+ {
+ store(p2);
+ T* old = min(p0, std::memory_order_seq_cst);
+ assert(old == p2);
+ assert(load() == p0);
+ }
+
+ // Test with larger pointer (no change)
+ {
+ store(p2);
+ T* old = min(p4, std::memory_order_seq_cst);
+ assert(old == p2);
+ assert(load() == p2);
+ }
+
+ // Test different memory orderings
+ {
+ store(p4);
+ T* old = min(p2, std::memory_order_seq_cst);
+ assert(old == p4);
+ assert(load() == p2);
+ }
+
+ {
+ store(p4);
+ T* old = min(p2, std::memory_order_relaxed);
+ assert(old == p4);
+ assert(load() == p2);
+ }
+
+ {
+ store(p4);
+ T* old = min(p0, std::memory_order_acquire);
+ assert(old == p4);
+ assert(load() == p0);
+ }
+
+ {
+ store(p4);
+ T* old = min(p0, std::memory_order_release);
+ assert(old == p4);
+ assert(load() == p0);
+ }
+
+ // Test return type
+ {
+ store(p2);
+ static_assert(std::is_same_v<decltype(min(p0, std::memory_order_seq_cst)), T*>);
+ }
+}
+
+#endif // TEST_STD_ATOMICS_ATOMIC_FETCH_MIN_HELPER_H
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 e74c2b58717a9..fd8fc54f3b928 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 9d2e095ab9ff7..1c7346df464c6 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 0adc9b3e864f4cba65342b184fcff203c46bb637 Mon Sep 17 00:00:00 2001
From: Gonzalo Brito Gadeschi <gonzalob at nvidia.com>
Date: Tue, 17 Mar 2026 05:18:39 -0700
Subject: [PATCH 2/3] use addressoff
---
libcxx/include/__atomic/support/c11.h | 8 ++++----
libcxx/include/__atomic/support/gcc.h | 8 ++++----
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__atomic/support/c11.h b/libcxx/include/__atomic/support/c11.h
index 5c744617b1281..d6519681568f4 100644
--- a/libcxx/include/__atomic/support/c11.h
+++ b/libcxx/include/__atomic/support/c11.h
@@ -268,7 +268,7 @@ __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val, mem
_Tp __new;
do {
__new = __old < __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
template <class _Tp>
@@ -278,7 +278,7 @@ __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order
_Tp __new;
do {
__new = __old < __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
@@ -289,7 +289,7 @@ __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val, mem
_Tp __new;
do {
__new = __old > __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
template <class _Tp>
@@ -299,7 +299,7 @@ __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order
_Tp __new;
do {
__new = __old > __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
diff --git a/libcxx/include/__atomic/support/gcc.h b/libcxx/include/__atomic/support/gcc.h
index 16f56e5ee86c6..ad711c8162841 100644
--- a/libcxx/include/__atomic/support/gcc.h
+++ b/libcxx/include/__atomic/support/gcc.h
@@ -265,7 +265,7 @@ __cxx_atomic_fetch_min(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val, mem
_Tp __new;
do {
__new = __old < __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
@@ -276,7 +276,7 @@ __cxx_atomic_fetch_min(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order
_Tp __new;
do {
__new = __old < __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
@@ -287,7 +287,7 @@ __cxx_atomic_fetch_max(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val, mem
_Tp __new;
do {
__new = __old > __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
@@ -298,7 +298,7 @@ __cxx_atomic_fetch_max(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val, memory_order
_Tp __new;
do {
__new = __old > __val ? __old : __val;
- } while (!__cxx_atomic_compare_exchange_weak(__a, &__old, __new, __order, memory_order_relaxed));
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__old), __new, __order, memory_order_relaxed));
return __old;
}
>From f839b5c6aec6cf5fc9e28b63416d6b32495187ec Mon Sep 17 00:00:00 2001
From: Gonzalo Brito Gadeschi <gonzalob at nvidia.com>
Date: Mon, 23 Mar 2026 07:07:06 -0700
Subject: [PATCH 3/3] cleanup concept use in tests
---
libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp | 7 +++----
libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp | 7 +++----
2 files changed, 6 insertions(+), 8 deletions(-)
diff --git a/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp b/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
index 1ce4d5601e376..f9b5a5009f4a0 100644
--- a/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_max.pass.cpp
@@ -16,16 +16,15 @@
#include <cassert>
#include <concepts>
#include <type_traits>
-#include <utility>
#include "test_macros.h"
#include "atomic_helpers.h"
#include "../atomics.types.operations/atomics.types.operations.req/atomic_fetch_max_helper.h"
template <typename T>
-concept has_fetch_max = requires(T t) {
- std::declval<T const>().fetch_max(std::declval<typename T::value_type>());
- std::declval<T const>().fetch_max(std::declval<typename T::value_type>(), std::declval<std::memory_order>());
+concept has_fetch_max = requires(const T& t, typename T::value_type v, std::memory_order m) {
+ t.fetch_max(v);
+ t.fetch_max(v, m);
};
template <typename T>
diff --git a/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp b/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
index 2bcbc31c99387..8e23a892ab863 100644
--- a/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.ref/fetch_min.pass.cpp
@@ -16,16 +16,15 @@
#include <cassert>
#include <concepts>
#include <type_traits>
-#include <utility>
#include "test_macros.h"
#include "atomic_helpers.h"
#include "../atomics.types.operations/atomics.types.operations.req/atomic_fetch_min_helper.h"
template <typename T>
-concept has_fetch_min = requires(T t) {
- std::declval<T const>().fetch_min(std::declval<typename T::value_type>());
- std::declval<T const>().fetch_min(std::declval<typename T::value_type>(), std::declval<std::memory_order>());
+concept has_fetch_min = requires(const T& t, typename T::value_type v, std::memory_order m) {
+ t.fetch_min(v);
+ t.fetch_min(v, m);
};
template <typename T>
More information about the libcxx-commits
mailing list