[PATCH v2] libstdc++: Improve and cleanup mdspan related code.
Jonathan Wakely
jwakely@redhat.com
Wed Oct 8 08:36:57 GMT 2025
On Fri, 03 Oct 2025 at 12:48 +0200, Tomasz Kamiński wrote:
>From: Luc Grosheintz <luc.grosheintz@gmail.com>
>
>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> 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.
> * testsuite/23_containers/mdspan/int_like.h: Rename _M_i to
> value.
>
>Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
>Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
>---
>v2 revers few whitespace changes.
>
> libstdc++-v3/include/std/mdspan | 16 +++++++++++-----
> .../testsuite/23_containers/mdspan/int_like.h | 12 ++++++------
> 2 files changed, 17 insertions(+), 11 deletions(-)
>
>diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
>index ce7e4178f67..469d4c3cb6c 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);
>+ __glibcxx_assert(__other >= 0);
>+ return static_cast<_IndexType>(std::move(__other));
Do we need the std::move here, if we know that __other has an integral
type?
> }
> else
> {
>@@ -981,7 +987,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> template<class _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>)
>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<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>;
>--
>2.51.0
>
>
More information about the Libstdc++
mailing list