Skip to content

Commit bb44136

Browse files
committed
[libc++][atomic_ref] fixup atomic_{add,sub} for floating points
1 parent 970e5cc commit bb44136

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

libcxx/include/__atomic/atomic_ref.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,20 @@ struct __atomic_ref_base<_Tp, /*_IsIntegral=*/false, /*_IsFloatingPoint=*/true>
209209
_LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); }
210210

211211
_LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
212-
return __atomic_fetch_add(this->__ptr_, __arg, __to_gcc_order(__order));
212+
_Tp __old = this->load(memory_order_relaxed);
213+
_Tp __new = __old + __arg;
214+
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
215+
__new = __old + __arg;
216+
}
217+
return __old;
213218
}
214219
_LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
215-
return __atomic_fetch_sub(this->__ptr_, __arg, __to_gcc_order(__order));
220+
_Tp __old = this->load(memory_order_relaxed);
221+
_Tp __new = __old - __arg;
222+
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
223+
__new = __old - __arg;
224+
}
225+
return __old;
216226
}
217227

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

0 commit comments

Comments
 (0)