| 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 |
// namespace std { |
| 14 |
// template<class Extents> |
| 15 |
// class layout_stride::mapping { |
| 16 |
// |
| 17 |
// ... |
| 18 |
// static constexpr bool is_always_unique() noexcept { return true; } |
| 19 |
// static constexpr bool is_always_exhaustive() noexcept { return false; } |
| 20 |
// static constexpr bool is_always_strided() noexcept { return true; } |
| 21 |
// |
| 22 |
// static constexpr bool is_unique() noexcept { return true; } |
| 23 |
// static constexpr bool is_exhaustive() noexcept; |
| 24 |
// static constexpr bool is_strided() noexcept { return true; } |
| 25 |
// ... |
| 26 |
// }; |
| 27 |
// } |
| 28 |
// |
| 29 |
// |
| 30 |
// layout_stride::mapping<E> is a trivially copyable type that models regular for each E. |
| 31 |
// |
| 32 |
// constexpr bool is_exhaustive() const noexcept; |
| 33 |
// |
| 34 |
// Returns: |
| 35 |
// - true if rank_ is 0. |
| 36 |
// - Otherwise, true if there is a permutation P of the integers in the range [0, rank_) such that |
| 37 |
// stride(p0) equals 1, and stride(pi) equals stride(pi_1) * extents().extent(pi_1) for i in the |
| 38 |
// range [1, rank_), where pi is the ith element of P. |
| 39 |
// - Otherwise, false. |
| 40 |
|
| 41 |
#include <mdspan> |
| 42 |
#include <type_traits> |
| 43 |
#include <concepts> |
| 44 |
#include <cassert> |
| 45 |
|
| 46 |
#include "test_macros.h" |
| 47 |
|
| 48 |
template <class E> |
| 49 |
constexpr void |
| 50 |
test_layout_mapping_stride(E ext, std::array<typename E::index_type, E::rank()> strides, bool exhaustive) { |
| 51 |
using M = std::layout_stride::mapping<E>; |
| 52 |
M m(ext, strides); |
| 53 |
const M c_m = m; |
| 54 |
assert(m.strides() == strides); |
| 55 |
assert(c_m.strides() == strides); |
| 56 |
assert(m.extents() == ext); |
| 57 |
assert(c_m.extents() == ext); |
| 58 |
assert(M::is_unique() == true); |
| 59 |
assert(m.is_exhaustive() == exhaustive); |
| 60 |
assert(c_m.is_exhaustive() == exhaustive); |
| 61 |
assert(M::is_strided() == true); |
| 62 |
assert(M::is_always_unique() == true); |
| 63 |
assert(M::is_always_exhaustive() == false); |
| 64 |
assert(M::is_always_strided() == true); |
| 65 |
|
| 66 |
ASSERT_NOEXCEPT(m.strides()); |
| 67 |
ASSERT_NOEXCEPT(c_m.strides()); |
| 68 |
ASSERT_NOEXCEPT(m.extents()); |
| 69 |
ASSERT_NOEXCEPT(c_m.extents()); |
| 70 |
ASSERT_NOEXCEPT(M::is_unique()); |
| 71 |
ASSERT_NOEXCEPT(m.is_exhaustive()); |
| 72 |
ASSERT_NOEXCEPT(c_m.is_exhaustive()); |
| 73 |
ASSERT_NOEXCEPT(M::is_strided()); |
| 74 |
ASSERT_NOEXCEPT(M::is_always_unique()); |
| 75 |
ASSERT_NOEXCEPT(M::is_always_exhaustive()); |
| 76 |
ASSERT_NOEXCEPT(M::is_always_strided()); |
| 77 |
|
| 78 |
for (typename E::rank_type r = 0; r < E::rank(); r++) { |
| 79 |
assert(m.stride(r) == strides[r]); |
| 80 |
assert(c_m.stride(r) == strides[r]); |
| 81 |
ASSERT_NOEXCEPT(m.stride(r)); |
| 82 |
ASSERT_NOEXCEPT(c_m.stride(r)); |
| 83 |
} |
| 84 |
|
| 85 |
typename E::index_type expected_size = 1; |
| 86 |
for (typename E::rank_type r = 0; r < E::rank(); r++) { |
| 87 |
if (ext.extent(r) == 0) { |
| 88 |
expected_size = 0; |
| 89 |
break; |
| 90 |
} |
| 91 |
expected_size += (ext.extent(r) - 1) * static_cast<typename E::index_type>(strides[r]); |
| 92 |
} |
| 93 |
assert(m.required_span_size() == expected_size); |
| 94 |
assert(c_m.required_span_size() == expected_size); |
| 95 |
ASSERT_NOEXCEPT(m.required_span_size()); |
| 96 |
ASSERT_NOEXCEPT(c_m.required_span_size()); |
| 97 |
|
| 98 |
static_assert(std::is_trivially_copyable_v<M>); |
| 99 |
static_assert(std::regular<M>); |
| 100 |
} |
| 101 |
|
| 102 |
constexpr bool test() { |
| 103 |
constexpr size_t D = std::dynamic_extent; |
| 104 |
test_layout_mapping_stride(std::extents<int>(), std::array<int, 0>{}, true); |
| 105 |
test_layout_mapping_stride(std::extents<signed char, 4, 5>(), std::array<signed char, 2>{1, 4}, true); |
| 106 |
test_layout_mapping_stride(std::extents<signed char, 4, 5>(), std::array<signed char, 2>{1, 5}, false); |
| 107 |
test_layout_mapping_stride(std::extents<unsigned, D, 4>(7), std::array<unsigned, 2>{20, 2}, false); |
| 108 |
test_layout_mapping_stride(std::extents<size_t, D, D, D, D>(3, 3, 3, 3), std::array<size_t, 4>{3, 1, 9, 27}, true); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
int main(int, char**) { |
| 113 |
test(); |
| 114 |
static_assert(test()); |
| 115 |
return 0; |
| 116 |
} |