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 <span> // dynamic_extent 42 #include <type_traits> 43 44 #include "test_macros.h" 45 46 #include "../CustomTestLayouts.h" 47 48 template <bool implicit, class FromL, class ToE, class FromE> 49 constexpr void test_conversion(FromE src_exts) { 50 using To = std::layout_stride::mapping<ToE>; 51 using From = typename FromL::template mapping<FromE>; 52 53 From src([&]() { 54 if constexpr (std::is_same_v<FromL, std::layout_stride>) { 55 // just construct some strides which aren't layout_left/layout_right 56 std::array<size_t, FromE::rank()> strides; 57 size_t stride = 2; 58 for (size_t r = 0; r < FromE::rank(); r++) { 59 strides[r] = stride; 60 stride *= src_exts.extent(r); 61 } 62 return From(src_exts, strides); 63 } else { 64 return From(src_exts); 65 } 66 }()); 67 68 ASSERT_NOEXCEPT(To(src)); 69 To dest(src); 70 assert(dest == src); 71 72 if constexpr (implicit) { 73 To dest_implicit = src; 74 assert(dest_implicit == src); 75 } else { 76 assert((!std::is_convertible_v<From, To>)); 77 } 78 } 79 80 template <class FromL, class T1, class T2> 81 constexpr void test_conversion() { 82 constexpr size_t D = std::dynamic_extent; 83 constexpr bool idx_convertible = 84 static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max()); 85 constexpr bool l_convertible = 86 std::is_same_v<FromL, std::layout_right> || std::is_same_v<FromL, std::layout_left> || 87 std::is_same_v<FromL, std::layout_stride>; 88 constexpr bool idx_l_convertible = idx_convertible && l_convertible; 89 90 // clang-format off 91 // adding extents convertibility expectation 92 test_conversion<idx_l_convertible && true, FromL, std::extents<T1>>(std::extents<T2>()); 93 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D>>(std::extents<T2, D>(0)); 94 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D>>(std::extents<T2, D>(5)); 95 test_conversion<idx_l_convertible && false, FromL, std::extents<T1, 5>>(std::extents<T2, D>(5)); 96 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5>>(std::extents<T2, 5>()); 97 test_conversion<idx_l_convertible && false, FromL, std::extents<T1, 5, 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, D>(5, 5)); 99 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D>>(std::extents<T2, D, 7>(5)); 100 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>()); 101 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)); 102 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D, D, D, D>>( 103 std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1)); 104 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5)); 105 test_conversion<idx_l_convertible && true, FromL, std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>()); 106 // clang-format on 107 } 108 109 template <class IdxT, size_t... Extents> 110 using ToM = std::layout_stride::mapping<std::extents<IdxT, Extents...>>; 111 112 template <class FromL, class IdxT, size_t... Extents> 113 using FromM = typename FromL::template mapping<std::extents<IdxT, Extents...>>; 114 115 template <class FromL> 116 constexpr void test_no_implicit_conversion() { 117 constexpr size_t D = std::dynamic_extent; 118 119 // Sanity check that one static to dynamic conversion works 120 static_assert(std::is_constructible_v<ToM<int, D>, FromM<FromL, int, 5>>); 121 static_assert(std::is_convertible_v<FromM<FromL, int, 5>, ToM<int, D>>); 122 123 // Check that dynamic to static conversion only works explicitly 124 static_assert(std::is_constructible_v<ToM<int, 5>, FromM<FromL, int, D>>); 125 static_assert(!std::is_convertible_v<FromM<FromL, int, D>, ToM<int, 5>>); 126 127 // Sanity check that one static to dynamic conversion works 128 static_assert(std::is_constructible_v<ToM<int, D, 7>, FromM<FromL, int, 5, 7>>); 129 static_assert(std::is_convertible_v<FromM<FromL, int, 5, 7>, ToM<int, D, 7>>); 130 131 // Check that dynamic to static conversion only works explicitly 132 static_assert(std::is_constructible_v<ToM<int, 5, 7>, FromM<FromL, int, D, 7>>); 133 static_assert(!std::is_convertible_v<FromM<FromL, int, D, 7>, ToM<int, 5, 7>>); 134 135 // Sanity check that smaller index_type to larger index_type conversion works 136 static_assert(std::is_constructible_v<ToM<size_t, 5>, FromM<FromL, int, 5>>); 137 static_assert(std::is_convertible_v<FromM<FromL, int, 5>, ToM<size_t, 5>>); 138 139 // Check that larger index_type to smaller index_type conversion works explicitly only 140 static_assert(std::is_constructible_v<ToM<int, 5>, FromM<FromL, size_t, 5>>); 141 static_assert(!std::is_convertible_v<FromM<FromL, size_t, 5>, ToM<int, 5>>); 142 } 143 144 template <class FromL> 145 constexpr void test_rank_mismatch() { 146 constexpr size_t D = std::dynamic_extent; 147 148 static_assert(!std::is_constructible_v<ToM<int, D>, FromM<FromL, int>>); 149 static_assert(!std::is_constructible_v<ToM<int>, FromM<FromL, int, D, D>>); 150 static_assert(!std::is_constructible_v<ToM<int, D>, FromM<FromL, int, D, D>>); 151 static_assert(!std::is_constructible_v<ToM<int, D, D, D>, FromM<FromL, int, D, D>>); 152 } 153 154 template <class FromL> 155 constexpr void test_static_extent_mismatch() { 156 constexpr size_t D = std::dynamic_extent; 157 158 static_assert(!std::is_constructible_v<ToM<int, D, 5>, FromM<FromL, int, D, 4>>); 159 static_assert(!std::is_constructible_v<ToM<int, 5>, FromM<FromL, int, 4>>); 160 static_assert(!std::is_constructible_v<ToM<int, 5, D>, FromM<FromL, int, 4, D>>); 161 } 162 163 template <class FromL> 164 constexpr void test_layout() { 165 test_conversion<FromL, int, int>(); 166 test_conversion<FromL, int, size_t>(); 167 test_conversion<FromL, size_t, int>(); 168 test_conversion<FromL, size_t, long>(); 169 // the implicit convertibility test doesn't apply to non std::layouts 170 if constexpr (!std::is_same_v<FromL, always_convertible_layout>) 171 test_no_implicit_conversion<FromL>(); 172 test_rank_mismatch<FromL>(); 173 test_static_extent_mismatch<FromL>(); 174 } 175 176 constexpr bool test() { 177 test_layout<std::layout_right>(); 178 test_layout<std::layout_left>(); 179 test_layout<std::layout_stride>(); 180 test_layout<always_convertible_layout>(); 181 return true; 182 } 183 184 int main(int, char**) { 185 test(); 186 static_assert(test()); 187 return 0; 188 } 189