[libcxx-commits] [libcxx] [libc++] Implement P0493R5: Atomic minimum/maximum (PR #180333)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Feb 21 01:37:33 PST 2026
================
@@ -258,6 +258,66 @@ __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) {
+#if __has_builtin(__atomic_fetch_max)
+ return __atomic_fetch_max(std::addressof(__a->__a_value), __pattern, __to_gcc_order(__order));
+#else
+ _Tp __ret = __cxx_atomic_load(__a, memory_order_relaxed);
+ _Tp __value;
+ do {
+ __value = __ret > __pattern ? __ret : __pattern;
+ } while (!__cxx_atomic_compare_exchange_weak(__a, std::addressof(__ret), __value, __order, memory_order_relaxed));
----------------
huixie90 wrote:
I would like to point out that this implementation unconditionally writes to the atomic even if the atomic value was already the max. I think this can be improved.
Although gcc is going to have this builtin so this implementation will be deprecated.
But I still think we can improve this. I wonder if others agree with me.
https://github.com/llvm/llvm-project/pull/180333
More information about the libcxx-commits
mailing list