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_LEFT_H 1806c3fb27SDimitry Andric #define _LIBCPP___MDSPAN_LAYOUT_LEFT_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 <array> 2906c3fb27SDimitry Andric #include <cinttypes> 3006c3fb27SDimitry Andric #include <cstddef> 3106c3fb27SDimitry Andric #include <limits> 3206c3fb27SDimitry Andric 3306c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 3406c3fb27SDimitry Andric # pragma GCC system_header 3506c3fb27SDimitry Andric #endif 3606c3fb27SDimitry Andric 3706c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 3806c3fb27SDimitry Andric #include <__undef_macros> 3906c3fb27SDimitry Andric 4006c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 4106c3fb27SDimitry Andric 4206c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23 4306c3fb27SDimitry Andric 4406c3fb27SDimitry Andric template <class _Extents> 4506c3fb27SDimitry Andric class layout_left::mapping { 4606c3fb27SDimitry Andric public: 4706c3fb27SDimitry Andric static_assert(__mdspan_detail::__is_extents<_Extents>::value, 4806c3fb27SDimitry Andric "layout_left::mapping template argument must be a specialization of extents."); 4906c3fb27SDimitry Andric 5006c3fb27SDimitry Andric using extents_type = _Extents; 5106c3fb27SDimitry Andric using index_type = typename extents_type::index_type; 5206c3fb27SDimitry Andric using size_type = typename extents_type::size_type; 5306c3fb27SDimitry Andric using rank_type = typename extents_type::rank_type; 5406c3fb27SDimitry Andric using layout_type = layout_left; 5506c3fb27SDimitry Andric 5606c3fb27SDimitry Andric private: 5706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) { 5806c3fb27SDimitry Andric if constexpr (extents_type::rank() == 0) 5906c3fb27SDimitry Andric return true; 6006c3fb27SDimitry Andric 6106c3fb27SDimitry Andric index_type __prod = __ext.extent(0); 6206c3fb27SDimitry Andric for (rank_type __r = 1; __r < extents_type::rank(); __r++) { 6306c3fb27SDimitry Andric bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod); 6406c3fb27SDimitry Andric if (__overflowed) 6506c3fb27SDimitry Andric return false; 6606c3fb27SDimitry Andric } 6706c3fb27SDimitry Andric return true; 6806c3fb27SDimitry Andric } 6906c3fb27SDimitry Andric 70*0fca6ea1SDimitry Andric static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()), 7106c3fb27SDimitry Andric "layout_left::mapping product of static extents must be representable as index_type."); 7206c3fb27SDimitry Andric 7306c3fb27SDimitry Andric public: 7406c3fb27SDimitry Andric // [mdspan.layout.left.cons], constructors 7506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept = default; 7606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default; 7706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) { 788a4dda33SDimitry Andric // not catching this could lead to out-of-bounds access later when used inside mdspan 798a4dda33SDimitry Andric // mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(10, 3) == -126 808a4dda33SDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 818a4dda33SDimitry Andric __required_span_size_is_representable(__ext), 8206c3fb27SDimitry Andric "layout_left::mapping extents ctor: product of extents must be representable as index_type."); 8306c3fb27SDimitry Andric } 8406c3fb27SDimitry Andric 8506c3fb27SDimitry Andric template <class _OtherExtents> 8606c3fb27SDimitry Andric requires(is_constructible_v<extents_type, _OtherExtents>) 8706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>) 8806c3fb27SDimitry Andric mapping(const mapping<_OtherExtents>& __other) noexcept 8906c3fb27SDimitry Andric : __extents_(__other.extents()) { 908a4dda33SDimitry Andric // not catching this could lead to out-of-bounds access later when used inside mdspan 918a4dda33SDimitry Andric // mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(10, 3) == -126 928a4dda33SDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 9306c3fb27SDimitry Andric __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()), 9406c3fb27SDimitry Andric "layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type."); 9506c3fb27SDimitry Andric } 9606c3fb27SDimitry Andric 9706c3fb27SDimitry Andric template <class _OtherExtents> 9806c3fb27SDimitry Andric requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1) 9906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>) 10006c3fb27SDimitry Andric mapping(const layout_right::mapping<_OtherExtents>& __other) noexcept 10106c3fb27SDimitry Andric : __extents_(__other.extents()) { 1028a4dda33SDimitry Andric // not catching this could lead to out-of-bounds access later when used inside mdspan 1038a4dda33SDimitry Andric // Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first 1048a4dda33SDimitry Andric // and thus this assertion should never be triggered, but keeping it here for consistency 1058a4dda33SDimitry Andric // layout_left::mapping<dextents<char, 1>> map( 1068a4dda33SDimitry Andric // layout_right::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) == 1078a4dda33SDimitry Andric // -56 1088a4dda33SDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 10906c3fb27SDimitry Andric __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()), 11006c3fb27SDimitry Andric "layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type."); 11106c3fb27SDimitry Andric } 11206c3fb27SDimitry Andric 11306c3fb27SDimitry Andric template <class _OtherExtents> 1145f757f3fSDimitry Andric requires(is_constructible_v<extents_type, _OtherExtents>) 1155f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0) 1165f757f3fSDimitry Andric mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept 1175f757f3fSDimitry Andric : __extents_(__other.extents()) { 1185f757f3fSDimitry Andric if constexpr (extents_type::rank() > 0) { 1195f757f3fSDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 1205f757f3fSDimitry Andric ([&]() { 1215f757f3fSDimitry Andric using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>; 1225f757f3fSDimitry Andric for (rank_type __r = 0; __r < extents_type::rank(); __r++) 1235f757f3fSDimitry Andric if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r))) 1245f757f3fSDimitry Andric return false; 1255f757f3fSDimitry Andric return true; 1265f757f3fSDimitry Andric }()), 1275f757f3fSDimitry Andric "layout_left::mapping from layout_stride ctor: strides are not compatible with layout_left."); 1285f757f3fSDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 1295f757f3fSDimitry Andric __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()), 1305f757f3fSDimitry Andric "layout_left::mapping from layout_stride ctor: other.required_span_size() must be representable as " 1315f757f3fSDimitry Andric "index_type."); 1325f757f3fSDimitry Andric } 1335f757f3fSDimitry Andric } 13406c3fb27SDimitry Andric 13506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default; 13606c3fb27SDimitry Andric 13706c3fb27SDimitry Andric // [mdspan.layout.left.obs], observers 13806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; } 13906c3fb27SDimitry Andric 14006c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept { 14106c3fb27SDimitry Andric index_type __size = 1; 14206c3fb27SDimitry Andric for (size_t __r = 0; __r < extents_type::rank(); __r++) 14306c3fb27SDimitry Andric __size *= __extents_.extent(__r); 14406c3fb27SDimitry Andric return __size; 14506c3fb27SDimitry Andric } 14606c3fb27SDimitry Andric 14706c3fb27SDimitry Andric template <class... _Indices> 14806c3fb27SDimitry Andric requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) && 14906c3fb27SDimitry Andric (is_nothrow_constructible_v<index_type, _Indices> && ...)) 15006c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept { 1518a4dda33SDimitry Andric // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never 1528a4dda33SDimitry Andric // return a value exceeding required_span_size(), which is used to know how large an allocation one needs 1538a4dda33SDimitry Andric // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks 1548a4dda33SDimitry Andric // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode 1555f757f3fSDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...), 15606c3fb27SDimitry Andric "layout_left::mapping: out of bounds indexing"); 15706c3fb27SDimitry Andric array<index_type, extents_type::rank()> __idx_a{static_cast<index_type>(__idx)...}; 15806c3fb27SDimitry Andric return [&]<size_t... _Pos>(index_sequence<_Pos...>) { 15906c3fb27SDimitry Andric index_type __res = 0; 16006c3fb27SDimitry Andric ((__res = __idx_a[extents_type::rank() - 1 - _Pos] + __extents_.extent(extents_type::rank() - 1 - _Pos) * __res), 16106c3fb27SDimitry Andric ...); 16206c3fb27SDimitry Andric return __res; 16306c3fb27SDimitry Andric }(make_index_sequence<sizeof...(_Indices)>()); 16406c3fb27SDimitry Andric } 16506c3fb27SDimitry Andric 16606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; } 16706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; } 16806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; } 16906c3fb27SDimitry Andric 17006c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; } 17106c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; } 17206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; } 17306c3fb27SDimitry Andric 17406c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept 17506c3fb27SDimitry Andric requires(extents_type::rank() > 0) 17606c3fb27SDimitry Andric { 1778a4dda33SDimitry Andric // While it would be caught by extents itself too, using a too large __r 1788a4dda33SDimitry Andric // is functionally an out of bounds access on the stored information needed to compute strides 1798a4dda33SDimitry Andric _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 1808a4dda33SDimitry Andric __r < extents_type::rank(), "layout_left::mapping::stride(): invalid rank index"); 18106c3fb27SDimitry Andric index_type __s = 1; 1828a4dda33SDimitry Andric for (rank_type __i = 0; __i < __r; __i++) 18306c3fb27SDimitry Andric __s *= __extents_.extent(__i); 18406c3fb27SDimitry Andric return __s; 18506c3fb27SDimitry Andric } 18606c3fb27SDimitry Andric 18706c3fb27SDimitry Andric template <class _OtherExtents> 18806c3fb27SDimitry Andric requires(_OtherExtents::rank() == extents_type::rank()) 18906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI friend constexpr bool 19006c3fb27SDimitry Andric operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept { 19106c3fb27SDimitry Andric return __lhs.extents() == __rhs.extents(); 19206c3fb27SDimitry Andric } 19306c3fb27SDimitry Andric 19406c3fb27SDimitry Andric private: 1958a4dda33SDimitry Andric _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{}; 19606c3fb27SDimitry Andric }; 19706c3fb27SDimitry Andric 19806c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 23 19906c3fb27SDimitry Andric 20006c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD 20106c3fb27SDimitry Andric 20206c3fb27SDimitry Andric _LIBCPP_POP_MACROS 20306c3fb27SDimitry Andric 20406c3fb27SDimitry Andric #endif // _LIBCPP___MDSPAN_LAYOUT_LEFT_H 205