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>
This commit is contained in:
parent
38958ac987
commit
0adc8c5f14
2 changed files with 54 additions and 1 deletions
|
@ -1283,7 +1283,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|||
|
||||
constexpr
|
||||
__atomic_float(_Fp __t) : _M_fp(__t)
|
||||
{ }
|
||||
{ __atomic_impl::__clear_padding(_M_fp); }
|
||||
|
||||
__atomic_float(const __atomic_float&) = delete;
|
||||
__atomic_float& operator=(const __atomic_float&) = delete;
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Add table
Reference in a new issue