xref: /llvm-project/libcxx/include/__mdspan/layout_left.h (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1b4ff8938SChristian Trott // -*- C++ -*-
2b4ff8938SChristian Trott //===----------------------------------------------------------------------===//
3b4ff8938SChristian Trott //
4b4ff8938SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5b4ff8938SChristian Trott // See https://llvm.org/LICENSE.txt for license information.
6b4ff8938SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7b4ff8938SChristian Trott //
8b4ff8938SChristian Trott //                        Kokkos v. 4.0
9b4ff8938SChristian Trott //       Copyright (2022) National Technology & Engineering
10b4ff8938SChristian Trott //               Solutions of Sandia, LLC (NTESS).
11b4ff8938SChristian Trott //
12b4ff8938SChristian Trott // Under the terms of Contract DE-NA0003525 with NTESS,
13b4ff8938SChristian Trott // the U.S. Government retains certain rights in this software.
14b4ff8938SChristian Trott //
15b4ff8938SChristian Trott //===---------------------------------------------------------------------===//
16b4ff8938SChristian Trott 
17b4ff8938SChristian Trott #ifndef _LIBCPP___MDSPAN_LAYOUT_LEFT_H
18b4ff8938SChristian Trott #define _LIBCPP___MDSPAN_LAYOUT_LEFT_H
19b4ff8938SChristian Trott 
20b4ff8938SChristian Trott #include <__assert>
21b4ff8938SChristian Trott #include <__config>
22b4ff8938SChristian Trott #include <__fwd/mdspan.h>
23b4ff8938SChristian Trott #include <__mdspan/extents.h>
24*5e19fd17SLouis Dionne #include <__type_traits/common_type.h>
25b4ff8938SChristian Trott #include <__type_traits/is_constructible.h>
26b4ff8938SChristian Trott #include <__type_traits/is_convertible.h>
27b4ff8938SChristian Trott #include <__type_traits/is_nothrow_constructible.h>
28b4ff8938SChristian Trott #include <__utility/integer_sequence.h>
29b4ff8938SChristian Trott #include <array>
30b4ff8938SChristian Trott 
31b4ff8938SChristian Trott #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32b4ff8938SChristian Trott #  pragma GCC system_header
33b4ff8938SChristian Trott #endif
34b4ff8938SChristian Trott 
35b4ff8938SChristian Trott _LIBCPP_PUSH_MACROS
36b4ff8938SChristian Trott #include <__undef_macros>
37b4ff8938SChristian Trott 
38b4ff8938SChristian Trott _LIBCPP_BEGIN_NAMESPACE_STD
39b4ff8938SChristian Trott 
40b4ff8938SChristian Trott #if _LIBCPP_STD_VER >= 23
41b4ff8938SChristian Trott 
42b4ff8938SChristian Trott template <class _Extents>
43b4ff8938SChristian Trott class layout_left::mapping {
44b4ff8938SChristian Trott public:
45b4ff8938SChristian Trott   static_assert(__mdspan_detail::__is_extents<_Extents>::value,
46b4ff8938SChristian Trott                 "layout_left::mapping template argument must be a specialization of extents.");
47b4ff8938SChristian Trott 
48b4ff8938SChristian Trott   using extents_type = _Extents;
49b4ff8938SChristian Trott   using index_type   = typename extents_type::index_type;
50b4ff8938SChristian Trott   using size_type    = typename extents_type::size_type;
51b4ff8938SChristian Trott   using rank_type    = typename extents_type::rank_type;
52b4ff8938SChristian Trott   using layout_type  = layout_left;
53b4ff8938SChristian Trott 
54b4ff8938SChristian Trott private:
55b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
56b4ff8938SChristian Trott     if constexpr (extents_type::rank() == 0)
57b4ff8938SChristian Trott       return true;
58b4ff8938SChristian Trott 
59b4ff8938SChristian Trott     index_type __prod = __ext.extent(0);
60b4ff8938SChristian Trott     for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
61b4ff8938SChristian Trott       bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
62b4ff8938SChristian Trott       if (__overflowed)
63b4ff8938SChristian Trott         return false;
64b4ff8938SChristian Trott     }
65b4ff8938SChristian Trott     return true;
66b4ff8938SChristian Trott   }
67b4ff8938SChristian Trott 
686b4b29f8SNikolas Klauser   static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),
69b4ff8938SChristian Trott                 "layout_left::mapping product of static extents must be representable as index_type.");
70b4ff8938SChristian Trott 
71b4ff8938SChristian Trott public:
72b4ff8938SChristian Trott   // [mdspan.layout.left.cons], constructors
73b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept               = default;
74b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
75b4ff8938SChristian 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(10, 3) == -126
78488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
79fc487657SChristian Trott         __required_span_size_is_representable(__ext),
80b4ff8938SChristian Trott         "layout_left::mapping extents ctor: product of extents must be representable as index_type.");
81b4ff8938SChristian Trott   }
82b4ff8938SChristian Trott 
83b4ff8938SChristian Trott   template <class _OtherExtents>
84b4ff8938SChristian Trott     requires(is_constructible_v<extents_type, _OtherExtents>)
85b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
86b4ff8938SChristian Trott       mapping(const mapping<_OtherExtents>& __other) noexcept
87b4ff8938SChristian 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(10, 3) == -126
90488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
91b4ff8938SChristian Trott         __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
92b4ff8938SChristian Trott         "layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type.");
93b4ff8938SChristian Trott   }
94b4ff8938SChristian 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_right::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_left::mapping<dextents<char, 1>> map(
104488c3db2SChristian Trott     //           layout_right::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_left::mapping converting ctor: other.required_span_size() must be representable as index_type.");
109b4ff8938SChristian Trott   }
110b4ff8938SChristian Trott 
111b4ff8938SChristian 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_left::mapping from layout_stride ctor: strides are not compatible with layout_left.");
126639a0986SChristian Trott       _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
127639a0986SChristian Trott           __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
128639a0986SChristian Trott           "layout_left::mapping from layout_stride ctor: other.required_span_size() must be representable as "
129639a0986SChristian Trott           "index_type.");
130639a0986SChristian Trott     }
131639a0986SChristian Trott   }
132b4ff8938SChristian Trott 
133b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
134b4ff8938SChristian Trott 
135b4ff8938SChristian Trott   // [mdspan.layout.left.obs], observers
136b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
137b4ff8938SChristian Trott 
138b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
139b4ff8938SChristian Trott     index_type __size = 1;
140b4ff8938SChristian Trott     for (size_t __r = 0; __r < extents_type::rank(); __r++)
141b4ff8938SChristian Trott       __size *= __extents_.extent(__r);
142b4ff8938SChristian Trott     return __size;
143b4ff8938SChristian Trott   }
144b4ff8938SChristian Trott 
145b4ff8938SChristian Trott   template <class... _Indices>
146b4ff8938SChristian Trott     requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
147b4ff8938SChristian Trott              (is_nothrow_constructible_v<index_type, _Indices> && ...))
148b4ff8938SChristian 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...),
154b4ff8938SChristian Trott                                  "layout_left::mapping: out of bounds indexing");
155b4ff8938SChristian Trott     array<index_type, extents_type::rank()> __idx_a{static_cast<index_type>(__idx)...};
156b4ff8938SChristian Trott     return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
157b4ff8938SChristian Trott       index_type __res = 0;
158b4ff8938SChristian Trott       ((__res = __idx_a[extents_type::rank() - 1 - _Pos] + __extents_.extent(extents_type::rank() - 1 - _Pos) * __res),
159b4ff8938SChristian Trott        ...);
160b4ff8938SChristian Trott       return __res;
161b4ff8938SChristian Trott     }(make_index_sequence<sizeof...(_Indices)>());
162b4ff8938SChristian Trott   }
163b4ff8938SChristian Trott 
164b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
165b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
166b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
167b4ff8938SChristian Trott 
168b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
169b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
170b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
171b4ff8938SChristian Trott 
172b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
173b4ff8938SChristian Trott     requires(extents_type::rank() > 0)
174b4ff8938SChristian Trott   {
175488c3db2SChristian Trott     // While it would be caught by extents itself too, using a too large __r
176488c3db2SChristian Trott     // is functionally an out of bounds access on the stored information needed to compute strides
177488c3db2SChristian Trott     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
178488c3db2SChristian Trott         __r < extents_type::rank(), "layout_left::mapping::stride(): invalid rank index");
179b4ff8938SChristian Trott     index_type __s = 1;
1800f4d7d81SChristian Trott     for (rank_type __i = 0; __i < __r; __i++)
181b4ff8938SChristian Trott       __s *= __extents_.extent(__i);
182b4ff8938SChristian Trott     return __s;
183b4ff8938SChristian Trott   }
184b4ff8938SChristian Trott 
185b4ff8938SChristian Trott   template <class _OtherExtents>
186b4ff8938SChristian Trott     requires(_OtherExtents::rank() == extents_type::rank())
187b4ff8938SChristian Trott   _LIBCPP_HIDE_FROM_ABI friend constexpr bool
188b4ff8938SChristian Trott   operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
189b4ff8938SChristian Trott     return __lhs.extents() == __rhs.extents();
190b4ff8938SChristian Trott   }
191b4ff8938SChristian Trott 
192b4ff8938SChristian Trott private:
193fc487657SChristian Trott   _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
194b4ff8938SChristian Trott };
195b4ff8938SChristian Trott 
196b4ff8938SChristian Trott #endif // _LIBCPP_STD_VER >= 23
197b4ff8938SChristian Trott 
198b4ff8938SChristian Trott _LIBCPP_END_NAMESPACE_STD
199b4ff8938SChristian Trott 
200b4ff8938SChristian Trott _LIBCPP_POP_MACROS
201b4ff8938SChristian Trott 
202b4ff8938SChristian Trott #endif // _LIBCPP___MDSPAN_LAYOUT_LEFT_H
203