]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Implement submdspan_mapping for layout_right_padded. [PR110352]
authorLuc Grosheintz <luc.grosheintz@gmail.com>
Mon, 8 Dec 2025 20:23:45 +0000 (21:23 +0100)
committerTomasz Kamiński <tkaminsk@redhat.com>
Tue, 9 Dec 2025 16:12:58 +0000 (17:12 +0100)
Implements submdspan for layout_right_padded as described in P3663.

PR libstdc++/110352

libstdc++-v3/ChangeLog:

* include/std/mdspan
(__mdspan::_SubMdspanMapping<_LayoutSide::__right, true>): Define.
(layout_right_padded::submdspan_mapping): New friend function.
* testsuite/23_containers/mdspan/submdspan/selections/right_padded.cc:
Instantiate tests for layout_right_padded.
* testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc:
Ditto.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
libstdc++-v3/include/std/mdspan
libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_mapping.cc

index 12413a0662fbc0dcc1396a40c04ba5c623592c8a..dc0aa4f9584fd217a5494368a997d00ac236a25c 100644 (file)
@@ -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.cc b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/selections/right_padded.cc
new file mode 100644 (file)
index 0000000..24ed6cb
--- /dev/null
@@ -0,0 +1,12 @@
+// { dg-do run { target c++26 } }
+// { dg-timeout-factor 2 }
+#include "testcases.h"
+
+int
+main()
+{
+  test_all<std::layout_right_padded<1>>();
+  test_all<std::layout_right_padded<8>>();
+  test_all<std::layout_right_padded<dyn>>();
+  return 0;
+}
index 50836968a06b816c9455d6a05a69146e0bbd747f..efd71d10f9d94ddbd17acb9aa4429a18babb17a4 100644 (file)
@@ -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;