On 11/07/19 20:45 +0100, Jonathan Wakely wrote:
This adds the new atomic types from C++2a, as proposed by P0019 and
P0020. To reduce duplication the calls to the compiler's atomic
built-ins are wrapped in new functions in the __atomic_impl namespace.
These functions are currently only used by std::atomic<floating-point>
and std::atomic_ref but could also be used for all other specializations
of std::atomic.
* include/bits/atomic_base.h (__atomic_impl): New namespace for
wrappers around atomic built-ins.
(__atomic_float, __atomic_ref): New class templates for use as base
classes.
* include/std/atomic (atomic<float>, atomic<double>)
(atomic<long double>): New explicit specializations.
(atomic_ref): New class template.
(__cpp_lib_atomic_ref): Define.
* include/std/version (__cpp_lib_atomic_ref): Define.
* testsuite/29_atomics/atomic/60695.cc: Adjust dg-error.
* testsuite/29_atomics/atomic_float/1.cc: New test.
* testsuite/29_atomics/atomic_float/requirements.cc: New test.
* testsuite/29_atomics/atomic_ref/deduction.cc: New test.
* testsuite/29_atomics/atomic_ref/float.cc: New test.
* testsuite/29_atomics/atomic_ref/generic.cc: New test.
* testsuite/29_atomics/atomic_ref/integral.cc: New test.
* testsuite/29_atomics/atomic_ref/pointer.cc: New test.
* testsuite/29_atomics/atomic_ref/requirements.cc: New test.
Testted x86_64-linux, committed to trunk.
I forgot to mention a couple of things about this patch.
For std::atomic<floating-point> and std::atomic_ref<floating-point>
I'm requiring the FP object to be aligned to __alignof__(T), not
alignof(T), which means that for IA-32 atomic<double> has stricter
alignment than a plain double (as is already the case for long long on
IA-32). This matches Clang's treatment of _Atomic double, but not
GCC's treatment of _Atomic double. I've tried to get the x86 psABI
group to specify the required alignment for atomics but there's still
no decision:
https://groups.google.com/forum/#!topic/ia32-abi/Tlu6Hs-ohPY
I'm more concerned about compatibility between our std::atomic and
libc++'s std::atomic (which is going to use Clang's _Atomic semantics)
than I am about compatibility between our std::atomic and GCC's C
_Atomic. That's why I decided to aim for compatibility with Clang not
GCC. Also, I think GCC gets alignment wrong for C _Atomic :-(
The C front-end supports _Atomic floating-point types, and seems to do
the right thing (including calling __atomic_feraiseexcept from
libatomic) but we can't use _Atomic in C++. It might make sense to
teach the C++ front-end to support _Atomic (maybe as an attribute, or
just using the _Atomic keyword directly as Clang++ does). That way the
C++ library wouldn't have to try and emulate what _Atomic does with
flakey template metaprogramming. But in the absence of compiler
support, atomic arithmetic on floating-point objects is implemented as
a relaxed atomic load, followed by non-atomic addition/subtraction on
a local variable, followed by a CAS loop to update the atomic object.