[libcxx-commits] [libcxx] cfa096d - [libc++][mdspan] Implement layout_right

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 29 07:13:20 PDT 2023


Author: Christian Trott
Date: 2023-06-29T10:12:17-04:00
New Revision: cfa096d9c92e758c4916dfd06d107fcec2edeca4

URL: https://github.com/llvm/llvm-project/commit/cfa096d9c92e758c4916dfd06d107fcec2edeca4
DIFF: https://github.com/llvm/llvm-project/commit/cfa096d9c92e758c4916dfd06d107fcec2edeca4.diff

LOG: [libc++][mdspan] Implement layout_right

This commit implements layout_right in support of C++23 mdspan
(https://wg21.link/p0009). layout_right is a layout mapping policy
whose index mapping corresponds to the memory layout of multidimensional
C-arrays, and is thus also referred to as the C-layout.

Co-authored-by: Damien L-G <dalg24 at gmail.com>

Differential Revision: https://reviews.llvm.org/D151267

Added: 
    libcxx/include/__fwd/mdspan.h
    libcxx/include/__mdspan/layout_right.h
    libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
    libcxx/test/std/containers/views/mdspan/layout_right/assert.conversion.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/assert.ctor.extents.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/assert.index_operator.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/assert.stride.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/comparison.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/ctor.default.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/ctor.mapping.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/extents.verify.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/index_operator.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/properties.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/required_span_size.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/static_requirements.pass.cpp
    libcxx/test/std/containers/views/mdspan/layout_right/stride.pass.cpp

Modified: 
    libcxx/docs/Status/Cxx23.rst
    libcxx/docs/Status/Cxx23Papers.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/__mdspan/extents.h
    libcxx/include/mdspan
    libcxx/include/module.modulemap.in
    libcxx/modules/std/mdspan.cppm
    libcxx/test/std/containers/views/mdspan/extents/CtorTestCombinations.h
    libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp
    libcxx/test/std/containers/views/mdspan/extents/ctor_default.pass.cpp
    libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp
    libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp
    libcxx/test/std/containers/views/mdspan/extents/ctor_from_span.pass.cpp

Removed: 
    libcxx/test/std/containers/views/mdspan/extents/ConvertibleToIntegral.h


################################################################################
diff  --git a/libcxx/docs/Status/Cxx23.rst b/libcxx/docs/Status/Cxx23.rst
index cb9ed5d27ea7c2..d99728b1bec84a 100644
--- a/libcxx/docs/Status/Cxx23.rst
+++ b/libcxx/docs/Status/Cxx23.rst
@@ -40,6 +40,7 @@ Paper Status
 
 .. note::
 
+   .. [#note-P0009R18] P0009R18: ``extents``, ``dextents``, and ``layout_right`` are implemented.
    .. [#note-P0533R9] P0533R9: ``isfinite``, ``isinf``, ``isnan`` and ``isnormal`` are implemented.
    .. [#note-P1413R3] P1413R3: ``std::aligned_storage_t`` and ``std::aligned_union_t`` are marked deprecated, but
       clang doesn't issue a diagnostic for deprecated using template declarations.

diff  --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 034ea99d1cfc60..b8bbe6909864b4 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -51,7 +51,7 @@
 "`P2442R1 <https://wg21.link/P2442R1>`__","LWG","Windowing range adaptors: ``views::chunk`` and ``views::slide``","February 2022","","","|ranges|"
 "`P2443R1 <https://wg21.link/P2443R1>`__","LWG","``views::chunk_by``","February 2022","","","|ranges|"
 "","","","","","",""
-"`P0009R18 <https://wg21.link/P0009R18>`__","LWG","mdspan: A Non-Owning Multidimensional Array Reference","July 2022","|In progress|",""
+"`P0009R18 <https://wg21.link/P0009R18>`__","LWG","mdspan: A Non-Owning Multidimensional Array Reference","July 2022","|In progress| [#note-P0009R18]_|",""
 "`P0429R9 <https://wg21.link/P0429R9>`__","LWG","A Standard ``flat_map``","July 2022","",""
 "`P1169R4 <https://wg21.link/P1169R4>`__","LWG","``static operator()``","July 2022","|Complete|","16.0"
 "`P1222R4 <https://wg21.link/P1222R4>`__","LWG","A Standard ``flat_set``","July 2022","",""

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 7de36c005c28a6..b77772b56d7dac 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -418,6 +418,7 @@ set(files
   __fwd/hash.h
   __fwd/ios.h
   __fwd/istream.h
+  __fwd/mdspan.h
   __fwd/memory_resource.h
   __fwd/ostream.h
   __fwd/pair.h
@@ -477,6 +478,7 @@ set(files
   __locale_dir/locale_base_api/locale_guard.h
   __mbstate_t.h
   __mdspan/extents.h
+  __mdspan/layout_right.h
   __memory/addressof.h
   __memory/align.h
   __memory/aligned_alloc.h

diff  --git a/libcxx/include/__fwd/mdspan.h b/libcxx/include/__fwd/mdspan.h
new file mode 100644
index 00000000000000..591e5f6e9511be
--- /dev/null
+++ b/libcxx/include/__fwd/mdspan.h
@@ -0,0 +1,63 @@
+// -*- 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_LAYOUTS_H
+#define _LIBCPP___MDSPAN_LAYOUTS_H
+
+#include <__config>
+
+#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
+
+/*
+// Will be implemented with follow on revision
+// Layout policy with a mapping which corresponds to FORTRAN-style array layouts
+struct layout_left {
+  template<class Extents>
+    class mapping;
+};
+*/
+
+// Layout policy with a mapping which corresponds to C-style array layouts
+struct layout_right {
+  template <class Extents>
+  class mapping;
+};
+
+/*
+// Will be implemented with follow on revision
+// Layout policy with a unique mapping where strides are arbitrary
+struct layout_stride {
+  template<class Extents>
+    class mapping;
+};
+*/
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___MDSPAN_LAYOUTS_H

diff  --git a/libcxx/include/__mdspan/extents.h b/libcxx/include/__mdspan/extents.h
index 03db92b9ef6501..8c9f12147b5726 100644
--- a/libcxx/include/__mdspan/extents.h
+++ b/libcxx/include/__mdspan/extents.h
@@ -220,7 +220,7 @@ struct __maybe_static_array {
 // value must be a positive integer otherwise returns false
 // if _From is not an integral, we just check positivity
 template <integral _To, class _From>
-  requires(is_integral_v<_From>)
+  requires(integral<_From>)
 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_representable_as(_From __value) {
   using _To_u   = make_unsigned_t<_To>;
   using _From_u = make_unsigned_t<_From>;
@@ -236,7 +236,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool __is_representable_as(_From __value) {
 }
 
 template <integral _To, class _From>
-  requires(!is_integral_v<_From>)
+  requires(!integral<_From>)
 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_representable_as(_From __value) {
   if constexpr (is_signed_v<_To>) {
     if (static_cast<_To>(__value) < 0)
@@ -437,9 +437,9 @@ using dextents = typename __mdspan_detail::__make_dextents<_IndexType, _Rank>::t
 template <class... _IndexTypes>
 extents(_IndexTypes...) -> extents<size_t, size_t((_IndexTypes(), dynamic_extent))...>;
 
-// Helper type traits for identifying a class as extents.
 namespace __mdspan_detail {
 
+// Helper type traits for identifying a class as extents.
 template <class _Tp>
 struct __is_extents : false_type {};
 
@@ -449,6 +449,44 @@ struct __is_extents<extents<_IndexType, _ExtentsPack...>> : true_type {};
 template <class _Tp>
 inline constexpr bool __is_extents_v = __is_extents<_Tp>::value;
 
+// Function to check whether a set of indices are a multidimensional
+// index into extents. This is a word of power in the C++ standard
+// requiring that the indices are larger than 0 and smaller than
+// the respective extents.
+
+template <integral _IndexType, class _From>
+  requires(integral<_From>)
+_LIBCPP_HIDE_FROM_ABI constexpr bool __is_index_in_extent(_IndexType __extent, _From __value) {
+  if constexpr (is_signed_v<_From>) {
+    if (__value < 0)
+      return false;
+  }
+  using _Tp = common_type_t<_IndexType, _From>;
+  return static_cast<_Tp>(__value) < static_cast<_Tp>(__extent);
+}
+
+template <integral _IndexType, class _From>
+  requires(!integral<_From>)
+_LIBCPP_HIDE_FROM_ABI constexpr bool __is_index_in_extent(_IndexType __extent, _From __value) {
+  if constexpr (is_signed_v<_IndexType>) {
+    if (static_cast<_IndexType>(__value) < 0)
+      return false;
+  }
+  return static_cast<_IndexType>(__value) < __extent;
+}
+
+template <size_t... _Idxs, class _Extents, class... _From>
+_LIBCPP_HIDE_FROM_ABI constexpr bool
+__is_multidimensional_index_in_impl(index_sequence<_Idxs...>, const _Extents& __ext, _From... __values) {
+  return (__mdspan_detail::__is_index_in_extent(__ext.extent(_Idxs), __values) && ...);
+}
+
+template <class _Extents, class... _From>
+_LIBCPP_HIDE_FROM_ABI constexpr bool __is_multidimensional_index_in(const _Extents& __ext, _From... __values) {
+  return __mdspan_detail::__is_multidimensional_index_in_impl(
+      make_index_sequence<_Extents::rank()>(), __ext, __values...);
+}
+
 } // namespace __mdspan_detail
 
 #endif // _LIBCPP_STD_VER >= 23

diff  --git a/libcxx/include/__mdspan/layout_right.h b/libcxx/include/__mdspan/layout_right.h
new file mode 100644
index 00000000000000..18ecb2008fe93d
--- /dev/null
+++ b/libcxx/include/__mdspan/layout_right.h
@@ -0,0 +1,162 @@
+// -*- 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_RIGHT_H
+#define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
+
+#include <__assert>
+#include <__config>
+#include <__fwd/mdspan.h>
+#include <__mdspan/extents.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 <cinttypes>
+#include <cstddef>
+#include <limits>
+
+#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
+
+template <class _Extents>
+class layout_right::mapping {
+public:
+  static_assert(__mdspan_detail::__is_extents<_Extents>::value,
+                "layout_right::mapping template argument must be a specialization of extents.");
+
+  using extents_type = _Extents;
+  using index_type   = typename extents_type::index_type;
+  using size_type    = typename extents_type::size_type;
+  using rank_type    = typename extents_type::rank_type;
+  using layout_type  = layout_right;
+
+private:
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
+    if constexpr (extents_type::rank() == 0)
+      return true;
+
+    index_type __prod = __ext.extent(0);
+    for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
+      bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
+      if (__overflowed)
+        return false;
+    }
+    return true;
+  }
+
+  static_assert((extents_type::rank_dynamic() > 0) || __required_span_size_is_representable(extents_type()),
+                "layout_right::mapping product of static extents must be representable as index_type.");
+
+public:
+  // [mdspan.layout.right.cons], constructors
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept               = default;
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {
+    _LIBCPP_ASSERT(__required_span_size_is_representable(__ext),
+                   "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
+  }
+
+  template <class _OtherExtents>
+    requires(is_constructible_v<extents_type, _OtherExtents>)
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
+      mapping(const mapping<_OtherExtents>& __other) noexcept
+      : __extents_(__other.extents()) {
+    _LIBCPP_ASSERT(
+        __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
+        "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
+  }
+
+// FIXME: add when we add other layouts
+#  if 0
+    template<class _OtherExtents>
+      constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
+        mapping(const layout_left::mapping<_OtherExtents>&) noexcept {}
+
+    template<class _OtherExtents>
+      constexpr explicit(extents_type::rank() > 0)
+        mapping(const layout_stride::mapping_<OtherExtents>&) noexcept;
+#  endif
+
+  _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
+
+  // [mdspan.layout.right.obs], observers
+  _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
+    index_type __size = 1;
+    for (size_t __r = 0; __r < extents_type::rank(); __r++)
+      __size *= __extents_.extent(__r);
+    return __size;
+  }
+
+  template <class... _Indices>
+    requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
+             (is_nothrow_constructible_v<index_type, _Indices> && ...))
+  _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
+    _LIBCPP_ASSERT(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
+                   "layout_right::mapping: out of bounds indexing");
+    return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
+      index_type __res = 0;
+      ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...);
+      return __res;
+    }(make_index_sequence<sizeof...(_Indices)>());
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
+
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
+    requires(extents_type::rank() > 0)
+  {
+    _LIBCPP_ASSERT(__r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
+    index_type __s = 1;
+    for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--)
+      __s *= __extents_.extent(__i);
+    return __s;
+  }
+
+  template <class _OtherExtents>
+    requires(_OtherExtents::rank() == extents_type::rank())
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool
+  operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
+    return __lhs.extents() == __rhs.extents();
+  }
+
+private:
+  extents_type __extents_{}; // exposition only
+};
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H

diff  --git a/libcxx/include/mdspan b/libcxx/include/mdspan
index 16b4e3e1641a47..39405712ec012d 100644
--- a/libcxx/include/mdspan
+++ b/libcxx/include/mdspan
@@ -8,7 +8,34 @@
 //===---------------------------------------------------------------------===//
 
 /*
-    extents synopsis
+
+// Overall mdspan synopsis
+
+namespace std {
+  // [mdspan.extents], class template extents
+  template<class IndexType, size_t... Extents>
+    class extents;
+
+  // [mdspan.extents.dextents], alias template dextents
+  template<class IndexType, size_t Rank>
+    using dextents = see below;
+
+  // [mdspan.layout], layout mapping
+  struct layout_left; // not implemented yet
+  struct layout_right;
+  struct layout_stride; // not implemented yet
+
+  // [mdspan.accessor.default], class template default_accessor
+  template<class ElementType>
+    class default_accessor; // not implemented yet
+
+  // [mdspan.mdspan], class template mdspan
+  template<class ElementType, class Extents, class LayoutPolicy = layout_right,
+           class AccessorPolicy = default_accessor<ElementType>>
+    class mdspan; // not implemented yet
+}
+
+// extents synopsis
 
 namespace std {
   template<class _IndexType, size_t... _Extents>
@@ -54,13 +81,70 @@ namespace std {
     explicit extents(Integrals...)
       -> see below;
 }
+
+// layout_right synopsis
+
+namespace std {
+  template<class Extents>
+  class layout_right::mapping {
+  public:
+    using extents_type = Extents;
+    using index_type = typename extents_type::index_type;
+    using size_type = typename extents_type::size_type;
+    using rank_type = typename extents_type::rank_type;
+    using layout_type = layout_right;
+
+    // [mdspan.layout.right.cons], constructors
+    constexpr mapping() noexcept = default;
+    constexpr mapping(const mapping&) noexcept = default;
+    constexpr mapping(const extents_type&) noexcept;
+    template<class OtherExtents>
+      constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
+        mapping(const mapping<OtherExtents>&) noexcept;
+    template<class OtherExtents>
+      constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
+        mapping(const layout_left::mapping<OtherExtents>&) noexcept;
+    template<class OtherExtents>
+      constexpr explicit(extents_type::rank() > 0)
+        mapping(const layout_stride::mapping<OtherExtents>&) noexcept;
+
+    constexpr mapping& operator=(const mapping&) noexcept = default;
+
+    // [mdspan.layout.right.obs], observers
+    constexpr const extents_type& extents() const noexcept { return extents_; }
+
+    constexpr index_type required_span_size() const noexcept;
+
+    template<class... Indices>
+      constexpr index_type operator()(Indices...) const noexcept;
+
+    static constexpr bool is_always_unique() noexcept { return true; }
+    static constexpr bool is_always_exhaustive() noexcept { return true; }
+    static constexpr bool is_always_strided() noexcept { return true; }
+
+    static constexpr bool is_unique() noexcept { return true; }
+    static constexpr bool is_exhaustive() noexcept { return true; }
+    static constexpr bool is_strided() noexcept { return true; }
+
+    constexpr index_type stride(rank_type) const noexcept;
+
+    template<class OtherExtents>
+      friend constexpr bool operator==(const mapping&, const mapping<OtherExtents>&) noexcept;
+
+  private:
+    extents_type extents_{};    // exposition only
+  };
+}
+
 */
 
 #ifndef _LIBCPP_MDSPAN
 #define _LIBCPP_MDSPAN
 
 #include <__config>
+#include <__fwd/mdspan.h>
 #include <__mdspan/extents.h>
+#include <__mdspan/layout_right.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header

diff  --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 4dff9f2bf713d8..06c5f5d8750c7a 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1174,6 +1174,8 @@ module std [system] {
 
     module __mdspan {
       module extents                         { private header "__mdspan/extents.h" }
+      module layout_right                    { private header "__mdspan/layout_right.h" }
+      module mdspan_fwd                      { private header "__fwd/mdspan.h" }
     }
   }
   module memory {

diff  --git a/libcxx/modules/std/mdspan.cppm b/libcxx/modules/std/mdspan.cppm
index b1ba7d1e5a3de9..af2c16f736adb1 100644
--- a/libcxx/modules/std/mdspan.cppm
+++ b/libcxx/modules/std/mdspan.cppm
@@ -18,12 +18,12 @@ export namespace std {
   // [mdspan.extents.dextents], alias template dextents
   using std::dextents;
 
-#if 0
   // [mdspan.layout], layout mapping
-  using std::layout_left;
+  // using std::layout_left;
   using std::layout_right;
-  using std::layout_stride;
+  // using std::layout_stride;
 
+#if 0
   // [mdspan.accessor.default], class template default_­accessor
   using std::default_accessor;
 

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ConvertibleToIntegral.h b/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
similarity index 63%
rename from libcxx/test/std/containers/views/mdspan/extents/ConvertibleToIntegral.h
rename to libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
index 1cdb198739ec96..92ff35d15ed4ad 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ConvertibleToIntegral.h
+++ b/libcxx/test/std/containers/views/mdspan/ConvertibleToIntegral.h
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef TEST_STD_CONTAINERS_CONVERTIBLE_TO_INTEGRAL_H
-#define TEST_STD_CONTAINERS_CONVERTIBLE_TO_INTEGRAL_H
+#ifndef TEST_STD_CONTAINERS_VIEWS_MDSPAN_LAYOUT_RIGHT_CONVERTIBLE_TO_INTEGRAL_H
+#define TEST_STD_CONTAINERS_VIEWS_MDSPAN_LAYOUT_RIGHT_CONVERTIBLE_TO_INTEGRAL_H
 
 struct IntType {
   int val;
@@ -16,7 +16,8 @@ struct IntType {
 
   constexpr bool operator==(const IntType& rhs) const { return val == rhs.val; }
   constexpr operator int() const noexcept { return val; }
-  constexpr operator unsigned char() const noexcept { return val; }
+  constexpr operator unsigned char() const { return val; }
+  constexpr operator char() const noexcept { return val; }
 };
 
-#endif // TEST_STD_CONTAINERS_CONVERTIBLE_TO_INTEGRAL_H
+#endif // TEST_STD_CONTAINERS_VIEWS_MDSPAN_LAYOUT_RIGHT_CONVERTIBLE_TO_INTEGRAL_H

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/CtorTestCombinations.h b/libcxx/test/std/containers/views/mdspan/extents/CtorTestCombinations.h
index 4914b5c1d87491..02731f8895ed8f 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/CtorTestCombinations.h
+++ b/libcxx/test/std/containers/views/mdspan/extents/CtorTestCombinations.h
@@ -14,7 +14,7 @@
 #include <array>
 #include <span>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "test_macros.h"
 
 // Helper file to implement combinatorical testing of extents constructor
@@ -94,6 +94,6 @@ constexpr bool test_index_type_combo() {
   test<size_t, int, Test>();
   test<size_t, size_t, Test>();
   test<int, IntType, Test>();
-  test<unsigned char, IntType, Test>();
+  test<char, IntType, Test>();
   return true;
 }

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp b/libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp
index 81d85c1faf0f75..2a3da30bb93660 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp
+++ b/libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp
@@ -18,7 +18,7 @@
 #include <mdspan>
 #include <cassert>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "test_macros.h"
 
 template <class E, class Expected>

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ctor_default.pass.cpp b/libcxx/test/std/containers/views/mdspan/extents/ctor_default.pass.cpp
index b2de473f8e8c6b..d958ee8f8371b4 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ctor_default.pass.cpp
+++ b/libcxx/test/std/containers/views/mdspan/extents/ctor_default.pass.cpp
@@ -20,7 +20,7 @@
 #include <cassert>
 #include <array>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "CtorTestCombinations.h"
 #include "test_macros.h"
 

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp
index 48286e1128498f..733f2a6038ff56 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp
+++ b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_array.pass.cpp
@@ -33,7 +33,7 @@
 #include <array>
 #include <type_traits>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "CtorTestCombinations.h"
 #include "test_macros.h"
 

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp
index fd3624e74a3d99..24eb53b1993383 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp
+++ b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_integral.pass.cpp
@@ -34,7 +34,7 @@
 #include <cassert>
 #include <type_traits>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "CtorTestCombinations.h"
 #include "test_macros.h"
 

diff  --git a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_span.pass.cpp b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_span.pass.cpp
index 2e69bab1461cae..b327aa4de7afec 100644
--- a/libcxx/test/std/containers/views/mdspan/extents/ctor_from_span.pass.cpp
+++ b/libcxx/test/std/containers/views/mdspan/extents/ctor_from_span.pass.cpp
@@ -34,7 +34,7 @@
 #include <span>
 #include <type_traits>
 
-#include "ConvertibleToIntegral.h"
+#include "../ConvertibleToIntegral.h"
 #include "CtorTestCombinations.h"
 #include "test_macros.h"
 

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/assert.conversion.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/assert.conversion.pass.cpp
new file mode 100644
index 00000000000000..9fa3e57671cbfb
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/assert.conversion.pass.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// XFAIL: availability-verbose_abort-missing
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <mdspan>
+
+// template<class OtherExtents>
+//   constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
+//     mapping(const mapping<OtherExtents>&) noexcept;
+
+// Constraints: is_constructible_v<extents_type, OtherExtents> is true.
+//
+// Preconditions: other.required_span_size() is representable as a value of type index_type.
+
+#include <mdspan>
+#include <cassert>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  constexpr size_t D = std::dynamic_extent;
+  std::extents<int, D, D> arg_exts{100, 5};
+  std::layout_right::mapping<std::extents<int, D, D>> arg(arg_exts);
+
+  // working case
+  {
+    [[maybe_unused]] std::layout_right::mapping<std::extents<size_t, D, 5>> m(arg); // should work
+  }
+  // mismatch of static extent
+  {
+    TEST_LIBCPP_ASSERT_FAILURE(([=] { std::layout_right::mapping<std::extents<int, D, 3>> m(arg); }()),
+                               "extents construction: mismatch of provided arguments with static extents.");
+  }
+  // value out of range
+  {
+    // check extents would be constructible
+    [[maybe_unused]] std::extents<char, D, 5> e(arg_exts);
+    // but the product is not, so we can't use it for layout_right
+    TEST_LIBCPP_ASSERT_FAILURE(
+        ([=] { std::layout_right::template mapping<std::extents<char, D, 5>> m(arg); }()),
+        "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
+  }
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/assert.ctor.extents.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/assert.ctor.extents.pass.cpp
new file mode 100644
index 00000000000000..a0cd07f76183b3
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/assert.ctor.extents.pass.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// XFAIL: availability-verbose_abort-missing
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <mdspan>
+
+// constexpr mapping(const extents_type& e) noexcept;
+//
+// Preconditions: The size of the multidimensional index space e is representable as a value of type index_type ([basic.fundamental]).
+//
+// Effects: Direct-non-list-initializes extents_ with e.
+
+#include <mdspan>
+#include <cassert>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  constexpr size_t D = std::dynamic_extent;
+
+  // value out of range
+  {
+    // the extents are representable but the product is not, so we can't use it for layout_right
+    TEST_LIBCPP_ASSERT_FAILURE(
+        ([=] { std::layout_right::template mapping<std::extents<char, D, 5>> m(std::extents<char, D, 5>(100)); }()),
+        "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
+  }
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/assert.index_operator.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/assert.index_operator.pass.cpp
new file mode 100644
index 00000000000000..923bef8fe1c61b
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/assert.index_operator.pass.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// XFAIL: availability-verbose_abort-missing
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <mdspan>
+
+// template<class... Indices>
+//   constexpr index_type operator()(Indices... i) const noexcept;
+//
+// Constraints:
+//   - sizeof...(Indices) == extents_type::rank() is true,
+//   - (is_convertible_v<Indices, index_type> && ...) is true, and
+//   - (is_nothrow_constructible_v<index_type, Indices> && ...) is true.
+//
+// Preconditions: extents_type::index-cast(i) is a multidimensional index in extents_ ([mdspan.overview]).
+
+#include <mdspan>
+#include <cassert>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  // value out of range
+  {
+    std::layout_right::template mapping<std::extents<unsigned char, 5>> m;
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-130), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(1000), "layout_right::mapping: out of bounds indexing");
+  }
+  {
+    std::layout_right::template mapping<std::extents<signed char, 5>> m;
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-130), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(1000), "layout_right::mapping: out of bounds indexing");
+  }
+  {
+    std::layout_right::template mapping<std::dextents<unsigned char, 1>> m(std::dextents<unsigned char, 1>(5));
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-130), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(1000), "layout_right::mapping: out of bounds indexing");
+  }
+  {
+    std::layout_right::template mapping<std::dextents<signed char, 1>> m(std::dextents<signed char, 1>(5));
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-130), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(1000), "layout_right::mapping: out of bounds indexing");
+  }
+  {
+    std::layout_right::template mapping<std::dextents<int, 3>> m(std::dextents<int, 3>(5, 7, 9));
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1, -1, -1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1, 0, 0), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(0, -1, 0), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(0, 0, -1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5, 3, 3), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(3, 7, 3), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(3, 3, 9), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5, 7, 9), "layout_right::mapping: out of bounds indexing");
+  }
+  {
+    std::layout_right::template mapping<std::dextents<unsigned, 3>> m(std::dextents<int, 3>(5, 7, 9));
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1, -1, -1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(-1, 0, 0), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(0, -1, 0), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(0, 0, -1), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5, 3, 3), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(3, 7, 3), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(3, 3, 9), "layout_right::mapping: out of bounds indexing");
+    TEST_LIBCPP_ASSERT_FAILURE(m(5, 7, 9), "layout_right::mapping: out of bounds indexing");
+  }
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/assert.stride.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/assert.stride.pass.cpp
new file mode 100644
index 00000000000000..cea67cc5d88a22
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/assert.stride.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// XFAIL: availability-verbose_abort-missing
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
+
+// <mdspan>
+
+// layout_right::mapping
+//
+// constexpr index_type stride(rank_type i) const noexcept;
+//
+//   Constraints: extents_type::rank() > 0 is true.
+//
+//   Preconditions: i < extents_type::rank() is true.
+//
+//   Returns: extents().rev-prod-of-extents(i).
+//
+//
+
+#include <mdspan>
+#include <cassert>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  // value out of range
+  {
+    std::layout_right::template mapping<std::dextents<int, 3>> m{std::dextents<int, 3>{100, 100, 100}};
+
+    TEST_LIBCPP_ASSERT_FAILURE(m.stride(4), "invalid rank index");
+  }
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/comparison.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/comparison.pass.cpp
new file mode 100644
index 00000000000000..6ca9041fbf2f8c
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/comparison.pass.cpp
@@ -0,0 +1,114 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// template<class OtherExtents>
+//   friend constexpr bool operator==(const mapping& x, const mapping<OtherExtents>& y) noexcept;
+//                                      `
+// Constraints: extents_type::rank() == OtherExtents::rank() is true.
+
+#include <mdspan>
+#include <type_traits>
+#include <concepts>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <class To, class From>
+constexpr void test_comparison(bool equal, To dest_exts, From src_exts) {
+  std::layout_right::mapping<To> dest(dest_exts);
+  std::layout_right::mapping<From> src(src_exts);
+  ASSERT_NOEXCEPT(dest == src);
+  assert((dest == src) == equal);
+  assert((dest != src) == !equal);
+}
+
+struct X {
+  constexpr bool does_not_match() { return true; }
+};
+
+constexpr X compare_layout_mappings(...) { return {}; }
+
+template <class E1, class E2>
+constexpr auto compare_layout_mappings(E1 e1, E2 e2)
+    -> decltype(std::layout_right::mapping<E1>(e1) == std::layout_right::mapping<E2>(e2)) {
+  return true;
+}
+
+template <class T1, class T2>
+constexpr void test_comparison_
diff erent_rank() {
+  constexpr size_t D = std::dynamic_extent;
+
+  // sanity check same rank
+  static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D>(5)));
+  static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, D>(5)));
+  static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, 5>()));
+  static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5>()));
+
+  // not equality comparable when rank is not the same
+  static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, D>(1)).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, 1>()).does_not_match());
+
+  static_assert(compare_layout_mappings(std::extents<T1, D>(1), std::extents<T2>()).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1, 1>(), std::extents<T2>()).does_not_match());
+
+  static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D, D>(5, 5)).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, D>(5)).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, 1>()).does_not_match());
+
+  static_assert(compare_layout_mappings(std::extents<T1, D, D>(5, 5), std::extents<T2, D>(5)).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1, 5, D>(5), std::extents<T2, D>(5)).does_not_match());
+  static_assert(compare_layout_mappings(std::extents<T1, 5, 5>(), std::extents<T2, 5>()).does_not_match());
+}
+
+template <class T1, class T2>
+constexpr void test_comparison_same_rank() {
+  constexpr size_t D = std::dynamic_extent;
+
+  test_comparison(true, std::extents<T1>(), std::extents<T2>());
+
+  test_comparison(true, std::extents<T1, D>(5), std::extents<T2, D>(5));
+  test_comparison(true, std::extents<T1, 5>(), std::extents<T2, D>(5));
+  test_comparison(true, std::extents<T1, D>(5), std::extents<T2, 5>());
+  test_comparison(true, std::extents<T1, 5>(), std::extents< T2, 5>());
+  test_comparison(false, std::extents<T1, D>(5), std::extents<T2, D>(7));
+  test_comparison(false, std::extents<T1, 5>(), std::extents<T2, D>(7));
+  test_comparison(false, std::extents<T1, D>(5), std::extents<T2, 7>());
+  test_comparison(false, std::extents<T1, 5>(), std::extents<T2, 7>());
+
+  test_comparison(true, std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), std::extents<T2, D, D, D, D, D>(5, 6, 7, 8, 9));
+  test_comparison(true, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 8, 9>(6, 7));
+  test_comparison(true, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 8, 9>());
+  test_comparison(
+      false, std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), std::extents<T2, D, D, D, D, D>(5, 6, 3, 8, 9));
+  test_comparison(false, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 3, 9>(6, 7));
+  test_comparison(false, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 3, 9>());
+}
+
+template <class T1, class T2>
+constexpr void test_comparison() {
+  test_comparison_same_rank<T1, T2>();
+  test_comparison_
diff erent_rank<T1, T2>();
+}
+
+constexpr bool test() {
+  test_comparison<int, int>();
+  test_comparison<int, size_t>();
+  test_comparison<size_t, int>();
+  test_comparison<size_t, long>();
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/ctor.default.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.default.pass.cpp
new file mode 100644
index 00000000000000..71242faa05a32b
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.default.pass.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// Test default construction:
+//
+// constexpr mapping() noexcept = default;
+
+#include <mdspan>
+#include <cassert>
+#include <cstdint>
+
+#include "test_macros.h"
+
+template <class E>
+constexpr void test_construction() {
+  using M = std::layout_right::mapping<E>;
+  ASSERT_NOEXCEPT(M{});
+  M m;
+  E e;
+
+  // check correct extents are returned
+  ASSERT_NOEXCEPT(m.extents());
+  assert(m.extents() == e);
+
+  // check required_span_size()
+  typename E::index_type expected_size = 1;
+  for (typename E::rank_type r = 0; r < E::rank(); r++)
+    expected_size *= e.extent(r);
+  assert(m.required_span_size() == expected_size);
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_construction<std::extents<int>>();
+  test_construction<std::extents<unsigned, D>>();
+  test_construction<std::extents<unsigned, 7>>();
+  test_construction<std::extents<unsigned, 7, 8>>();
+  test_construction<std::extents<int64_t, D, 8, D, D>>();
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.pass.cpp
new file mode 100644
index 00000000000000..d9fa771fcfcd1d
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.extents.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// constexpr mapping(const extents_type&) noexcept;
+//
+// Preconditions: The size of the multidimensional index space e is representable
+//                as a value of type index_type ([basic.fundamental]).
+//
+// Effects: Direct-non-list-initializes extents_ with e.
+
+#include <mdspan>
+#include <cassert>
+#include <cstdint>
+
+#include "test_macros.h"
+
+template <class E>
+constexpr void test_construction(E e) {
+  using M = std::layout_right::mapping<E>;
+  ASSERT_NOEXCEPT(M{e});
+  M m(e);
+
+  // check correct extents are returned
+  ASSERT_NOEXCEPT(m.extents());
+  assert(m.extents() == e);
+
+  // check required_span_size()
+  typename E::index_type expected_size = 1;
+  for (typename E::rank_type r = 0; r < E::rank(); r++)
+    expected_size *= e.extent(r);
+  assert(m.required_span_size() == expected_size);
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_construction(std::extents<int>());
+  test_construction(std::extents<unsigned, D>(7));
+  test_construction(std::extents<unsigned, 7>());
+  test_construction(std::extents<unsigned, 7, 8>());
+  test_construction(std::extents<int64_t, D, 8, D, D>(7, 9, 10));
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/ctor.mapping.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.mapping.pass.cpp
new file mode 100644
index 00000000000000..1f2461a539029a
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/ctor.mapping.pass.cpp
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// template<class OtherExtents>
+//   constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
+//     mapping(const mapping<OtherExtents>&) noexcept;
+
+// Constraints: is_constructible_v<extents_type, OtherExtents> is true.
+//
+// Preconditions: other.required_span_size() is representable as a value of type index_type
+
+#include <mdspan>
+#include <type_traits>
+#include <cassert>
+#include <limits>
+
+#include "test_macros.h"
+
+template <class To, class From>
+constexpr void test_implicit_conversion(To dest, From src) {
+  assert(dest == src);
+}
+
+template <bool implicit, class ToE, class FromE>
+constexpr void test_conversion(FromE src_exts) {
+  using To   = std::layout_right::mapping<ToE>;
+  using From = std::layout_right::mapping<FromE>;
+  From src(src_exts);
+
+  ASSERT_NOEXCEPT(To(src));
+  To dest(src);
+
+  assert(dest == src);
+  if constexpr (implicit) {
+    dest = src;
+    assert(dest == src);
+    test_implicit_conversion<To, From>(src, src);
+  }
+}
+
+template <class T1, class T2>
+constexpr void test_conversion() {
+  constexpr size_t D = std::dynamic_extent;
+  constexpr bool idx_convertible =
+      static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max());
+
+  // clang-format off
+  test_conversion<idx_convertible && true,  std::extents<T1>>(std::extents<T2>());
+  test_conversion<idx_convertible && true,  std::extents<T1, D>>(std::extents<T2, D>(5));
+  test_conversion<idx_convertible && false, std::extents<T1, 5>>(std::extents<T2, D>(5));
+  test_conversion<idx_convertible && true,  std::extents<T1, 5>>(std::extents<T2, 5>());
+  test_conversion<idx_convertible && false, std::extents<T1, 5, D>>(std::extents<T2, D, D>(5, 5));
+  test_conversion<idx_convertible && true,  std::extents<T1, D, D>>(std::extents<T2, D, D>(5, 5));
+  test_conversion<idx_convertible && true,  std::extents<T1, D, D>>(std::extents<T2, D, 7>(5));
+  test_conversion<idx_convertible && true,  std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>());
+  test_conversion<idx_convertible && false, std::extents<T1, 5, D, 8, D, D>>(std::extents<T2, D, D, 8, 9, 1>(5, 7));
+  test_conversion<idx_convertible && true,  std::extents<T1, D, D, D, D, D>>(
+                                            std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1));
+  test_conversion<idx_convertible && true,  std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5));
+  test_conversion<idx_convertible && true,  std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>());
+  // clang-format on
+}
+
+template <class IdxT, size_t... Extents>
+using mapping_t = typename std::layout_right::template mapping<std::extents<IdxT, Extents...>>;
+
+constexpr void test_no_implicit_conversion() {
+  constexpr size_t D = std::dynamic_extent;
+
+  // Sanity check that one static to dynamic conversion works
+  static_assert(std::is_constructible_v<mapping_t<int, D>, mapping_t<int, 5>>);
+  static_assert(std::is_convertible_v<mapping_t<int, 5>, mapping_t<int, D>>);
+
+  // Check that dynamic to static conversion only works explicitly
+  static_assert(std::is_constructible_v<mapping_t<int, 5>, mapping_t<int, D>>);
+  static_assert(!std::is_convertible_v<mapping_t<int, D>, mapping_t<int, 5>>);
+
+  // Sanity check that one static to dynamic conversion works
+  static_assert(std::is_constructible_v<mapping_t<int, D, 7>, mapping_t<int, 5, 7>>);
+  static_assert(std::is_convertible_v<mapping_t<int, 5, 7>, mapping_t<int, D, 7>>);
+
+  // Check that dynamic to static conversion only works explicitly
+  static_assert(std::is_constructible_v<mapping_t<int, 5, 7>, mapping_t<int, D, 7>>);
+  static_assert(!std::is_convertible_v<mapping_t<int, D, 7>, mapping_t<int, 5, 7>>);
+
+  // Sanity check that smaller index_type to larger index_type conversion works
+  static_assert(std::is_constructible_v<mapping_t<size_t, 5>, mapping_t<int, 5>>);
+  static_assert(std::is_convertible_v<mapping_t<int, 5>, mapping_t<size_t, 5>>);
+
+  // Check that larger index_type to smaller index_type conversion works explicitly only
+  static_assert(std::is_constructible_v<mapping_t<int, 5>, mapping_t<size_t, 5>>);
+  static_assert(!std::is_convertible_v<mapping_t<size_t, 5>, mapping_t<int, 5>>);
+}
+
+constexpr void test_rank_mismatch() {
+  constexpr size_t D = std::dynamic_extent;
+
+  static_assert(!std::is_constructible_v<mapping_t<int, D>, mapping_t<int>>);
+  static_assert(!std::is_constructible_v<mapping_t<int>, mapping_t<int, D, D>>);
+  static_assert(!std::is_constructible_v<mapping_t<int, D>, mapping_t<int, D, D>>);
+  static_assert(!std::is_constructible_v<mapping_t<int, D, D, D>, mapping_t<int, D, D>>);
+}
+
+constexpr void test_static_extent_mismatch() {
+  constexpr size_t D = std::dynamic_extent;
+
+  static_assert(!std::is_constructible_v<mapping_t<int, D, 5>, mapping_t<int, D, 4>>);
+  static_assert(!std::is_constructible_v<mapping_t<int, 5>, mapping_t<int, 4>>);
+  static_assert(!std::is_constructible_v<mapping_t<int, 5, D>, mapping_t<int, 4, D>>);
+}
+
+constexpr bool test() {
+  test_conversion<int, int>();
+  test_conversion<int, size_t>();
+  test_conversion<size_t, int>();
+  test_conversion<size_t, long>();
+  test_no_implicit_conversion();
+  test_rank_mismatch();
+  test_static_extent_mismatch();
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/extents.verify.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/extents.verify.cpp
new file mode 100644
index 00000000000000..249d79d45439d4
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/extents.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// template<class Extents>
+// class layout_left::mapping;
+
+// If Extents is not a specialization of extents, then the program is
+// ill-formed.
+
+// Mandates: If Extents::rank_dynamic() == 0 is true, then the size of the
+// multidimensional index space Extents() is representable as a value of type
+// typename Extents::index_type.
+
+#include <mdspan>
+
+void not_extents() {
+  // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}layout_right::mapping template argument must be a specialization of extents}}
+  [[maybe_unused]] std::layout_right::mapping<void> mapping;
+}
+
+void representable() {
+  // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}layout_right::mapping product of static extents must be representable as index_type.}}
+  [[maybe_unused]] std::layout_right::mapping<std::extents<char, 20, 20>> mapping;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/index_operator.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/index_operator.pass.cpp
new file mode 100644
index 00000000000000..19072658889eeb
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/index_operator.pass.cpp
@@ -0,0 +1,116 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// Test default iteration:
+//
+// template<class... Indices>
+//   constexpr index_type operator()(Indices...) const noexcept;
+//
+// Constraints:
+//   * sizeof...(Indices) == extents_type::rank() is true,
+//   * (is_convertible_v<Indices, index_type> && ...) is true, and
+//   * (is_nothrow_constructible_v<index_type, Indices> && ...) is true.
+//
+// Preconditions:
+//   * extents_type::index-cast(i) is a multidimensional index in extents_.
+
+#include <mdspan>
+#include <cassert>
+#include <cstdint>
+
+#include "test_macros.h"
+
+#include "../ConvertibleToIntegral.h"
+
+template<class Mapping, class ... Indices>
+concept operator_constraints = requires(Mapping m, Indices ... idxs) {
+  {std::is_same_v<decltype(m(idxs...)), typename Mapping::index_type>};
+};
+
+template<class Mapping, class ... Indices>
+  requires(
+    operator_constraints<Mapping, Indices...>
+  )
+constexpr bool check_operator_constraints(Mapping m, Indices ... idxs) {
+  (void) m(idxs...);
+  return true;
+}
+
+template<class Mapping, class ... Indices>
+constexpr bool check_operator_constraints(Mapping, Indices ...) {
+  return false;
+}
+
+template <class M, class T, class... Args>
+constexpr void iterate_right(M m, T& count, Args... args) {
+  constexpr size_t r = sizeof...(Args);
+  if constexpr (M::extents_type::rank() == r) {
+    ASSERT_NOEXCEPT(m(args...));
+    assert(count == m(args...));
+    count++;
+  } else {
+    for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
+      iterate_right(m, count, args..., i);
+    }
+  }
+}
+
+template <class E, class... Args>
+constexpr void test_iteration(Args... args) {
+  using M = std::layout_right::mapping<E>;
+  M m(E(args...));
+
+  typename E::index_type count = 0;
+  iterate_right(m, count);
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_iteration<std::extents<int>>();
+  test_iteration<std::extents<unsigned, D>>(1);
+  test_iteration<std::extents<unsigned, D>>(7);
+  test_iteration<std::extents<unsigned, 7>>();
+  test_iteration<std::extents<unsigned, 7, 8>>();
+  test_iteration<std::extents<char, D, D, D, D>>(1, 1, 1, 1);
+
+  // Check operator constraint for number of arguments
+  static_assert(check_operator_constraints(std::layout_right::mapping<std::extents<int, D>>(std::extents<int, D>(1)), 0));
+  static_assert(!check_operator_constraints(std::layout_right::mapping<std::extents<int, D>>(std::extents<int, D>(1)), 0, 0));
+
+  // Check operator constraint for convertibility of arguments to index_type
+  static_assert(check_operator_constraints(std::layout_right::mapping<std::extents<int, D>>(std::extents<int, D>(1)), IntType(0)));
+  static_assert(!check_operator_constraints(std::layout_right::mapping<std::extents<unsigned, D>>(std::extents<unsigned, D>(1)), IntType(0)));
+
+  // Check operator constraint for no-throw-constructibility of index_type from arguments
+  static_assert(!check_operator_constraints(std::layout_right::mapping<std::extents<unsigned char, D>>(std::extents<unsigned char, D>(1)), IntType(0)));
+
+  return true;
+}
+
+constexpr bool test_large() {
+  constexpr size_t D = std::dynamic_extent;
+  test_iteration<std::extents<int64_t, D, 8, D, D>>(7, 9, 10);
+  test_iteration<std::extents<int64_t, D, 8, 1, D>>(7, 10);
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  // The large test iterates over ~10k loop indices.
+  // With assertions enabled this triggered the maximum default limit
+  // for steps in consteval expressions. Assertions roughly double the
+  // total number of instructions, so this was already close to the maximum.
+  test_large();
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/properties.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/properties.pass.cpp
new file mode 100644
index 00000000000000..26eb2e0fe65619
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/properties.pass.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// namespace std {
+//   template<class Extents>
+//   class layout_right::mapping {
+//
+//     ...
+//     static constexpr bool is_always_unique() noexcept { return true; }
+//     static constexpr bool is_always_exhaustive() noexcept { return true; }
+//     static constexpr bool is_always_strided() noexcept { return true; }
+//
+//     static constexpr bool is_unique() noexcept { return true; }
+//     static constexpr bool is_exhaustive() noexcept { return true; }
+//     static constexpr bool is_strided() noexcept { return true; }
+//     ...
+//   };
+// }
+
+#include <mdspan>
+#include <type_traits>
+#include <concepts>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <class E>
+constexpr void test_layout_mapping_right() {
+  using M = std::layout_right::template mapping<E>;
+  assert(M::is_unique() == true);
+  assert(M::is_exhaustive() == true);
+  assert(M::is_strided() == true);
+  assert(M::is_always_unique() == true);
+  assert(M::is_always_exhaustive() == true);
+  assert(M::is_always_strided() == true);
+  ASSERT_NOEXCEPT(std::declval<M>().is_unique());
+  ASSERT_NOEXCEPT(std::declval<M>().is_exhaustive());
+  ASSERT_NOEXCEPT(std::declval<M>().is_strided());
+  ASSERT_NOEXCEPT(M::is_always_unique());
+  ASSERT_NOEXCEPT(M::is_always_exhaustive());
+  ASSERT_NOEXCEPT(M::is_always_strided());
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_layout_mapping_right<std::extents<int>>();
+  test_layout_mapping_right<std::extents<char, 4, 5>>();
+  test_layout_mapping_right<std::extents<unsigned, D, 4>>();
+  test_layout_mapping_right<std::extents<size_t, D, D, D, D>>();
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/required_span_size.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/required_span_size.pass.cpp
new file mode 100644
index 00000000000000..a2efa4ade6a0bf
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/required_span_size.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// constexpr index_type required_span_size() const noexcept;
+//
+// Returns: extents().fwd-prod-of-extents(extents_type::rank()).
+
+
+#include <mdspan>
+#include <cassert>
+#include <cstdint>
+
+#include "test_macros.h"
+
+template <class E>
+constexpr void test_required_span_size(E e, typename E::index_type expected_size) {
+  using M = std::layout_right::mapping<E>;
+  const M m(e);
+
+  ASSERT_NOEXCEPT(m.required_span_size());
+  assert(m.required_span_size() == expected_size);
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_required_span_size(std::extents<int>(), 1);
+  test_required_span_size(std::extents<unsigned, D>(0), 0);
+  test_required_span_size(std::extents<unsigned, D>(1), 1);
+  test_required_span_size(std::extents<unsigned, D>(7), 7);
+  test_required_span_size(std::extents<unsigned, 7>(), 7);
+  test_required_span_size(std::extents<unsigned, 7, 8>(), 56);
+  test_required_span_size(std::extents<int64_t, D, 8, D, D>(7, 9, 10), 5040);
+  test_required_span_size(std::extents<int64_t, 1, 8, D, D>(9, 10), 720);
+  test_required_span_size(std::extents<int64_t, 1, 0, D, D>(9, 10), 0);
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/static_requirements.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/static_requirements.pass.cpp
new file mode 100644
index 00000000000000..33e736b15029c7
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/static_requirements.pass.cpp
@@ -0,0 +1,131 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// A type M meets the layout mapping requirements if
+//    - M models copyable and equality_comparable,
+//    - is_nothrow_move_constructible_v<M> is true,
+//    - is_nothrow_move_assignable_v<M> is true,
+//    - is_nothrow_swappable_v<M> is true, and
+//
+// the following types and expressions are well-formed and have the specified semantics.
+//
+//  typename M::extents_type
+//    Result: A type that is a specialization of extents.
+//
+//  typename M::index_type
+//    Result: typename M::extents_type::index_type.
+//
+//  typename M::rank_type
+//    Result: typename M::extents_type::rank_type.
+//
+//  typename M::layout_type
+//    Result: A type MP that meets the layout mapping policy requirements ([mdspan.layout.policy.reqmts]) and for which is-mapping-of<MP, M> is true.
+//
+//  m.extents()
+//    Result: const typename M::extents_type&
+//
+//  m(i...)
+//    Result: typename M::index_type
+//    Returns: A nonnegative integer less than numeric_limits<typename M::index_type>::max() and less than or equal to numeric_limits<size_t>::max().
+//
+//  m(i...) == m(static_cast<typename M::index_type>(i)...)
+//    Result: bool
+//    Returns: true
+//
+//  m.required_span_size()
+//    Result: typename M::index_type
+//    Returns: If the size of the multidimensional index space m.extents() is 0, then 0, else 1 plus the maximum value of m(i...) for all i.
+//
+//  m.is_unique()
+//    Result: bool
+//    Returns: true only if for every i and j where (i != j || ...) is true, m(i...) != m(j...) is true.
+//
+//  m.is_exhaustive()
+//    Result: bool
+//    Returns: true only if for all k in the range [0, m.required_span_size()) there exists an i such that m(i...) equals k.
+//
+//  m.is_strided()
+//    Result: bool
+//    Returns: true only if for every rank index r of m.extents() there exists an integer
+//             sr such that, for all i where (i+dr) is a multidimensional index in m.extents() ([mdspan.overview]),
+//             m((i + dr)...) - m(i...) equals sr
+//
+//  m.stride(r)
+//    Preconditions: m.is_strided() is true.
+//    Result: typename M::index_type
+//    Returns: sr as defined in m.is_strided() above.
+//
+//  M::is_always_unique()
+//    Result: A constant expression ([expr.const]) of type bool.
+//    Returns: true only if m.is_unique() is true for all possible objects m of type M.
+//
+//  M::is_always_exhaustive()
+//    Result: A constant expression ([expr.const]) of type bool.
+//    Returns: true only if m.is_exhaustive() is true for all possible objects m of type M.
+//
+//  M::is_always_strided()
+//    Result: A constant expression ([expr.const]) of type bool.
+//    Returns: true only if m.is_strided() is true for all possible objects m of type M.
+
+#include <mdspan>
+#include <type_traits>
+#include <concepts>
+#include <cassert>
+
+#include "test_macros.h"
+
+// Common requirements of all layout mappings
+template <class M, size_t... Idxs>
+void test_mapping_requirements(std::index_sequence<Idxs...>) {
+  using E = typename M::extents_type;
+  static_assert(std::__mdspan_detail::__is_extents_v<E>);
+  static_assert(std::is_copy_constructible_v<M>);
+  static_assert(std::is_nothrow_move_constructible_v<M>);
+  static_assert(std::is_nothrow_move_assignable_v<M>);
+  static_assert(std::is_nothrow_swappable_v<M>);
+  ASSERT_SAME_TYPE(typename M::index_type, typename E::index_type);
+  ASSERT_SAME_TYPE(typename M::size_type, typename E::size_type);
+  ASSERT_SAME_TYPE(typename M::rank_type, typename E::rank_type);
+  ASSERT_SAME_TYPE(typename M::layout_type, std::layout_right);
+  ASSERT_SAME_TYPE(typename M::layout_type::template mapping<E>, M);
+  static_assert(std::is_same_v<decltype(std::declval<M>().extents()), const E&>);
+  static_assert(std::is_same_v<decltype(std::declval<M>()(Idxs...)), typename M::index_type>);
+  static_assert(std::is_same_v<decltype(std::declval<M>().required_span_size()), typename M::index_type>);
+  static_assert(std::is_same_v<decltype(std::declval<M>().is_unique()), bool>);
+  static_assert(std::is_same_v<decltype(std::declval<M>().is_exhaustive()), bool>);
+  static_assert(std::is_same_v<decltype(std::declval<M>().is_strided()), bool>);
+  if constexpr (E::rank() > 0)
+    static_assert(std::is_same_v<decltype(std::declval<M>().stride(0)), typename M::index_type>);
+  static_assert(std::is_same_v<decltype(M::is_always_unique()), bool>);
+  static_assert(std::is_same_v<decltype(M::is_always_exhaustive()), bool>);
+  static_assert(std::is_same_v<decltype(M::is_always_strided()), bool>);
+}
+
+template <class L, class E>
+void test_layout_mapping_requirements() {
+  using M = typename L::template mapping<E>;
+  test_mapping_requirements<M>(std::make_index_sequence<E::rank()>());
+}
+
+template <class E>
+void test_layout_mapping_right() {
+  test_layout_mapping_requirements<std::layout_right, E>();
+}
+
+int main(int, char**) {
+  constexpr size_t D = std::dynamic_extent;
+  test_layout_mapping_right<std::extents<int>>();
+  test_layout_mapping_right<std::extents<char, 4, 5>>();
+  test_layout_mapping_right<std::extents<unsigned, D, 4>>();
+  test_layout_mapping_right<std::extents<size_t, D, D, D, D>>();
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/views/mdspan/layout_right/stride.pass.cpp b/libcxx/test/std/containers/views/mdspan/layout_right/stride.pass.cpp
new file mode 100644
index 00000000000000..9a27859afb9592
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/layout_right/stride.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <mdspan>
+
+// constexpr index_type stride(rank_type i) const noexcept;
+//
+//   Constraints: extents_type::rank() > 0 is true.
+//
+//   Preconditions: i < extents_type::rank() is true.
+//
+//   Returns: extents().rev-prod-of-extents(i).
+
+#include <mdspan>
+#include <cassert>
+#include <array>
+#include <cstdint>
+#include <cstdio>
+#include "test_macros.h"
+
+template <class E, class... Args>
+constexpr void test_stride(std::array<typename E::index_type, E::rank()> strides, Args... args) {
+  using M = std::layout_right::mapping<E>;
+  M m(E(args...));
+
+  ASSERT_NOEXCEPT(m.stride(0));
+  for (size_t r = 0; r < E::rank(); r++)
+    assert(strides[r] == m.stride(r));
+}
+
+constexpr bool test() {
+  constexpr size_t D = std::dynamic_extent;
+  test_stride<std::extents<unsigned, D>>(std::array<unsigned, 1>{1}, 7);
+  test_stride<std::extents<unsigned, 7>>(std::array<unsigned, 1>{1});
+  test_stride<std::extents<unsigned, 7, 8>>(std::array<unsigned, 2>{8, 1});
+  test_stride<std::extents<int64_t, D, 8, D, D>>(std::array<int64_t, 4>{720, 90, 10, 1}, 7, 9, 10);
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}


        


More information about the libcxx-commits mailing list