[libcxx-commits] [libcxx] [libc++] Partially implement P2642R6: Padded mdspan layouts (PR #187873)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Mar 21 11:00:01 PDT 2026
================
@@ -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): required span size must be representable as index_type.");
+ }
+ if constexpr (__static_padding_stride == dynamic_extent)
+ __stride_1_ = __stride_1_type(__stride_1);
+ }
+ }
+
+ template <class _OtherIndexType>
+ requires is_convertible_v<_OtherIndexType, index_type> && is_nothrow_constructible_v<index_type, _OtherIndexType>
+ _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext, _OtherIndexType __pad) : __extents_(__ext) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __mdspan_detail::__is_representable_as<index_type>(__pad),
+ "layout_left_padded::mapping(extents, pad): pad must be representable as index_type.");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ static_cast<index_type>(__pad) > 0, "layout_left_padded::mapping(extents, pad): pad must be greater than 0.");
+
+ if constexpr (padding_value != dynamic_extent)
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(padding_value == static_cast<index_type>(__pad),
+ "layout_left_padded::mapping(extents, pad): pad must equal padding_value.");
+
+ if constexpr (__rank_ > 1) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __mdspan_detail::__least_multiple_at_least_is_representable_as<index_type>(
+ static_cast<index_type>(__pad), __ext.extent(0)),
+ "layout_left_padded::mapping(extents, pad): padded stride must be representable as index_type.");
+
+ const index_type __stride_1 =
+ __mdspan_detail::__least_multiple_at_least(static_cast<index_type>(__pad), __ext.extent(0));
+
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __padded_product_is_representable(__ext, __stride_1),
+ "layout_left_padded::mapping(extents, pad): required span size must be representable as index_type.");
+
+ if constexpr (__static_padding_stride == dynamic_extent)
+ __stride_1_ = __stride_1_type(__stride_1);
+ }
+ }
+
+ template <class _OtherExtents>
+ requires is_constructible_v<extents_type, _OtherExtents>
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
+ mapping(const layout_left::mapping<_OtherExtents>& __other)
+ : mapping(extents_type(__other.extents())) {
+ static_assert(_OtherExtents::rank() <= 1 || __static_padding_stride == dynamic_extent ||
+ _OtherExtents::static_extent(0) == dynamic_extent ||
+ __static_padding_stride == _OtherExtents::static_extent(0));
+
+ if constexpr (__rank_ > 1 && padding_value != dynamic_extent) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __other.stride(1) ==
+ __mdspan_detail::__least_multiple_at_least(
+ static_cast<index_type>(padding_value), static_cast<index_type>(__other.extents().extent(0))),
+ "layout_left_padded::mapping from layout_left ctor: source stride must match the padded stride implied by "
+ "padding_value.");
+ }
+
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
+ "layout_left_padded::mapping from layout_left ctor: other.required_span_size() must be representable as "
+ "index_type.");
+ }
+
+ template <class _OtherExtents>
+ requires is_constructible_v<extents_type, _OtherExtents>
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit(!(__rank_ == 0 && is_convertible_v<_OtherExtents, extents_type>))
+ mapping(const layout_stride::mapping<_OtherExtents>& __other)
+ : __extents_(extents_type(__other.extents())) {
+ if constexpr (__rank_ > 1 && padding_value != dynamic_extent)
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __other.stride(1) ==
+ __mdspan_detail::__least_multiple_at_least(
+ static_cast<index_type>(padding_value), static_cast<index_type>(__other.extents().extent(0))),
+ "layout_left_padded::mapping from layout_stride ctor: source stride must match the padded stride implied by "
+ "padding_value.");
+
+ if constexpr (__rank_ > 0)
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __other.stride(0) == 1, "layout_left_padded::mapping from layout_stride ctor: stride(0) must equal 1.");
+
+ if constexpr (__rank_ > 2) {
+ using _Common = common_type_t<index_type, typename layout_stride::mapping<_OtherExtents>::index_type>;
+ _Common __expected = static_cast<_Common>(__other.stride(1));
+ for (rank_type __r = 2; __r < __rank_; ++__r) {
+ __expected *= static_cast<_Common>(__other.extents().extent(__r - 1));
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ static_cast<_Common>(__other.stride(__r)) == __expected,
+ "layout_left_padded::mapping from layout_stride ctor: strides are not compatible with layout_left_padded.");
+ }
+ }
+
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
+ "layout_left_padded::mapping from layout_stride ctor: other.required_span_size() must be representable as "
+ "index_type.");
+
+ if constexpr (__rank_ > 1 && __static_padding_stride == dynamic_extent) {
+# if 0 // TODO
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __mdspan_detail::__is_representable_as<index_type>(__other.stride(1)),
+ "layout_left_padded::mapping from layout_stride ctor: source padded stride must be representable as "
+ "index_type.");
+# endif
----------------
eiytoq wrote:
Submitted [LWG4545](https://cplusplus.github.io/LWG/issue4545) for this.
https://github.com/llvm/llvm-project/pull/187873
More information about the libcxx-commits
mailing list