xref: /llvm-project/libcxx/include/__mdspan/layout_right.h (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1cfa096d9SChristian Trott // -*- C++ -*-
2cfa096d9SChristian Trott //===----------------------------------------------------------------------===//
3cfa096d9SChristian Trott //
4cfa096d9SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5cfa096d9SChristian Trott // See https://llvm.org/LICENSE.txt for license information.
6cfa096d9SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7cfa096d9SChristian Trott //
8cfa096d9SChristian Trott //                        Kokkos v. 4.0
9cfa096d9SChristian Trott //       Copyright (2022) National Technology & Engineering
10cfa096d9SChristian Trott //               Solutions of Sandia, LLC (NTESS).
11cfa096d9SChristian Trott //
12cfa096d9SChristian Trott // Under the terms of Contract DE-NA0003525 with NTESS,
13cfa096d9SChristian Trott // the U.S. Government retains certain rights in this software.
14cfa096d9SChristian Trott //
15cfa096d9SChristian Trott //===---------------------------------------------------------------------===//
16cfa096d9SChristian Trott 
17cfa096d9SChristian Trott #ifndef _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
18cfa096d9SChristian Trott #define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
19cfa096d9SChristian Trott 
20cfa096d9SChristian Trott #include <__assert>
21cfa096d9SChristian Trott #include <__config>
22*e99c4906SNikolas Klauser #include <__cstddef/size_t.h>
23cfa096d9SChristian Trott #include <__fwd/mdspan.h>
24cfa096d9SChristian Trott #include <__mdspan/extents.h>
255e19fd17SLouis Dionne #include <__type_traits/common_type.h>
26cfa096d9SChristian Trott #include <__type_traits/is_constructible.h>
27cfa096d9SChristian Trott #include <__type_traits/is_convertible.h>
28cfa096d9SChristian Trott #include <__type_traits/is_nothrow_constructible.h>
29cfa096d9SChristian Trott #include <__utility/integer_sequence.h>
30cfa096d9SChristian Trott 
31cfa096d9SChristian Trott #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32cfa096d9SChristian Trott #  pragma GCC system_header
33cfa096d9SChristian Trott #endif
34cfa096d9SChristian Trott 
35cfa096d9SChristian Trott _LIBCPP_PUSH_MACROS
36cfa096d9SChristian Trott #include <__undef_macros>
37cfa096d9SChristian Trott 
38cfa096d9SChristian Trott _LIBCPP_BEGIN_NAMESPACE_STD
39cfa096d9SChristian Trott 
40cfa096d9SChristian Trott #if _LIBCPP_STD_VER >= 23
41cfa096d9SChristian Trott 
42cfa096d9SChristian Trott template <class _Extents>
43cfa096d9SChristian Trott class layout_right::mapping {
44cfa096d9SChristian Trott public:
45cfa096d9SChristian Trott   static_assert(__mdspan_detail::__is_extents<_Extents>::value,
46cfa096d9SChristian Trott                 "layout_right::mapping template argument must be a specialization of extents.");
47cfa096d9SChristian Trott 
48cfa096d9SChristian Trott   using extents_type = _Extents;
49cfa096d9SChristian Trott   using index_type   = typename extents_type::index_type;
50cfa096d9SChristian Trott   using size_type    = typename extents_type::size_type;
51cfa096d9SChristian Trott   using rank_type    = typename extents_type::rank_type;
52cfa096d9SChristian Trott   using layout_type  = layout_right;
53cfa096d9SChristian Trott 
54cfa096d9SChristian Trott private:
55cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
56cfa096d9SChristian Trott     if constexpr (extents_type::rank() == 0)
57cfa096d9SChristian Trott       return true;
58cfa096d9SChristian Trott 
59cfa096d9SChristian Trott     index_type __prod = __ext.extent(0);
60cfa096d9SChristian Trott     for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
61cfa096d9SChristian Trott       bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
62cfa096d9SChristian Trott       if (__overflowed)
63cfa096d9SChristian Trott         return false;
64cfa096d9SChristian Trott     }
65cfa096d9SChristian Trott     return true;
66cfa096d9SChristian Trott   }
67cfa096d9SChristian Trott 
686b4b29f8SNikolas Klauser   static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),
69cfa096d9SChristian Trott                 "layout_right::mapping product of static extents must be representable as index_type.");
70cfa096d9SChristian Trott 
71cfa096d9SChristian Trott public:
72cfa096d9SChristian Trott   // [mdspan.layout.right.cons], constructors
73cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept               = default;
74cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
75cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {
76488c3db2SChristian Trott     // not catching this could lead to out-of-bounds access later when used inside mdspan
77488c3db2SChristian Trott     // mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(3, 10) == -126
78488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
79fc487657SChristian Trott         __required_span_size_is_representable(__ext),
80cfa096d9SChristian Trott         "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
81cfa096d9SChristian Trott   }
82cfa096d9SChristian Trott 
83cfa096d9SChristian Trott   template <class _OtherExtents>
84cfa096d9SChristian Trott     requires(is_constructible_v<extents_type, _OtherExtents>)
85cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
86cfa096d9SChristian Trott       mapping(const mapping<_OtherExtents>& __other) noexcept
87cfa096d9SChristian Trott       : __extents_(__other.extents()) {
88488c3db2SChristian Trott     // not catching this could lead to out-of-bounds access later when used inside mdspan
89488c3db2SChristian Trott     // mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(3, 10) == -126
90488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
91cfa096d9SChristian Trott         __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
92cfa096d9SChristian Trott         "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
93cfa096d9SChristian Trott   }
94cfa096d9SChristian Trott 
95b4ff8938SChristian Trott   template <class _OtherExtents>
96b4ff8938SChristian Trott     requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1)
97b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
98b4ff8938SChristian Trott       mapping(const layout_left::mapping<_OtherExtents>& __other) noexcept
99b4ff8938SChristian Trott       : __extents_(__other.extents()) {
100488c3db2SChristian Trott     // not catching this could lead to out-of-bounds access later when used inside mdspan
101488c3db2SChristian Trott     // Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first
102488c3db2SChristian Trott     //       and thus this assertion should never be triggered, but keeping it here for consistency
103488c3db2SChristian Trott     // layout_right::mapping<dextents<char, 1>> map(
104488c3db2SChristian Trott     //           layout_left::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) ==
105488c3db2SChristian Trott     //           -56
106488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
107b4ff8938SChristian Trott         __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
108b4ff8938SChristian Trott         "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
109b4ff8938SChristian Trott   }
110b4ff8938SChristian Trott 
111cfa096d9SChristian Trott   template <class _OtherExtents>
112639a0986SChristian Trott     requires(is_constructible_v<extents_type, _OtherExtents>)
113639a0986SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)
114639a0986SChristian Trott       mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept
115639a0986SChristian Trott       : __extents_(__other.extents()) {
116639a0986SChristian Trott     if constexpr (extents_type::rank() > 0) {
117639a0986SChristian Trott       _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
118639a0986SChristian Trott           ([&]() {
119639a0986SChristian Trott             using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>;
120639a0986SChristian Trott             for (rank_type __r = 0; __r < extents_type::rank(); __r++)
121639a0986SChristian Trott               if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r)))
122639a0986SChristian Trott                 return false;
123639a0986SChristian Trott             return true;
124639a0986SChristian Trott           }()),
125639a0986SChristian Trott           "layout_right::mapping from layout_stride ctor: strides are not compatible with layout_right.");
126639a0986SChristian Trott       _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
127639a0986SChristian Trott           __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
128639a0986SChristian Trott           "layout_right::mapping from layout_stride ctor: other.required_span_size() must be representable as "
129639a0986SChristian Trott           "index_type.");
130639a0986SChristian Trott     }
131639a0986SChristian Trott   }
132cfa096d9SChristian Trott 
133cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
134cfa096d9SChristian Trott 
135cfa096d9SChristian Trott   // [mdspan.layout.right.obs], observers
136cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
137cfa096d9SChristian Trott 
138cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
139cfa096d9SChristian Trott     index_type __size = 1;
140cfa096d9SChristian Trott     for (size_t __r = 0; __r < extents_type::rank(); __r++)
141cfa096d9SChristian Trott       __size *= __extents_.extent(__r);
142cfa096d9SChristian Trott     return __size;
143cfa096d9SChristian Trott   }
144cfa096d9SChristian Trott 
145cfa096d9SChristian Trott   template <class... _Indices>
146cfa096d9SChristian Trott     requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
147cfa096d9SChristian Trott              (is_nothrow_constructible_v<index_type, _Indices> && ...))
148cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
149488c3db2SChristian Trott     // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never
150488c3db2SChristian Trott     // return a value exceeding required_span_size(), which is used to know how large an allocation one needs
151488c3db2SChristian Trott     // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks
152488c3db2SChristian Trott     // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode
153ec4005ffSKonstantin Varlamov     _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
154cfa096d9SChristian Trott                                  "layout_right::mapping: out of bounds indexing");
155cfa096d9SChristian Trott     return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
156cfa096d9SChristian Trott       index_type __res = 0;
157cfa096d9SChristian Trott       ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...);
158cfa096d9SChristian Trott       return __res;
159cfa096d9SChristian Trott     }(make_index_sequence<sizeof...(_Indices)>());
160cfa096d9SChristian Trott   }
161cfa096d9SChristian Trott 
162cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
163cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
164cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
165cfa096d9SChristian Trott 
166cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
167cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
168cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
169cfa096d9SChristian Trott 
170cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
171cfa096d9SChristian Trott     requires(extents_type::rank() > 0)
172cfa096d9SChristian Trott   {
173488c3db2SChristian Trott     // While it would be caught by extents itself too, using a too large __r
174488c3db2SChristian Trott     // is functionally an out of bounds access on the stored information needed to compute strides
175488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
176488c3db2SChristian Trott         __r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
177cfa096d9SChristian Trott     index_type __s = 1;
178cfa096d9SChristian Trott     for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--)
179cfa096d9SChristian Trott       __s *= __extents_.extent(__i);
180cfa096d9SChristian Trott     return __s;
181cfa096d9SChristian Trott   }
182cfa096d9SChristian Trott 
183cfa096d9SChristian Trott   template <class _OtherExtents>
184cfa096d9SChristian Trott     requires(_OtherExtents::rank() == extents_type::rank())
185cfa096d9SChristian Trott   _LIBCPP_HIDE_FROM_ABI friend constexpr bool
186cfa096d9SChristian Trott   operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
187cfa096d9SChristian Trott     return __lhs.extents() == __rhs.extents();
188cfa096d9SChristian Trott   }
189cfa096d9SChristian Trott 
190cfa096d9SChristian Trott private:
191fc487657SChristian Trott   _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
192cfa096d9SChristian Trott };
193cfa096d9SChristian Trott 
194cfa096d9SChristian Trott #endif // _LIBCPP_STD_VER >= 23
195cfa096d9SChristian Trott 
196cfa096d9SChristian Trott _LIBCPP_END_NAMESPACE_STD
197cfa096d9SChristian Trott 
198cfa096d9SChristian Trott _LIBCPP_POP_MACROS
199cfa096d9SChristian Trott 
200cfa096d9SChristian Trott #endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
201