2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 // Copyright (2022) National Technology & Engineering
10 // Solutions of Sandia, LLC (NTESS).
12 // Under the terms of Contract DE-NA0003525 with NTESS,
13 // the U.S. Government retains certain rights in this software.
15 //===---------------------------------------------------------------------===//
17 #ifndef _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
18 #define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
22 #include <__cstddef/size_t.h>
23 #include <__fwd/mdspan.h>
24 #include <__mdspan/extents.h>
25 #include <__type_traits/common_type.h>
26 #include <__type_traits/is_constructible.h>
27 #include <__type_traits/is_convertible.h>
28 #include <__type_traits/is_nothrow_constructible.h>
29 #include <__utility/integer_sequence.h>
31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32 # pragma GCC system_header
36 #include <__undef_macros>
38 _LIBCPP_BEGIN_NAMESPACE_STD
40 #if _LIBCPP_STD_VER >= 23
42 template <class _Extents
>
43 class layout_right::mapping
{
45 static_assert(__mdspan_detail::__is_extents
<_Extents
>::value
,
46 "layout_right::mapping template argument must be a specialization of extents.");
48 using extents_type
= _Extents
;
49 using index_type
= typename
extents_type::index_type
;
50 using size_type
= typename
extents_type::size_type
;
51 using rank_type
= typename
extents_type::rank_type
;
52 using layout_type
= layout_right
;
55 _LIBCPP_HIDE_FROM_ABI
static constexpr bool __required_span_size_is_representable(const extents_type
& __ext
) {
56 if constexpr (extents_type::rank() == 0)
59 index_type __prod
= __ext
.extent(0);
60 for (rank_type __r
= 1; __r
< extents_type::rank(); __r
++) {
61 bool __overflowed
= __builtin_mul_overflow(__prod
, __ext
.extent(__r
), &__prod
);
68 static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),
69 "layout_right::mapping product of static extents must be representable as index_type.");
72 // [mdspan.layout.right.cons], constructors
73 _LIBCPP_HIDE_FROM_ABI
constexpr mapping() noexcept
= default;
74 _LIBCPP_HIDE_FROM_ABI
constexpr mapping(const mapping
&) noexcept
= default;
75 _LIBCPP_HIDE_FROM_ABI
constexpr mapping(const extents_type
& __ext
) noexcept
: __extents_(__ext
) {
76 // not catching this could lead to out-of-bounds access later when used inside mdspan
77 // mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(3, 10) == -126
78 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
79 __required_span_size_is_representable(__ext
),
80 "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
83 template <class _OtherExtents
>
84 requires(is_constructible_v
<extents_type
, _OtherExtents
>)
85 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherExtents
, extents_type
>)
86 mapping(const mapping
<_OtherExtents
>& __other
) noexcept
87 : __extents_(__other
.extents()) {
88 // not catching this could lead to out-of-bounds access later when used inside mdspan
89 // mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(3, 10) == -126
90 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
91 __mdspan_detail::__is_representable_as
<index_type
>(__other
.required_span_size()),
92 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
95 template <class _OtherExtents
>
96 requires(is_constructible_v
<extents_type
, _OtherExtents
> && _OtherExtents::rank() <= 1)
97 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(!is_convertible_v
<_OtherExtents
, extents_type
>)
98 mapping(const layout_left::mapping
<_OtherExtents
>& __other
) noexcept
99 : __extents_(__other
.extents()) {
100 // not catching this could lead to out-of-bounds access later when used inside mdspan
101 // Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first
102 // and thus this assertion should never be triggered, but keeping it here for consistency
103 // layout_right::mapping<dextents<char, 1>> map(
104 // layout_left::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) ==
106 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
107 __mdspan_detail::__is_representable_as
<index_type
>(__other
.required_span_size()),
108 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
111 template <class _OtherExtents
>
112 requires(is_constructible_v
<extents_type
, _OtherExtents
>)
113 _LIBCPP_HIDE_FROM_ABI
constexpr explicit(extents_type::rank() > 0)
114 mapping(const layout_stride::mapping
<_OtherExtents
>& __other
) noexcept
115 : __extents_(__other
.extents()) {
116 if constexpr (extents_type::rank() > 0) {
117 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
119 using _CommonType
= common_type_t
<typename
extents_type::index_type
, typename
_OtherExtents::index_type
>;
120 for (rank_type __r
= 0; __r
< extents_type::rank(); __r
++)
121 if (static_cast<_CommonType
>(stride(__r
)) != static_cast<_CommonType
>(__other
.stride(__r
)))
125 "layout_right::mapping from layout_stride ctor: strides are not compatible with layout_right.");
126 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
127 __mdspan_detail::__is_representable_as
<index_type
>(__other
.required_span_size()),
128 "layout_right::mapping from layout_stride ctor: other.required_span_size() must be representable as "
133 _LIBCPP_HIDE_FROM_ABI
constexpr mapping
& operator=(const mapping
&) noexcept
= default;
135 // [mdspan.layout.right.obs], observers
136 _LIBCPP_HIDE_FROM_ABI
constexpr const extents_type
& extents() const noexcept
{ return __extents_
; }
138 _LIBCPP_HIDE_FROM_ABI
constexpr index_type
required_span_size() const noexcept
{
139 index_type __size
= 1;
140 for (size_t __r
= 0; __r
< extents_type::rank(); __r
++)
141 __size
*= __extents_
.extent(__r
);
145 template <class... _Indices
>
146 requires((sizeof...(_Indices
) == extents_type::rank()) && (is_convertible_v
<_Indices
, index_type
> && ...) &&
147 (is_nothrow_constructible_v
<index_type
, _Indices
> && ...))
148 _LIBCPP_HIDE_FROM_ABI
constexpr index_type
operator()(_Indices
... __idx
) const noexcept
{
149 // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never
150 // return a value exceeding required_span_size(), which is used to know how large an allocation one needs
151 // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks
152 // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode
153 _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_
, __idx
...),
154 "layout_right::mapping: out of bounds indexing");
155 return [&]<size_t... _Pos
>(index_sequence
<_Pos
...>) {
156 index_type __res
= 0;
157 ((__res
= static_cast<index_type
>(__idx
) + __extents_
.extent(_Pos
) * __res
), ...);
159 }(make_index_sequence
<sizeof...(_Indices
)>());
162 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_always_unique() noexcept
{ return true; }
163 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_always_exhaustive() noexcept
{ return true; }
164 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_always_strided() noexcept
{ return true; }
166 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_unique() noexcept
{ return true; }
167 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_exhaustive() noexcept
{ return true; }
168 _LIBCPP_HIDE_FROM_ABI
static constexpr bool is_strided() noexcept
{ return true; }
170 _LIBCPP_HIDE_FROM_ABI
constexpr index_type
stride(rank_type __r
) const noexcept
171 requires(extents_type::rank() > 0)
173 // While it would be caught by extents itself too, using a too large __r
174 // is functionally an out of bounds access on the stored information needed to compute strides
175 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
176 __r
< extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
178 for (rank_type __i
= extents_type::rank() - 1; __i
> __r
; __i
--)
179 __s
*= __extents_
.extent(__i
);
183 template <class _OtherExtents
>
184 requires(_OtherExtents::rank() == extents_type::rank())
185 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool
186 operator==(const mapping
& __lhs
, const mapping
<_OtherExtents
>& __rhs
) noexcept
{
187 return __lhs
.extents() == __rhs
.extents();
191 _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_
{};
194 #endif // _LIBCPP_STD_VER >= 23
196 _LIBCPP_END_NAMESPACE_STD
200 #endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H