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 | // Test default iteration: |
14 | // |
15 | // template<class... Indices> |
16 | // constexpr index_type operator()(Indices...) const noexcept; |
17 | // |
18 | // Constraints: |
19 | // * sizeof...(Indices) == extents_type::rank() is true, |
20 | // * (is_convertible_v<Indices, index_type> && ...) is true, and |
21 | // * (is_nothrow_constructible_v<index_type, Indices> && ...) is true. |
22 | // |
23 | // Preconditions: |
24 | // * extents_type::index-cast(i) is a multidimensional index in extents_. |
25 | |
26 | #include <mdspan> |
27 | #include <cassert> |
28 | #include <cstdint> |
29 | |
30 | #include "test_macros.h" |
31 | |
32 | #include "../ConvertibleToIntegral.h" |
33 | |
34 | template <class Mapping, class... Indices> |
35 | concept operator_constraints = requires(Mapping m, Indices... idxs) { |
36 | { std::is_same_v<decltype(m(idxs...)), typename Mapping::index_type> }; |
37 | }; |
38 | |
39 | template <class Mapping, class... Indices> |
40 | requires(operator_constraints<Mapping, Indices...>) |
41 | constexpr bool check_operator_constraints(Mapping m, Indices... idxs) { |
42 | (void)m(idxs...); |
43 | return true; |
44 | } |
45 | |
46 | template <class Mapping, class... Indices> |
47 | constexpr bool check_operator_constraints(Mapping, Indices...) { |
48 | return false; |
49 | } |
50 | |
51 | template <class M, class... Args> |
52 | constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()>& strides, Args... args) { |
53 | constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args)); |
54 | if constexpr (-1 == r) { |
55 | ASSERT_NOEXCEPT(m(args...)); |
56 | std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) { |
57 | return ((args * strides[Pos]) + ... + 0); |
58 | }(std::make_index_sequence<M::extents_type::rank()>())); |
59 | assert(expected_val == static_cast<std::size_t>(m(args...))); |
60 | } else { |
61 | for (typename M::index_type i = 0; i < m.extents().extent(r); i++) { |
62 | iterate_stride(m, strides, i, args...); |
63 | } |
64 | } |
65 | } |
66 | |
67 | template <class E, class... Args> |
68 | constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args) { |
69 | using M = std::layout_stride::mapping<E>; |
70 | M m(E(args...), strides); |
71 | |
72 | iterate_stride(m, strides); |
73 | } |
74 | |
75 | constexpr bool test() { |
76 | constexpr std::size_t D = std::dynamic_extent; |
77 | test_iteration<std::extents<int>>(std::array<int, 0>{}); |
78 | test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1); |
79 | test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7); |
80 | test_iteration<std::extents<unsigned, 7>>(std::array<int, 1>{4}); |
81 | test_iteration<std::extents<unsigned, 7, 8>>(std::array<int, 2>{25, 3}); |
82 | test_iteration<std::extents<signed char, D, D, D, D>>(std::array<int, 4>{1, 1, 1, 1}, 1, 1, 1, 1); |
83 | |
84 | // Check operator constraint for number of arguments |
85 | static_assert(check_operator_constraints( |
86 | std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0)); |
87 | static_assert(!check_operator_constraints( |
88 | std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0, 0)); |
89 | |
90 | // Check operator constraint for convertibility of arguments to index_type |
91 | static_assert(check_operator_constraints( |
92 | std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), IntType(0))); |
93 | static_assert(!check_operator_constraints( |
94 | std::layout_stride::mapping<std::extents<unsigned, D>>(std::extents<unsigned, D>(1), std::array{1}), IntType(0))); |
95 | |
96 | // Check operator constraint for no-throw-constructibility of index_type from arguments |
97 | static_assert(!check_operator_constraints( |
98 | std::layout_stride::mapping<std::extents<unsigned char, D>>(std::extents<unsigned char, D>(1), std::array{1}), |
99 | IntType(0))); |
100 | |
101 | return true; |
102 | } |
103 | |
104 | constexpr bool test_large() { |
105 | constexpr std::size_t D = std::dynamic_extent; |
106 | test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10); |
107 | test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10); |
108 | return true; |
109 | } |
110 | |
111 | int main(int, char**) { |
112 | test(); |
113 | static_assert(test()); |
114 | |
115 | // The large test iterates over ~10k loop indices. |
116 | // With assertions enabled this triggered the maximum default limit |
117 | // for steps in consteval expressions. Assertions roughly double the |
118 | // total number of instructions, so this was already close to the maximum. |
119 | test_large(); |
120 | return 0; |
121 | } |
122 | |