1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 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 7 // 8 // Kokkos v. 4.0 9 // Copyright (2022) National Technology & Engineering 10 // Solutions of Sandia, LLC (NTESS). 11 // 12 // Under the terms of Contract DE-NA0003525 with NTESS, 13 // the U.S. Government retains certain rights in this software. 14 // 15 //===---------------------------------------------------------------------===// 16 17 #ifndef _LIBCPP___MDSPAN_LAYOUT_RIGHT_H 18 #define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H 19 20 #include <__assert> 21 #include <__config> 22 #include <__fwd/mdspan.h> 23 #include <__mdspan/extents.h> 24 #include <__type_traits/is_constructible.h> 25 #include <__type_traits/is_convertible.h> 26 #include <__type_traits/is_nothrow_constructible.h> 27 #include <__utility/integer_sequence.h> 28 #include <cinttypes> 29 #include <cstddef> 30 #include <limits> 31 32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 33 # pragma GCC system_header 34 #endif 35 36 _LIBCPP_PUSH_MACROS 37 #include <__undef_macros> 38 39 _LIBCPP_BEGIN_NAMESPACE_STD 40 41 #if _LIBCPP_STD_VER >= 23 42 43 template <class _Extents> 44 class layout_right::mapping { 45 public: 46 static_assert(__mdspan_detail::__is_extents<_Extents>::value, 47 "layout_right::mapping template argument must be a specialization of extents."); 48 49 using extents_type = _Extents; 50 using index_type = typename extents_type::index_type; 51 using size_type = typename extents_type::size_type; 52 using rank_type = typename extents_type::rank_type; 53 using layout_type = layout_right; 54 55 private: 56 _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) { 57 if constexpr (extents_type::rank() == 0) 58 return true; 59 60 index_type __prod = __ext.extent(0); 61 for (rank_type __r = 1; __r < extents_type::rank(); __r++) { 62 bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod); 63 if (__overflowed) 64 return false; 65 } 66 return true; 67 } 68 69 static_assert((extents_type::rank_dynamic() > 0) || __required_span_size_is_representable(extents_type()), 70 "layout_right::mapping product of static extents must be representable as index_type."); 71 72 public: 73 // [mdspan.layout.right.cons], constructors 74 _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept = default; 75 _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default; 76 _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) { 77 _LIBCPP_ASSERT(__required_span_size_is_representable(__ext), 78 "layout_right::mapping extents ctor: product of extents must be representable as index_type."); 79 } 80 81 template <class _OtherExtents> 82 requires(is_constructible_v<extents_type, _OtherExtents>) 83 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>) 84 mapping(const mapping<_OtherExtents>& __other) noexcept 85 : __extents_(__other.extents()) { 86 _LIBCPP_ASSERT( 87 __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()), 88 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type."); 89 } 90 91 template <class _OtherExtents> 92 requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1) 93 _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>) 94 mapping(const layout_left::mapping<_OtherExtents>& __other) noexcept 95 : __extents_(__other.extents()) { 96 _LIBCPP_ASSERT( 97 __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()), 98 "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type."); 99 } 100 101 // FIXME: add when we add other layouts 102 # if 0 103 template<class _OtherExtents> 104 constexpr explicit(extents_type::rank() > 0) 105 mapping(const layout_stride::mapping_<OtherExtents>&) noexcept; 106 # endif 107 108 _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default; 109 110 // [mdspan.layout.right.obs], observers 111 _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; } 112 113 _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept { 114 index_type __size = 1; 115 for (size_t __r = 0; __r < extents_type::rank(); __r++) 116 __size *= __extents_.extent(__r); 117 return __size; 118 } 119 120 template <class... _Indices> 121 requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) && 122 (is_nothrow_constructible_v<index_type, _Indices> && ...)) 123 _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept { 124 _LIBCPP_ASSERT(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...), 125 "layout_right::mapping: out of bounds indexing"); 126 return [&]<size_t... _Pos>(index_sequence<_Pos...>) { 127 index_type __res = 0; 128 ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...); 129 return __res; 130 }(make_index_sequence<sizeof...(_Indices)>()); 131 } 132 133 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; } 134 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; } 135 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; } 136 137 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; } 138 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; } 139 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; } 140 141 _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept 142 requires(extents_type::rank() > 0) 143 { 144 _LIBCPP_ASSERT(__r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index"); 145 index_type __s = 1; 146 for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--) 147 __s *= __extents_.extent(__i); 148 return __s; 149 } 150 151 template <class _OtherExtents> 152 requires(_OtherExtents::rank() == extents_type::rank()) 153 _LIBCPP_HIDE_FROM_ABI friend constexpr bool 154 operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept { 155 return __lhs.extents() == __rhs.extents(); 156 } 157 158 private: 159 extents_type __extents_{}; // exposition only 160 }; 161 162 #endif // _LIBCPP_STD_VER >= 23 163 164 _LIBCPP_END_NAMESPACE_STD 165 166 _LIBCPP_POP_MACROS 167 168 #endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H 169