mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libstdc++: Add missing clear_padding in __atomic_float constructor
For 80-bit long double we need to clear the padding bits on construction. libstdc++-v3/ChangeLog: * include/bits/atomic_base.h (__atomic_float::__atomic_float(Fp)): Clear padding. * testsuite/29_atomics/atomic_float/compare_exchange_padding.cc: New test. Signed-off-by: xndcn <xndchn@gmail.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
libstdc++-v3/testsuite/29_atomics/atomic_float/compare_exchange_padding.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// { dg-do run { target c++20 } } | ||
// { dg-options "-O0" } | ||
// { dg-additional-options "[atomic_link_flags [get_multilibs]] -latomic" } | ||
|
||
#include <atomic> | ||
#include <cstring> | ||
#include <limits> | ||
#include <testsuite_hooks.h> | ||
|
||
template<typename T> | ||
void __attribute__((noinline,noipa)) | ||
fill_padding(T& f) | ||
{ | ||
T mask; | ||
std::memset(&mask, 0xff, sizeof(T)); | ||
__builtin_clear_padding(&mask); | ||
unsigned char* ptr_f = (unsigned char*)&f; | ||
unsigned char* ptr_mask = (unsigned char*)&mask; | ||
for (unsigned i = 0; i < sizeof(T); i++) | ||
{ | ||
if (ptr_mask[i] == 0x00) | ||
{ | ||
ptr_f[i] = 0xff; | ||
} | ||
} | ||
} | ||
|
||
void | ||
test01() | ||
{ | ||
// test for long double with padding (float80) | ||
if constexpr (std::numeric_limits<long double>::digits == 64) | ||
{ | ||
long double f = 0.5f; // long double has padding bits on x86 | ||
fill_padding(f); | ||
std::atomic<long double> as{ f }; // padding cleared on constructor | ||
long double t = 1.5; | ||
|
||
as.fetch_add(t); | ||
long double s = f + t; | ||
t = as.load(); | ||
VERIFY(s == t); // padding ignored on comparison | ||
fill_padding(s); | ||
VERIFY(as.compare_exchange_weak(s, f)); // padding cleared on cmpexchg | ||
fill_padding(f); | ||
VERIFY(as.compare_exchange_strong(f, t)); // padding cleared on cmpexchg | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
test01(); | ||
} |