xref: /freebsd-src/contrib/llvm-project/libcxx/include/__mdspan/layout_right.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
106c3fb27SDimitry Andric // -*- C++ -*-
206c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
306c3fb27SDimitry Andric //
406c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
506c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
606c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
706c3fb27SDimitry Andric //
806c3fb27SDimitry Andric //                        Kokkos v. 4.0
906c3fb27SDimitry Andric //       Copyright (2022) National Technology & Engineering
1006c3fb27SDimitry Andric //               Solutions of Sandia, LLC (NTESS).
1106c3fb27SDimitry Andric //
1206c3fb27SDimitry Andric // Under the terms of Contract DE-NA0003525 with NTESS,
1306c3fb27SDimitry Andric // the U.S. Government retains certain rights in this software.
1406c3fb27SDimitry Andric //
1506c3fb27SDimitry Andric //===---------------------------------------------------------------------===//
1606c3fb27SDimitry Andric 
1706c3fb27SDimitry Andric #ifndef _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
1806c3fb27SDimitry Andric #define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
1906c3fb27SDimitry Andric 
2006c3fb27SDimitry Andric #include <__assert>
2106c3fb27SDimitry Andric #include <__config>
2206c3fb27SDimitry Andric #include <__fwd/mdspan.h>
2306c3fb27SDimitry Andric #include <__mdspan/extents.h>
2406c3fb27SDimitry Andric #include <__type_traits/is_constructible.h>
2506c3fb27SDimitry Andric #include <__type_traits/is_convertible.h>
2606c3fb27SDimitry Andric #include <__type_traits/is_nothrow_constructible.h>
2706c3fb27SDimitry Andric #include <__utility/integer_sequence.h>
2806c3fb27SDimitry Andric #include <cinttypes>
2906c3fb27SDimitry Andric #include <cstddef>
3006c3fb27SDimitry Andric #include <limits>
3106c3fb27SDimitry Andric 
3206c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
3306c3fb27SDimitry Andric #  pragma GCC system_header
3406c3fb27SDimitry Andric #endif
3506c3fb27SDimitry Andric 
3606c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
3706c3fb27SDimitry Andric #include <__undef_macros>
3806c3fb27SDimitry Andric 
3906c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
4006c3fb27SDimitry Andric 
4106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23
4206c3fb27SDimitry Andric 
4306c3fb27SDimitry Andric template <class _Extents>
4406c3fb27SDimitry Andric class layout_right::mapping {
4506c3fb27SDimitry Andric public:
4606c3fb27SDimitry Andric   static_assert(__mdspan_detail::__is_extents<_Extents>::value,
4706c3fb27SDimitry Andric                 "layout_right::mapping template argument must be a specialization of extents.");
4806c3fb27SDimitry Andric 
4906c3fb27SDimitry Andric   using extents_type = _Extents;
5006c3fb27SDimitry Andric   using index_type   = typename extents_type::index_type;
5106c3fb27SDimitry Andric   using size_type    = typename extents_type::size_type;
5206c3fb27SDimitry Andric   using rank_type    = typename extents_type::rank_type;
5306c3fb27SDimitry Andric   using layout_type  = layout_right;
5406c3fb27SDimitry Andric 
5506c3fb27SDimitry Andric private:
5606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
5706c3fb27SDimitry Andric     if constexpr (extents_type::rank() == 0)
5806c3fb27SDimitry Andric       return true;
5906c3fb27SDimitry Andric 
6006c3fb27SDimitry Andric     index_type __prod = __ext.extent(0);
6106c3fb27SDimitry Andric     for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
6206c3fb27SDimitry Andric       bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
6306c3fb27SDimitry Andric       if (__overflowed)
6406c3fb27SDimitry Andric         return false;
6506c3fb27SDimitry Andric     }
6606c3fb27SDimitry Andric     return true;
6706c3fb27SDimitry Andric   }
6806c3fb27SDimitry Andric 
69*0fca6ea1SDimitry Andric   static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),
7006c3fb27SDimitry Andric                 "layout_right::mapping product of static extents must be representable as index_type.");
7106c3fb27SDimitry Andric 
7206c3fb27SDimitry Andric public:
7306c3fb27SDimitry Andric   // [mdspan.layout.right.cons], constructors
7406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept               = default;
7506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
7606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {
778a4dda33SDimitry Andric     // not catching this could lead to out-of-bounds access later when used inside mdspan
788a4dda33SDimitry Andric     // mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(3, 10) == -126
798a4dda33SDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
808a4dda33SDimitry Andric         __required_span_size_is_representable(__ext),
8106c3fb27SDimitry Andric         "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
8206c3fb27SDimitry Andric   }
8306c3fb27SDimitry Andric 
8406c3fb27SDimitry Andric   template <class _OtherExtents>
8506c3fb27SDimitry Andric     requires(is_constructible_v<extents_type, _OtherExtents>)
8606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
8706c3fb27SDimitry Andric       mapping(const mapping<_OtherExtents>& __other) noexcept
8806c3fb27SDimitry Andric       : __extents_(__other.extents()) {
898a4dda33SDimitry Andric     // not catching this could lead to out-of-bounds access later when used inside mdspan
908a4dda33SDimitry Andric     // mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(3, 10) == -126
918a4dda33SDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
9206c3fb27SDimitry Andric         __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
9306c3fb27SDimitry Andric         "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
9406c3fb27SDimitry Andric   }
9506c3fb27SDimitry Andric 
9606c3fb27SDimitry Andric   template <class _OtherExtents>
9706c3fb27SDimitry Andric     requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1)
9806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
9906c3fb27SDimitry Andric       mapping(const layout_left::mapping<_OtherExtents>& __other) noexcept
10006c3fb27SDimitry Andric       : __extents_(__other.extents()) {
1018a4dda33SDimitry Andric     // not catching this could lead to out-of-bounds access later when used inside mdspan
1028a4dda33SDimitry Andric     // Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first
1038a4dda33SDimitry Andric     //       and thus this assertion should never be triggered, but keeping it here for consistency
1048a4dda33SDimitry Andric     // layout_right::mapping<dextents<char, 1>> map(
1058a4dda33SDimitry Andric     //           layout_left::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) ==
1068a4dda33SDimitry Andric     //           -56
1078a4dda33SDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
10806c3fb27SDimitry Andric         __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
10906c3fb27SDimitry Andric         "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
11006c3fb27SDimitry Andric   }
11106c3fb27SDimitry Andric 
11206c3fb27SDimitry Andric   template <class _OtherExtents>
1135f757f3fSDimitry Andric     requires(is_constructible_v<extents_type, _OtherExtents>)
1145f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)
1155f757f3fSDimitry Andric       mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept
1165f757f3fSDimitry Andric       : __extents_(__other.extents()) {
1175f757f3fSDimitry Andric     if constexpr (extents_type::rank() > 0) {
1185f757f3fSDimitry Andric       _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1195f757f3fSDimitry Andric           ([&]() {
1205f757f3fSDimitry Andric             using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>;
1215f757f3fSDimitry Andric             for (rank_type __r = 0; __r < extents_type::rank(); __r++)
1225f757f3fSDimitry Andric               if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r)))
1235f757f3fSDimitry Andric                 return false;
1245f757f3fSDimitry Andric             return true;
1255f757f3fSDimitry Andric           }()),
1265f757f3fSDimitry Andric           "layout_right::mapping from layout_stride ctor: strides are not compatible with layout_right.");
1275f757f3fSDimitry Andric       _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1285f757f3fSDimitry Andric           __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
1295f757f3fSDimitry Andric           "layout_right::mapping from layout_stride ctor: other.required_span_size() must be representable as "
1305f757f3fSDimitry Andric           "index_type.");
1315f757f3fSDimitry Andric     }
1325f757f3fSDimitry Andric   }
13306c3fb27SDimitry Andric 
13406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
13506c3fb27SDimitry Andric 
13606c3fb27SDimitry Andric   // [mdspan.layout.right.obs], observers
13706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
13806c3fb27SDimitry Andric 
13906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
14006c3fb27SDimitry Andric     index_type __size = 1;
14106c3fb27SDimitry Andric     for (size_t __r = 0; __r < extents_type::rank(); __r++)
14206c3fb27SDimitry Andric       __size *= __extents_.extent(__r);
14306c3fb27SDimitry Andric     return __size;
14406c3fb27SDimitry Andric   }
14506c3fb27SDimitry Andric 
14606c3fb27SDimitry Andric   template <class... _Indices>
14706c3fb27SDimitry Andric     requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
14806c3fb27SDimitry Andric              (is_nothrow_constructible_v<index_type, _Indices> && ...))
14906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
1508a4dda33SDimitry Andric     // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never
1518a4dda33SDimitry Andric     // return a value exceeding required_span_size(), which is used to know how large an allocation one needs
1528a4dda33SDimitry Andric     // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks
1538a4dda33SDimitry Andric     // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode
1545f757f3fSDimitry Andric     _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
15506c3fb27SDimitry Andric                                  "layout_right::mapping: out of bounds indexing");
15606c3fb27SDimitry Andric     return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
15706c3fb27SDimitry Andric       index_type __res = 0;
15806c3fb27SDimitry Andric       ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...);
15906c3fb27SDimitry Andric       return __res;
16006c3fb27SDimitry Andric     }(make_index_sequence<sizeof...(_Indices)>());
16106c3fb27SDimitry Andric   }
16206c3fb27SDimitry Andric 
16306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
16406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
16506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
16606c3fb27SDimitry Andric 
16706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
16806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
16906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
17006c3fb27SDimitry Andric 
17106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
17206c3fb27SDimitry Andric     requires(extents_type::rank() > 0)
17306c3fb27SDimitry Andric   {
1748a4dda33SDimitry Andric     // While it would be caught by extents itself too, using a too large __r
1758a4dda33SDimitry Andric     // is functionally an out of bounds access on the stored information needed to compute strides
1768a4dda33SDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1778a4dda33SDimitry Andric         __r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
17806c3fb27SDimitry Andric     index_type __s = 1;
17906c3fb27SDimitry Andric     for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--)
18006c3fb27SDimitry Andric       __s *= __extents_.extent(__i);
18106c3fb27SDimitry Andric     return __s;
18206c3fb27SDimitry Andric   }
18306c3fb27SDimitry Andric 
18406c3fb27SDimitry Andric   template <class _OtherExtents>
18506c3fb27SDimitry Andric     requires(_OtherExtents::rank() == extents_type::rank())
18606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI friend constexpr bool
18706c3fb27SDimitry Andric   operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
18806c3fb27SDimitry Andric     return __lhs.extents() == __rhs.extents();
18906c3fb27SDimitry Andric   }
19006c3fb27SDimitry Andric 
19106c3fb27SDimitry Andric private:
1928a4dda33SDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
19306c3fb27SDimitry Andric };
19406c3fb27SDimitry Andric 
19506c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 23
19606c3fb27SDimitry Andric 
19706c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
19806c3fb27SDimitry Andric 
19906c3fb27SDimitry Andric _LIBCPP_POP_MACROS
20006c3fb27SDimitry Andric 
20106c3fb27SDimitry Andric #endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
202