-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_index.hpp
182 lines (167 loc) · 4.43 KB
/
hpc_index.hpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#include <cstddef>
#include <hpc_macros.hpp>
#include <type_traits>
namespace hpc {
template <class Integral>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr std::enable_if_t<std::is_integral<Integral>::value, Integral>
weaken(Integral i) noexcept
{
return i;
}
#ifdef HPC_ENABLE_STRONG_INDICES
template <class Tag, class Integral = std::ptrdiff_t>
class index
{
Integral i;
public:
using integral_type = Integral;
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index(std::ptrdiff_t i_in) noexcept : i(integral_type(i_in))
{
}
HPC_ALWAYS_INLINE
index() noexcept = default;
HPC_ALWAYS_INLINE
index(index const&) noexcept = default;
HPC_ALWAYS_INLINE
index(index&&) noexcept = default;
HPC_ALWAYS_INLINE index&
operator=(index const&) noexcept = default;
HPC_ALWAYS_INLINE index&
operator=(index&&) noexcept = default;
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index
operator+(index const& other) const noexcept
{
return index(i + other.i);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index
operator-(index const& other) const noexcept
{
return index(i - other.i);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index&
operator++() noexcept
{
++i;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index
operator++(int) const noexcept
{
auto const old = *this;
++i;
return old;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index&
operator--() noexcept
{
--i;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index
operator--(int) const noexcept
{
auto const old = *this;
--i;
return old;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(index const& other) const noexcept
{
return i == other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(index const& other) const noexcept
{
return i != other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(index const& other) const noexcept
{
return i > other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(index const& other) const noexcept
{
return i < other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(index const& other) const noexcept
{
return i >= other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(index const& other) const noexcept
{
return i <= other.i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index&
operator+=(index const& other) noexcept
{
i += other.i;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE index&
operator-=(index const& other) noexcept
{
i -= other.i;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr integral_type
get() const noexcept
{
return i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE explicit constexpr operator integral_type() const noexcept
{
return i;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index
operator*(integral_type const n) const noexcept
{
return index(i * n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index
operator/(integral_type const n) const noexcept
{
return index(i / n);
}
template <class Tag2, class Integral2>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE explicit constexpr operator ::hpc::index<Tag2, Integral2>() const noexcept
{
return i;
}
};
template <class L, class R>
class product_tag
{
};
template <class L, class R, class LI, class RI>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index<product_tag<L, R>, decltype(LI() * RI())>
operator*(index<L, LI> left, index<R, RI> right) noexcept
{
return left.get() * right.get();
}
template <class L, class R, class LI, class RI>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr index<L, decltype(LI() / RI())>
operator/(index<product_tag<L, R>, LI> left, index<R, RI> right) noexcept
{
return left.get() / right.get();
}
template <class Tag, class Integral>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr Integral
weaken(index<Tag, Integral> i) noexcept
{
return i.get();
}
template <class Tag, class Integral, class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr T*
operator+(T* p, index<Tag, Integral> i) noexcept
{
return p + weaken(i);
}
#else
template <class Tag, class Integral = std::ptrdiff_t>
using index = Integral;
#endif
} // namespace hpc