Debian Codegraph @

packages/kokkos/tpls/mdspan/include/experimental/__p0009_bits/utility.hpp

raw
1 #pragma once
2
3 #include <cstddef>
4 #include <type_traits>
5 #include <array>
6 #include <utility>
7
8 namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
9 namespace detail {
10
11 // type alias used for rank-based tag dispatch
12 //
13 // this is used to enable alternatives to constexpr if when building for C++14
14 //
15 template <std::size_t N>
16 using with_rank = std::integral_constant<std::size_t, N>;
17
18 template <class I1, class I2>
19 MDSPAN_INLINE_FUNCTION
20 constexpr bool common_integral_compare(I1 x, I2 y)
21 {
22 static_assert(std::is_integral<I1>::value &&
23 std::is_integral<I2>::value, "");
24
25 using I = std::common_type_t<I1, I2>;
26 return static_cast<I>(x) == static_cast<I>(y);
27 }
28
29 template <class T1, class T2, class F>
30 MDSPAN_INLINE_FUNCTION
31 constexpr bool rankwise_equal(with_rank<0>, const T1&, const T2&, F)
32 {
33 return true;
34 }
35
36 template <std::size_t N, class T1, class T2, class F>
37 MDSPAN_INLINE_FUNCTION
38 constexpr bool rankwise_equal(with_rank<N>, const T1& x, const T2& y, F func)
39 {
40 bool match = true;
41
42 for (std::size_t r = 0; r < N; r++) {
43 match = match && common_integral_compare(func(x, r), func(y, r));
44 }
45
46 return match;
47 }
48
49 constexpr struct
50 {
51 template <class T, class I>
52 MDSPAN_INLINE_FUNCTION
53 constexpr auto operator()(const T& x, I i) const
54 {
55 return x.extent(i);
56 }
57 } extent;
58
59 constexpr struct
60 {
61 template <class T, class I>
62 MDSPAN_INLINE_FUNCTION
63 constexpr auto operator()(const T& x, I i) const
64 {
65 return x.stride(i);
66 }
67 } stride;
68
69 // same as std::integral_constant but with __host__ __device__ annotations on
70 // the implicit conversion function and the call operator
71 template <class T, T v>
72 struct integral_constant {
73 using value_type = T;
74 using type = integral_constant<T, v>;
75
76 static constexpr T value = v;
77
78 MDSPAN_INLINE_FUNCTION_DEFAULTED
79 constexpr integral_constant() = default;
80
81 // These interop functions work, because other than the value_type operator
82 // everything of std::integral_constant works on device (defaulted functions)
83 MDSPAN_FUNCTION
84 constexpr integral_constant(std::integral_constant<T,v>) {};
85
86 MDSPAN_FUNCTION constexpr operator std::integral_constant<T,v>() const noexcept {
87 return std::integral_constant<T,v>{};
88 }
89
90 MDSPAN_FUNCTION constexpr operator value_type() const noexcept {
91 return value;
92 }
93
94 MDSPAN_FUNCTION constexpr value_type operator()() const noexcept {
95 return value;
96 }
97 };
98
99 // The tuple implementation only comes in play when using capabilities
100 // such as submdspan which require C++17 anyway
101 #if MDSPAN_HAS_CXX_17
102 template<class T, size_t Idx>
103 struct tuple_member {
104 using type = T;
105 static constexpr size_t idx = Idx;
106 T val;
107 MDSPAN_FUNCTION constexpr T& get() { return val; }
108 MDSPAN_FUNCTION constexpr const T& get() const { return val; }
109 };
110
111 // A helper class which will be used via a fold expression to
112 // select the type with the correct Idx in a pack of tuple_member
113 template<size_t SearchIdx, size_t Idx, class T>
114 struct tuple_idx_matcher {
115 using type = tuple_member<T, Idx>;
116 template<class Other>
117 MDSPAN_FUNCTION
118 constexpr auto operator | (Other v) const {
119 if constexpr (Idx == SearchIdx) { return *this; }
120 else { return v; }
121 }
122 };
123
124 template<class IdxSeq, class ... Elements>
125 struct tuple_impl;
126
127 template<size_t ... Idx, class ... Elements>
128 struct tuple_impl<std::index_sequence<Idx...>, Elements...>: public tuple_member<Elements, Idx> ... {
129
130 MDSPAN_FUNCTION
131 constexpr tuple_impl(Elements ... vals):tuple_member<Elements, Idx>{vals}... {}
132
133 template<size_t N>
134 MDSPAN_FUNCTION
135 constexpr auto& get() {
136 using base_t = decltype((tuple_idx_matcher<N, Idx, Elements>() | ...) );
137 return base_t::type::get();
138 }
139 template<size_t N>
140 MDSPAN_FUNCTION
141 constexpr const auto& get() const {
142 using base_t = decltype((tuple_idx_matcher<N, Idx, Elements>() | ...) );
143 return base_t::type::get();
144 }
145 };
146
147 // A simple tuple-like class for representing slices internally and is compatible with device code
148 // This doesn't support type access since we don't need it
149 // This is not meant as an external API
150 template<class ... Elements>
151 struct tuple: public tuple_impl<decltype(std::make_index_sequence<sizeof...(Elements)>()), Elements...> {
152 MDSPAN_FUNCTION
153 constexpr tuple(Elements ... vals):tuple_impl<decltype(std::make_index_sequence<sizeof...(Elements)>()), Elements ...>(vals ...) {}
154 };
155
156 template<size_t Idx, class ... Args>
157 MDSPAN_FUNCTION
158 constexpr auto& get(tuple<Args...>& vals) { return vals.template get<Idx>(); }
159
160 template<size_t Idx, class ... Args>
161 MDSPAN_FUNCTION
162 constexpr const auto& get(const tuple<Args...>& vals) { return vals.template get<Idx>(); }
163
164 template<class ... Elements>
165 tuple(Elements ...) -> tuple<Elements...>;
166 #endif
167 } // namespace detail
168
169 constexpr struct mdspan_non_standard_tag {
170 } mdspan_non_standard;
171
172 } // namespace MDSPAN_IMPL_STANDARD_NAMESPACE