[libcxx-commits] [libcxx] [libcxx] atomic min/max (PR #186694)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Mar 21 07:12:51 PDT 2026


================
@@ -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, std::addressof(__old), __new, __order, memory_order_relaxed));
----------------
huixie90 wrote:

I think we should aim to use the builtin first.  the c11 builtin and clang's gcc builtin already supports to generate
```
atomicrmw fmax
```

https://godbolt.org/z/q5j1qb68M

However,  the `x87_80 long double` might not work, in which case you can always do `if constexpr` to check if the builtin is supported and fallback to the CAS loop



https://github.com/llvm/llvm-project/pull/186694


More information about the libcxx-commits mailing list