From: Luc Grosheintz Date: Tue, 30 Sep 2025 10:55:18 +0000 (+0200) Subject: libstdc++: Improve and cleanup mdspan related code. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5733ecea0795b3b4f152dc33bb3aacd4ecfcf05f;p=thirdparty%2Fgcc.git 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 , 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 Reviewed-by: Tomasz KamiƄski Signed-off-by: Luc Grosheintz --- diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan index 6714b19a884..8d2421819a5 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 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping> && is_constructible_v + typename _RightPaddedMapping::extents_type> constexpr explicit(!is_convertible_v) @@ -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 - 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 - 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 _OIndexType> - constexpr mapping(const extents_type& __exts, _OIndexType __pad) + constexpr + mapping(const extents_type& __exts, _OIndexType __pad) : _M_storage(__exts, __mdspan::__index_type_cast(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 _OIndexType> - constexpr mapping(const extents_type& __exts, _OIndexType __pad) + constexpr + mapping(const extents_type& __exts, _OIndexType __pad) : _M_storage(__exts, __mdspan::__index_type_cast(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 310dd8ddf20..e9172c13455 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 public: explicit CustomIndexType(int i) - : _M_i(i) + : value(i) { } CustomIndexType() = delete; @@ -31,25 +31,25 @@ template 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;