From: Luc Grosheintz Date: Mon, 29 Sep 2025 06:00:19 +0000 (+0200) Subject: libstdc++: Implement std::layout_right_padded [PR110352]. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bfd41adc6178abb55d2a019f0d7e755ee097f548;p=thirdparty%2Fgcc.git libstdc++: Implement std::layout_right_padded [PR110352]. This commit adds the right padded layout as described in N5014, with LWG4372 (dynamic padding value) and LWG4314 (move in operator()). PR libstdc++/110352 libstdc++-v3/ChangeLog: * include/std/mdspan (_RightPaddedIndices): Traits for right padded layouts. (layout_right::mapping::mapping) New overload for right padded layouts. (layout_right_padded): Add implementation. * src/c++23/std.cc.in (layout_right_padded): Add. * testsuite/23_containers/mdspan/layouts/ctors.cc: Update test for right padded layouts. * testsuite/23_containers/mdspan/layouts/empty.cc: Ditto. * testsuite/23_containers/mdspan/layouts/mapping.cc: Ditto. * testsuite/23_containers/mdspan/layouts/padded.cc: Ditto. * testsuite/23_containers/mdspan/layouts/padded_neg.cc: Ditto. * testsuite/23_containers/mdspan/layouts/padded_traits.h: Ditto. Reviewed-by: Tomasz KamiƄski Signed-off-by: Luc Grosheintz --- diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan index 7f14ca613f3..6714b19a884 100644 --- a/libstdc++-v3/include/std/mdspan +++ b/libstdc++-v3/include/std/mdspan @@ -977,6 +977,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : mapping(__other.extents(), __mdspan::__internal_ctor{}) { __glibcxx_assert(*this == __other); } +#if __glibcxx_padded_layouts + template + requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping> + && is_constructible_v + constexpr + explicit(!is_convertible_v) + mapping(const _RightPaddedMapping& __other) noexcept + : mapping(__other.extents(), __mdspan::__internal_ctor{}) + { + constexpr size_t __rank = extents_type::rank(); + constexpr size_t __ostride_sta = __mdspan::__get_static_stride< + _RightPaddedMapping>(); + + if constexpr (__rank > 1) + { + if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent + && __ostride_sta != dynamic_extent) + static_assert(extents_type::static_extent(__rank - 1) + == __ostride_sta); + else + __glibcxx_assert(__other.stride(__rank - 2) + == __other.extents().extent(__rank - 1)); + } + } +#endif + constexpr mapping& operator=(const mapping&) noexcept = default; @@ -1370,6 +1398,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __res; } + template + constexpr typename _Extents::index_type + __linear_index_rightpad(const _Extents& __exts, _Stride __stride, + _Indices... __indices) + { + // i[n-1] + stride*(i[n-2] + extents.extent(n-2])*...) + using _IndexType = typename _Extents::index_type; + _IndexType __res = 0; + if constexpr (sizeof...(__indices) > 0) + { + _IndexType __mult = 1; + array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...}; + + auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType) mutable + { + --__pos; + __res += __ind_arr[__pos] * __mult; + __mult *= __exts.extent(__pos); + }; + + auto __update = [&](_IndexType, auto... __rest) + { + __res += __ind_arr[__exts.rank() - 1]; + __mult = __stride.extent(0); + (__update_rest(__rest), ...); + }; + __update(__indices...); + } + return __res; + } + template struct _LeftPaddedLayoutTraits { @@ -1396,6 +1455,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } }; + template + struct _RightPaddedLayoutTraits + { + using _LayoutSame = layout_right; + using _LayoutOther = layout_left; + + constexpr static size_t _S_ext_idx = _Rank - 1; + constexpr static size_t _S_stride_idx = _Rank - 2; + constexpr static size_t _S_unpad_begin = 0; + constexpr static size_t _S_unpad_end = _Rank - 1; + + template + constexpr static auto _S_make_padded_extent( + extents<_IndexType, _StaticStride> __stride, + const extents<_IndexType, _Extents...>& __exts) + { + auto __impl = [&](integer_sequence) + { + return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{ + __exts.extent(_Is)..., __stride.extent(0)}; + }; + return __impl(make_index_sequence()); + } + }; + template class _PaddedStorage { @@ -1825,6 +1909,170 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept { return __self._M_storage._M_equal(__other); } }; + + template + template + class layout_right_padded<_PaddingValue>::mapping { + public: + static constexpr size_t padding_value = _PaddingValue; + using extents_type = _Extents; + using index_type = typename extents_type::index_type; + using size_type = typename extents_type::size_type; + using rank_type = typename extents_type::rank_type; + using layout_type = layout_right_padded<_PaddingValue>; + + private: + static constexpr size_t _S_rank = extents_type::rank(); + using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue, + _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>; + [[no_unique_address]] _PaddedStorage _M_storage; + + consteval friend size_t + __mdspan::__get_static_stride(); + + constexpr index_type + _M_extent(size_t __r) const noexcept + { return _M_storage._M_extents.extent(__r); } + + constexpr index_type + _M_padstride() const noexcept + { return _M_storage._M_stride.extent(0); } + + public: + constexpr + mapping() noexcept + { } + + constexpr + mapping(const mapping&) noexcept = default; + + constexpr + mapping(const extents_type& __exts) + : _M_storage(__exts) + { } + + template<__mdspan::__valid_index_type _OIndexType> + constexpr mapping(const extents_type& __exts, _OIndexType __pad) + : _M_storage(__exts, + __mdspan::__index_type_cast(std::move(__pad))) + { } + + template + requires is_constructible_v + constexpr explicit(!is_convertible_v<_OExtents, extents_type>) + mapping(const layout_right::mapping<_OExtents>& __other) + : _M_storage(__other) + { } + + template + requires is_constructible_v<_OExtents, extents_type> + constexpr explicit(_OExtents::rank() > 0) + mapping(const typename layout_stride::mapping<_OExtents>& __other) + : _M_storage(__other) + { __glibcxx_assert(*this == __other); } + + template + requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping> + && is_constructible_v + constexpr explicit(_S_rank > 1 && (padding_value != dynamic_extent + || _RightPaddedMapping::padding_value == dynamic_extent)) + mapping(const _RightPaddedMapping& __other) + : _M_storage(layout_right{}, __other) + { } + + template + requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping> + || __mdspan::__mapping_of) + && (_S_rank <= 1) + && is_constructible_v + constexpr explicit(!is_convertible_v< + typename _LeftPaddedMapping::extents_type, extents_type>) + mapping(const _LeftPaddedMapping& __other) noexcept + : _M_storage(layout_left{}, __other) + { } + + constexpr mapping& operator=(const mapping&) noexcept = default; + + constexpr const extents_type& + extents() const noexcept { return _M_storage._M_extents; } + + constexpr array + strides() const noexcept + { + array __ret; + if constexpr (_S_rank > 0) + __ret[_S_rank - 1] = 1; + if constexpr (_S_rank > 1) + __ret[_S_rank - 2] = _M_padstride(); + if constexpr (_S_rank > 2) + for(size_t __i = _S_rank - 2; __i > 0; --__i) + __ret[__i - 1] = __ret[__i] * _M_extent(__i); + return __ret; + } + + constexpr index_type + required_span_size() const noexcept + { return _M_storage._M_required_span_size(); } + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 4314. Missing move in mdspan layout mapping::operator() + template<__mdspan::__valid_index_type... _Indices> + requires (sizeof...(_Indices) == _S_rank) + constexpr index_type + operator()(_Indices... __indices) const noexcept + { + return __mdspan::__linear_index_rightpad( + extents(), _M_storage._M_stride, + static_cast(std::move(__indices))...); + } + + static constexpr bool + is_always_exhaustive() noexcept + { return _PaddedStorage::_M_is_always_exhaustive(); } + + constexpr bool + is_exhaustive() noexcept + { return _M_storage._M_is_exhaustive(); } + + static constexpr bool + is_always_unique() noexcept { return true; } + + static constexpr bool + is_always_strided() noexcept { return true; } + + static constexpr bool + is_unique() noexcept { return true; } + + static constexpr bool + is_strided() noexcept { return true; } + + constexpr index_type + stride(rank_type __r) const noexcept + { + __glibcxx_assert(__r < _S_rank); + if constexpr (_S_rank <= 1) + return 1; + else if (__r == _S_rank - 1) + return 1; + else if (__r == _S_rank - 2) + return _M_padstride(); + else + return static_cast( + static_cast(_M_padstride()) * + static_cast(__mdspan::__fwd_prod( + extents(), __r + 1, _S_rank - 1))); + } + + template + requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping> + && _RightPaddedMapping::extents_type::rank() == _S_rank) + friend constexpr bool + operator==(const mapping& __self, const _RightPaddedMapping& __other) + noexcept + { return __self._M_storage._M_equal(__other); } + }; #endif // __glibcxx_padded_layouts template diff --git a/libstdc++-v3/src/c++23/std.cc.in b/libstdc++-v3/src/c++23/std.cc.in index f10bab59e2c..c1b4e4c88b7 100644 --- a/libstdc++-v3/src/c++23/std.cc.in +++ b/libstdc++-v3/src/c++23/std.cc.in @@ -1871,9 +1871,10 @@ export namespace std using std::mdspan; #if __glibcxx_padded_layouts using std::layout_left_padded; + using std::layout_right_padded; #endif - // FIXME layout_right_padded, strided_slice, submdspan_mapping_result, - // full_extent_t, full_extent, submdspan_extents, mdsubspan + // FIXME strided_slice, submdspan_mapping_result, full_extent_t, full_extent, + // submdspan_extents, mdsubspan } #endif diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc index 8cba8094abc..27065a0dfc6 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/ctors.cc @@ -469,6 +469,7 @@ main() test_all(); #if __cplusplus > 202302L test_padded_all(); + test_padded_all(); #endif from_left_or_right::test_all(); diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc index 05188432f14..8840d07879c 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/empty.cc @@ -142,6 +142,7 @@ main() static_assert(test_all()); #if __cplusplus > 202302L static_assert(test_padded_all()); + static_assert(test_padded_all()); #endif return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc index 10ce622523d..d1486e4e11c 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/mapping.cc @@ -373,7 +373,7 @@ template<> #if __cplusplus > 202302L template - requires is_left_padded + requires is_left_padded || is_right_padded struct TestStride2D { static constexpr void @@ -457,7 +457,7 @@ template<> #if __cplusplus > 202302L template - requires is_left_padded + requires is_left_padded || is_right_padded struct TestStride3D { static constexpr void @@ -701,6 +701,7 @@ main() test_all(); #if __cplusplus > 202302L test_padded_all(); + test_padded_all(); #endif test_has_op_eq(); @@ -708,6 +709,7 @@ main() test_has_op_eq(); #if __cplusplus > 202302L test_padded_has_op_eq(); + test_padded_has_op_eq(); #endif test_has_op_eq_peculiar(); diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded.cc index d43b84ef875..cf4821a3c74 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded.cc @@ -668,6 +668,10 @@ main() test_all(); static_assert(test_all()); + test_all(); + static_assert(test_all()); + test_from_pad_all(); + test_from_pad_all(); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_neg.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_neg.cc index f0dbfe9d9c6..a758f74287f 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_neg.cc @@ -15,6 +15,7 @@ template typename Layout> return true; } static_assert(test_from_extens_representable_sta()); // { dg-error "from here" } +static_assert(test_from_extens_representable_sta()); // { dg-error "from here" } template typename Layout> constexpr bool @@ -28,6 +29,7 @@ template typename Layout> return true; } static_assert(test_from_extents_representable_padded_size()); // { dg-error "expansion of" } +static_assert(test_from_extents_representable_padded_size()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -40,6 +42,7 @@ template typename Layout> return true; } static_assert(test_from_extents_representable_stride()); // { dg-error "expansion of" } +static_assert(test_from_extents_representable_stride()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -51,6 +54,7 @@ template typename Layout> return true; } static_assert(test_from_pad_representable_stride()); // { dg-error "expansion of" } +static_assert(test_from_pad_representable_stride()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -62,6 +66,7 @@ template typename Layout> return true; } static_assert(test_from_pad_representable_padded_size()); // { dg-error "expansion of" } +static_assert(test_from_pad_representable_padded_size()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -76,6 +81,7 @@ template typename Layout> return true; } static_assert(test_from_left()); // { dg-error "required from here" } +static_assert(test_from_left()); // { dg-error "required from here" } template typename Layout> constexpr bool @@ -90,6 +96,7 @@ template typename Layout> return true; } static_assert(test_from_left_bad_runtime_stride()); // { dg-error "expansion of" } +static_assert(test_from_left_bad_runtime_stride()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -103,6 +110,7 @@ template typename Layout> return true; } static_assert(test_from_left_representable_extents()); // { dg-error "expansion of" } +static_assert(test_from_left_representable_extents()); // { dg-error "expansion of" } template typename Layout, size_t PaddingValue> constexpr bool @@ -116,6 +124,8 @@ template typename Layout, size_t PaddingValue> } static_assert(test_pad_overflow()); // { dg-error "expansion of" } static_assert(test_pad_overflow()); // { dg-error "expansion of" } +static_assert(test_pad_overflow()); // { dg-error "expansion of" } +static_assert(test_pad_overflow()); // { dg-error "expansion of" } template typename Layout, size_t PaddingValue> constexpr bool @@ -128,6 +138,8 @@ template typename Layout, size_t PaddingValue> } static_assert(test_from_pad_negative()); // { dg-error "expansion of" } static_assert(test_from_pad_negative()); // { dg-error "expansion of" } +static_assert(test_from_pad_negative()); // { dg-error "expansion of" } +static_assert(test_from_pad_negative()); // { dg-error "expansion of" } template typename Layout, size_t Pad> constexpr bool @@ -142,6 +154,8 @@ template typename Layout, size_t Pad> } static_assert(test_static_pad_same()); // { dg-error "expansion of" } static_assert(test_static_pad_same()); // { dg-error "expansion of" } +static_assert(test_static_pad_same()); // { dg-error "expansion of" } +static_assert(test_static_pad_same()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -156,6 +170,7 @@ template typename Layout> return true; } static_assert(test_from_stride_wrong_stride0()); // { dg-error "expansion of" } +static_assert(test_from_stride_wrong_stride0()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -170,6 +185,7 @@ template typename Layout> return true; } static_assert(test_from_stride_wrong_stride1()); // { dg-error "expansion of" } +static_assert(test_from_stride_wrong_stride1()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -184,6 +200,7 @@ template typename Layout> return true; } static_assert(test_from_stride_wrong_stride2()); +static_assert(test_from_stride_wrong_stride2()); template typename Layout> constexpr bool @@ -200,6 +217,7 @@ template typename Layout> return true; } static_assert(test_from_stride_oversized()); // { dg-error "expansion of" } +static_assert(test_from_stride_oversized()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -213,6 +231,7 @@ template typename Layout> return true; } static_assert(test_from_samepad_dyn()); // { dg-error "expansion of" } +static_assert(test_from_samepad_dyn()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -226,6 +245,7 @@ template typename Layout> return true; } static_assert(test_from_samepad_sta()); // { dg-error "expansion of" } +static_assert(test_from_samepad_sta()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -240,6 +260,7 @@ template typename Layout> return true; } static_assert(test_from_samepad_oversized()); // { dg-error "expansion of" } +static_assert(test_from_samepad_oversized()); // { dg-error "expansion of" } template typename Layout, size_t RunId> constexpr bool @@ -278,6 +299,10 @@ static_assert(test_to_same_not_exhaustive()); // { d static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } +static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } +static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } +static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } +static_assert(test_to_same_not_exhaustive()); // { dg-error "expansion of" } template typename Layout> constexpr bool @@ -290,6 +315,7 @@ template typename Layout> return true; } static_assert(test_statically_bad_padding_value1()); // { dg-error "required from" } +static_assert(test_statically_bad_padding_value1()); // { dg-error "required from" } template typename Layout> constexpr bool @@ -301,6 +327,7 @@ template typename Layout> return true; } static_assert(test_statically_bad_padding_value2()); // { dg-error "required from" } +static_assert(test_statically_bad_padding_value2()); // { dg-error "required from" } template typename Layout> constexpr bool @@ -312,6 +339,7 @@ template typename Layout> return true; } static_assert(test_statically_oversized()); // { dg-error "from here" } +static_assert(test_statically_oversized()); // { dg-error "from here" } // { dg-prune-output "padding_value must be representable as index_type" } // { dg-prune-output "non-constant condition for static assertion" } diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_traits.h b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_traits.h index 9171d8c1ce1..788ae82fcc4 100644 --- a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_traits.h +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/padded_traits.h @@ -12,15 +12,34 @@ template = true; #endif +template + constexpr static bool is_right_padded = false; + +#if __cplusplus > 202302L +template + constexpr static bool is_right_padded> + = true; +#endif + template constexpr bool - is_padded_layout = is_left_padded; + is_padded_layout = is_left_padded || is_right_padded; #if __cplusplus > 202302L +template + constexpr auto + dynamic_extents_array(const Extents& exts) + { + std::array ret; + for(size_t i = 0; i < Extents::rank(); ++i) + ret[i] = exts.extent(i); + return ret; + } enum class PaddingSide { - Left + Left, + Right }; struct DeducePaddingSide @@ -28,12 +47,22 @@ struct DeducePaddingSide template typename Layout> constexpr static PaddingSide from_template() - { return PaddingSide::Left; } + { + if constexpr (std::same_as, std::layout_left_padded<0>>) + return PaddingSide::Left; + else + return PaddingSide::Right; + } template constexpr static PaddingSide from_typename() - { return PaddingSide::Left; } + { + if constexpr (is_left_padded) + return PaddingSide::Left; + else + return PaddingSide::Right; + } }; template @@ -69,5 +98,52 @@ template<> { return exts.extent(0); } }; +template<> + struct LayoutTraits + { + using layout_same = std::layout_right; + using layout_other = std::layout_left; + + template + constexpr static auto + make_extents(const std::extents& exts) + { + constexpr size_t rank = sizeof...(Extents); + auto impl = [&](std::index_sequence) + { + auto dyn_exts = make_array(dynamic_extents_array(exts)); + return std::extents(dyn_exts); + }; + return impl(std::make_index_sequence()); + } + + template + using extents_type = decltype(make_extents(std::declval())); + + template + constexpr static std::array + make_array(std::array a) + { + std::ranges::reverse(a); + return a; + } + + template + constexpr static auto + padded_stride(const Mapping& m) + { + auto rank = Mapping::extents_type::rank(); + return m.stride(rank - 2); + } + + template + constexpr static auto + padded_extent(const Extents& exts) + { + auto rank = Extents::rank(); + return exts.extent(rank - 1); + } + }; + #endif #endif