File: index_operator.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (121 lines) | stat: -rw-r--r-- 4,697 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <mdspan>

// Test default iteration:
//
// template<class... Indices>
//   constexpr index_type operator()(Indices...) const noexcept;
//
// Constraints:
//   * sizeof...(Indices) == extents_type::rank() is true,
//   * (is_convertible_v<Indices, index_type> && ...) is true, and
//   * (is_nothrow_constructible_v<index_type, Indices> && ...) is true.
//
// Preconditions:
//   * extents_type::index-cast(i) is a multidimensional index in extents_.

#include <mdspan>
#include <cassert>
#include <cstdint>

#include "test_macros.h"

#include "../ConvertibleToIntegral.h"

template <class Mapping, class... Indices>
concept operator_constraints = requires(Mapping m, Indices... idxs) {
  { std::is_same_v<decltype(m(idxs...)), typename Mapping::index_type> };
};

template <class Mapping, class... Indices>
  requires(operator_constraints<Mapping, Indices...>)
constexpr bool check_operator_constraints(Mapping m, Indices... idxs) {
  (void)m(idxs...);
  return true;
}

template <class Mapping, class... Indices>
constexpr bool check_operator_constraints(Mapping, Indices...) {
  return false;
}

template <class M, class... Args>
constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()>& strides, Args... args) {
  constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
  if constexpr (-1 == r) {
    ASSERT_NOEXCEPT(m(args...));
    std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) {
      return ((args * strides[Pos]) + ... + 0);
    }(std::make_index_sequence<M::extents_type::rank()>()));
    assert(expected_val == static_cast<std::size_t>(m(args...)));
  } else {
    for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
      iterate_stride(m, strides, i, args...);
    }
  }
}

template <class E, class... Args>
constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args) {
  using M = std::layout_stride::mapping<E>;
  M m(E(args...), strides);

  iterate_stride(m, strides);
}

constexpr bool test() {
  constexpr std::size_t D = std::dynamic_extent;
  test_iteration<std::extents<int>>(std::array<int, 0>{});
  test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1);
  test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7);
  test_iteration<std::extents<unsigned, 7>>(std::array<int, 1>{4});
  test_iteration<std::extents<unsigned, 7, 8>>(std::array<int, 2>{25, 3});
  test_iteration<std::extents<signed char, D, D, D, D>>(std::array<int, 4>{1, 1, 1, 1}, 1, 1, 1, 1);

  // Check operator constraint for number of arguments
  static_assert(check_operator_constraints(
      std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0));
  static_assert(!check_operator_constraints(
      std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), 0, 0));

  // Check operator constraint for convertibility of arguments to index_type
  static_assert(check_operator_constraints(
      std::layout_stride::mapping<std::extents<int, D>>(std::extents<int, D>(1), std::array{1}), IntType(0)));
  static_assert(!check_operator_constraints(
      std::layout_stride::mapping<std::extents<unsigned, D>>(std::extents<unsigned, D>(1), std::array{1}), IntType(0)));

  // Check operator constraint for no-throw-constructibility of index_type from arguments
  static_assert(!check_operator_constraints(
      std::layout_stride::mapping<std::extents<unsigned char, D>>(std::extents<unsigned char, D>(1), std::array{1}),
      IntType(0)));

  return true;
}

constexpr bool test_large() {
  constexpr std::size_t D = std::dynamic_extent;
  test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10);
  test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10);
  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  // The large test iterates over ~10k loop indices.
  // With assertions enabled this triggered the maximum default limit
  // for steps in consteval expressions. Assertions roughly double the
  // total number of instructions, so this was already close to the maximum.
  test_large();
  return 0;
}