Debian Codegraph @

libstdc++-v3/testsuite/29_atomics/atomic_float/compare_exchange_padding.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-options "-O0" }
3 // { dg-require-effective-target libatomic_available }
4 // { dg-additional-options "[atomic_link_flags [get_multilibs]] -latomic" }
5
6 #include <atomic>
7 #include <cstring>
8 #include <limits>
9 #include <testsuite_hooks.h>
10
11 template<typename T>
12 void __attribute__((noinline,noipa))
13 fill_padding(T& f)
14 {
15 T mask;
16 std::memset(&mask, 0xff, sizeof(T));
17 __builtin_clear_padding(&mask);
18 unsigned char* ptr_f = (unsigned char*)&f;
19 unsigned char* ptr_mask = (unsigned char*)&mask;
20 for (unsigned i = 0; i < sizeof(T); i++)
21 {
22 if (ptr_mask[i] == 0x00)
23 {
24 ptr_f[i] = 0xff;
25 }
26 }
27 }
28
29 void
30 test01()
31 {
32 // test for long double with padding (float80)
33 if constexpr (std::numeric_limits<long double>::digits == 64)
34 {
35 long double f = 0.5f; // long double has padding bits on x86
36 fill_padding(f);
37 std::atomic<long double> as{ f }; // padding cleared on constructor
38 long double t = 1.5;
39
40 as.fetch_add(t);
41 long double s = f + t;
42 t = as.load();
43 VERIFY(s == t); // padding ignored on comparison
44 fill_padding(s);
45 VERIFY(as.compare_exchange_weak(s, f)); // padding cleared on cmpexchg
46 fill_padding(f);
47 VERIFY(as.compare_exchange_strong(f, t)); // padding cleared on cmpexchg
48 }
49 }
50
51 int main()
52 {
53 test01();
54 }