[PATCH v2 08/10] libstdc++: Implement submdspan_mapping for layout_right_padded. [PR110352]

Luc Grosheintz luc.grosheintz@gmail.com
Tue Nov 18 14:24:59 GMT 2025


Implements submdspan for layout_right_padded as described in P3663.

	PR libstdc++/110352

libstdc++-v3/ChangeLog:

	* include/std/mdspan (layout_right_padded::submdspan_mapping):
	New friend method.
	* testsuite/23_containers/mdspan/submdspan/submdspan.cc:
	Instantiate test for layout_right_padded.
	* testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc:
	Ditto.

Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
---
 libstdc++-v3/include/std/mdspan               | 43 +++++++++++++++++++
 .../mdspan/submdspan/submdspan.cc             |  7 ++-
 .../mdspan/submdspan/submdspan_mapping.cc     |  4 ++
 3 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index 154213e02a9..15052452425 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -1429,20 +1429,54 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	    constexpr auto __rank = sizeof...(_Slices);
 	    auto __impl = [&]<size_t... _Is>(index_sequence<_Is...>)
 	    {
 	      return __is_compact_block<
 		_IndexType, _SubRank, _Slices...[__rank - 1 - _Is]...>();
 	    };
 	    return __impl(make_index_sequence<__rank>());
 	  }
       };
 
+    template<>
+      struct _SubMdspanMapping<_LayoutSide::__right, true>
+      {
+	using _Layout = layout_right;
+	template<size_t _Pad> using _PaddedLayout = layout_right_padded<_Pad>;
+
+	template<typename _Mapping, size_t _Us>
+	  static consteval size_t
+	  _S_pad()
+	  {
+	    using _Extents = typename _Mapping::extents_type;
+	    constexpr auto __rank = _Extents::rank();
+	    constexpr auto __sta_exts = __static_extents<_Extents>(_Us + 1, __rank - 1);
+	    constexpr auto __sta_padstride = __get_static_stride<_Mapping>();
+	    if constexpr (__sta_padstride == dynamic_extent
+			  || !__all_static(__sta_exts))
+	      return dynamic_extent;
+	    else
+	      return __sta_padstride * __fwd_prod(__sta_exts);
+	  }
+
+	template<typename _IndexType, size_t _SubRank, typename... _Slices>
+	  static consteval bool
+	  _S_is_unpadded_submdspan()
+	  {
+	    constexpr auto __rank = sizeof...(_Slices);
+	    if constexpr (_SubRank == 1)
+	      return __is_unit_stride_slice<_Slices...[__rank - 1], _IndexType>;
+	    else
+	      return false;
+	  }
+      };
+
+
     template<typename _Mapping>
       constexpr auto
       __submdspan_mapping_impl(const _Mapping& __mapping)
       { return submdspan_mapping_result{__mapping, 0}; }
 
     template<typename _Mapping, typename... _Slices>
       requires (sizeof...(_Slices) > 0)
       constexpr auto
       __submdspan_mapping_impl(const _Mapping& __mapping, _Slices... __slices)
       {
@@ -2822,20 +2856,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		  extents(), __r + 1, _S_rank - 1)));
 	}
 
 	template<typename _RightPaddedMapping>
 	  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); }
+
+#if __glibcxx_submdspan
+      private:
+	template<__mdspan::__valid_canonical_slice_type<index_type>... _Slices>
+	  requires (extents_type::rank() == sizeof...(_Slices))
+	  friend constexpr auto
+	  submdspan_mapping(const mapping& __mapping, _Slices... __slices)
+	  { return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
+#endif // __glibcxx_submdspan
       };
 #endif // __glibcxx_padded_layouts
 
   template<typename _ElementType>
     struct default_accessor
     {
       static_assert(!is_array_v<_ElementType>,
 	"ElementType must not be an array type");
       static_assert(!is_abstract_v<_ElementType>,
 	"ElementType must not be an abstract class type");
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
index 927b5d06c6b..f8382847fe3 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
@@ -358,17 +358,20 @@ template<typename Layout>
   {
     test_all_cheap<Layout>();
     test_all_expensive<Layout>();
     return true;
   }
 
 int
 main()
 {
   test_all<std::layout_left>();
+  test_all<std::layout_right>();
+  test_all<std::layout_stride>();
   test_all<std::layout_left_padded<1>>();
   test_all<std::layout_left_padded<8>>();
   test_all<std::layout_left_padded<dyn>>();
-  test_all<std::layout_right>();
-  test_all<std::layout_stride>();
+  test_all<std::layout_right_padded<1>>();
+  test_all<std::layout_right_padded<8>>();
+  test_all<std::layout_right_padded<dyn>>();
   return 0;
 }
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
index 50836968a06..efd71d10f9d 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
@@ -283,15 +283,19 @@ template<typename Layout>
 int
 main()
 {
   test_return_types_unpadded_all<std::layout_left>();
   test_return_types_unpadded_all<std::layout_right>();
 
   test_return_types_padded_all<std::layout_left_padded<1>>();
   test_return_types_padded_all<std::layout_left_padded<2>>();
   test_return_types_padded_all<std::layout_left_padded<dyn>>();
 
+  test_return_types_padded_all<std::layout_right_padded<1>>();
+  test_return_types_padded_all<std::layout_right_padded<2>>();
+  test_return_types_padded_all<std::layout_right_padded<dyn>>();
+
   test_layout_stride_return_types();
   static_assert(test_layout_stride_return_types());
   return 0;
 }
 
-- 
2.51.2



More information about the Libstdc++ mailing list