[libstdc++/65033] Give alignment info to libatomic
Jonathan Wakely
jwakely@redhat.com
Wed Mar 25 18:49:00 GMT 2015
On 25/03/15 11:36 -0700, Richard Henderson wrote:
>On 03/25/2015 09:22 AM, Jonathan Wakely wrote:
>> private:
>> - _Tp _M_i;
>> + // Align 1/2/4/8/16-byte types the same as integer types of that size.
>> + // This matches the alignment effects of the C11 _Atomic qualifier.
>> + static constexpr int _S_alignment
>> + = sizeof(_Tp) == sizeof(char) ? alignof(char)
>> + : sizeof(_Tp) == sizeof(short) ? alignof(short)
>> + : sizeof(_Tp) == sizeof(int) ? alignof(int)
>> + : sizeof(_Tp) == sizeof(long) ? alignof(long)
>> + : sizeof(_Tp) == sizeof(long long) ? alignof(long long)
>> +#ifdef _GLIBCXX_USE_INT128
>> + : sizeof(_Tp) == sizeof(__int128) ? alignof(__int128)
>> +#endif
>> + : alignof(_Tp);
>> +
>> + alignas(_S_alignment) _Tp _M_i;
>
>
>Surely not by reducing a larger alignment applied to _Tp.
>I.e.
>
> static constexpr int _S_min_alignment
> = sizeof(_Tp) == sizeof(char) ? alignof(char)
> : sizeof(_Tp) == sizeof(short) ? alignof(short)
> : sizeof(_Tp) == sizeof(int) ? alignof(int)
> : sizeof(_Tp) == sizeof(long) ? alignof(long)
> : sizeof(_Tp) == sizeof(long long) ? alignof(long long)
>#ifdef _GLIBCXX_USE_INT128
> : sizeof(_Tp) == sizeof(__int128) ? alignof(__int128)
>#endif
> : 0;
>
> static constexpr int _S_alignment
> = _S_min_alignment > alignof(_Tp) ? _S_min_alignment : alignof(_Tp);
Doh, good catch. I'll make that change and add a test with a type that
has alignof(X) > sizeof(X).
On 25/03/15 11:39 -0700, Richard Henderson wrote:
>On 03/25/2015 09:22 AM, Jonathan Wakely wrote:
>> +static_assert( alignof(std::atomic<twoints>) > alignof(int),
>> + "std::atomic not suitably aligned" );
>
>This is only true if int64_t has alignment larger than int32_t,
>which is unfortunately not always the case.
Huh, didn't realise that. I could change the tests to check it's
alignof(std::int64_t) as the next assertion does, but is it safe to
assume that struct twoints { int a; int b; } is exactly 64 bits
everywhere?
I'd prefer not to have the test say "if sizeof(twoints) ==
sizeof(long), test this, otherwise if sizeof(twoints) == ..."
More information about the Libstdc++
mailing list