|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <gmpxx.h> |
| 4 | + |
3 | 5 | #include <bit> |
4 | 6 | #include <cmath> |
5 | 7 | #include <concepts> |
6 | 8 | #include <cstdint> |
7 | | -#include <gmpxx.h> |
8 | 9 | #include <limits> |
9 | 10 | #include <string> |
10 | 11 | #include <type_traits> |
11 | | - |
12 | 12 | #include <udb/bits.hpp> |
13 | 13 | #include <udb/defines.hpp> |
14 | 14 |
|
15 | 15 | namespace udb { |
16 | 16 |
|
17 | | -using uint128_t = unsigned __int128; |
18 | | -using int128_t = __int128; |
| 17 | + using uint128_t = unsigned __int128; |
| 18 | + using int128_t = __int128; |
19 | 19 |
|
20 | | -struct __not_impl_type { |
21 | | - constexpr __not_impl_type() = default; |
22 | | - constexpr explicit __not_impl_type(const __not_impl_type &o) {} |
| 20 | + struct __not_impl_type { |
| 21 | + constexpr __not_impl_type() = default; |
| 22 | + constexpr explicit __not_impl_type(const __not_impl_type &o) {} |
23 | 23 |
|
24 | | - template <typename T, typename... Args> |
25 | | - requires(!std::same_as<T, __not_impl_type>) |
26 | | - constexpr __not_impl_type(T a, Args... args) {} |
| 24 | + template <typename T, typename... Args> |
| 25 | + requires(!std::same_as<T, __not_impl_type>) |
| 26 | + constexpr __not_impl_type(T a, Args... args) {} |
27 | 27 |
|
28 | | - template <typename T> |
29 | | - requires(!std::convertible_to<T, __not_impl_type>) |
30 | | - constexpr // required since C++14 |
31 | | - void |
32 | | - operator=(T &&) const noexcept {} |
| 28 | + template <typename T> |
| 29 | + requires(!std::convertible_to<T, __not_impl_type>) |
| 30 | + constexpr // required |
| 31 | + // since C++14 |
| 32 | + void |
| 33 | + operator=(T &&) const noexcept {} |
33 | 34 |
|
34 | | - template <typename T> |
35 | | - constexpr // required since C++14 |
36 | | - bool |
37 | | - operator==(T &&) const noexcept { |
38 | | - return false; |
39 | | - } |
40 | | -}; |
| 35 | + template <typename T> |
| 36 | + constexpr // required since C++14 |
| 37 | + bool |
| 38 | + operator==(T &&) const noexcept { |
| 39 | + return false; |
| 40 | + } |
| 41 | + }; |
41 | 42 |
|
42 | | -template <unsigned N> using Bits = _Bits<N, false>; |
| 43 | + template <unsigned N> |
| 44 | + using Bits = _Bits<N, false>; |
43 | 45 |
|
44 | | -// static_assert(static_cast<uint64_t>(_Bits<1023> { 5 }) == 5); |
| 46 | + // static_assert(static_cast<uint64_t>(_Bits<1023> { 5 }) == 5); |
45 | 47 |
|
46 | | -} // namespace udb |
| 48 | +} // namespace udb |
47 | 49 |
|
48 | 50 | namespace udb { |
49 | | -template <unsigned Size> class Bitfield; |
50 | | - |
51 | | -template <unsigned ParentSize, unsigned Start, unsigned Size> |
52 | | -class BitfieldMember { |
53 | | -public: |
54 | | - static constexpr unsigned Width = Size; |
55 | | - |
56 | | - BitfieldMember(Bitfield<ParentSize> &parent) : m_parent(parent) {} |
57 | | - BitfieldMember(const BitfieldMember &other) : m_parent(other.m_parent) {} |
58 | | - |
59 | | - static constexpr Bits<Size> MaximumValue = |
60 | | - (Bits<1>(1).template sll<Size>()) - 1; |
61 | | - static constexpr Bits<ParentSize> Mask = MaximumValue.template sll<Start>(); |
62 | | - |
| 51 | + template <unsigned Size> |
| 52 | + class Bitfield; |
| 53 | + |
| 54 | + template <unsigned ParentSize, unsigned Start, unsigned Size> |
| 55 | + class BitfieldMember { |
| 56 | + public: |
| 57 | + static constexpr unsigned Width = Size; |
| 58 | + |
| 59 | + BitfieldMember(Bitfield<ParentSize> &parent) : m_parent(parent) {} |
| 60 | + BitfieldMember(const BitfieldMember &other) : m_parent(other.m_parent) {} |
| 61 | + |
| 62 | + static constexpr Bits<Size> MaximumValue = |
| 63 | + (Bits<1>(1).template sll<Size>()) - 1; |
| 64 | + static constexpr Bits<ParentSize> Mask = MaximumValue.template sll<Start>(); |
| 65 | + |
| 66 | + template <unsigned N> |
| 67 | + requires(N >= Size) |
| 68 | + operator Bits<N>() const; |
| 69 | + |
| 70 | + operator PossiblyUndefinedBits() const; |
| 71 | + |
| 72 | + bool operator!() const { return !static_cast<Bits<Size>>(*this).get(); } |
| 73 | + |
| 74 | + template <unsigned N, bool Signed> |
| 75 | + BitfieldMember &operator=(const _Bits<N, Signed> &value); |
| 76 | + |
| 77 | + BitfieldMember &operator=(const BitfieldMember &other); |
| 78 | + |
| 79 | + template <std::integral Type> |
| 80 | + bool operator==(const Type &other) const { |
| 81 | + return other == static_cast<Bits<Size>>(*this); |
| 82 | + } |
| 83 | + |
| 84 | + template <unsigned N, bool Signed> |
| 85 | + bool operator==(const _Bits<N, Signed> &other) const { |
| 86 | + return other == static_cast<Bits<Size>>(*this); |
| 87 | + } |
| 88 | + |
| 89 | + template <unsigned N, bool Signed> |
| 90 | + bool operator>(const _Bits<N, Signed> &other) const { |
| 91 | + return static_cast<Bits<Size>>(*this) > other; |
| 92 | + } |
| 93 | + |
| 94 | + template <unsigned N, bool Signed> |
| 95 | + bool operator>=(const _Bits<N, Signed> &other) const { |
| 96 | + return static_cast<Bits<Size>>(*this) >= other; |
| 97 | + } |
| 98 | + |
| 99 | + template <unsigned N, bool Signed> |
| 100 | + bool operator<=(const _Bits<N, Signed> &other) const { |
| 101 | + return static_cast<Bits<Size>>(*this) <= other; |
| 102 | + } |
| 103 | + |
| 104 | + template <unsigned OtherParentSize, unsigned OtherStart, unsigned OtherSize> |
| 105 | + bool operator<(const BitfieldMember<OtherParentSize, OtherStart, OtherSize> |
| 106 | + &other) const { |
| 107 | + return static_cast<Bits<Size>>(*this) > |
| 108 | + static_cast<Bits<OtherSize>>(*this); |
| 109 | + } |
| 110 | + |
| 111 | + template <unsigned N, bool Signed> |
| 112 | + Bits<Size> operator>>(const _Bits<N, Signed> &shamt) const { |
| 113 | + return static_cast<Bits<Size>>(*this) >> shamt; |
| 114 | + } |
| 115 | + template <typename T> |
| 116 | + requires(std::integral<T>) |
| 117 | + Bits<Size> operator>>(const T &shamt) const { |
| 118 | + return static_cast<Bits<Size>>(*this) >> shamt; |
| 119 | + } |
| 120 | + |
| 121 | + template <unsigned N, bool Signed> |
| 122 | + Bits<Size> operator&(const _Bits<N, Signed> &other) const { |
| 123 | + return static_cast<Bits<Size>>(*this) & other; |
| 124 | + } |
| 125 | + |
| 126 | + Bits<Bits<Size>::InfinitePrecision> operator<<(const int &shamt) const { |
| 127 | + return static_cast<Bits<Size>>(*this) << shamt; |
| 128 | + } |
| 129 | + |
| 130 | + template <unsigned Shamt> |
| 131 | + Bits<Size + Shamt> sll() const { |
| 132 | + return static_cast<Bits<Size>>(*this).template sll<Shamt>(); |
| 133 | + } |
| 134 | + |
| 135 | + private: |
| 136 | + Bitfield<ParentSize> &m_parent; |
| 137 | + }; |
| 138 | + |
| 139 | + template <unsigned Size> |
| 140 | + class Bitfield { |
| 141 | + public: |
| 142 | + Bitfield() = default; |
| 143 | + Bitfield(const Bits<Size> &value) : m_value(value) {} |
| 144 | + |
| 145 | + Bitfield &operator=(const Bits<Size> &value) { |
| 146 | + m_value = value; |
| 147 | + return *this; |
| 148 | + } |
| 149 | + template <std::integral Type> |
| 150 | + Bitfield &operator=(const Type &value) { |
| 151 | + m_value = value; |
| 152 | + return *this; |
| 153 | + } |
| 154 | + operator Bits<Size> &() { return m_value; } |
| 155 | + operator Bits<Size>() const { return m_value; } |
| 156 | + |
| 157 | + protected: |
| 158 | + Bits<Size> m_value; |
| 159 | + }; |
| 160 | + |
| 161 | + static_assert(std::is_copy_constructible_v<BitfieldMember<64, 0, 1>>); |
| 162 | + |
| 163 | + template <unsigned ParentSize, unsigned Start, unsigned Size> |
63 | 164 | template <unsigned N> |
64 | 165 | requires(N >= Size) |
65 | | - operator Bits<N>() const; |
66 | | - |
67 | | - operator PossiblyUndefinedBits() const; |
68 | | - |
69 | | - bool operator!() const { return !static_cast<Bits<Size>>(*this).get(); } |
70 | | - |
71 | | - template <unsigned N, bool Signed> |
72 | | - BitfieldMember &operator=(const _Bits<N, Signed> &value); |
73 | | - |
74 | | - BitfieldMember &operator=(const BitfieldMember &other); |
75 | | - |
76 | | - template <std::integral Type> bool operator==(const Type &other) const { |
77 | | - return other == static_cast<Bits<Size>>(*this); |
| 166 | + BitfieldMember<ParentSize, Start, Size>::template operator Bits<N>() const { |
| 167 | + return (static_cast<Bits<ParentSize>>(m_parent) >> Start) & MaximumValue; |
78 | 168 | } |
79 | 169 |
|
80 | | - template <unsigned N, bool Signed> |
81 | | - bool operator==(const _Bits<N, Signed> &other) const { |
82 | | - return other == static_cast<Bits<Size>>(*this); |
| 170 | + template <unsigned ParentSize, unsigned Start, unsigned Size> |
| 171 | + BitfieldMember<ParentSize, Start, Size>::operator PossiblyUndefinedBits() |
| 172 | + const { |
| 173 | + return (static_cast<Bits<ParentSize>>(m_parent) >> Start) & MaximumValue; |
83 | 174 | } |
84 | 175 |
|
| 176 | + template <unsigned ParentSize, unsigned Start, unsigned Size> |
85 | 177 | template <unsigned N, bool Signed> |
86 | | - bool operator>(const _Bits<N, Signed> &other) const { |
87 | | - return static_cast<Bits<Size>>(*this) > other; |
88 | | - } |
89 | | - |
90 | | - template <unsigned N, bool Signed> |
91 | | - bool operator>=(const _Bits<N, Signed> &other) const { |
92 | | - return static_cast<Bits<Size>>(*this) >= other; |
93 | | - } |
94 | | - |
95 | | - template <unsigned N, bool Signed> |
96 | | - bool operator<=(const _Bits<N, Signed> &other) const { |
97 | | - return static_cast<Bits<Size>>(*this) <= other; |
98 | | - } |
99 | | - |
100 | | - template <unsigned OtherParentSize, unsigned OtherStart, unsigned OtherSize> |
101 | | - bool operator<(const BitfieldMember<OtherParentSize, OtherStart, OtherSize> |
102 | | - &other) const { |
103 | | - return static_cast<Bits<Size>>(*this) > static_cast<Bits<OtherSize>>(*this); |
104 | | - } |
105 | | - |
106 | | - template <unsigned N, bool Signed> |
107 | | - Bits<Size> operator>>(const _Bits<N, Signed> &shamt) const { |
108 | | - return static_cast<Bits<Size>>(*this) >> shamt; |
109 | | - } |
110 | | - template <typename T> |
111 | | - requires(std::integral<T>) |
112 | | - Bits<Size> operator>>(const T &shamt) const { |
113 | | - return static_cast<Bits<Size>>(*this) >> shamt; |
114 | | - } |
115 | | - |
116 | | - template <unsigned N, bool Signed> |
117 | | - Bits<Size> operator&(const _Bits<N, Signed> &other) const { |
118 | | - return static_cast<Bits<Size>>(*this) & other; |
119 | | - } |
120 | | - |
121 | | - Bits<Bits<Size>::InfinitePrecision> operator<<(const int &shamt) const { |
122 | | - return static_cast<Bits<Size>>(*this) << shamt; |
123 | | - } |
124 | | - |
125 | | - template <unsigned Shamt> Bits<Size + Shamt> sll() const { |
126 | | - return static_cast<Bits<Size>>(*this).template sll<Shamt>(); |
127 | | - } |
128 | | - |
129 | | -private: |
130 | | - Bitfield<ParentSize> &m_parent; |
131 | | -}; |
132 | | - |
133 | | -template <unsigned Size> class Bitfield { |
134 | | -public: |
135 | | - Bitfield() = default; |
136 | | - Bitfield(const Bits<Size> &value) : m_value(value) {} |
137 | | - |
138 | | - Bitfield &operator=(const Bits<Size> &value) { |
139 | | - m_value = value; |
| 178 | + BitfieldMember<ParentSize, Start, Size> |
| 179 | + &BitfieldMember<ParentSize, Start, Size>::template operator=( |
| 180 | + const _Bits<N, Signed> &value) { |
| 181 | + m_parent = (static_cast<Bits<ParentSize>>(m_parent) & ~Mask) | |
| 182 | + ((value.template sll<Size>()) & Mask); |
140 | 183 | return *this; |
141 | 184 | } |
142 | | - template <std::integral Type> Bitfield &operator=(const Type &value) { |
143 | | - m_value = value; |
| 185 | + |
| 186 | + template <unsigned ParentSize, unsigned Start, unsigned Size> |
| 187 | + BitfieldMember<ParentSize, Start, Size> |
| 188 | + &BitfieldMember<ParentSize, Start, Size>::template operator=( |
| 189 | + const BitfieldMember<ParentSize, Start, Size> &other) { |
| 190 | + m_parent = (static_cast<Bits<ParentSize>>(m_parent) & ~Mask) | |
| 191 | + ((static_cast<Bits<Size>>(other).template sll<Size>()) & Mask); |
144 | 192 | return *this; |
145 | 193 | } |
146 | | - operator Bits<Size> &() { return m_value; } |
147 | | - operator Bits<Size>() const { return m_value; } |
148 | | - |
149 | | -protected: |
150 | | - Bits<Size> m_value; |
151 | | -}; |
152 | | - |
153 | | -static_assert(std::is_copy_constructible_v<BitfieldMember<64, 0, 1>>); |
154 | | - |
155 | | -template <unsigned ParentSize, unsigned Start, unsigned Size> |
156 | | -template <unsigned N> |
157 | | - requires(N >= Size) |
158 | | -BitfieldMember<ParentSize, Start, Size>::template operator Bits<N>() const { |
159 | | - return (static_cast<Bits<ParentSize>>(m_parent) >> Start) & MaximumValue; |
160 | | -} |
161 | | - |
162 | | -template <unsigned ParentSize, unsigned Start, unsigned Size> |
163 | | -BitfieldMember<ParentSize, Start, Size>::operator PossiblyUndefinedBits() |
164 | | - const { |
165 | | - return (static_cast<Bits<ParentSize>>(m_parent) >> Start) & MaximumValue; |
166 | | -} |
167 | | - |
168 | | -template <unsigned ParentSize, unsigned Start, unsigned Size> |
169 | | -template <unsigned N, bool Signed> |
170 | | -BitfieldMember<ParentSize, Start, Size> & |
171 | | - BitfieldMember<ParentSize, Start, Size>::template |
172 | | - operator=(const _Bits<N, Signed> &value) { |
173 | | - m_parent = (static_cast<Bits<ParentSize>>(m_parent) & ~Mask) | |
174 | | - ((value.template sll<Size>()) & Mask); |
175 | | - return *this; |
176 | | -} |
177 | | - |
178 | | -template <unsigned ParentSize, unsigned Start, unsigned Size> |
179 | | -BitfieldMember<ParentSize, Start, Size> & |
180 | | - BitfieldMember<ParentSize, Start, Size>::template |
181 | | - operator=(const BitfieldMember<ParentSize, Start, Size> &other) { |
182 | | - m_parent = (static_cast<Bits<ParentSize>>(m_parent) & ~Mask) | |
183 | | - ((static_cast<Bits<Size>>(other).template sll<Size>()) & Mask); |
184 | | - return *this; |
185 | | -} |
186 | | -} // namespace udb |
| 194 | +} // namespace udb |
0 commit comments