[libcxx-commits] [libcxx] [libc++] Partially implement P2642R6: Padded mdspan layouts (PR #187873)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Mar 21 10:33:31 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: eiytoq (eiytoq)

<details>
<summary>Changes</summary>

Partially implement P2642R6, except for `submdspan_mapping`.

Partial: #<!-- -->105425
Fixes: #<!-- -->171365

---

Patch is 174.10 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/187873.diff


48 Files Affected:

- (modified) libcxx/docs/Status/Cxx2cIssues.csv (+1-1) 
- (modified) libcxx/docs/Status/Cxx2cPapers.csv (+1-1) 
- (modified) libcxx/include/CMakeLists.txt (+3) 
- (modified) libcxx/include/__fwd/mdspan.h (+16) 
- (added) libcxx/include/__mdspan/layout_common.h (+143) 
- (modified) libcxx/include/__mdspan/layout_left.h (+29) 
- (added) libcxx/include/__mdspan/layout_left_padded.h (+409) 
- (modified) libcxx/include/__mdspan/layout_right.h (+31) 
- (added) libcxx/include/__mdspan/layout_right_padded.h (+423) 
- (modified) libcxx/include/__mdspan/layout_stride.h (+11-24) 
- (modified) libcxx/include/mdspan (+229-16) 
- (modified) libcxx/modules/std/mdspan.inc (+5) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left/ctor.layout_left_padded.pass.cpp (+97) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/comparison.pass.cpp (+91) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.copy.pass.cpp (+55) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.default.pass.cpp (+53) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.extents.pass.cpp (+59) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.layout_left.pass.cpp (+79) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.layout_right.pass.cpp (+74) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.layout_right_padded.pass.cpp (+74) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.layout_stride.pass.cpp (+78) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.mapping.assert.pass.cpp (+53) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.mapping.pass.cpp (+86) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/ctor.padding.pass.cpp (+52) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/extents.verify.cpp (+36) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/index_operator.pass.cpp (+105) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/properties.pass.cpp (+88) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/required_span_size.pass.cpp (+53) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/static_requirements.pass.cpp (+68) 
- (added) libcxx/test/std/containers/views/mdspan/layout_left_padded/stride.pass.cpp (+61) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right/ctor.layout_right_padded.pass.cpp (+97) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/comparison.pass.cpp (+92) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.copy.pass.cpp (+57) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.default.pass.cpp (+53) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.extents.pass.cpp (+59) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.layout_left.pass.cpp (+74) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.layout_left_padded.pass.cpp (+74) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.layout_right.pass.cpp (+79) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.layout_stride.pass.cpp (+78) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.mapping.assert.pass.cpp (+52) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.mapping.pass.cpp (+86) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/ctor.padding.pass.cpp (+52) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/extents.verify.cpp (+36) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/index_operator.pass.cpp (+105) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/properties.pass.cpp (+87) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/required_span_size.pass.cpp (+53) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/static_requirements.pass.cpp (+68) 
- (added) libcxx/test/std/containers/views/mdspan/layout_right_padded/stride.pass.cpp (+61) 


``````````diff
diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 60b1bd6ff70da..5036ebfb1691a 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -204,7 +204,7 @@
 "`LWG4366 <https://wg21.link/LWG4366>`__","Heterogeneous comparison of ``expected`` may be ill-formed","2025-11 (Kona)","","","`#171362 <https://github.com/llvm/llvm-project/issues/171362>`__",""
 "`LWG4369 <https://wg21.link/LWG4369>`__","``check-types`` function for ``upon_error`` and ``upon_stopped`` is wrong","2025-11 (Kona)","","","`#171363 <https://github.com/llvm/llvm-project/issues/171363>`__",""
 "`LWG4370 <https://wg21.link/LWG4370>`__","Comparison of ``optional<T>`` to ``T`` may be ill-formed","2025-11 (Kona)","|Complete|","22","`#171364 <https://github.com/llvm/llvm-project/issues/171364>`__",""
-"`LWG4372 <https://wg21.link/LWG4372>`__","Weaken *Mandates:* for dynamic padding values in padded layouts","2025-11 (Kona)","","","`#171365 <https://github.com/llvm/llvm-project/issues/171365>`__",""
+"`LWG4372 <https://wg21.link/LWG4372>`__","Weaken *Mandates:* for dynamic padding values in padded layouts","2025-11 (Kona)","|Complete|","23","`#171365 <https://github.com/llvm/llvm-project/issues/171365>`__",""
 "`LWG4375 <https://wg21.link/LWG4375>`__","``std::simd::bit_ceil`` should not be ``noexcept``","2025-11 (Kona)","","","`#171366 <https://github.com/llvm/llvm-project/issues/171366>`__",""
 "`LWG4376 <https://wg21.link/LWG4376>`__","ABI tag in return type of [simd.mask.unary] is overconstrained","2025-11 (Kona)","","","`#171367 <https://github.com/llvm/llvm-project/issues/171367>`__",""
 "`LWG4377 <https://wg21.link/LWG4377>`__","Misleading note about lock-free property of ``std::atomic_ref``","2025-11 (Kona)","","","`#171368 <https://github.com/llvm/llvm-project/issues/171368>`__",""
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 29642fc53cac6..3d84652940a3d 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -60,7 +60,7 @@
 "`P2810R4 <https://wg21.link/P2810R4>`__","``is_debugger_present`` ``is_replaceable``","2024-03 (Tokyo)","","","`#105422 <https://github.com/llvm/llvm-project/issues/105422>`__",""
 "`P1068R11 <https://wg21.link/P1068R11>`__","Vector API for random number generation","2024-03 (Tokyo)","","","`#105423 <https://github.com/llvm/llvm-project/issues/105423>`__",""
 "`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Complete|","22","`#105424 <https://github.com/llvm/llvm-project/issues/105424>`__","The changes to ``tuple``'s equality overload from P2165R4 are not yet implemented."
-"`P2642R6 <https://wg21.link/P2642R6>`__","Padded ``mdspan`` layouts","2024-03 (Tokyo)","","","`#105425 <https://github.com/llvm/llvm-project/issues/105425>`__",""
+"`P2642R6 <https://wg21.link/P2642R6>`__","Padded ``mdspan`` layouts","2024-03 (Tokyo)","|Partial|","23","`#105425 <https://github.com/llvm/llvm-project/issues/105425>`__",""
 "`P3029R1 <https://wg21.link/P3029R1>`__","Better ``mdspan``'s CTAD","2024-03 (Tokyo)","|Complete|","19","`#105426 <https://github.com/llvm/llvm-project/issues/105426>`__",""
 "","","","","","",""
 "`P2747R2 <https://wg21.link/P2747R2>`__","``constexpr`` placement new","2024-06 (St. Louis)","|Complete|","20","`#105427 <https://github.com/llvm/llvm-project/issues/105427>`__",""
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 53165f0336b2d..3a6061099c74d 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -575,6 +575,9 @@ set(files
   __mdspan/default_accessor.h
   __mdspan/extents.h
   __mdspan/layout_left.h
+  __mdspan/layout_left_padded.h
+  __mdspan/layout_common.h
+  __mdspan/layout_right_padded.h
   __mdspan/layout_right.h
   __mdspan/layout_stride.h
   __mdspan/mdspan.h
diff --git a/libcxx/include/__fwd/mdspan.h b/libcxx/include/__fwd/mdspan.h
index 8889567a047f6..8c069577ab2a9 100644
--- a/libcxx/include/__fwd/mdspan.h
+++ b/libcxx/include/__fwd/mdspan.h
@@ -18,6 +18,8 @@
 #define _LIBCPP___MDSPAN_LAYOUTS_H
 
 #include <__config>
+#include <cstddef>
+#include <span>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -48,6 +50,20 @@ struct layout_stride {
   class mapping;
 };
 
+#  if _LIBCPP_STD_VER >= 26
+template <size_t _PaddingValue = dynamic_extent>
+struct layout_left_padded {
+  template <class _Extents>
+  class mapping;
+};
+
+template <size_t _PaddingValue = dynamic_extent>
+struct layout_right_padded {
+  template <class _Extents>
+  class mapping;
+};
+#  endif
+
 #endif // _LIBCPP_STD_VER >= 23
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__mdspan/layout_common.h b/libcxx/include/__mdspan/layout_common.h
new file mode 100644
index 0000000000000..77ad5a0f4e6b4
--- /dev/null
+++ b/libcxx/include/__mdspan/layout_common.h
@@ -0,0 +1,143 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//                        Kokkos v. 4.0
+//       Copyright (2022) National Technology & Engineering
+//               Solutions of Sandia, LLC (NTESS).
+//
+// Under the terms of Contract DE-NA0003525 with NTESS,
+// the U.S. Government retains certain rights in this software.
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MDSPAN_LAYOUT_COMMON_H
+#define _LIBCPP___MDSPAN_LAYOUT_COMMON_H
+
+#include <__config>
+#include <__fwd/mdspan.h>
+#include <__mdspan/extents.h>
+#include <__memory/addressof.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 23
+
+namespace __mdspan_detail {
+
+template <class _Layout, class _Mapping>
+constexpr bool __is_mapping_of =
+    is_same_v<typename _Layout::template mapping<typename _Mapping::extents_type>, _Mapping>;
+
+template <class _Mapping>
+concept __layout_mapping_alike = requires {
+  requires __is_mapping_of<typename _Mapping::layout_type, _Mapping>;
+  requires __is_extents_v<typename _Mapping::extents_type>;
+  { _Mapping::is_always_strided() } -> same_as<bool>;
+  { _Mapping::is_always_exhaustive() } -> same_as<bool>;
+  { _Mapping::is_always_unique() } -> same_as<bool>;
+  bool_constant<_Mapping::is_always_strided()>::value;
+  bool_constant<_Mapping::is_always_exhaustive()>::value;
+  bool_constant<_Mapping::is_always_unique()>::value;
+};
+
+#  if _LIBCPP_STD_VER >= 26
+
+template <class _Integral>
+_LIBCPP_HIDE_FROM_ABI constexpr _Integral __least_multiple_at_least(_Integral __multiplier, _Integral __minimum) {
+  if (__multiplier == static_cast<_Integral>(0))
+    return __minimum;
+
+  _Integral __factor = __minimum / __multiplier;
+  if (__minimum % __multiplier != static_cast<_Integral>(0))
+    ++__factor;
+
+  return __factor * __multiplier;
+}
+
+template <class _To, class _Integral>
+_LIBCPP_HIDE_FROM_ABI constexpr bool
+__least_multiple_at_least_is_representable_as(_Integral __multiplier, _Integral __minimum) {
+  if (__multiplier == static_cast<_Integral>(0))
+    return __mdspan_detail::__is_representable_as<_To>(__minimum);
+
+  _Integral __factor = __minimum / __multiplier;
+  if (__minimum % __multiplier != static_cast<_Integral>(0)) {
+    bool __overflowed_add = __builtin_add_overflow(__factor, static_cast<_Integral>(1), std::addressof(__factor));
+    if (__overflowed_add)
+      return false;
+  }
+
+  _Integral __result    = 0;
+  bool __overflowed_mul = __builtin_mul_overflow(__factor, __multiplier, std::addressof(__result));
+  return !__overflowed_mul && __mdspan_detail::__is_representable_as<_To>(__result);
+}
+
+template <template <size_t> class _LayoutTemplate, class _Layout>
+struct __is_layout_specialization_of : false_type {};
+
+template <template <size_t> class _LayoutTemplate, size_t _PaddingValue>
+struct __is_layout_specialization_of<_LayoutTemplate, _LayoutTemplate<_PaddingValue>> : true_type {};
+
+template <class _Mapping>
+concept __layout_left_padded_mapping_of =
+    __layout_mapping_alike<_Mapping> &&
+    __is_layout_specialization_of<layout_left_padded, typename _Mapping::layout_type>::value;
+
+template <class _Mapping>
+concept __layout_right_padded_mapping_of =
+    __layout_mapping_alike<_Mapping> &&
+    __is_layout_specialization_of<layout_right_padded, typename _Mapping::layout_type>::value;
+
+template <class _Mapping>
+concept __layout_right_mapping_of = __layout_mapping_alike<_Mapping> && __is_mapping_of<layout_right, _Mapping>;
+
+template <class _Mapping>
+concept __layout_left_mapping_of = __layout_mapping_alike<_Mapping> && __is_mapping_of<layout_left, _Mapping>;
+
+_LIBCPP_HIDE_FROM_ABI constexpr size_t
+__compute_static_padding_stride(size_t __rank, size_t __padding_value, size_t __static_extent) {
+  if (__rank <= 1)
+    return 0uz;
+  if (__padding_value == dynamic_extent || __static_extent == dynamic_extent)
+    return dynamic_extent;
+  return __least_multiple_at_least(__padding_value, __static_extent);
+}
+
+template <class _Mapping>
+  requires __layout_left_padded_mapping_of<_Mapping> || __layout_right_padded_mapping_of<_Mapping>
+constexpr size_t __static_padding_stride_of = [] {
+  using _Extents          = _Mapping::extents_type;
+  constexpr size_t __rank = _Extents::rank();
+
+  if constexpr (__layout_left_padded_mapping_of<_Mapping>) {
+    constexpr size_t __static_extent = __rank == 0 ? 0 : _Extents::static_extent(0);
+    return __compute_static_padding_stride(__rank, _Mapping::padding_value, __static_extent);
+  }
+  if constexpr (__layout_right_padded_mapping_of<_Mapping>) {
+    constexpr size_t __static_extent = __rank == 0 ? 0 : _Extents::static_extent(__rank - 1);
+    return __compute_static_padding_stride(__rank, _Mapping::padding_value, __static_extent);
+  }
+}();
+
+#  endif // _LIBCPP_STD_VER >= 26
+
+} // namespace __mdspan_detail
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___MDSPAN_LAYOUT_PADDED_COMMON_H
diff --git a/libcxx/include/__mdspan/layout_left.h b/libcxx/include/__mdspan/layout_left.h
index 2f515afb6c860..6eff932067d49 100644
--- a/libcxx/include/__mdspan/layout_left.h
+++ b/libcxx/include/__mdspan/layout_left.h
@@ -21,6 +21,7 @@
 #include <__config>
 #include <__fwd/mdspan.h>
 #include <__mdspan/extents.h>
+#include <__mdspan/layout_common.h>
 #include <__memory/addressof.h>
 #include <__type_traits/common_type.h>
 #include <__type_traits/is_constructible.h>
@@ -109,6 +110,34 @@ class layout_left::mapping {
         "layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type.");
   }
 
