[gcc r16-4290] libstdc++: Improve and cleanup mdspan related code.

Tomasz Kaminski tkaminsk@gcc.gnu.org
Wed Oct 8 10:31:47 GMT 2025


https://gcc.gnu.org/g:5733ecea0795b3b4f152dc33bb3aacd4ecfcf05f

commit r16-4290-g5733ecea0795b3b4f152dc33bb3aacd4ecfcf05f
Author: Luc Grosheintz <luc.grosheintz@gmail.com>
Date:   Tue Sep 30 12:55:18 2025 +0200

    libstdc++: Improve and cleanup mdspan related code.
    
    The improvement is that in __index_type_cast, we don't need to check at
    runtime if we know that _IndexType is smaller than _OIndexType.
    
    The cleanup is whitespace (overlength lines) in <mdspan>, grouping is_always_foo
    and is_foo together, and de-uglifying a variable in test code.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/mdspan (__mdspan::__index_type_cast): Optimize by
            skipping a __glibcxx_assert if it's know at compile-time.
            (std::layout_left_padded, std::layout_righ_padded): Reorder
            is_always_strided and is_unique member functions.
            * testsuite/23_containers/mdspan/int_like.h: Rename _M_i to
            value.
    
    Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
    Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
    Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>

Diff:
---
 libstdc++-v3/include/std/mdspan                    | 42 +++++++++++++---------
 .../testsuite/23_containers/mdspan/int_like.h      | 12 +++----
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index 6714b19a8843..8d2421819a55 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -77,11 +77,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
 	if constexpr (std::is_integral_v<_OIndexType>)
 	  {
-	    __glibcxx_assert(cmp_less_equal(__other,
-		__gnu_cxx::__int_traits<_IndexType>::__max));
+	    constexpr _IndexType __index_type_max
+	      = __gnu_cxx::__int_traits<_IndexType>::__max;
+	    constexpr _OIndexType __oindex_type_max
+	      = __gnu_cxx::__int_traits<_OIndexType>::__max;
+
+	    if constexpr (__index_type_max < __oindex_type_max)
+	      __glibcxx_assert(cmp_less_equal(__other, __index_type_max));
+
 	    if constexpr (std::is_signed_v<_OIndexType>)
 	      __glibcxx_assert(__other >= 0);
-	    return std::move(__other);
+	    return static_cast<_IndexType>(__other);
 	  }
 	else
 	  {
@@ -821,8 +827,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	mapping(const _LeftpadMapping& __other) noexcept
 	: mapping(__other.extents(), __mdspan::__internal_ctor{})
 	{
-	  constexpr size_t __ostride_sta = __mdspan::__get_static_stride<
-	    _LeftpadMapping>();
+	  constexpr size_t __ostride_sta
+	    = __mdspan::__get_static_stride<_LeftpadMapping>();
 
 	  if constexpr (extents_type::rank() > 1)
 	    {
@@ -981,7 +987,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       template<typename _RightPaddedMapping>
 	requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
 		 && is_constructible_v<extents_type,
-				       typename _RightPaddedMapping::extents_type>
+		      typename _RightPaddedMapping::extents_type>
 	constexpr
 	explicit(!is_convertible_v<typename _RightPaddedMapping::extents_type,
 				   extents_type>)
@@ -989,8 +995,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	: mapping(__other.extents(), __mdspan::__internal_ctor{})
 	{
 	  constexpr size_t __rank = extents_type::rank();
-	  constexpr size_t __ostride_sta = __mdspan::__get_static_stride<
-	    _RightPaddedMapping>();
+	  constexpr size_t __ostride_sta
+	    = __mdspan::__get_static_stride<_RightPaddedMapping>();
 
 	  if constexpr (__rank > 1)
 	    {
@@ -1441,7 +1447,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	constexpr static const size_t _S_unpad_end = _Rank;
 
 	template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
-	  constexpr static auto _S_make_padded_extent(
+	  constexpr static auto
+	  _S_make_padded_extent(
 	    extents<_IndexType, _StaticStride> __stride,
 	    const extents<_IndexType, _Extents...>& __exts)
 	  {
@@ -1467,7 +1474,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	constexpr static size_t _S_unpad_end = _Rank - 1;
 
 	template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
-	  constexpr static auto _S_make_padded_extent(
+	  constexpr static auto
+	  _S_make_padded_extent(
 	    extents<_IndexType, _StaticStride> __stride,
 	    const extents<_IndexType, _Extents...>& __exts)
 	  {
@@ -1792,7 +1800,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{ }
 
 	template<__mdspan::__valid_index_type<index_type> _OIndexType>
-	  constexpr mapping(const extents_type& __exts, _OIndexType __pad)
+	  constexpr
+	  mapping(const extents_type& __exts, _OIndexType __pad)
 	  : _M_storage(__exts,
 	      __mdspan::__index_type_cast<index_type>(std::move(__pad)))
 	  { }
@@ -1881,10 +1890,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	is_always_unique() noexcept { return true; }
 
 	static constexpr bool
-	is_always_strided() noexcept { return true; }
+	is_unique() noexcept { return true; }
 
 	static constexpr bool
-	is_unique() noexcept { return true; }
+	is_always_strided() noexcept { return true; }
 
 	static constexpr bool
 	is_strided() noexcept { return true; }
@@ -1952,7 +1961,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{ }
 
 	template<__mdspan::__valid_index_type<index_type> _OIndexType>
-	  constexpr mapping(const extents_type& __exts, _OIndexType __pad)
+	  constexpr
+	  mapping(const extents_type& __exts, _OIndexType __pad)
 	  : _M_storage(__exts,
 	      __mdspan::__index_type_cast<index_type>(std::move(__pad)))
 	  { }
@@ -2040,10 +2050,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	is_always_unique() noexcept { return true; }
 
 	static constexpr bool
-	is_always_strided() noexcept { return true; }
+	is_unique() noexcept { return true; }
 
 	static constexpr bool
-	is_unique() noexcept { return true; }
+	is_always_strided() noexcept { return true; }
 
 	static constexpr bool
 	is_strided() noexcept { return true; }
diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h b/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
index 310dd8ddf207..e9172c134559 100644
--- a/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
+++ b/libstdc++-v3/testsuite/23_containers/mdspan/int_like.h
@@ -15,7 +15,7 @@ template<CustomIndexKind Kind>
   public:
     explicit
     CustomIndexType(int i)
-    : _M_i(i)
+    : value(i)
     { }
 
     CustomIndexType() = delete;
@@ -31,25 +31,25 @@ template<CustomIndexKind Kind>
     constexpr
     operator int() const noexcept
     requires (Kind == CustomIndexKind::Const)
-    { return _M_i; }
+    { return value; }
 
     constexpr
     operator int() const
     requires (Kind == CustomIndexKind::Throwing)
-    { return _M_i; }
+    { return value; }
 
     constexpr
     operator int() noexcept
     requires (Kind == CustomIndexKind::Mutating)
-    { return _M_i; }
+    { return value; }
 
     constexpr
     operator int() && noexcept
     requires (Kind == CustomIndexKind::RValue)
-    { return _M_i; }
+    { return value; }
 
   private:
-    int _M_i;
+    int value;
   };
 
 using IntLike = CustomIndexType<CustomIndexKind::Const>;


More information about the Libstdc++-cvs mailing list