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/selections/right_padded_1.cc:
        Instantiate tests for layout_right_padded.
        * testsuite/23_containers/mdspan/submdspan/selections/right_padded_8.cc:
        Ditto.
        * 
testsuite/23_containers/mdspan/submdspan/selections/right_padded_dyn.cc:
        Ditto.
        * testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc:
        Ditto.

Signed-off-by: Luc Grosheintz <[email protected]>
---
 libstdc++-v3/include/std/mdspan               | 46 +++++++++++++++++++
 .../submdspan/selections/right_padded_1.cc    |  9 ++++
 .../submdspan/selections/right_padded_8.cc    |  9 ++++
 .../submdspan/selections/right_padded_dyn.cc  |  9 ++++
 .../mdspan/submdspan/submdspan_mapping.cc     |  4 ++
 5 files changed, 77 insertions(+)
 create mode 100644 
libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_1.cc
 create mode 100644 
libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_8.cc
 create mode 100644 
libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_dyn.cc

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index 520cf322d8c..b9d993e47d6 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -1374,6 +1374,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
       };
 
+    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
+             = __mdspan::__static_extents<_Extents>(_Us + 1, __rank - 1);
+           constexpr auto __sta_padstride
+             = __mdspan::__get_static_stride<_Mapping>();
+           if constexpr (__sta_padstride == dynamic_extent
+                         || !__mdspan::__all_static(__sta_exts))
+             return dynamic_extent;
+           else
+             return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
+         }
+
+       template<size_t _Nm>
+         static consteval bool
+         _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
+                                  size_t __sub_rank)
+         {
+           if (__sub_rank == 1)
+             return __slice_kinds[_Nm - 1] == _SliceKind::__unit_strided_slice
+               || __slice_kinds[_Nm - 1] == _SliceKind::__full;
+           else
+             return false;
+         }
+      };
+
+
     template<typename _Mapping>
       constexpr auto
       __submdspan_mapping_impl(const _Mapping& __mapping)
@@ -2775,6 +2812,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          operator==(const mapping& __self, const _RightPaddedMapping& __other)
          noexcept
          { return __self._M_storage._M_equal(__other); }
+
+#if __glibcxx_submdspan
+      private:
+       template<typename... _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
 
diff --git 
a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_1.cc
 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_1.cc
new file mode 100644
index 00000000000..af18f6ba5be
--- /dev/null
+++ 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_1.cc
@@ -0,0 +1,9 @@
+// { dg-do run { target c++26 } }
+#include "testcases.h"
+
+int
+main()
+{
+  test_all<std::layout_right_padded<1>>();
+  return 0;
+}
diff --git 
a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_8.cc
 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_8.cc
new file mode 100644
index 00000000000..cbe5a6cd645
--- /dev/null
+++ 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_8.cc
@@ -0,0 +1,9 @@
+// { dg-do run { target c++26 } }
+#include "testcases.h"
+
+int
+main()
+{
+  test_all<std::layout_right_padded<8>>();
+  return 0;
+}
diff --git 
a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_dyn.cc
 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_dyn.cc
new file mode 100644
index 00000000000..fd65b48a185
--- /dev/null
+++ 
b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded_dyn.cc
@@ -0,0 +1,9 @@
+// { dg-do run { target c++26 } }
+#include "testcases.h"
+
+int
+main()
+{
+  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
@@ -290,6 +290,10 @@ main()
   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.52.0

Reply via email to