Skip to content

Commit d8d9b5a

Browse files
committed
[libc++][atomic_ref] Use __atomic_fetch_{add,sub} builtins on floating-points whenever possible
Fix #135109 Clang is able to emit an `atomicrmw` instruction from the `__atomic_fetch_add` and `__atomic_fetch_sub` builtins on floating-point types.
1 parent f5c5f9f commit d8d9b5a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

libcxx/include/__atomic/atomic_ref.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,28 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
322322
atomic_ref& operator=(const atomic_ref&) = delete;
323323

324324
_LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
325+
# ifdef _LIBCPP_COMPILER_CLANG_BASED
326+
return __atomic_fetch_add(this->__ptr_, __arg, std::__to_gcc_order(__order));
327+
# else
325328
_Tp __old = this->load(memory_order_relaxed);
326329
_Tp __new = __old + __arg;
327330
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
328331
__new = __old + __arg;
329332
}
330333
return __old;
334+
# endif
331335
}
332336
_LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
337+
# ifdef _LIBCPP_COMPILER_CLANG_BASED
338+
return __atomic_fetch_sub(this->__ptr_, __arg, std::__to_gcc_order(__order));
339+
# else
333340
_Tp __old = this->load(memory_order_relaxed);
334341
_Tp __new = __old - __arg;
335342
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
336343
__new = __old - __arg;
337344
}
338345
return __old;
346+
# endif
339347
}
340348

341349
_LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; }

0 commit comments

Comments
 (0)