On Thu, Dec 4, 2025 at 10:31 PM Luc Grosheintz <[email protected]>
wrote:

> 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 <[email protected]>
> ---
>
This really shows how flexible the approach is, it is very easy to add
mappings.
Just repetition of on static assert for __mdspan::__acceptable_slice_type,
and separating
test file.


>  libstdc++-v3/include/std/mdspan               | 39 +++++++++++++++++++
>  .../mdspan/submdspan/submdspan.cc             |  1 +
>  .../mdspan/submdspan/submdspan_mapping.cc     |  6 +++
>  .../mdspan/submdspan/submdspan_neg.cc         |  9 +++++
>  4 files changed, 55 insertions(+)
>
> diff --git a/libstdc++-v3/include/std/mdspan
> b/libstdc++-v3/include/std/mdspan
> index 54739c0008d..d8da7b41868 100644
> --- a/libstdc++-v3/include/std/mdspan
> +++ b/libstdc++-v3/include/std/mdspan
> @@ -1284,40 +1284,71 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         template<size_t _Pad> using _PaddedLayout =
> layout_left_padded<_Pad>;
>
>         template<typename _Mapping, size_t _Us>
>           static consteval size_t
>           _S_pad()
>           {
>             using _Extents = typename _Mapping::extents_type;
>             constexpr auto __sta_exts =
> __mdspan::__static_extents<_Extents>(0, _Us);
>             if constexpr (!__mdspan::__all_static(__sta_exts))
>               return dynamic_extent;
>             else
>               return __mdspan::__fwd_prod(__sta_exts);
>           }
>
>         template<size_t _SubRank, size_t _Nm>
>           static consteval bool
>           _S_is_unpadded_submdspan(span<const _SliceKind, _Nm>
> __slice_kinds)
>           { return __mdspan::__is_block<_SubRank>(__slice_kinds); }
>        };
>
> +    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
> +             = __mdspan::__static_extents<_Extents>(_Us + 1, __rank);
> +           if constexpr (!__mdspan::__all_static(__sta_exts))
> +             return dynamic_extent;
> +           else
> +             return __fwd_prod(__sta_exts);
> +         }
> +
> +       template<size_t _SubRank, size_t _Nm>
> +         static consteval bool
> +         _S_is_unpadded_submdspan(span<const _SliceKind, _Nm>
> __slice_kinds)
> +         {
> +           auto __rev_slice_kinds = array<_SliceKind, _Nm>{};
> +           for(size_t __i = 0; __i < _Nm; ++__i)
> +             __rev_slice_kinds[__i] = __slice_kinds[_Nm - 1 - __i];
> +           return __mdspan::__is_block<_SubRank>(span(__rev_slice_kinds));
> +         }
> +      };
> +
>      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)
>        {
>         constexpr auto __side = __mdspan::__mapping_side<_Mapping>();
>         using _Trait = _SubMdspanMapping<__side>;
>
>         constexpr auto __slice_kinds =
> __mdspan::__make_slice_kind_array<_Slices...>();
>         auto __offset = __mdspan::__suboffset(__mapping, __slices...);
>         auto __sub_exts = __mdspan::__subextents(__mapping.extents(),
> __slices...);
>         using _SubExts = decltype(__sub_exts);
>         constexpr auto __sub_rank = _SubExts::rank();
>         if constexpr (__sub_rank == 0)
>           return submdspan_mapping_result{
> @@ -1656,40 +1687,48 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        }
>
>        template<typename _OExtents>
>         requires (extents_type::rank() == _OExtents::rank())
>         friend constexpr bool
>         operator==(const mapping& __self, const mapping<_OExtents>&
> __other)
>         noexcept
>         { return __self.extents() == __other.extents(); }
>
>      private:
>        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_submdspan
> +      template<__mdspan::__acceptable_slice_type<index_type>... _Slices>
>
This will turn into static_assert inside  __submdspan_mapping_impl.

> +       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>;
>         { _Mp::is_always_exhaustive() } -> same_as<bool>;
>         { _Mp::is_always_unique() } -> same_as<bool>;
>         bool_constant<_Mp::is_always_strided()>::value;
>         bool_constant<_Mp::is_always_exhaustive()>::value;
>         bool_constant<_Mp::is_always_unique()>::value;
>        };
>
>      template<typename _Mapping, typename... _Indices>
>        constexpr typename _Mapping::index_type
>        __linear_index_strides(const _Mapping& __m, _Indices... __indices)
> 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
> @@ -348,22 +348,23 @@ template<typename Layout>
>      };
>
>      run(std::extents(3, 5, 7));
>      run(std::extents<int, 3, 5, 7>{});
>      return true;
>    }
>
>  template<typename Layout>
>    constexpr bool
>    test_all()
>    {
>      test_all_cheap<Layout>();
>      test_all_expensive<Layout>();
>      return true;
>    }
>
>  int
>  main()
>  {
>    test_all<std::layout_left>();
> +  test_all<std::layout_right>();
>
Comment about having this main in separate file for right apply here.

>    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
> @@ -112,25 +112,31 @@ template<typename Layout>
>        auto m = typename Layout::mapping(Traits::make_extents(exts));
>        auto slices = std::tuple{s0, size_t(0), all, s3, size_t(0)};
>        auto result = call_submdspan_mapping(m, Traits::make_tuple(slices));
>        auto padding_value = decltype(result.mapping)::padding_value;
>        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;
>    }
>
>  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 cdc8a2b7e23..5d977822dfe 100644
> ---
> a/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
> +++
> b/libstdc++-v3/testsuite/23_containers/mdspan/submdspan/submdspan_neg.cc
> @@ -7,112 +7,121 @@ template<typename Layout, typename... Slices>
>    constexpr bool
>    check_slice_range(Slices... slices)
>    {
>      auto m = typename Layout::mapping<std::extents<int, 3, 5, 7>>{};
>      auto storage = std::vector<double>(m.required_span_size());
>      auto md = std::mdspan(storage.data(), m);
>
>      auto submd = submdspan(md, slices...);           // { dg-error
> "expansion of" }
>      (void) submd;
>      return true;
>    }
>
>  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" }
>
>  namespace adl
>  {
>    struct NoFull
>    {
>      template<typename Extents>
>        class mapping
>        {
>        public:
>         using extents_type = Extents;
>         using index_type = typename extents_type::index_type;
>
>        private:
>         friend constexpr auto
>         submdspan_mapping(mapping, int)
>         { return std::submdspan_mapping_result{mapping{}, 0}; }
>        };
>    };
>
>    struct WrongReturnValue
> --
> 2.52.0
>
>

Reply via email to