+#  if _LIBCPP_STD_VER >= 26 // _LIBCPP_STD_VER >= 26
+
+  template <class _LayoutLeftPaddedMapping>
+    requires __mdspan_detail::__layout_left_padded_mapping_of<_LayoutLeftPaddedMapping> &&
+             is_constructible_v<extents_type, typename _LayoutLeftPaddedMapping::extents_type>
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit(
+      !is_convertible_v<typename _LayoutLeftPaddedMapping::extents_type, extents_type>)
+      mapping(const _LayoutLeftPaddedMapping& __other) noexcept
+      : __extents_(__other.extents()) {
+    static_assert(
+        _Extents::rank() <= 1 || _Extents::static_extent(0) == dynamic_extent ||
+            __mdspan_detail::__static_padding_stride_of<_LayoutLeftPaddedMapping> == dynamic_extent ||
+            _Extents::static_extent(0) == __mdspan_detail::__static_padding_stride_of<_LayoutLeftPaddedMapping>,
+        "layout_left::mapping converting from layout_left_padded ctor: incompatible static extent(0) and "
+        "source static padding stride.");
+
+    if constexpr (extents_type::rank() > 1)
+      _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+          __other.stride(1) == __other.extents().extent(0),
+          "layout_left::mapping from layout_left_padded ctor: other.stride(1) must equal other.extents().extent(0).");
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+        __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
+        "layout_left::mapping from layout_left_padded ctor: other.required_span_size() must be representable as "
+        "index_type.");
+  }
+
+#  endif // _LIBCPP_STD_VER >= 26
+
   template <class _OtherExtents>
     requires(is_constructible_v<extents_type, _OtherExtents>)
   _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)
