-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaggedScalar.hpp
More file actions
220 lines (185 loc) · 6.78 KB
/
TaggedScalar.hpp
File metadata and controls
220 lines (185 loc) · 6.78 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) 2023 Francesco Cavaliere
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef CAV_INCLUDE_UTILS_TAGGEDSCALAR_HPP
#define CAV_INCLUDE_UTILS_TAGGEDSCALAR_HPP
#include "../comptime/mp_utils.hpp"
#include "../comptime/syntactic_sugars.hpp"
#include "../mish/util_functions.hpp"
#include "../numeric/limits.hpp"
namespace cav {
/// @brief TaggedScalar wrap a native arithmetic type and define the conversions between
/// TaggedScalar as explicit. The conversion with native types are kept implicit for compatibility.
///
/// Upon an arithmentic operation, the result is checked to avoid overflow, infinite, Nan or any
/// non-wanted edge case that is not relevant for my usage of this type.
template <typename T, typename = UNIQUE_TYPE>
struct TaggedScalar {
using type = T;
T value;
constexpr TaggedScalar(T val)
: value(val) {
assert(std::isfinite(val));
};
constexpr TaggedScalar()
: value(){};
constexpr operator T() const {
assert(std::isfinite(value));
return value;
}
template <typename T2, typename TagT2>
explicit constexpr operator TaggedScalar<T2, TagT2>() const {
assert(std::isfinite(value));
return value;
}
};
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator+(TaggedScalar<T, TagT> n) {
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator-(TaggedScalar<T, TagT> n) {
assert(!check_overflow_mul(n.value, -1));
assert(std::isfinite(-n.value));
return {-n.value};
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator++(TaggedScalar<T, TagT>& n) {
assert(!check_overflow_sum(n.value, 1));
++n.value;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator--(TaggedScalar<T, TagT>& n) {
assert(!check_overflow_dif(n.value, 1));
--n.value;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator++(TaggedScalar<T, TagT>& n, int) {
assert(!check_overflow_sum(n.value, 1));
auto old = n;
++n.value;
assert(std::isfinite(n.value));
return old;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator--(TaggedScalar<T, TagT>& n, int) {
assert(!check_overflow_dif(n.value, 1));
auto old = n;
--n.value;
assert(std::isfinite(n.value));
return old;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator+(TaggedScalar<T, TagT> n, auto other) {
assert(!check_overflow_sum(n.value, other));
return {n.value + other};
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator-(TaggedScalar<T, TagT> n, auto other) {
assert(!check_overflow_dif(n.value, other));
return {n.value - other};
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator*(TaggedScalar<T, TagT> n, auto other) {
assert(!check_overflow_mul(n.value, other));
return {n.value * other};
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator/(TaggedScalar<T, TagT> n, auto other) {
assert(!check_overflow_div(n.value, other));
return {n.value / other};
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator+=(TaggedScalar<T, TagT>& n, auto other) {
assert(!check_overflow_sum(n.value, other));
n.value += other;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator-=(TaggedScalar<T, TagT>& n, auto other) {
assert(!check_overflow_dif(n.value, other));
n.value -= other;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator*=(TaggedScalar<T, TagT>& n, auto other) {
assert(!check_overflow_mul(n.value, other));
n.value *= other;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator/=(TaggedScalar<T, TagT>& n, auto other) {
assert(!check_overflow_div(n.value, other));
n.value /= other;
assert(std::isfinite(n.value));
return n;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator+(TaggedScalar<T, TagT> n, TaggedScalar<T, TagT> other) {
return n + other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator-(TaggedScalar<T, TagT> n, TaggedScalar<T, TagT> other) {
return n - other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator*(TaggedScalar<T, TagT> n, TaggedScalar<T, TagT> other) {
return n * other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT> operator/(TaggedScalar<T, TagT> n, TaggedScalar<T, TagT> other) {
return n / other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator+=(TaggedScalar<T, TagT>& n, TaggedScalar<T, TagT> other) {
return n += other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator-=(TaggedScalar<T, TagT>& n, TaggedScalar<T, TagT> other) {
return n -= other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator*=(TaggedScalar<T, TagT>& n, TaggedScalar<T, TagT> other) {
return n *= other.value;
}
template <typename T, typename TagT>
constexpr TaggedScalar<T, TagT>& operator/=(TaggedScalar<T, TagT>& n, TaggedScalar<T, TagT> other) {
return n /= other.value;
}
////////////// EXETEND LIMITS TO SUPPORT SCALEDINT //////////////
template <typename T, typename TagT>
struct limits<TaggedScalar<T, TagT>> : limits<T> {};
} // namespace cav
#if __has_include(<fmt/core.h>)
#include <fmt/core.h>
namespace fmt {
template <typename T, typename TagT>
struct formatter<cav::TaggedScalar<T, TagT>> : fmt::formatter<T> {
using base = fmt::formatter<T>;
using tagget_type = cav::TaggedScalar<T, TagT>;
auto format(tagget_type c, format_context& ctx) noexcept {
return fmt::formatter<T>::format(static_cast<T>(c), ctx);
}
};
} // namespace fmt
#endif
#endif /* CAV_INCLUDE_UTILS_TAGGEDSCALAR_HPP */