layout_right.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
  17. #define _LIBCPP___MDSPAN_LAYOUT_RIGHT_H
  18. #include <__assert>
  19. #include <__config>
  20. #include <__fwd/mdspan.h>
  21. #include <__mdspan/extents.h>
  22. #include <__type_traits/is_constructible.h>
  23. #include <__type_traits/is_convertible.h>
  24. #include <__type_traits/is_nothrow_constructible.h>
  25. #include <__utility/integer_sequence.h>
  26. #include <cinttypes>
  27. #include <cstddef>
  28. #include <limits>
  29. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  30. # pragma GCC system_header
  31. #endif
  32. _LIBCPP_PUSH_MACROS
  33. #include <__undef_macros>
  34. _LIBCPP_BEGIN_NAMESPACE_STD
  35. #if _LIBCPP_STD_VER >= 23
  36. template <class _Extents>
  37. class layout_right::mapping {
  38. public:
  39. static_assert(__mdspan_detail::__is_extents<_Extents>::value,
  40. "layout_right::mapping template argument must be a specialization of extents.");
  41. using extents_type = _Extents;
  42. using index_type = typename extents_type::index_type;
  43. using size_type = typename extents_type::size_type;
  44. using rank_type = typename extents_type::rank_type;
  45. using layout_type = layout_right;
  46. private:
  47. _LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {
  48. if constexpr (extents_type::rank() == 0)
  49. return true;
  50. index_type __prod = __ext.extent(0);
  51. for (rank_type __r = 1; __r < extents_type::rank(); __r++) {
  52. bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);
  53. if (__overflowed)
  54. return false;
  55. }
  56. return true;
  57. }
  58. static_assert((extents_type::rank_dynamic() > 0) || __required_span_size_is_representable(extents_type()),
  59. "layout_right::mapping product of static extents must be representable as index_type.");
  60. public:
  61. // [mdspan.layout.right.cons], constructors
  62. _LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept = default;
  63. _LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;
  64. _LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {
  65. // not catching this could lead to out-of-bounds access later when used inside mdspan
  66. // mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(3, 10) == -126
  67. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  68. __required_span_size_is_representable(__ext),
  69. "layout_right::mapping extents ctor: product of extents must be representable as index_type.");
  70. }
  71. template <class _OtherExtents>
  72. requires(is_constructible_v<extents_type, _OtherExtents>)
  73. _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
  74. mapping(const mapping<_OtherExtents>& __other) noexcept
  75. : __extents_(__other.extents()) {
  76. // not catching this could lead to out-of-bounds access later when used inside mdspan
  77. // mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(3, 10) == -126
  78. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  79. __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
  80. "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
  81. }
  82. template <class _OtherExtents>
  83. requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1)
  84. _LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
  85. mapping(const layout_left::mapping<_OtherExtents>& __other) noexcept
  86. : __extents_(__other.extents()) {
  87. // not catching this could lead to out-of-bounds access later when used inside mdspan
  88. // Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first
  89. // and thus this assertion should never be triggered, but keeping it here for consistency
  90. // layout_right::mapping<dextents<char, 1>> map(
  91. // layout_left::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) ==
  92. // -56
  93. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  94. __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
  95. "layout_right::mapping converting ctor: other.required_span_size() must be representable as index_type.");
  96. }
  97. template <class _OtherExtents>
  98. requires(is_constructible_v<extents_type, _OtherExtents>)
  99. _LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)
  100. mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept
  101. : __extents_(__other.extents()) {
  102. if constexpr (extents_type::rank() > 0) {
  103. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  104. ([&]() {
  105. using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>;
  106. for (rank_type __r = 0; __r < extents_type::rank(); __r++)
  107. if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r)))
  108. return false;
  109. return true;
  110. }()),
  111. "layout_right::mapping from layout_stride ctor: strides are not compatible with layout_right.");
  112. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  113. __mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),
  114. "layout_right::mapping from layout_stride ctor: other.required_span_size() must be representable as "
  115. "index_type.");
  116. }
  117. }
  118. _LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;
  119. // [mdspan.layout.right.obs], observers
  120. _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }
  121. _LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {
  122. index_type __size = 1;
  123. for (size_t __r = 0; __r < extents_type::rank(); __r++)
  124. __size *= __extents_.extent(__r);
  125. return __size;
  126. }
  127. template <class... _Indices>
  128. requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&
  129. (is_nothrow_constructible_v<index_type, _Indices> && ...))
  130. _LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {
  131. // Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never
  132. // return a value exceeding required_span_size(), which is used to know how large an allocation one needs
  133. // Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks
  134. // However, mdspan does check this on its own, so for now we avoid double checking in hardened mode
  135. _LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),
  136. "layout_right::mapping: out of bounds indexing");
  137. return [&]<size_t... _Pos>(index_sequence<_Pos...>) {
  138. index_type __res = 0;
  139. ((__res = static_cast<index_type>(__idx) + __extents_.extent(_Pos) * __res), ...);
  140. return __res;
  141. }(make_index_sequence<sizeof...(_Indices)>());
  142. }
  143. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }
  144. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }
  145. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }
  146. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }
  147. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }
  148. _LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }
  149. _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept
  150. requires(extents_type::rank() > 0)
  151. {
  152. // While it would be caught by extents itself too, using a too large __r
  153. // is functionally an out of bounds access on the stored information needed to compute strides
  154. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  155. __r < extents_type::rank(), "layout_right::mapping::stride(): invalid rank index");
  156. index_type __s = 1;
  157. for (rank_type __i = extents_type::rank() - 1; __i > __r; __i--)
  158. __s *= __extents_.extent(__i);
  159. return __s;
  160. }
  161. template <class _OtherExtents>
  162. requires(_OtherExtents::rank() == extents_type::rank())
  163. _LIBCPP_HIDE_FROM_ABI friend constexpr bool
  164. operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {
  165. return __lhs.extents() == __rhs.extents();
  166. }
  167. private:
  168. _LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};
  169. };
  170. #endif // _LIBCPP_STD_VER >= 23
  171. _LIBCPP_END_NAMESPACE_STD
  172. _LIBCPP_POP_MACROS
  173. #endif // _LIBCPP___MDSPAN_LAYOUT_RIGHT_H