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