| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <mdspan> |
| 12 | |
| 13 | // template<class StridedLayoutMapping> |
| 14 | // constexpr explicit(see below) |
| 15 | // mapping(const StridedLayoutMapping& other) noexcept; |
| 16 | // |
| 17 | // Constraints: |
| 18 | // - layout-mapping-alike<StridedLayoutMapping> is satisfied. |
| 19 | // - is_constructible_v<extents_type, typename StridedLayoutMapping::extents_type> is true. |
| 20 | // - StridedLayoutMapping::is_always_unique() is true. |
| 21 | // - StridedLayoutMapping::is_always_strided() is true. |
| 22 | // |
| 23 | // Preconditions: |
| 24 | // - StridedLayoutMapping meets the layout mapping requirements ([mdspan.layout.policy.reqmts]), |
| 25 | // - other.stride(r) > 0 is true for every rank index r of extents(), |
| 26 | // - other.required_span_size() is representable as a value of type index_type ([basic.fundamental]), and |
| 27 | // - OFFSET(other) == 0 is true. |
| 28 | // |
| 29 | // Effects: Direct-non-list-initializes extents_ with other.extents(), and for all d in the range [0, rank_), |
| 30 | // direct-non-list-initializes strides_[d] with other.stride(d). |
| 31 | // |
| 32 | // Remarks: The expression inside explicit is equivalent to: |
| 33 | // - !(is_convertible_v<typename StridedLayoutMapping::extents_type, extents_type> && |
| 34 | // (is-mapping-of<layout_left, LayoutStrideMapping> || |
| 35 | // is-mapping-of<layout_right, LayoutStrideMapping> || |
| 36 | // is-mapping-of<layout_stride, LayoutStrideMapping>)) |
| 37 | |
| 38 | #include <mdspan> |
| 39 | #include <cassert> |
| 40 | #include <limits> |
| 41 | #include <type_traits> |
| 42 | |
| 43 | #include "test_macros.h" |
| 44 | |
| 45 | #include "../CustomTestLayouts.h" |
| 46 | |
| 47 | template <bool implicit, class FromL, class ToE, class FromE> |
| 48 | constexpr void test_conversion(FromE src_exts) { |
| 49 | using To = std::layout_stride::mapping<ToE>; |
| 50 | using From = typename FromL::template mapping<FromE>; |
| 51 | |
| 52 | From src([&]() { |
| 53 | if constexpr (std::is_same_v<FromL, std::layout_stride>) { |
| 54 | // just construct some strides which aren't layout_left/layout_right |
| 55 | std::array<size_t, FromE::rank()> strides; |
| 56 | size_t stride = 2; |
| 57 | for (size_t r = 0; r < FromE::rank(); r++) { |
| 58 | strides[r] = stride; |
| 59 | stride *= src_exts.extent(r); |
| 60 | } |
| 61 | return From(src_exts, strides); |
| 62 | } else { |
| 63 | return From(src_exts); |
| 64 | } |
| 65 | }()); |
| 66 | |
| 67 | ASSERT_NOEXCEPT(To(src)); |
| 68 | To dest(src); |
| 69 | assert(dest == src); |
| 70 | |
| 71 | if constexpr (implicit) { |
| 72 | To dest_implicit = src; |
| 73 | assert(dest_implicit == src); |
| 74 | } else { |
| 75 | assert((!std::is_convertible_v<From, To>)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | template <class FromL, class T1, class T2> |
| 80 | constexpr void test_conversion() { |
| 81 | constexpr size_t D = std::dynamic_extent; |
| 82 | constexpr bool idx_convertible = |
| 83 | static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max()); |
| 84 | constexpr bool l_convertible = |
| 85 | std::is_same_v<FromL, std::layout_right> || std::is_same_v<FromL, std::layout_left> || |
| 86 | std::is_same_v<FromL, std::layout_stride>; |
| 87 | constexpr bool idx_l_convertible = idx_convertible && l_convertible; |
| 88 | |
| 89 | // clang-format off |
| 90 | // adding extents convertibility expectation |
| 91 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1>>(std::extents<T2>()); |
| 92 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D>>(std::extents<T2, D>(0)); |
| 93 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D>>(std::extents<T2, D>(5)); |
| 94 | test_conversion<idx_l_convertible && false, FromL, std::extents<T1, 5>>(std::extents<T2, D>(5)); |
| 95 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5>>(std::extents<T2, 5>()); |
| 96 | test_conversion<idx_l_convertible && false, FromL, std::extents<T1, 5, D>>(std::extents<T2, D, D>(5, 5)); |
| 97 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D>>(std::extents<T2, D, D>(5, 5)); |
| 98 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D>>(std::extents<T2, D, 7>(5)); |
| 99 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>()); |
| 100 | test_conversion<idx_l_convertible && false, FromL, std::extents<T1, 5, D, 8, D, D>>(std::extents<T2, D, D, 8, 9, 1>(5, 7)); |
| 101 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D, D, D, D>>( |
| 102 | std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1)); |
| 103 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5)); |
| 104 | test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>()); |
| 105 | // clang-format on |
| 106 | } |
| 107 | |
| 108 | template <class IdxT, size_t... Extents> |
| 109 | using ToM = std::layout_stride::mapping<std::extents<IdxT, Extents...>>; |
| 110 | |
| 111 | template <class FromL, class IdxT, size_t... Extents> |
| 112 | using FromM = typename FromL::template mapping<std::extents<IdxT, Extents...>>; |
| 113 | |
| 114 | template <class FromL> |
| 115 | constexpr void test_no_implicit_conversion() { |
| 116 | constexpr size_t D = std::dynamic_extent; |
| 117 | |
| 118 | // Sanity check that one static to dynamic conversion works |
| 119 | static_assert(std::is_constructible_v<ToM<int, D>, FromM<FromL, int, 5>>); |
| 120 | static_assert(std::is_convertible_v<FromM<FromL, int, 5>, ToM<int, D>>); |
| 121 | |
| 122 | // Check that dynamic to static conversion only works explicitly |
| 123 | static_assert(std::is_constructible_v<ToM<int, 5>, FromM<FromL, int, D>>); |
| 124 | static_assert(!std::is_convertible_v<FromM<FromL, int, D>, ToM<int, 5>>); |
| 125 | |
| 126 | // Sanity check that one static to dynamic conversion works |
| 127 | static_assert(std::is_constructible_v<ToM<int, D, 7>, FromM<FromL, int, 5, 7>>); |
| 128 | static_assert(std::is_convertible_v<FromM<FromL, int, 5, 7>, ToM<int, D, 7>>); |
| 129 | |
| 130 | // Check that dynamic to static conversion only works explicitly |
| 131 | static_assert(std::is_constructible_v<ToM<int, 5, 7>, FromM<FromL, int, D, 7>>); |
| 132 | static_assert(!std::is_convertible_v<FromM<FromL, int, D, 7>, ToM<int, 5, 7>>); |
| 133 | |
| 134 | // Sanity check that smaller index_type to larger index_type conversion works |
| 135 | static_assert(std::is_constructible_v<ToM<size_t, 5>, FromM<FromL, int, 5>>); |
| 136 | static_assert(std::is_convertible_v<FromM<FromL, int, 5>, ToM<size_t, 5>>); |
| 137 | |
| 138 | // Check that larger index_type to smaller index_type conversion works explicitly only |
| 139 | static_assert(std::is_constructible_v<ToM<int, 5>, FromM<FromL, size_t, 5>>); |
| 140 | static_assert(!std::is_convertible_v<FromM<FromL, size_t, 5>, ToM<int, 5>>); |
| 141 | } |
| 142 | |
| 143 | template <class FromL> |
| 144 | constexpr void test_rank_mismatch() { |
| 145 | constexpr size_t D = std::dynamic_extent; |
| 146 | |
| 147 | static_assert(!std::is_constructible_v<ToM<int, D>, FromM<FromL, int>>); |
| 148 | static_assert(!std::is_constructible_v<ToM<int>, FromM<FromL, int, D, D>>); |
| 149 | static_assert(!std::is_constructible_v<ToM<int, D>, FromM<FromL, int, D, D>>); |
| 150 | static_assert(!std::is_constructible_v<ToM<int, D, D, D>, FromM<FromL, int, D, D>>); |
| 151 | } |
| 152 | |
| 153 | template <class FromL> |
| 154 | constexpr void test_static_extent_mismatch() { |
| 155 | constexpr size_t D = std::dynamic_extent; |
| 156 | |
| 157 | static_assert(!std::is_constructible_v<ToM<int, D, 5>, FromM<FromL, int, D, 4>>); |
| 158 | static_assert(!std::is_constructible_v<ToM<int, 5>, FromM<FromL, int, 4>>); |
| 159 | static_assert(!std::is_constructible_v<ToM<int, 5, D>, FromM<FromL, int, 4, D>>); |
| 160 | } |
| 161 | |
| 162 | template <class FromL> |
| 163 | constexpr void test_layout() { |
| 164 | test_conversion<FromL, int, int>(); |
| 165 | test_conversion<FromL, int, size_t>(); |
| 166 | test_conversion<FromL, size_t, int>(); |
| 167 | test_conversion<FromL, size_t, long>(); |
| 168 | // the implicit convertibility test doesn't apply to non std::layouts |
| 169 | if constexpr (!std::is_same_v<FromL, always_convertible_layout>) |
| 170 | test_no_implicit_conversion<FromL>(); |
| 171 | test_rank_mismatch<FromL>(); |
| 172 | test_static_extent_mismatch<FromL>(); |
| 173 | } |
| 174 | |
| 175 | constexpr bool test() { |
| 176 | test_layout<std::layout_right>(); |
| 177 | test_layout<std::layout_left>(); |
| 178 | test_layout<std::layout_stride>(); |
| 179 | test_layout<always_convertible_layout>(); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | int main(int, char**) { |
| 184 | test(); |
| 185 | static_assert(test()); |
| 186 | return 0; |
| 187 | } |
| 188 | |