[PATCH v1 6/6] libstdc++: Implement submdspan_mapping for layout_stride. [PR110352]

Luc Grosheintz luc.grosheintz@gmail.com
Fri Nov 14 14:09:42 GMT 2025


Add submdspan_mapping for layout_stride as in P3663.

	PR libstdc++/110352

libstdc++-v3/ChangeLog:

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

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

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index b38485ddd3d..35e46b94b27 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -1969,20 +1969,45 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  if (__self.extents() != __other.extents())
 	    return false;
 	  if constexpr (extents_type::rank() > 0)
 	    for (size_t __i = 0; __i < extents_type::rank(); ++__i)
 	      if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
 		return false;
 	  return __mdspan::__offset(__other) == 0;
 	}
 
     private:
+#if __glibcxx_submdspan
+      constexpr auto
+      _M_submdspan_mapping_impl() const
+      { return submdspan_mapping_result{*this, 0}; }
+
+      template<typename... _Slices>
+	requires (sizeof...(_Slices) > 0)
+	constexpr auto
+	_M_submdspan_mapping_impl(_Slices... __slices) const
+	{
+	  auto __offset = __mdspan::__suboffset(*this, __slices...);
+	  auto __sub_exts = submdspan_extents(extents(), __slices...);
+	  auto __sub_strides
+	    = __mdspan::__substrides<decltype(__sub_exts)>(*this, __slices...);
+	  return submdspan_mapping_result{
+	    layout_stride::mapping(__sub_exts, __sub_strides), __offset};
+	}
+
+      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 __mapping._M_submdspan_mapping_impl(__slices...); }
+#endif
+
       using _Strides = typename __array_traits<index_type,
 					       extents_type::rank()>::_Type;
       [[no_unique_address]] extents_type _M_extents;
       [[no_unique_address]] _Strides _M_strides;
     };
 
 #ifdef __glibcxx_padded_layouts
   namespace __mdspan
   {
     constexpr size_t
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
index cd6e9454b17..645c4711294 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan.cc
@@ -359,12 +359,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>();
+  test_all<std::layout_stride>();
   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 cc832cdb415..cf6167dc3b0 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc
@@ -116,27 +116,45 @@ template<typename Layout>
       VERIFY(padding_value == expected);
     };
 
     check(std::extents(std::cw<3>, std::cw<5>, std::cw<7>, 11, 13), 3*5);
     check(std::extents(std::cw<3>, std::cw<5>, 7, 11, 13), 3*5);
     check(std::extents(std::cw<3>, 5, 7, 11, 13), dyn);
     check(std::extents(3, 5, 7, 11, 13), dyn);
     return true;
   }
 
+constexpr bool
+test_layout_stride_return_types()
+{
+  auto exts = std::extents(3, 5);
+  auto m = std::layout_stride::mapping(exts, std::array{2, 12});
+
+  using index_type = decltype(exts)::index_type;
+  auto s1 = std::strided_slice{index_type(2), index_type(2),
+			       std::cw<index_type(2)>};
+  auto result = submdspan_mapping(m, index_type(1), s1);
+  using layout_type = decltype(result.mapping)::layout_type;
+  static_assert(std::same_as<layout_type, std::layout_stride>);
+  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_stride_return_types();
+  static_assert(test_layout_stride_return_types());
+
   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 3d6b0191e17..59688b2330c 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
@@ -18,94 +18,103 @@ 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" }
+static_assert(test_int_under<std::layout_stride>()); // { 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" }
+static_assert(test_int_over<std::layout_stride>());  // { 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" }
+static_assert(test_tuple_under<std::layout_stride>());   // { 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" }
+static_assert(test_tuple_reversed<std::layout_stride>()); // { 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" }
+static_assert(test_tuple_over<std::layout_stride>()); // { 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" }
+static_assert(test_strided_slice_zero<std::layout_stride>()); // { 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" }
+static_assert(test_strided_slice_offset_under<std::layout_stride>()); // { 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" }
+static_assert(test_strided_slice_offset_over<std::layout_stride>()); // { 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" }
+static_assert(test_strided_slice_extent_over<std::layout_stride>()); // { 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