1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: LIBCXX-AIX-FIXME
10 // XFAIL: !has-64-bit-atomics
11 
12 // https://github.com/llvm/llvm-project/issues/72893
13 // XFAIL: target={{x86_64-.*}} && tsan
14 
15 // floating-point-type fetch_sub(floating-point-type,
16 //                               memory_order = memory_order::seq_cst) volatile noexcept;
17 // floating-point-type fetch_sub(floating-point-type,
18 //                               memory_order = memory_order::seq_cst) noexcept;
19 
20 #include <atomic>
21 #include <cassert>
22 #include <concepts>
23 #include <type_traits>
24 #include <vector>
25 
26 #include "test_helper.h"
27 #include "test_macros.h"
28 
29 #ifndef TEST_HAS_NO_THREADS
30 #  include "make_test_thread.h"
31 #  include <thread>
32 #endif
33 
34 template <class T>
35 concept HasVolatileFetchSub = requires(volatile std::atomic<T>& a, T t) { a.fetch_sub(t); };
36 
37 template <class T, template <class> class MaybeVolatile = std::type_identity_t>
test_impl()38 void test_impl() {
39   static_assert(HasVolatileFetchSub<T> == std::atomic<T>::is_always_lock_free);
40   static_assert(noexcept(std::declval<MaybeVolatile<std::atomic<T>>&>().fetch_sub(T(0))));
41 
42   // fetch_sub
43   {
44     MaybeVolatile<std::atomic<T>> a(T(3.1));
45     std::same_as<T> decltype(auto) r = a.fetch_sub(T(1.2), std::memory_order::relaxed);
46     assert(r == T(3.1));
47     assert(a.load() == T(3.1) - T(1.2));
48   }
49 
50 #ifndef TEST_HAS_NO_THREADS
51   // fetch_sub concurrent
52   {
53     constexpr auto number_of_threads = 4;
54     constexpr auto loop              = 1000;
55 
56     MaybeVolatile<std::atomic<T>> at;
57 
58     std::vector<std::thread> threads;
59     threads.reserve(number_of_threads);
60     for (auto i = 0; i < number_of_threads; ++i) {
61       threads.push_back(support::make_test_thread([&at]() {
62         for (auto j = 0; j < loop; ++j) {
63           at.fetch_sub(T(1.234), std::memory_order::relaxed);
64         }
65       }));
66     }
67 
68     for (auto& thread : threads) {
69       thread.join();
70     }
71 
72     const auto accu_neg = [](T t, int n) {
73       T res(0);
74       for (auto i = 0; i < n; ++i) {
75         res -= t;
76       }
77       return res;
78     };
79 
80     assert(at.load() == accu_neg(T(1.234), number_of_threads * loop));
81   }
82 #endif
83 
84   // memory_order::release
85   {
86     auto store = [](MaybeVolatile<std::atomic<T>>& x, T old_val, T new_val) {
87       x.fetch_sub(old_val - new_val, std::memory_order::release);
88     };
89     auto load = [](MaybeVolatile<std::atomic<T>>& x) { return x.load(std::memory_order::acquire); };
90     test_acquire_release<T, MaybeVolatile>(store, load);
91   }
92 
93   // memory_order::seq_cst
94   {
95     auto fetch_sub = [](MaybeVolatile<std::atomic<T>>& x, T old_value, T new_val) { x.fetch_sub(old_value - new_val); };
96     auto fetch_sub_with_order = [](MaybeVolatile<std::atomic<T>>& x, T old_value, T new_val) {
97       x.fetch_sub(old_value - new_val, std::memory_order::seq_cst);
98     };
99     auto load = [](auto& x) { return x.load(); };
100     test_seq_cst<T, MaybeVolatile>(fetch_sub, load);
101     test_seq_cst<T, MaybeVolatile>(fetch_sub_with_order, load);
102   }
103 }
104 
105 template <class T>
test()106 void test() {
107   test_impl<T>();
108   if constexpr (std::atomic<T>::is_always_lock_free) {
109     test_impl<T, std::add_volatile_t>();
110   }
111 }
112 
main(int,char **)113 int main(int, char**) {
114   test<float>();
115   test<double>();
116   // TODO https://github.com/llvm/llvm-project/issues/47978
117   // test<long double>();
118 
119   return 0;
120 }
121