diff --git a/libcxx/include/__mdspan/layout_left_padded.h b/libcxx/include/__mdspan/layout_left_padded.h
new file mode 100644
index 0000000000000..1888fcd5cf691
--- /dev/null
+++ b/libcxx/include/__mdspan/layout_left_padded.h
@@ -0,0 +1,409 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//                        Kokkos v. 4.0
+//       Copyright (2022) National Technology & Engineering
+//               Solutions of Sandia, LLC (NTESS).
+//
+// Under the terms of Contract DE-NA0003525 with NTESS,
+// the U.S. Government retains certain rights in this software.
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MDSPAN_LAYOUT_LEFT_PADDED_H
+#define _LIBCPP___MDSPAN_LAYOUT_LEFT_PADDED_H
+
+#include <__assert>
+#include <__config>
+#include <__fwd/mdspan.h>
+#include <__mdspan/extents.h>
+#include <__mdspan/layout_common.h>
+#include <__mdspan/layout_left.h>
+#include <__mdspan/layout_right.h>
+#include <__mdspan/layout_stride.h>
+#include <__memory/addressof.h>
+#include <__type_traits/common_type.h>
+#include <__type_traits/is_constructible.h>
+#include <__type_traits/is_convertible.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__utility/integer_sequence.h>
+#include <array>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+template <size_t _PaddingValue>
+template <class _Extents>
+class layout_left_padded<_PaddingValue>::mapping {
+public:
+  static_assert(__mdspan_detail::__is_extents<_Extents>::value,
+                "layout_left_padded::mapping template argument must be a specialization of extents.");
+
+  static constexpr size_t padding_value = _PaddingValue;
+
+  using extents_type = _Extents;
+  using index_type   = extents_type::index_type;
+  using size_type    = extents_type::size_type;
+  using rank_type    = extents_type::rank_type;
+  using layout_type  = layout_left_padded;
+
+private:
+  static constexpr rank_type __rank_            = extents_type::rank();
+  static constexpr size_t __first_static_extent = __rank_ == 0 ? 0uz : extents_type::static_extent(0);
+
+  static constexpr size_t __static_padding_stride =
+      __mdspan_detail::__compute_static_padding_stride(__rank_, padding_value, __first_static_extent);
+
+  // Do not store value if __static_padding_stride is not dynamic_extent.
+  using __stride_1_type = std::extents<index_type, __static_padding_stride>;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr index_type __stride_1() const noexcept {
+    if constexpr (__rank_ <= 1)
+      return static_cast<index_type>(0);
+    if constexpr (__static_padding_stride != dynamic_extent)
+      return static_cast<index_type>(__static_padding_stride);
+    return __stride_1_.extent(0);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool __index_space_size_is_representable(const extents_type& __ext) {
+    for (rank_type __r = 0; __r < __rank_; ++__r) {
+      if (__ext.extent(__r) == static_cast<index_type>(0))
+        return true;
+    }
+
+    index_type __prod = static_cast<index_type>(1);
+
+    for (rank_type __r = 0; __r < __rank_; ++__r) {
+      bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), std::addressof(__prod));
+      if (__overflowed)
+        return false;
+    }
+
+    return true;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool
+  __padded_product_is_representable(const extents_type& __ext, index_type __stride_1) {
+    if (__stride_1 == static_cast<index_type>(0))
+      return true;
+    for (rank_type __r = 1; __r < __rank_; ++__r) {
+      if (__ext.extent(__r) == static_cast<index_type>(0))
+        return true;
+    }
+
+    index_type __prod = __stride_1;
+    for (rank_type __r = 1; __r < __rank_; ++__r) {
+      bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), std::addressof(__prod));
+      if (__overflowed)
+        return false;
+    }
+    return true;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static consteval bool __static_padded_product_is_representable() {
+    if constexpr (__rank_ <= 1 || padding_value == dynamic_extent || extents_type::rank_dynamic() > 0)
+      return true;
+    for (rank_type __r = 0; __r < __rank_; ++__r) {
+      if (extents_type::static_extent(__r) == 0)
+        return true;
+    }
+    if constexpr (__static_padding_stride == dynamic_extent ||
+                  !__mdspan_detail::__is_representable_as<index_type>(__static_padding_stride))
+      return false;
+    size_t __prod = __static_padding_stride;
+    for (rank_type __r = 1; __r < __rank_; ++__r) {
+      bool __overflowed = __builtin_mul_overflow(__prod, extents_type::static_extent(__r), std::addressof(__prod));
+      if (__overflowed)
+        return false;
+    }
+    return __mdspan_detail::__is_representable_as<index_type>(__prod);
+  }
+
+  static_assert(extents_type::rank_dynamic() != 0 || __index_space_size_is_representable(extents_type{}),
+                "layout_left_padded::mapping index space for static extents must be representable as index_type.");
+
+  static_assert(padding_value == dynamic_extent || __mdspan_detail::__is_representable_as<index_type>(padding_value),
+                "layout_left_padded::mapping padding_value must be representable as index_type.");
+
+  static_assert(__rank_ <= 1 || padding_value == dynamic_extent || __first_static_extent == dynamic_extent ||
+                    (__mdspan_detail::__least_multiple_at_least_is_representable_as<size_t>(
+                         padding_value, __first_static_extent) &&
+                     __mdspan_detail::__least_multiple_at_least_is_representable_as<index_type>(
+                         padding_value, __first_static_extent)),
+                "layout_left_padded::mapping padded stride for the first static extent must be representable as "
+                "size_t and index_type.");
+
+  static_assert(__static_padded_product_is_representable(),
+                "layout_left_padded::mapping required span size for static extents must be representable as size_t "
+                "and index_type.");
+
+public:
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept : mapping(extents_type{}) {}
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) : __extents_(__ext) {
+    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+        __index_space_size_is_representable(__ext),
+        "layout_left_padded::mapping(extents): index space size must be representable as index_type.");
+
+    if constexpr (__rank_ > 1) {
+      index_type __stride_1 = 0;
+      if constexpr (padding_value == dynamic_extent) {
+        __stride_1 = __ext.extent(0);
+      } else {
+        _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+            __mdspan_detail::__least_multiple_at_least_is_representable_as<index_type>(
+                static_cast<index_type>(padding_value), __ext.extent(0)),
+            "layout_left_padded::mapping(extents): padded stride must be representable as index_type.");
+        __stride_1 =
+            __mdspan_detail::__least_multiple_at_least(static_cast<index_type>(padding_value), __ext.extent(0));
+        _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+            __padded_product_is_representable(__ext, __stride_1),
+            "layout_left_padded::mapping(extents): re...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/187873


More information about the libcxx-commits mailing list