[PATCH v2 05/10] libstdc++: Implement submdspan_mapping for layout_right. [PR110352]

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


Adds submdspan_mapping for layout_right as described in P3663.

	PR libstdc++/110352

libstdc++-v3/ChangeLog:

	* include/std/mdspan (layout_right::mapping::submdspan_mapping): New
	friend function.
	* testsuite/23_containers/mdspan/submdspan/submdspan.cc:
	Instantiate tests for layout_right.
	* testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc:
	Ditto.
	* testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc:
	Ditto.

Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
---
 libstdc++-v3/include/std/mdspan               | 56 +++++++++++++++++++
 .../mdspan/submdspan/submdspan.cc             |  1 +
 .../mdspan/submdspan/submdspan_mapping.cc     |  6 ++
 .../mdspan/submdspan/submdspan_neg.cc         |  9 +++
 4 files changed, 72 insertions(+)

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index 712826ea7e7..594a92640a4 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -1317,20 +1317,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<_LayoutSide _Side, typename _IndexType, size_t _SubRank,
              typename... _Slices>
       static consteval size_t
       __padded_block_begin()
       {
 	if constexpr (_Side == _LayoutSide::__left)
 	  return __padded_block_begin_generic<_IndexType, _SubRank,
 					      _Slices...>();
+	else
+	  {
+	    constexpr auto __rank = sizeof...(_Slices);
+	    auto __impl = [&]<size_t... _Is>(index_sequence<_Is...>)
+	    {
+	      auto __u = __padded_block_begin_generic<
+		_IndexType, _SubRank, _Slices...[__rank - 1 - _Is]...>();
+	      if (__u == dynamic_extent)
+		return dynamic_extent;
+	      else
+		return __rank - 1 - __u;
+	    };
+	    return __impl(make_index_sequence<__rank>());
+	  }
       }
 
     template<_LayoutSide _Side>
       struct _SubMdspanMapping;
 
     template<>
       struct _SubMdspanMapping<_LayoutSide::__left>
       {
 	using _Layout = layout_left;
 	template<size_t _Pad> using _PaddedLayout = layout_left_padded<_Pad>;
@@ -1346,20 +1360,54 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	    else
 	      return __fwd_prod(__sta_exts);
 	  }
 
 	template<typename _IndexType, size_t _SubRank, typename... _Slices>
 	  static consteval bool
 	  _S_is_unpadded_submdspan()
 	  { return __is_compact_block<_IndexType, _SubRank, _Slices...>(); }
       };
 
+    template<>
+      struct _SubMdspanMapping<_LayoutSide::__right>
+      {
+	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);
+	    if constexpr (!__all_static(__sta_exts))
+	      return dynamic_extent;
+	    else
+	      return __fwd_prod(__sta_exts);
+	  }
+
+	template<typename _IndexType, size_t _SubRank, typename... _Slices>
+	  static consteval bool
+	  _S_is_unpadded_submdspan()
+	  {
+	    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<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)
       {
@@ -1716,20 +1764,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       template<typename _OExtents>
 	constexpr explicit
 	mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
 	: _M_extents(__oexts)
 	{
 	  static_assert(__mdspan::__representable_size<_OExtents, index_type>,
 	    "The size of OtherExtents must be representable as index_type");
 	  __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
 	}
 
+#if __glibcxx_padded_layouts
+      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
+
 	[[no_unique_address]] extents_type _M_extents{};
     };
 
   namespace __mdspan
   {
     template<typename _Mp>
       concept __mapping_alike = requires
       {
 	requires __is_extents<typename _Mp::extents_type>;
 	{ _Mp::is_always_strided() } -> same_as<bool>;
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
index 53e91407a9c..cd6e9454b17 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
@@ -358,12 +358,13 @@ 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>();
   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 a37d3cd588f..cc832cdb415 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
@@ -122,15 +122,21 @@ template<typename Layout>
     check(std::extents(3, 5, 7, 11, 13), dyn);
     return true;
   }
 
 int
 main()
 {
   test_layout_unpadded_return_types<std::layout_left>();
   static_assert(test_layout_unpadded_return_types<std::layout_left>());
 
+  test_layout_unpadded_return_types<std::layout_right>();
+  static_assert(test_layout_unpadded_return_types<std::layout_right>());
+
   test_layout_unpadded_padding_value<std::layout_left>();
   static_assert(test_layout_unpadded_padding_value<std::layout_left>());
+
+  test_layout_unpadded_padding_value<std::layout_right>();
+  static_assert(test_layout_unpadded_padding_value<std::layout_right>());
   return 0;
 }
 
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
index 4f9aad81cb7..3d6b0191e17 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
@@ -17,86 +17,95 @@ template<typename Layout, typename... Slices>
   }
 
 template<typename Layout>
   constexpr bool
   test_int_under()
   {
     check_slice_range<Layout>(1, -1, 2);             // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_int_under<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_int_under<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_int_over()
   {
     check_slice_range<Layout>(1, 5, 2);              // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_int_over<std::layout_left>());    // { dg-error "expansion of" }
+static_assert(test_int_over<std::layout_right>());   // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_tuple_under()
   {
     check_slice_range<Layout>(1, std::tuple{-1, 2}, 2);  // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_tuple_under<std::layout_left>());     // { dg-error "expansion of" }
+static_assert(test_tuple_under<std::layout_right>());    // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_tuple_reversed()
   {
     check_slice_range<Layout>(1, std::tuple{3, 2}, 2);   // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_tuple_reversed<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_tuple_reversed<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_tuple_over()
   {
     check_slice_range<Layout>(1, std::tuple{0, 6}, 2); // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_tuple_over<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_tuple_over<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_strided_slice_zero()
   {
     check_slice_range<Layout>(1, std::strided_slice{1, 1, 0}, 2);  // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_strided_slice_zero<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_strided_slice_zero<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_strided_slice_offset_under()
   {
     check_slice_range<Layout>(1, std::strided_slice{-1, 1, 1}, 2);   // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_strided_slice_offset_under<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_strided_slice_offset_under<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_strided_slice_offset_over()
   {
     check_slice_range<Layout>(1, std::strided_slice{6, 0, 1}, 2);    // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_strided_slice_offset_over<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_strided_slice_offset_over<std::layout_right>());  // { dg-error "expansion of" }
 
 template<typename Layout>
   constexpr bool
   test_strided_slice_extent_over()
   {
     check_slice_range<Layout>(1, std::strided_slice{1, 5, 1}, 2);    // { dg-error "expansion of" }
     return true;
   }
 static_assert(test_strided_slice_extent_over<std::layout_left>());   // { dg-error "expansion of" }
+static_assert(test_strided_slice_extent_over<std::layout_right>());  // { dg-error "expansion of" }
 
 // { dg-prune-output "static assertion failed" }
 // { dg-prune-output "__glibcxx_assert_fail" }
 // { dg-prune-output "non-constant condition" }
-- 
2.51.2



More information about the Libstdc++ mailing list