CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
uint256.inl
Go to the documentation of this file.
1
9namespace CppCommon {
10
11inline uint256_t::uint256_t() noexcept
12 : _upper(0), _lower(0)
13{
14}
15
16inline uint256_t::uint256_t(int8_t value) noexcept
17 : _upper(0), _lower(value)
18{
19}
20
21inline uint256_t::uint256_t(uint8_t value) noexcept
22 : _upper(0), _lower(value)
23{
24}
25
26inline uint256_t::uint256_t(int16_t value) noexcept
27 : _upper(0), _lower(value)
28{
29}
30
31inline uint256_t::uint256_t(uint16_t value) noexcept
32 : _upper(0), _lower(value)
33{
34}
35
36inline uint256_t::uint256_t(int32_t value) noexcept
37 : _upper(0), _lower(value)
38{
39}
40
41inline uint256_t::uint256_t(uint32_t value) noexcept
42 : _upper(0), _lower(value)
43{
44}
45
46inline uint256_t::uint256_t(int64_t value) noexcept
47 : _upper(0), _lower(value)
48{
49}
50
51inline uint256_t::uint256_t(uint64_t value) noexcept
52 : _upper(0), _lower(value)
53{
54}
55
56inline uint256_t::uint256_t(uint128_t value) noexcept
57 : _upper(0), _lower(value)
58{
59}
60
61template <typename T>
62inline uint256_t::uint256_t(const T& value) noexcept
63 : _upper(0), _lower(value)
64{
65 static_assert((std::is_integral<T>::value || std::is_same<T, uint128_t>::value || std::is_same<T, uint256_t>::value), "Input argument type must be an integer!");
66}
67
68template <typename TUpper, typename TLower>
69inline uint256_t::uint256_t(const TUpper& upper, const TLower& lower) noexcept
70 : _upper(upper), _lower(lower)
71{
72 static_assert(((std::is_integral<TUpper>::value || std::is_same<TUpper, uint128_t>::value || std::is_same<TUpper, uint256_t>::value) && (std::is_integral<TLower>::value || std::is_same<TLower, uint128_t>::value || std::is_same<TLower, uint256_t>::value)), "Input argument types must be integers!");
73}
74
75template <typename TUpperUpper, typename TUpperLower, typename TLowerUpper, typename TLowerLower>
76inline uint256_t::uint256_t(const TUpperUpper& upper_upper, const TUpperLower& upper_lower, const TLowerUpper& lower_upper, const TLowerLower& lower_lower) noexcept
77 : _upper(upper_upper, upper_lower), _lower(lower_upper, lower_lower)
78{
79 static_assert((std::is_integral<TUpperUpper>::value && std::is_integral<TUpperLower>::value && std::is_integral<TLowerUpper>::value && std::is_integral<TLowerLower>::value), "Input argument types must be integers!");
80}
81
82template <typename T>
83inline uint256_t& uint256_t::operator=(const T& value) noexcept
84{
85 static_assert((std::is_integral<T>::value || std::is_same<T, uint128_t>::value || std::is_same<T, uint256_t>::value), "Input argument type must be an integer!");
86
87 _upper = 0;
88 _lower = value;
89 return *this;
90}
91
92inline uint256_t operator+(const uint256_t& value1, const uint256_t& value2) noexcept
93{
94 return uint256_t(value1._upper + value2._upper + (((value1._lower + value2._lower) < value1._lower) ? 1 : 0), value1._lower + value2._lower);
95}
96
97inline uint256_t operator-(const uint256_t& value1, const uint256_t& value2) noexcept
98{
99 return uint256_t(value1._upper - value2._upper - (((value1._lower - value2._lower) > value1._lower) ? 1 : 0), value1._lower - value2._lower);
100}
101
102inline uint256_t operator/(const uint256_t& value1, const uint256_t& value2)
103{
104 return uint256_t::divmod(value1, value2).first;
105}
106
107inline uint256_t operator%(const uint256_t& value1, const uint256_t& value2)
108{
109 return uint256_t::divmod(value1, value2).second;
110}
111
112inline uint256_t operator&(const uint256_t& value1, const uint256_t& value2) noexcept
113{
114 return uint256_t(value1._upper & value2._upper, value1._lower & value2._lower);
115}
116
117inline uint256_t operator|(const uint256_t& value1, const uint256_t& value2) noexcept
118{
119 return uint256_t(value1._upper | value2._upper, value1._lower | value2._lower);
120}
121
122inline uint256_t operator^(const uint256_t& value1, const uint256_t& value2) noexcept
123{
124 return uint256_t(value1._upper ^ value2._upper, value1._lower ^ value2._lower);
125}
126
127inline bool operator==(const uint256_t& value1, const uint256_t& value2) noexcept
128{
129 return ((value1._upper == value2._upper) && (value1._lower == value2._lower));
130}
131
132inline bool operator!=(const uint256_t& value1, const uint256_t& value2) noexcept
133{
134 return ((value1._upper != value2._upper) || (value1._lower != value2._lower));
135}
136
137inline bool operator<(const uint256_t& value1, const uint256_t& value2) noexcept
138{
139 return (value1._upper == value2._upper) ? (value1._lower < value2._lower) : (value1._upper < value2._upper);
140}
141
142inline bool operator>(const uint256_t& value1, const uint256_t& value2) noexcept
143{
144 return (value1._upper == value2._upper) ? (value1._lower > value2._lower) : (value1._upper > value2._upper);
145}
146
147inline bool operator<=(const uint256_t& value1, const uint256_t& value2) noexcept
148{
149 return ((value1 < value2) || (value1 == value2));
150}
151
152inline bool operator>=(const uint256_t& value1, const uint256_t& value2) noexcept
153{
154 return ((value1 > value2) || (value1 == value2));
155}
156
157inline bool operator&&(const uint256_t& value1, const uint256_t& value2) noexcept
158{
159 return ((bool)value1 && (bool)value2);
160}
161
162inline bool operator||(const uint256_t& value1, const uint256_t& value2) noexcept
163{
164 return ((bool)value1 || (bool)value2);
165}
166
167inline std::ostream& operator<<(std::ostream& os, const uint256_t& value)
168{
169 if (os.flags() & os.oct)
170 os << value.string(8);
171 else if (os.flags() & os.dec)
172 os << value.string(10);
173 else if (os.flags() & os.hex)
174 os << value.string(16);
175 return os;
176}
177
178inline std::wostream& operator<<(std::wostream& os, const uint256_t& value)
179{
180 if (os.flags() & os.oct)
181 os << value.wstring(8);
182 else if (os.flags() & os.dec)
183 os << value.wstring(10);
184 else if (os.flags() & os.hex)
185 os << value.wstring(16);
186 return os;
187}
188
189inline void uint256_t::swap(uint256_t& value) noexcept
190{
191 using std::swap;
192 swap(_upper, value._upper);
193 swap(_lower, value._lower);
194}
195
196inline void swap(uint256_t& value1, uint256_t& value2) noexcept
197{
198 value1.swap(value2);
199}
200
201} // namespace CppCommon
202
203#if defined(FMT_VERSION)
204template <>
205struct fmt::formatter<CppCommon::uint256_t> : formatter<std::string_view>
206{
207 template <typename FormatContext>
208 auto format(const CppCommon::uint256_t& value, FormatContext& ctx) const
209 {
210 return formatter<string_view>::format(value.string(10), ctx);
211 }
212};
213#endif
214
216template <>
217struct std::hash<CppCommon::uint256_t>
218{
219 typedef CppCommon::uint256_t argument_type;
220 typedef size_t result_type;
221
222 result_type operator() (const argument_type& value) const
223 {
224 result_type result = 17;
225 std::hash<CppCommon::uint128_t> hasher;
226 result = result * 31 + hasher(value.upper());
227 result = result * 31 + hasher(value.lower());
228 return result;
229 }
230};
Unsigned 128-bit integer type.
Definition uint128.h:28
Unsigned 256-bit integer type.
Definition uint256.h:21
friend void swap(uint256_t &value1, uint256_t &value2) noexcept
Definition uint256.inl:196
std::wstring wstring(size_t base=10, size_t length=0) const
Get wide string from the current 128-bit integer.
Definition uint256.cpp:135
std::string string(size_t base=10, size_t length=0) const
Get string from the current 128-bit integer.
Definition uint256.cpp:110
static std::pair< uint256_t, uint256_t > divmod(const uint256_t &x, const uint256_t &y)
Calculate quotient and remainder when dividing X by Y.
Definition uint256.cpp:160
uint256_t() noexcept
Definition uint256.inl:11
uint256_t & operator=(const T &value) noexcept
Definition uint256.inl:83
C++ Common project definitions.
bool operator==(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:115
uint128_t operator-(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:85
bool operator!=(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:120
constexpr auto operator|(TEnum value1, TEnum value2) noexcept -> typename std::enable_if< IsEnumFlags< TEnum >::value, Flags< TEnum > >::type
Definition flags.inl:24
std::ostream & operator<<(std::ostream &os, const uint128_t &value)
Definition uint128.inl:155
constexpr auto operator^(TEnum value1, TEnum value2) noexcept -> typename std::enable_if< IsEnumFlags< TEnum >::value, Flags< TEnum > >::type
Definition flags.inl:30
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition filecache.inl:23
bool operator<(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:125
bool operator>(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:130
constexpr auto operator&(TEnum value1, TEnum value2) noexcept -> typename std::enable_if< IsEnumFlags< TEnum >::value, Flags< TEnum > >::type
Definition flags.inl:18
uint128_t operator%(const uint128_t &value1, const uint128_t &value2)
Definition uint128.inl:95
bool operator<=(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:135
uint128_t operator+(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:80
uint128_t operator/(const uint128_t &value1, const uint128_t &value2)
Definition uint128.inl:90
bool operator>=(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:140
bool operator&&(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:145
bool operator||(const uint128_t &value1, const uint128_t &value2) noexcept
Definition uint128.inl:150