forked from telosprotocol/xbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xint.h
executable file
·254 lines (231 loc) · 9.18 KB
/
xint.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2018-2020 Telos Foundation & contributors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <memory.h>
#include <functional>
#include "xbase.h"
namespace top
{
namespace base
{
//_predefine_bits_ must be >=64
template<int _predefine_bits_>
class xuint_t
{
public:
enum
{
enum_xint_size_bits = _predefine_bits_,
enum_xint_size_bytes = (_predefine_bits_ + 7) / 8,
enum_xint_size_2bytes = (_predefine_bits_ + 15) / 16,
enum_xint_size_4bytes = (_predefine_bits_ + 31) / 32,
enum_xint_size_8bytes = (_predefine_bits_ + 63) / 64,
};
public:
xuint_t()
{
memset(raw_uint8, 0, sizeof(raw_uint8));
}
xuint_t(const uint64_t init_value)
{
raw_uint64[0] = init_value;
}
xuint_t(uint8_t int_bytes[enum_xint_size_bytes])
{
memcpy(raw_uint8, int_bytes, enum_xint_size_bytes);
}
xuint_t(uint64_t int_8bytes[enum_xint_size_8bytes])
{
memcpy(raw_uint8, int_8bytes, (enum_xint_size_8bytes << 3));
}
xuint_t(const xuint_t & obj)
{
memcpy(raw_uint8, obj.raw_uint8, sizeof(raw_uint8));
}
xuint_t & operator = (const xuint_t & right)
{
memcpy(raw_uint8, right.raw_uint8, sizeof(raw_uint8));
return *this;
}
~xuint_t(){};
public:
inline uint8_t* data() const {return (uint8_t*)raw_uint8;}
inline int size() const {return enum_xint_size_bytes;} //how many bytes for length
bool operator < (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) < 0);
}
bool operator <= (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) <= 0);
}
bool operator > (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) > 0);
}
bool operator >= (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) >= 0);
}
bool operator == (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) == 0);
}
bool operator != (const xuint_t & right) const
{
return (memcmp(raw_uint8, right.raw_uint8, sizeof(raw_uint8)) != 0);
}
void bit_set(uint32_t i) {
if(i < enum_xint_size_bits)
raw_uint32[i >> 5] |= ( ((uint32_t)1) << (i & 31) );
}
void bit_clear(uint32_t i) {
if(i < enum_xint_size_bits)
raw_uint32[i >> 5] &= ~( ((uint32_t)1) << (i & 31) );
}
bool bit_is_set(uint32_t i) const {
if(i >= enum_xint_size_bits)
return false;
uint32_t value = raw_uint32[i >> 5] & ( ((uint32_t)1) << (i & 31) );
return value != 0;
}
void bit_clear_all() {
memset(raw_uint8, 0, sizeof(raw_uint8));
}
void bit_set_all() {
memset(raw_uint8, 0xFF, sizeof(raw_uint8));
}
void bit_merge(const xuint_t & from) //must be size same of bitset
{
for(int i = 0; i < enum_xint_size_8bytes; ++i)
{
raw_uint64[i] |= from.raw_uint64[i];
}
}
void reset()
{
memset(raw_uint8, 0, sizeof(raw_uint8));
}
bool empty() const
{
for(int i = 0; i < enum_xint_size_8bytes; ++i)
{
if(raw_uint64[i] != 0)
return false;
}
return true;
}
public:
union
{
uint8_t raw_uint8[enum_xint_size_bytes];
uint16_t raw_uint16[enum_xint_size_2bytes];
uint32_t raw_uint32[enum_xint_size_4bytes];
uint64_t raw_uint64[enum_xint_size_8bytes];
};
static_assert(enum_xint_size_8bytes > 0,"_predefine_bits_ must be >=64");
static_assert(enum_xint_size_bits <= (enum_xint_size_8bytes * 64),"_predefine_bits_ must be times of 64");
};
//varbiset is an optimization bitset that have boundry of max bits,then alloc some bits to use
template<int _const_max_bits_count_>
class xvarbitset : public xuint_t<_const_max_bits_count_>
{
typedef xuint_t<_const_max_bits_count_> base_class;
protected:
enum {enum_const_max_bits_count = _const_max_bits_count_};
public:
xvarbitset(const uint16_t alloc_bits_count)
:base_class()
{
alloc_bits(alloc_bits_count);
}
xvarbitset(const xvarbitset & obj)
:base_class(obj)
{
m_alloced_bits_count = obj.m_alloced_bits_count;
m_alloced_bytes_count = obj.m_alloced_bytes_count;
m_alloced_8bytes_count = obj.m_alloced_8bytes_count;
}
~xvarbitset(){};
protected://just open for subclass
xvarbitset()
:base_class()
{
alloc_bits(enum_const_max_bits_count);
}
void alloc_bits(const uint16_t alloc_bits_count)
{
m_alloced_bits_count = (alloc_bits_count <= enum_const_max_bits_count) ? alloc_bits_count : enum_const_max_bits_count;
m_alloced_bytes_count = (m_alloced_bits_count + 7) / 8;
m_alloced_8bytes_count = (m_alloced_bytes_count + 7) / 8;
}
private:
xvarbitset & operator = (const xvarbitset &);
public:
inline const int get_alloc_bits() const noexcept {return m_alloced_bits_count;}
inline const int get_alloc_bytes() const noexcept {return m_alloced_bytes_count;}
bool set(const uint32_t pos)
{
if(pos >= m_alloced_bits_count)//must be range of [0,m_bits_count-1];
{
xassert(0);
return false;
}
base_class::raw_uint32[pos >> 5] |= ( ((uint32_t)1) << (pos & 31) );
return true;
}
bool clear(const uint32_t pos)
{
if(pos >= m_alloced_bits_count)//must be range of [0,m_bits_count-1];
{
xassert(0);
return false;
}
base_class::raw_uint32[pos >> 5] &= ~( ((uint32_t)1) << (pos & 31) );
return true;
}
bool is_set(const uint32_t pos) const
{
if(pos >= m_alloced_bits_count) //must be range of [0,m_bits_count-1];
return false;
const uint32_t value = base_class::raw_uint32[pos >> 5] & ( ((uint32_t)1) << (pos & 31) );
return value != 0;
}
void clear_all()
{
memset(base_class::raw_uint8, 0, m_alloced_bytes_count);
}
void set_all()
{
memset(base_class::raw_uint8, 0xFF, m_alloced_bytes_count);
}
bool merge_from(xvarbitset & other) //must be size same of bitset
{
if(other.get_alloc_bits() != get_alloc_bits())
return false;
for(int i = 0; i < m_alloced_8bytes_count; ++i)
{
base_class::raw_uint64[i] |= other.raw_uint64[i];
}
return true;
}
private:
int m_alloced_bits_count;
int m_alloced_bytes_count;
int m_alloced_8bytes_count;
};
}
typedef top::base::xuint_t<128> uint128_t;
typedef top::base::xuint_t<160> uint160_t;
typedef top::base::xuint_t<256> uint256_t;
typedef top::base::xuint_t<512> uint512_t;
//GCC 4.7.1+ or Clang support extend 128bit type,so define xuint128_t as special optimization
//note:128bit has lower performance than 64bit
//_MSC_VER - Windows Visual Studio Compiler flag
#if defined(__LINUX_PLATFORM__) || defined(__MAC_PLATFORM__)
typedef __uint128_t xuint128_t;
#else
typedef uint128_t xuint128_t;
#endif
}; //end of namespace top