[libcxx-commits] [libcxx] [libc++][In progress] Floating Point Atomic (PR #67799)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Oct 9 09:48:43 PDT 2023
https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/67799
>From 3391961d064b498115b565cd80fdba9aac5b239f Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Wed, 28 Jun 2023 15:47:39 +0100
Subject: [PATCH 1/3] [libc++][In progress] Floating Point Atomic
- implement P0020R6 Floating Point Atomic
Differential Revision: https://reviews.llvm.org/D153981
---
libcxx/include/__atomic/atomic.h | 118 ++++++++++++++++++
.../atomics.types.float/copy.compile.pass.cpp | 29 +++++
.../atomics.types.float/ctor.pass.cpp | 39 ++++++
.../atomics.types.float/fetch_add.pass.cpp | 81 ++++++++++++
.../atomics.types.float/fetch_sub.pass.cpp | 50 ++++++++
.../atomics.types.float/lockfree.pass.cpp | 52 ++++++++
.../typedef.compile.pass.cpp | 25 ++++
7 files changed, 394 insertions(+)
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/copy.compile.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/lockfree.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp
diff --git a/libcxx/include/__atomic/atomic.h b/libcxx/include/__atomic/atomic.h
index 47de6b958a96c1b..7b17c1cbbd98640 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -14,11 +14,15 @@
#include <__atomic/cxx_atomic_impl.h>
#include <__atomic/memory_order.h>
#include <__config>
+#include <__functional/operations.h>
#include <__memory/addressof.h>
+#include <__type_traits/is_floating_point.h>
#include <__type_traits/is_function.h>
#include <__type_traits/is_same.h>
#include <__type_traits/remove_pointer.h>
+#include <__utility/forward.h>
#include <cstddef>
+#include <cstdio>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -136,6 +140,120 @@ struct atomic<_Tp*>
atomic& operator=(const atomic&) volatile = delete;
};
+#if _LIBCPP_STD_VER >= 20
+template <class _Tp> requires is_floating_point_v<_Tp>
+struct atomic<_Tp>
+ : public __atomic_base<_Tp>
+{
+ private:
+ // see lib/Sema/SemaChecking.cpp approx. line 6748, function IsAllowedValueType
+ // // LLVM Parser does not allow atomicrmw with x86_fp80 type.
+ //if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
+ // &Context.getTargetInfo().getLongDoubleFormat() ==
+ // &llvm::APFloat::x87DoubleExtended())
+
+ // this is platform dependent. How can we get the correct answer?
+ _LIBCPP_HIDE_FROM_ABI static constexpr bool __has_rmw_builtin = !std::is_same_v<_Tp, long double>;
+
+ template <class _This, class _BuiltinOp, class _Operation>
+ _LIBCPP_HIDE_FROM_ABI static _Tp __rmw_op(_This&& __self, _Tp __operand, memory_order __m, _BuiltinOp __builtin_op, _Operation __operation) {
+ if constexpr (__has_rmw_builtin) {
+ return __builtin_op(std::addressof(std::forward<_This>(__self).__a_), __operand, __m);
+ } else {
+ _Tp __old = __self.load(memory_order_relaxed);
+ _Tp __new = __operation(__old, __operand);
+ while(!__self.compare_exchange_weak(__old, __new, __m, memory_order_relaxed)) {
+ if constexpr (std::is_same_v<_Tp, long double>){
+ // https://reviews.llvm.org/D53965
+ // https://bugs.llvm.org/show_bug.cgi?id=48634
+ // clang bug: __old is not updated on failure for atomic<long double>
+ __old = __self.load(memory_order_relaxed);
+ }
+ __new = __operation(__old, __operand);
+ }
+ return __old;
+ }
+ }
+
+ template <class _This>
+ _LIBCPP_HIDE_FROM_ABI static _Tp __fetch_add(_This&& __self, _Tp __operand, memory_order __m) {
+ auto __builtin_op = [](auto __a, auto __operand, auto __order){
+ return std::__cxx_atomic_fetch_add(__a, __operand, __order);
+ };
+ return __rmw_op(std::forward<_This>(__self), __operand, __m, __builtin_op, std::plus<>{});
+ }
+
+ template <class _This>
+ _LIBCPP_HIDE_FROM_ABI static _Tp __fetch_sub(_This&& __self, _Tp __operand, memory_order __m) {
+ auto __builtin_op = [](auto __a, auto __operand, auto __order){
+ return std::__cxx_atomic_fetch_sub(__a, __operand, __order);
+ };
+ return __rmw_op(std::forward<_This>(__self), __operand, __m, __builtin_op, std::minus<>{});
+ }
+
+ public:
+ using __base = __atomic_base<_Tp>;
+ using value_type = _Tp;
+ using difference_type = value_type;
+
+ _LIBCPP_HIDE_FROM_ABI constexpr atomic() noexcept = default;
+ _LIBCPP_HIDE_FROM_ABI constexpr atomic(_Tp __d) noexcept : __base(__d) {}
+
+ atomic(const atomic&) = delete;
+ atomic& operator=(const atomic&) = delete;
+ atomic& operator=(const atomic&) volatile = delete;
+
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp operator=(_Tp __d) volatile noexcept
+ {__base::store(__d); return __d;}
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp operator=(_Tp __d) noexcept
+ {__base::store(__d); return __d;}
+
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
+ requires __base::is_always_lock_free {
+ return __fetch_add(*this, __op, __m);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
+ return __fetch_add(*this, __op, __m);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
+ requires __base::is_always_lock_free {
+ return __fetch_sub(*this, __op, __m);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI
+ _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
+ return __fetch_sub(*this, __op, __m);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) volatile noexcept
+ requires __base::is_always_lock_free {
+ return fetch_add(__op) + __op;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) noexcept {
+ return fetch_add(__op) + __op;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) volatile noexcept
+ requires __base::is_always_lock_free {
+ return fetch_sub(__op) - __op;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) noexcept {
+ return fetch_sub(__op) - __op;
+ }
+
+};
+
+#endif // _LIBCPP_STD_VER >= 20
+
// atomic_is_lock_free
template <class _Tp>
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/copy.compile.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/copy.compile.pass.cpp
new file mode 100644
index 000000000000000..3984d621ad3ab04
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/copy.compile.pass.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// atomic(const atomic&) = delete;
+// atomic& operator=(const atomic&) = delete;
+// atomic& operator=(const atomic&) volatile = delete;
+
+#include <atomic>
+#include <type_traits>
+
+template <class T>
+void test() {
+ static_assert(!std::is_copy_assignable_v<std::atomic<T>>);
+ static_assert(!std::is_copy_constructible_v<std::atomic<T>>);
+ static_assert(!std::is_move_constructible_v<std::atomic<T>>);
+ static_assert(!std::is_move_assignable_v<std::atomic<T>>);
+ static_assert(!std::is_assignable_v<volatile std::atomic<T>&, const std::atomic<T>&>);
+}
+
+template void test<float>();
+template void test<double>();
+template void test<long double>();
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
new file mode 100644
index 000000000000000..8fc6ac0f34cf1ed
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// constexpr atomic() noexcept;
+// constexpr atomic(floating-point-type) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+
+#include "test_macros.h"
+
+template <class T>
+void test() {
+ // constexpr atomic() noexcept;
+ {
+ constexpr std::atomic<T> a = {};
+ assert(a.load() == T(0));
+ }
+
+ // constexpr atomic(floating-point-type) noexcept;
+ {
+ constexpr std::atomic<T> a = T(5.2);
+ assert(a.load() == T(5.2));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
new file mode 100644
index 000000000000000..32f5f4097165ae4
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// floating-point-type fetch_add(floating-point-type,
+// memory_order = memory_order::seq_cst) volatile noexcept;
+// floating-point-type fetch_add(floating-point-type,
+// memory_order = memory_order::seq_cst) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <thread>
+#include <vector>
+
+#include "test_macros.h"
+#include "make_test_thread.h"
+
+template <class T>
+void test() {
+ // fetch_add
+ {
+ std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ assert(r == T(3.1));
+ assert(a.load() == T(3.1) + T(1.2));
+ }
+
+ // fetch_add volatile
+ {
+ volatile std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ assert(r == T(3.1));
+ assert(a.load() == T(3.1) + T(1.2));
+ }
+
+ // fetch_add concurrent
+ {
+ constexpr auto numberOfThreads = 4;
+ constexpr auto loop = 1000;
+
+ std::atomic<T> at;
+
+ std::vector<std::thread> threads;
+ threads.reserve(numberOfThreads);
+ for (auto i = 0; i < numberOfThreads; ++i) {
+ threads.emplace_back([&at]() {
+ for (auto j = 0; j < loop; ++j) {
+ at.fetch_add(T(1.234));
+ }
+ });
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ const auto times = [](T t, int n) {
+ T res(0);
+ for (auto i = 0; i < n; ++i) {
+ res += t;
+ }
+ return res;
+ };
+
+ assert(at.load() == times(1.234, numberOfThreads * loop));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
new file mode 100644
index 000000000000000..ce3aa57e506acfd
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// floating-point-type fetch_add(floating-point-type,
+// memory_order = memory_order::seq_cst) volatile noexcept;
+// floating-point-type fetch_add(floating-point-type,
+// memory_order = memory_order::seq_cst) noexcept;
+// floating-point-type fetch_sub(floating-point-type,
+// memory_order = memory_order::seq_cst) volatile noexcept;
+// floating-point-type fetch_sub(floating-point-type,
+// memory_order = memory_order::seq_cst) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+
+#include "test_macros.h"
+
+template <class T>
+void test() {
+ // fetch_add
+ {
+ std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ assert(r == T(3.1));
+ assert(a.load() == T(3.1) + T(1.2));
+ }
+
+ // fetch_sub
+ {
+ std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a.fetch_sub(1.2);
+ assert(r == T(3.1));
+ assert(a.load() == T(3.1) - T(1.2));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/lockfree.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/lockfree.pass.cpp
new file mode 100644
index 000000000000000..b632df79c330486
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/lockfree.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// static constexpr bool is_always_lock_free = implementation-defined;
+// bool is_lock_free() const volatile noexcept;
+// bool is_lock_free() const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+
+#include "test_macros.h"
+
+template <class T>
+concept isLockFreeNoexcept = requires(T t) {
+ { t.is_lock_free() } noexcept;
+};
+
+template <class T>
+void test() {
+ static_assert(isLockFreeNoexcept<const std::atomic<T>>);
+ static_assert(isLockFreeNoexcept<const volatile std::atomic<T>>);
+
+ // static constexpr bool is_always_lock_free = implementation-defined;
+ { [[maybe_unused]] constexpr std::same_as<const bool> decltype(auto) r = std::atomic<T>::is_always_lock_free; }
+
+ // bool is_lock_free() const volatile noexcept;
+ {
+ const volatile std::atomic<T> a;
+ [[maybe_unused]] std::same_as<bool> decltype(auto) r = a.is_lock_free();
+ }
+
+ // bool is_lock_free() const noexcept;
+ {
+ const std::atomic<T> a;
+ [[maybe_unused]] std::same_as<bool> decltype(auto) r = a.is_lock_free();
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp
new file mode 100644
index 000000000000000..1046665f1c8fa26
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// using value_type = floating-point-type;
+// using difference_type = value_type;
+
+#include <atomic>
+#include <type_traits>
+
+template <class T>
+void test() {
+ static_assert(std::is_same_v<typename std::atomic<T>::value_type, T>);
+ static_assert(std::is_same_v<typename std::atomic<T>::difference_type, T>);
+}
+
+template void test<float>();
+template void test<double>();
+template void test<long double>();
>From 565478b39936c0a1a7818da572db0cab2eeac509 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Mon, 2 Oct 2023 16:16:02 +0100
Subject: [PATCH 2/3] more tests
---
libcxx/include/__atomic/atomic.h | 56 ++++++------
.../atomics.types.float/fetch_add.pass.cpp | 22 +++--
.../atomics.types.float/fetch_sub.pass.cpp | 59 ++++++++++---
.../operator.minus_equals.pass.cpp | 85 +++++++++++++++++++
.../operator.plus_equals.pass.cpp | 85 +++++++++++++++++++
...ompile.pass.cpp => types.compile.pass.cpp} | 3 +
6 files changed, 260 insertions(+), 50 deletions(-)
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.minus_equals.pass.cpp
create mode 100644 libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.plus_equals.pass.cpp
rename libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/{typedef.compile.pass.cpp => types.compile.pass.cpp} (77%)
diff --git a/libcxx/include/__atomic/atomic.h b/libcxx/include/__atomic/atomic.h
index 7b17c1cbbd98640..36cfc1c9bbd0a39 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -146,49 +146,43 @@ struct atomic<_Tp>
: public __atomic_base<_Tp>
{
private:
- // see lib/Sema/SemaChecking.cpp approx. line 6748, function IsAllowedValueType
- // // LLVM Parser does not allow atomicrmw with x86_fp80 type.
- //if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
+ // The builtin __cxx_atomic_fetch_add does not work for
+ // long double on some platforms with fp80 type
+ // There is no way on the libc++ side to test whether it is
+ // ok to use the builtin for a certain type.
+ // Therefore, we do not use the builtin here
+ // For more details, see
+ // lib/Sema/SemaChecking.cpp function IsAllowedValueType
+ // LLVM Parser does not allow atomicrmw with x86_fp80 type.
+ // if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
// &Context.getTargetInfo().getLongDoubleFormat() ==
// &llvm::APFloat::x87DoubleExtended())
-
- // this is platform dependent. How can we get the correct answer?
- _LIBCPP_HIDE_FROM_ABI static constexpr bool __has_rmw_builtin = !std::is_same_v<_Tp, long double>;
-
- template <class _This, class _BuiltinOp, class _Operation>
- _LIBCPP_HIDE_FROM_ABI static _Tp __rmw_op(_This&& __self, _Tp __operand, memory_order __m, _BuiltinOp __builtin_op, _Operation __operation) {
- if constexpr (__has_rmw_builtin) {
- return __builtin_op(std::addressof(std::forward<_This>(__self).__a_), __operand, __m);
- } else {
- _Tp __old = __self.load(memory_order_relaxed);
- _Tp __new = __operation(__old, __operand);
- while(!__self.compare_exchange_weak(__old, __new, __m, memory_order_relaxed)) {
- if constexpr (std::is_same_v<_Tp, long double>){
- // https://reviews.llvm.org/D53965
- // https://bugs.llvm.org/show_bug.cgi?id=48634
- // clang bug: __old is not updated on failure for atomic<long double>
- __old = __self.load(memory_order_relaxed);
- }
- __new = __operation(__old, __operand);
+ // For more info
+ // https://reviews.llvm.org/D53965
+
+ template <class _This, class _Operation>
+ _LIBCPP_HIDE_FROM_ABI static _Tp __rmw_op(_This&& __self, _Tp __operand, memory_order __m, _Operation __operation) {
+ _Tp __old = __self.load(memory_order_relaxed);
+ _Tp __new = __operation(__old, __operand);
+ while(!__self.compare_exchange_weak(__old, __new, __m, memory_order_relaxed)) {
+ if constexpr (std::is_same_v<_Tp, long double>){
+ // https://bugs.llvm.org/show_bug.cgi?id=48634
+ // clang bug: __old is not updated on failure for atomic<long double>
+ __old = __self.load(memory_order_relaxed);
}
- return __old;
+ __new = __operation(__old, __operand);
}
+ return __old;
}
template <class _This>
_LIBCPP_HIDE_FROM_ABI static _Tp __fetch_add(_This&& __self, _Tp __operand, memory_order __m) {
- auto __builtin_op = [](auto __a, auto __operand, auto __order){
- return std::__cxx_atomic_fetch_add(__a, __operand, __order);
- };
- return __rmw_op(std::forward<_This>(__self), __operand, __m, __builtin_op, std::plus<>{});
+ return __rmw_op(std::forward<_This>(__self), __operand, __m, std::plus<>{});
}
template <class _This>
_LIBCPP_HIDE_FROM_ABI static _Tp __fetch_sub(_This&& __self, _Tp __operand, memory_order __m) {
- auto __builtin_op = [](auto __a, auto __operand, auto __order){
- return std::__cxx_atomic_fetch_sub(__a, __operand, __order);
- };
- return __rmw_op(std::forward<_This>(__self), __operand, __m, __builtin_op, std::minus<>{});
+ return __rmw_op(std::forward<_This>(__self), __operand, __m, std::minus<>{});
}
public:
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
index 32f5f4097165ae4..a156364af93e373 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
@@ -21,34 +21,40 @@
#include "test_macros.h"
#include "make_test_thread.h"
+template <class T>
+concept HasVolatileFetchAdd = requires(volatile std::atomic<T> a, T t) { a.fetch_add(t); };
+
template <class T>
void test() {
+ static_assert(noexcept(std::declval<std::atomic<T>&>().fetch_add(T(0))));
+ static_assert(HasVolatileFetchAdd<T> == std::atomic<T>::is_always_lock_free);
+
// fetch_add
{
std::atomic<T> a(3.1);
- std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ std::same_as<T> decltype(auto) r = a.fetch_add(T(1.2));
assert(r == T(3.1));
assert(a.load() == T(3.1) + T(1.2));
}
// fetch_add volatile
- {
+ if constexpr (std::atomic<T>::is_always_lock_free) {
volatile std::atomic<T> a(3.1);
- std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ std::same_as<T> decltype(auto) r = a.fetch_add(T(1.2));
assert(r == T(3.1));
assert(a.load() == T(3.1) + T(1.2));
}
// fetch_add concurrent
{
- constexpr auto numberOfThreads = 4;
- constexpr auto loop = 1000;
+ constexpr auto number_of_threads = 4;
+ constexpr auto loop = 1000;
std::atomic<T> at;
std::vector<std::thread> threads;
- threads.reserve(numberOfThreads);
- for (auto i = 0; i < numberOfThreads; ++i) {
+ threads.reserve(number_of_threads);
+ for (auto i = 0; i < number_of_threads; ++i) {
threads.emplace_back([&at]() {
for (auto j = 0; j < loop; ++j) {
at.fetch_add(T(1.234));
@@ -68,7 +74,7 @@ void test() {
return res;
};
- assert(at.load() == times(1.234, numberOfThreads * loop));
+ assert(at.load() == times(1.234, number_of_threads * loop));
}
}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
index ce3aa57e506acfd..b37383d0dec5294 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
@@ -7,10 +7,6 @@
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
-// floating-point-type fetch_add(floating-point-type,
-// memory_order = memory_order::seq_cst) volatile noexcept;
-// floating-point-type fetch_add(floating-point-type,
-// memory_order = memory_order::seq_cst) noexcept;
// floating-point-type fetch_sub(floating-point-type,
// memory_order = memory_order::seq_cst) volatile noexcept;
// floating-point-type fetch_sub(floating-point-type,
@@ -19,26 +15,67 @@
#include <atomic>
#include <cassert>
#include <concepts>
+#include <thread>
+#include <vector>
#include "test_macros.h"
+#include "make_test_thread.h"
+
+template <class T>
+concept HasVolatileFetchSub = requires(volatile std::atomic<T> a, T t) { a.fetch_sub(t); };
template <class T>
void test() {
- // fetch_add
+ static_assert(noexcept(std::declval<std::atomic<T>&>().fetch_sub(T(0))));
+ static_assert(HasVolatileFetchSub<T> == std::atomic<T>::is_always_lock_free);
+
+ // fetch_sub
{
std::atomic<T> a(3.1);
- std::same_as<T> decltype(auto) r = a.fetch_add(1.2);
+ std::same_as<T> decltype(auto) r = a.fetch_sub(T(1.2));
assert(r == T(3.1));
- assert(a.load() == T(3.1) + T(1.2));
+ assert(a.load() == T(3.1) - T(1.2));
}
- // fetch_sub
- {
- std::atomic<T> a(3.1);
- std::same_as<T> decltype(auto) r = a.fetch_sub(1.2);
+ // fetch_sub volatile
+ if constexpr (std::atomic<T>::is_always_lock_free) {
+ volatile std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a.fetch_sub(T(1.2));
assert(r == T(3.1));
assert(a.load() == T(3.1) - T(1.2));
}
+
+ // fetch_sub concurrent
+ {
+ constexpr auto number_of_threads = 4;
+ constexpr auto loop = 1000;
+
+ std::atomic<T> at;
+
+ std::vector<std::thread> threads;
+ threads.reserve(number_of_threads);
+ for (auto i = 0; i < number_of_threads; ++i) {
+ threads.emplace_back([&at]() {
+ for (auto j = 0; j < loop; ++j) {
+ at.fetch_sub(T(1.234));
+ }
+ });
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ const auto accu_neg = [](T t, int n) {
+ T res(0);
+ for (auto i = 0; i < n; ++i) {
+ res -= t;
+ }
+ return res;
+ };
+
+ assert(at.load() == accu_neg(1.234, number_of_threads * loop));
+ }
}
int main(int, char**) {
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.minus_equals.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.minus_equals.pass.cpp
new file mode 100644
index 000000000000000..5a04e9e94027d4a
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.minus_equals.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
+
+// floating-point-type operator-=(floating-point-type) volatile noexcept;
+// floating-point-type operator-=(floating-point-type) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <thread>
+#include <vector>
+
+#include "test_macros.h"
+#include "make_test_thread.h"
+
+template <class T>
+concept HasVolatileMinusEquals = requires(volatile std::atomic<T> a, T t) { a -= t; };
+
+template <class T>
+void test() {
+ static_assert(noexcept(std::declval<std::atomic<T>&>() -= T(0)));
+ static_assert(HasVolatileMinusEquals<T> == std::atomic<T>::is_always_lock_free);
+
+ // -=
+ {
+ std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a -= T(1.2);
+ assert(r == T(3.1) - T(1.2));
+ assert(a.load() == T(3.1) - T(1.2));
+ }
+
+ // -= volatile
+ if constexpr (std::atomic<T>::is_always_lock_free) {
+ volatile std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a -= T(1.2);
+ assert(r == T(3.1) - T(1.2));
+ assert(a.load() == T(3.1) - T(1.2));
+ }
+
+ // -= concurrent
+ {
+ constexpr auto number_of_threads = 4;
+ constexpr auto loop = 1000;
+
+ std::atomic<T> at;
+
+ std::vector<std::thread> threads;
+ threads.reserve(number_of_threads);
+ for (auto i = 0; i < number_of_threads; ++i) {
+ threads.emplace_back([&at]() {
+ for (auto j = 0; j < loop; ++j) {
+ at -= T(1.234);
+ }
+ });
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ const auto accu_neg = [](T t, int n) {
+ T res(0);
+ for (auto i = 0; i < n; ++i) {
+ res -= t;
+ }
+ return res;
+ };
+
+ assert(at.load() == accu_neg(1.234, number_of_threads * loop));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.plus_equals.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.plus_equals.pass.cpp
new file mode 100644
index 000000000000000..9cace80331736b8
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/operator.plus_equals.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
+
+// floating-point-type operator+=(floating-point-type) volatile noexcept;
+// floating-point-type operator+=(floating-point-type) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <thread>
+#include <vector>
+
+#include "test_macros.h"
+#include "make_test_thread.h"
+
+template <class T>
+concept HasVolatilePlusEquals = requires(volatile std::atomic<T> a, T t) { a += t; };
+
+template <class T>
+void test() {
+ static_assert(noexcept(std::declval<std::atomic<T>&>() += T(0)));
+ static_assert(HasVolatilePlusEquals<T> == std::atomic<T>::is_always_lock_free);
+
+ // +=
+ {
+ std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a += T(1.2);
+ assert(r == T(3.1) + T(1.2));
+ assert(a.load() == T(3.1) + T(1.2));
+ }
+
+ // += volatile
+ if constexpr (std::atomic<T>::is_always_lock_free) {
+ volatile std::atomic<T> a(3.1);
+ std::same_as<T> decltype(auto) r = a += T(1.2);
+ assert(r == T(3.1) + T(1.2));
+ assert(a.load() == T(3.1) + T(1.2));
+ }
+
+ // += concurrent
+ {
+ constexpr auto number_of_threads = 4;
+ constexpr auto loop = 1000;
+
+ std::atomic<T> at;
+
+ std::vector<std::thread> threads;
+ threads.reserve(number_of_threads);
+ for (auto i = 0; i < number_of_threads; ++i) {
+ threads.emplace_back([&at]() {
+ for (auto j = 0; j < loop; ++j) {
+ at += T(1.234);
+ }
+ });
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+
+ const auto times = [](T t, int n) {
+ T res(0);
+ for (auto i = 0; i < n; ++i) {
+ res += t;
+ }
+ return res;
+ };
+
+ assert(at.load() == times(1.234, number_of_threads * loop));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/types.compile.pass.cpp
similarity index 77%
rename from libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp
rename to libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/types.compile.pass.cpp
index 1046665f1c8fa26..1a4e6dfe0b31553 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/typedef.compile.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/types.compile.pass.cpp
@@ -10,6 +10,7 @@
// using value_type = floating-point-type;
// using difference_type = value_type;
+// The atomic floating-point specializations are standard-layout structs. They each have a trivial destructor.
#include <atomic>
#include <type_traits>
@@ -18,6 +19,8 @@ template <class T>
void test() {
static_assert(std::is_same_v<typename std::atomic<T>::value_type, T>);
static_assert(std::is_same_v<typename std::atomic<T>::difference_type, T>);
+ static_assert(std::is_standard_layout_v<std::atomic<T>>);
+ static_assert(std::is_trivially_destructible_v<std::atomic<T>>);
}
template void test<float>();
>From f4669e718596a19cc7828bcd30d837b8169a0bb7 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Mon, 9 Oct 2023 17:48:21 +0100
Subject: [PATCH 3/3] address feedback and rebase
---
libcxx/docs/ReleaseNotes/18.rst | 1 +
libcxx/docs/Status/Cxx20Papers.csv | 2 +-
libcxx/include/__atomic/atomic.h | 75 +++++++++----------
libcxx/include/atomic | 66 ++++++++++++++++
.../atomics.types.float/locakfree.pass.cpp | 49 ++++++++++++
.../atomics.types.float/ctor.pass.cpp | 42 +++++++++--
.../atomics.types.float/fetch_add.pass.cpp | 1 +
.../atomics.types.float/fetch_sub.pass.cpp | 1 +
8 files changed, 189 insertions(+), 48 deletions(-)
create mode 100644 libcxx/test/libcxx/atomics/atomics.types.generic/atomics.types.float/locakfree.pass.cpp
diff --git a/libcxx/docs/ReleaseNotes/18.rst b/libcxx/docs/ReleaseNotes/18.rst
index b8d21334a77a5e2..eefd440abc10756 100644
--- a/libcxx/docs/ReleaseNotes/18.rst
+++ b/libcxx/docs/ReleaseNotes/18.rst
@@ -53,6 +53,7 @@ Implemented Papers
- P2443R1 - ``views::chunk_by``
- P2538R1 - ADL-proof ``std::projected``
- P2614R2 - Deprecate ``numeric_limits::has_denorm``
+- P0020R6 - Floating Point Atomic
Improvements and New Features
diff --git a/libcxx/docs/Status/Cxx20Papers.csv b/libcxx/docs/Status/Cxx20Papers.csv
index dd6fcf9a7583ce3..3b28cfa49e18a28 100644
--- a/libcxx/docs/Status/Cxx20Papers.csv
+++ b/libcxx/docs/Status/Cxx20Papers.csv
@@ -2,7 +2,7 @@
"`P0463R1 <https://wg21.link/P0463R1>`__","LWG","Endian just Endian","Toronto","|Complete|","7.0"
"`P0674R1 <https://wg21.link/P0674R1>`__","LWG","Extending make_shared to Support Arrays","Toronto","|Complete|","15.0"
"","","","","","",""
-"`P0020R6 <https://wg21.link/P0020R6>`__","LWG","Floating Point Atomic","Albuquerque","",""
+"`P0020R6 <https://wg21.link/P0020R6>`__","LWG","Floating Point Atomic","Albuquerque","|Complete|","18.0"
"`P0053R7 <https://wg21.link/P0053R7>`__","LWG","C++ Synchronized Buffered Ostream","Albuquerque","",""
"`P0202R3 <https://wg21.link/P0202R3>`__","LWG","Add constexpr modifiers to functions in <algorithm> and <utility> Headers","Albuquerque","|Complete|","12.0"
"`P0415R1 <https://wg21.link/P0415R1>`__","LWG","Constexpr for ``std::complex``\ ","Albuquerque","|Complete|","16.0"
diff --git a/libcxx/include/__atomic/atomic.h b/libcxx/include/__atomic/atomic.h
index 36cfc1c9bbd0a39..c4159dcfb6ffdb2 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -141,36 +141,36 @@ struct atomic<_Tp*>
};
#if _LIBCPP_STD_VER >= 20
-template <class _Tp> requires is_floating_point_v<_Tp>
-struct atomic<_Tp>
- : public __atomic_base<_Tp>
-{
+template <class _Tp>
+ requires is_floating_point_v<_Tp>
+struct atomic<_Tp> : public __atomic_base<_Tp> {
private:
// The builtin __cxx_atomic_fetch_add does not work for
// long double on some platforms with fp80 type
// There is no way on the libc++ side to test whether it is
// ok to use the builtin for a certain type.
// Therefore, we do not use the builtin here
- // For more details, see
+ // For more details, see
// lib/Sema/SemaChecking.cpp function IsAllowedValueType
// LLVM Parser does not allow atomicrmw with x86_fp80 type.
// if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
// &Context.getTargetInfo().getLongDoubleFormat() ==
// &llvm::APFloat::x87DoubleExtended())
// For more info
+ // https://github.com/llvm/llvm-project/issues/68602
// https://reviews.llvm.org/D53965
template <class _This, class _Operation>
_LIBCPP_HIDE_FROM_ABI static _Tp __rmw_op(_This&& __self, _Tp __operand, memory_order __m, _Operation __operation) {
_Tp __old = __self.load(memory_order_relaxed);
_Tp __new = __operation(__old, __operand);
- while(!__self.compare_exchange_weak(__old, __new, __m, memory_order_relaxed)) {
- if constexpr (std::is_same_v<_Tp, long double>){
- // https://bugs.llvm.org/show_bug.cgi?id=48634
- // clang bug: __old is not updated on failure for atomic<long double>
- __old = __self.load(memory_order_relaxed);
- }
- __new = __operation(__old, __operand);
+ while (!__self.compare_exchange_weak(__old, __new, __m, memory_order_relaxed)) {
+ if constexpr (std::is_same_v<_Tp, long double>) {
+ // https://github.com/llvm/llvm-project/issues/47978
+ // clang bug: __old is not updated on failure for atomic<long double>
+ __old = __self.load(memory_order_relaxed);
+ }
+ __new = __operation(__old, __operand);
}
return __old;
}
@@ -193,57 +193,54 @@ struct atomic<_Tp>
_LIBCPP_HIDE_FROM_ABI constexpr atomic() noexcept = default;
_LIBCPP_HIDE_FROM_ABI constexpr atomic(_Tp __d) noexcept : __base(__d) {}
- atomic(const atomic&) = delete;
- atomic& operator=(const atomic&) = delete;
+ atomic(const atomic&) = delete;
+ atomic& operator=(const atomic&) = delete;
atomic& operator=(const atomic&) volatile = delete;
- _LIBCPP_HIDE_FROM_ABI
- _Tp operator=(_Tp __d) volatile noexcept
- {__base::store(__d); return __d;}
- _LIBCPP_HIDE_FROM_ABI
- _Tp operator=(_Tp __d) noexcept
- {__base::store(__d); return __d;}
+ _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __d) volatile noexcept {
+ __base::store(__d);
+ return __d;
+ }
+ _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __d) noexcept {
+ __base::store(__d);
+ return __d;
+ }
- _LIBCPP_HIDE_FROM_ABI
- _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
- requires __base::is_always_lock_free {
+ _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
+ requires __base::is_always_lock_free
+ {
return __fetch_add(*this, __op, __m);
}
- _LIBCPP_HIDE_FROM_ABI
- _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
+ _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
return __fetch_add(*this, __op, __m);
}
- _LIBCPP_HIDE_FROM_ABI
- _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
- requires __base::is_always_lock_free {
+ _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile noexcept
+ requires __base::is_always_lock_free
+ {
return __fetch_sub(*this, __op, __m);
}
- _LIBCPP_HIDE_FROM_ABI
- _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
+ _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) noexcept {
return __fetch_sub(*this, __op, __m);
}
_LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) volatile noexcept
- requires __base::is_always_lock_free {
+ requires __base::is_always_lock_free
+ {
return fetch_add(__op) + __op;
}
- _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) noexcept {
- return fetch_add(__op) + __op;
- }
+ _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) noexcept { return fetch_add(__op) + __op; }
_LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) volatile noexcept
- requires __base::is_always_lock_free {
- return fetch_sub(__op) - __op;
- }
-
- _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) noexcept {
+ requires __base::is_always_lock_free
+ {
return fetch_sub(__op) - __op;
}
+ _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) noexcept { return fetch_sub(__op) - __op; }
};
#endif // _LIBCPP_STD_VER >= 20
diff --git a/libcxx/include/atomic b/libcxx/include/atomic
index 2f122a707bdc33a..c52bf7521635f02 100644
--- a/libcxx/include/atomic
+++ b/libcxx/include/atomic
@@ -262,6 +262,72 @@ struct atomic<T*>
void notify_all() noexcept;
};
+template<>
+struct atomic<floating-point-type> {
+ using value_type = floating-point-type;
+ using difference_type = value_type;
+
+ static constexpr bool is_always_lock_free = implementation-defined;
+ bool is_lock_free() const volatile noexcept;
+ bool is_lock_free() const noexcept;
+
+ constexpr atomic() noexcept;
+ constexpr atomic(floating-point-type) noexcept;
+ atomic(const atomic&) = delete;
+ atomic& operator=(const atomic&) = delete;
+ atomic& operator=(const atomic&) volatile = delete;
+
+ void store(floating-point-type, memory_order = memory_order::seq_cst) volatile noexcept;
+ void store(floating-point-type, memory_order = memory_order::seq_cst) noexcept;
+ floating-point-type operator=(floating-point-type) volatile noexcept;
+ floating-point-type operator=(floating-point-type) noexcept;
+ floating-point-type load(memory_order = memory_order::seq_cst) volatile noexcept;
+ floating-point-type load(memory_order = memory_order::seq_cst) noexcept;
+ operator floating-point-type() volatile noexcept;
+ operator floating-point-type() noexcept;
+
+ floating-point-type exchange(floating-point-type,
+ memory_order = memory_order::seq_cst) volatile noexcept;
+ floating-point-type exchange(floating-point-type,
+ memory_order = memory_order::seq_cst) noexcept;
+ bool compare_exchange_weak(floating-point-type&, floating-point-type,
+ memory_order, memory_order) volatile noexcept;
+ bool compare_exchange_weak(floating-point-type&, floating-point-type,
+ memory_order, memory_order) noexcept;
+ bool compare_exchange_strong(floating-point-type&, floating-point-type,
+ memory_order, memory_order) volatile noexcept;
+ bool compare_exchange_strong(floating-point-type&, floating-point-type,
+ memory_order, memory_order) noexcept;
+ bool compare_exchange_weak(floating-point-type&, floating-point-type,
+ memory_order = memory_order::seq_cst) volatile noexcept;
+ bool compare_exchange_weak(floating-point-type&, floating-point-type,
+ memory_order = memory_order::seq_cst) noexcept;
+ bool compare_exchange_strong(floating-point-type&, floating-point-type,
+ memory_order = memory_order::seq_cst) volatile noexcept;
+ bool compare_exchange_strong(floating-point-type&, floating-point-type,
+ memory_order = memory_order::seq_cst) noexcept;
+
+ floating-point-type fetch_add(floating-point-type,
+ memory_order = memory_order::seq_cst) volatile noexcept;
+ floating-point-type fetch_add(floating-point-type,
+ memory_order = memory_order::seq_cst) noexcept;
+ floating-point-type fetch_sub(floating-point-type,
+ memory_order = memory_order::seq_cst) volatile noexcept;
+ floating-point-type fetch_sub(floating-point-type,
+ memory_order = memory_order::seq_cst) noexcept;
+
+ floating-point-type operator+=(floating-point-type) volatile noexcept;
+ floating-point-type operator+=(floating-point-type) noexcept;
+ floating-point-type operator-=(floating-point-type) volatile noexcept;
+ floating-point-type operator-=(floating-point-type) noexcept;
+
+ void wait(floating-point-type, memory_order = memory_order::seq_cst) const volatile noexcept;
+ void wait(floating-point-type, memory_order = memory_order::seq_cst) const noexcept;
+ void notify_one() volatile noexcept;
+ void notify_one() noexcept;
+ void notify_all() volatile noexcept;
+ void notify_all() noexcept;
+};
// [atomics.nonmembers], non-member functions
template<class T>
diff --git a/libcxx/test/libcxx/atomics/atomics.types.generic/atomics.types.float/locakfree.pass.cpp b/libcxx/test/libcxx/atomics/atomics.types.generic/atomics.types.float/locakfree.pass.cpp
new file mode 100644
index 000000000000000..5388ae1c6710fb9
--- /dev/null
+++ b/libcxx/test/libcxx/atomics/atomics.types.generic/atomics.types.float/locakfree.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// static constexpr bool is_always_lock_free = implementation-defined;
+// bool is_lock_free() const volatile noexcept;
+// bool is_lock_free() const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+
+#include "test_macros.h"
+
+template <class T>
+void test() {
+ // static constexpr bool is_always_lock_free = implementation-defined;
+ {
+ bool r = std::atomic<T>::is_always_lock_free;
+ assert(r == __atomic_always_lock_free(sizeof(T), 0));
+ }
+
+ // bool is_lock_free() const volatile noexcept;
+ {
+ const volatile std::atomic<T> a;
+ bool r = a.is_lock_free();
+ assert(r == __cxx_atomic_is_lock_free(sizeof(T)));
+ }
+
+ // bool is_lock_free() const noexcept;
+ {
+ const std::atomic<T> a;
+ bool r = a.is_lock_free();
+ assert(r == __cxx_atomic_is_lock_free(sizeof(T)));
+ }
+}
+
+int main(int, char**) {
+ test<float>();
+ test<double>();
+ test<long double>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
index 8fc6ac0f34cf1ed..16df7ff23488759 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/ctor.pass.cpp
@@ -17,23 +17,49 @@
#include "test_macros.h"
template <class T>
-void test() {
+constinit std::atomic<T> a1 = T();
+
+template <class T>
+constinit std::atomic<T> a2 = T(5.2);
+
+template <class T>
+constexpr void testOne() {
+ static_assert(std::is_nothrow_constructible_v<std::atomic<T>>);
+ static_assert(std::is_nothrow_constructible_v<std::atomic<T>, T>);
+
// constexpr atomic() noexcept;
{
- constexpr std::atomic<T> a = {};
- assert(a.load() == T(0));
+ std::atomic<T> a = {};
+ if (!TEST_IS_CONSTANT_EVALUATED) {
+ assert(a.load() == T(0));
+ }
}
// constexpr atomic(floating-point-type) noexcept;
{
- constexpr std::atomic<T> a = T(5.2);
- assert(a.load() == T(5.2));
+ std::atomic<T> a = T(5.2);
+ if (!TEST_IS_CONSTANT_EVALUATED) {
+ assert(a.load() == T(5.2));
+ }
+ }
+
+ // test constinit
+ if (!TEST_IS_CONSTANT_EVALUATED) {
+ assert(a1<T> == T(0.0));
+ assert(a2<T> == T(5.2));
}
}
+constexpr bool test() {
+ testOne<float>();
+ testOne<double>();
+ testOne<long double>();
+ return true;
+}
+
int main(int, char**) {
- test<float>();
- test<double>();
- test<long double>();
+ test();
+ static_assert(test());
+
return 0;
}
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
index a156364af93e373..54780e18e9bec4a 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp
@@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+// UNSUPPORTED: no-threads
// UNSUPPORTED: c++03, c++11, c++14, c++17
// floating-point-type fetch_add(floating-point-type,
diff --git a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
index b37383d0dec5294..02a42144a4692e1 100644
--- a/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_sub.pass.cpp
@@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+// UNSUPPORTED: no-threads
// UNSUPPORTED: c++03, c++11, c++14, c++17
// floating-point-type fetch_sub(floating-point-type,
More information about the libcxx-commits
mailing list