proxsuite 0.7.3
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
fix_index.hpp
Go to the documentation of this file.
1#ifndef VEG_META_INT_FIX_HPP_7S9Y48TFS
2#define VEG_META_INT_FIX_HPP_7S9Y48TFS
3
8
9namespace proxsuite {
10namespace linalg {
11namespace veg {
12struct Dyn;
13template<isize N>
14struct Fix;
15
16namespace _detail {
17template<typename L, typename R>
19{
20 using Add = void;
21 using Sub = void;
22 using Mul = void;
23 using Div = void;
24 using Mod = void;
25
26 using CmpEq = void;
27 using CmpNEq = void;
28 using CmpLT = void;
29 using CmpLE = void;
30 using CmpGT = void;
31 using CmpGE = void;
32};
33
34namespace idx {
35namespace adl {
36template<typename T>
37struct IdxBase
38{};
39} // namespace adl
40} // namespace idx
41namespace _meta {
42template<typename T>
44{};
45template<isize N>
46struct is_fix<Fix<N>> : true_type
47{};
48} // namespace _meta
49} // namespace _detail
50
51namespace concepts {
52VEG_DEF_CONCEPT(typename T,
53 index,
55} // namespace concepts
56
57enum struct Ternary : unsigned char
58{
62};
63
64constexpr auto no = Ternary::no;
65constexpr auto maybe = Ternary::maybe;
66constexpr auto yes = Ternary::yes;
70
71template<Ternary T>
72struct Boolean;
73
74template<Ternary T>
75struct Boolean
76{
77 constexpr Boolean() VEG_NOEXCEPT = default;
78 using type = meta::constant<Ternary, T>;
79
80 VEG_INLINE constexpr Boolean(Boolean<maybe> /*b*/,
81 Unsafe /*tag*/) VEG_NOEXCEPT;
82 VEG_INLINE constexpr Boolean // NOLINT(hicpp-explicit-conversions)
84
85 VEG_NODISCARD VEG_INLINE constexpr friend auto operator!(Boolean /*arg*/)
86 VEG_NOEXCEPT->Boolean<T == yes ? no : yes>
87 {
88 return {};
89 }
90 VEG_NODISCARD VEG_INLINE explicit constexpr operator bool() const VEG_NOEXCEPT
91 {
92 return T == yes;
93 }
94};
95
96template<isize N>
98{
99 constexpr Fix() VEG_NOEXCEPT = default;
100 VEG_INLINE constexpr Fix(Dyn /*arg*/, Unsafe /*tag*/) VEG_NOEXCEPT;
101 VEG_INLINE constexpr Fix // NOLINT(hicpp-explicit-conversions)
102 (Dyn arg) VEG_NOEXCEPT;
104 requires((M != N)),
105 constexpr Fix,
106 (/*arg*/, Fix<M>)) = delete;
107
108 VEG_NODISCARD VEG_INLINE explicit constexpr operator isize() const
110 {
111 return N;
112 }
114 {
115 return {};
116 }
117 VEG_NODISCARD VEG_INLINE constexpr auto operator-() const
118 VEG_NOEXCEPT->Fix<-N>
119 {
120 return {};
121 }
122
123#define VEG_OP(Op, Name, TypeName) \
124 VEG_TEMPLATE((typename R), \
125 requires(VEG_CONCEPT(index<R>)), \
126 VEG_NODISCARD VEG_INLINE constexpr auto \
127 operator Op, \
128 (b, R)) \
129 const VEG_NOEXCEPT->typename _detail::binary_traits<Fix, R>::TypeName \
130 { \
131 return _detail::binary_traits<Fix, R>::Name##_fn(*this, b); \
132 } \
133 VEG_NOM_SEMICOLON
134
135 VEG_OP(+, add, Add);
136 VEG_OP(-, sub, Sub);
137 VEG_OP(*, mul, Mul);
138
139#undef VEG_OP
140
142 (typename R),
143 requires(VEG_CONCEPT(index<R>) &&
145 VEG_NODISCARD VEG_INLINE constexpr auto operator/,
146 (b, R))
148 {
150 }
151
153 (typename R),
154 requires(VEG_CONCEPT(index<R>) &&
156 VEG_NODISCARD VEG_INLINE constexpr auto
157 operator%,
158 (b, R))
160 {
162 }
163
164#define VEG_CMP(Name, TypeName, Op) \
165 VEG_TEMPLATE((typename R), \
166 requires(VEG_CONCEPT(index<R>)), \
167 VEG_NODISCARD VEG_INLINE constexpr auto \
168 operator Op, /* NOLINT */ \
169 (b, R)) \
170 const VEG_NOEXCEPT->typename _detail::binary_traits<Fix, R>::TypeName \
171 { \
172 return _detail::binary_traits<Fix, R>::cmp_##Name##_fn(*this, b); \
173 } \
174 VEG_NOM_SEMICOLON
175
176 VEG_CMP(eq, CmpEq, ==);
177 VEG_CMP(neq, CmpNEq, !=);
178 VEG_CMP(lt, CmpLT, <);
179 VEG_CMP(le, CmpLE, <=);
180 VEG_CMP(gt, CmpGT, >);
181 VEG_CMP(ge, CmpGE, >=);
182
183#undef VEG_CMP
184
185#undef VEG_CMP
186};
187
188namespace _detail {
189struct Error
190{
191 constexpr auto operator()(u64 const* fail = nullptr) const VEG_NOEXCEPT->u64
192 {
193 return *fail;
194 }
195};
196
197using parser = auto (*)(char, Error) -> u64;
198constexpr auto
200{
201 return (c == '0') ? 0 : (c == '1' ? 1 : e());
202}
203constexpr auto
205{
206 return (c >= '0' && c <= '7') ? u64(c - '0') : e();
207}
208constexpr auto
210{
211 return (c >= '0' && c <= '9') ? u64(c - '0') : e();
212}
213constexpr auto
215{
216 return (c >= '0' && c <= '9') //
217 ? u64(c - '0')
218 : (c >= 'a' && c <= 'f') //
219 ? u64(c - 'a')
220 : (c >= 'A' && c <= 'F') //
221 ? u64(c - 'A')
222 : e();
223}
224
225constexpr auto
227{
228 return radix == 2
230 : (radix == 8
232 : (radix == 10 ? parse_digit_10
233 : (radix == 16 ? parse_digit_16 : nullptr)));
234}
235
236constexpr auto
237parse_num(char const* str, u64 len, u64 radix, Error e) VEG_NOEXCEPT -> u64
238{
239 return (len == 0) ? 0
240 : radix * parse_num(str, len - 1, radix, e) +
241 (parse_digit(radix)(str[len - 1], e));
242}
243
244constexpr auto
245parse_int(char const* str, u64 len, Error e) VEG_NOEXCEPT -> u64
246{
247 return (len == 0) //
248 ? e()
249 : ((str[0] == '0') //
250 ? ((len == 1) //
251 ? 0
252 : (str[1] == 'b' || str[1] == 'B') //
253 ? parse_num(str + 2, len - 2, 2, e)
254 : (str[1] == 'x' || str[1] == 'X') //
255 ? parse_num(str + 2, len - 2, 16, e)
256 : parse_num(str + 1, len - 1, 8, e))
257 : parse_num(str, len, 10, e));
258}
259
260template<char... Chars>
262{
263 static constexpr char value[] = { Chars... };
264};
265
266template<isize N, isize M>
267struct binary_traits<Fix<N>, Fix<M>>
268{
269
270#define VEG_OP(Name, TypeName, Op) \
271 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = \
272 Fix<isize(usize(isize{ N }) Op usize(isize{ M }))>; \
273 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Fix<N>, Fix<M>) \
274 VEG_NOEXCEPT -> TypeName \
275 { \
276 return {}; \
277 } \
278 static_assert(true, "")
279
280#define VEG_CMP(Name, TypeName, Op) \
281 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = \
282 Boolean<(N Op M) ? yes : no>; \
283 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Fix<N>, Fix<M>) \
284 VEG_NOEXCEPT -> TypeName \
285 { \
286 return {}; \
287 } \
288 static_assert(true, "")
289
290 VEG_OP(add, Add, +);
291 VEG_OP(sub, Sub, -);
292 VEG_OP(mul, Mul, *);
293 VEG_CMP(cmp_eq, CmpEq, ==);
294 VEG_CMP(cmp_neq, CmpNEq, !=);
295 VEG_CMP(cmp_lt, CmpLT, <);
296 VEG_CMP(cmp_le, CmpLE, <=);
297 VEG_CMP(cmp_gt, CmpGT, >);
298 VEG_CMP(cmp_ge, CmpGE, >=);
299
300 using Div = meta::if_t<M == 0, void, Fix<N / (M != 0 ? M : 1)>>;
301 using Mod = meta::if_t<M == 0, void, Fix<N % (M != 0 ? M : 1)>>;
302
303 VEG_NODISCARD VEG_INLINE static constexpr auto div_fn(Fix<N> /*a*/,
304 Fix<M> /*b*/)
306 {
307 return Div();
308 }
309 VEG_NODISCARD VEG_INLINE static constexpr auto mod_fn(Fix<N> /*a*/,
310 Fix<M> /*b*/)
312 {
313 return Mod();
314 }
315
316#undef VEG_OP
317#undef VEG_CMP
318};
319namespace idx {
320namespace adl {
321} // namespace adl
322} // namespace idx
323} // namespace _detail
324
325inline namespace literals {
326template<char... Chars>
327VEG_INLINE constexpr auto
328operator""_c() VEG_NOEXCEPT
329{
332}
333} // namespace literals
334} // namespace veg
335} // namespace linalg
336} // namespace proxsuite
337
339#endif /* end of include guard VEG_META_INT_FIX_HPP_7S9Y48TFS */
#define VEG_OP(Op, Name, TypeName)
Definition dyn_index.hpp:75
#define VEG_CONCEPT(...)
Definition macros.hpp:1238
#define VEG_INLINE
Definition macros.hpp:118
#define VEG_DEF_CONCEPT(Tpl, Name,...)
Definition macros.hpp:321
constexpr auto parse_digit_10(char c, Error e) VEG_NOEXCEPT -> u64
constexpr auto parse_digit_16(char c, Error e) VEG_NOEXCEPT -> u64
constexpr auto parse_int(char const *str, u64 len, Error e) VEG_NOEXCEPT -> u64
constexpr auto parse_digit_2(char c, Error e) VEG_NOEXCEPT -> u64
constexpr auto parse_num(char const *str, u64 len, u64 radix, Error e) VEG_NOEXCEPT -> u64
auto(*)(char, Error) -> u64 parser
constexpr auto parse_digit(u64 radix) VEG_NOEXCEPT -> parser
constexpr auto parse_digit_8(char c, Error e) VEG_NOEXCEPT -> u64
typename _detail::_meta::conditional_< B >::template type< T, F > if_t
Definition core.hpp:83
bool_constant< false > false_type
Definition macros.hpp:900
bool_constant< true > true_type
Definition macros.hpp:899
meta::constant< Ternary, Ternary::maybe > maybe_c
Definition fix_index.hpp:68
constexpr auto maybe
Definition fix_index.hpp:65
meta::constant< Ternary, Ternary::yes > yes_c
Definition fix_index.hpp:69
constexpr auto no
Definition fix_index.hpp:64
constexpr auto yes
Definition fix_index.hpp:66
meta::constant< Ternary, Ternary::no > no_c
Definition fix_index.hpp:67
std::uint64_t u64
Definition typedefs.hpp:46
_detail::_meta::make_signed< usize >::Type isize
Definition typedefs.hpp:43
#define VEG_NODISCARD
Definition prologue.hpp:97
#define VEG_NOEXCEPT
Definition prologue.hpp:30
meta::constant< Ternary, T > type
Definition fix_index.hpp:78
constexpr Boolean() VEG_NOEXCEPT=default
VEG_NODISCARD VEG_INLINE constexpr auto operator+() const VEG_NOEXCEPT -> Fix
VEG_TEMPLATE((typename R), requires(VEG_CONCEPT(index< R >) &&VEG_CONCEPT(index< typename _detail::binary_traits< Fix, R >::Div >)), VEG_NODISCARD VEG_INLINE constexpr auto operator/,(b, R)) const VEG_NOEXCEPT -> typename _detail::binary_traits< Fix, R >::Div
VEG_NODISCARD VEG_INLINE constexpr auto operator-() const VEG_NOEXCEPT -> Fix<-N >
VEG_TEMPLATE((typename R), requires(VEG_CONCEPT(index< R >) &&VEG_CONCEPT(index< typename _detail::binary_traits< Fix, R >::Mod >)), VEG_NODISCARD VEG_INLINE constexpr auto operator%,(b, R)) const VEG_NOEXCEPT -> typename _detail::binary_traits< Fix, R >::Mod
constexpr Fix() VEG_NOEXCEPT=default
VEG_CMP(neq, CmpNEq, !=)
VEG_TEMPLATE((isize M), requires((M !=N)), constexpr Fix,(, Fix< M >))=delete
constexpr auto operator()(u64 const *fail=nullptr) const VEG_NOEXCEPT -> u64
VEG_NODISCARD static VEG_INLINE constexpr auto mod_fn(Fix< N >, Fix< M >) VEG_NOEXCEPT -> Mod
meta::if_t< M==0, void, Fix< N %(M !=0 ? M :1)> > Mod
meta::if_t< M==0, void, Fix< N/(M !=0 ? M :1)> > Div
VEG_NODISCARD static VEG_INLINE constexpr auto div_fn(Fix< N >, Fix< M >) VEG_NOEXCEPT -> Div