| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-require-atomic-cmpxchg-word "" } |
| 3 |
// { dg-add-options libatomic } |
| 4 |
// { dg-additional-options "-fno-tree-sra" } |
| 5 |
|
| 6 |
#include <atomic> |
| 7 |
#include <cstring> |
| 8 |
|
| 9 |
#include <testsuite_hooks.h> |
| 10 |
|
| 11 |
struct S { char c; short s; }; |
| 12 |
|
| 13 |
void __attribute__((noinline,noipa)) |
| 14 |
fill_struct(S& s) |
| 15 |
{ std::memset(&s, 0xff, sizeof(S)); } |
| 16 |
|
| 17 |
bool |
| 18 |
compare_struct(const S& a, const S& b) |
| 19 |
{ return std::memcmp(&a, &b, sizeof(S)) == 0; } |
| 20 |
|
| 21 |
int |
| 22 |
main () |
| 23 |
{ |
| 24 |
S s; |
| 25 |
fill_struct(s); |
| 26 |
s.c = 'a'; |
| 27 |
s.s = 42; |
| 28 |
|
| 29 |
std::atomic<S> as{ s }; |
| 30 |
auto ts = as.load(); // SRA might prevent copying of padding bits here. |
| 31 |
VERIFY( !compare_struct(s, ts) ); // padding cleared on construction |
| 32 |
as.exchange(s); |
| 33 |
auto es = as.load(); // SRA might prevent copying of padding bits here. |
| 34 |
VERIFY( compare_struct(ts, es) ); // padding cleared on exchange |
| 35 |
|
| 36 |
S n; |
| 37 |
fill_struct(n); |
| 38 |
n.c = 'b'; |
| 39 |
n.s = 71; |
| 40 |
// padding cleared on compexchg |
| 41 |
VERIFY( as.compare_exchange_weak(s, n) ); |
| 42 |
VERIFY( as.compare_exchange_strong(n, s) ); |
| 43 |
return 0; |
| 44 |
} |