-
Notifications
You must be signed in to change notification settings - Fork 1
/
sizeduint.h
192 lines (158 loc) · 4.93 KB
/
sizeduint.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
#ifndef JWUTIL_SIZEDUINT_H
#define JWUTIL_SIZEDUINT_H
#include <limits.h>
#include <type_traits>
#include <array>
namespace jw_util
{
template <unsigned int bits>
class SizedUInt
{
private:
typedef std::uint32_t DataType;
typedef std::uint64_t OverflowDataType;
static constexpr unsigned int size = bits / 32;
static_assert(sizeof(DataType) * CHAR_BIT == 32, "The number of bits in DataType is not 32");
static_assert(sizeof(OverflowDataType) * CHAR_BIT == 64, "The number of bits in OverflowDataType is not 64");
static_assert(bits % 32 == 0, "The number of bits must be a multiple of 32");
public:
template <unsigned int op_a_bits, unsigned int op_b_bits>
class Operation
{
public:
enum Operator {OpAdd, OpSub, OpMul, OpDiv, OpMod};
Operation(const SizedUInt<op_a_bits> &op_a, const Operator op_type, const SizedUInt<op_b_bits> &op_b)
: op_a(op_a)
, op_type(op_type)
, op_b(op_b)
{}
private:
const SizedUInt<op_a_bits> &op_a;
const Operator op_type;
const SizedUInt<op_b_bits> &op_b;
template <unsigned int res_bits>
void eval(SizedUInt<res_bits> &res)
{
switch (op_type)
{
case OpAdd:
eval_add(res);
break;
case OpSub:
eval_sub(res);
break;
case OpMul:
eval_mul(res);
break;
case OpDiv:
case OpMod:
break;
}
}
template <unsigned int res_bits>
void eval_add(SizedUInt<res_bits> &res)
{
OverflowDataType carry = 0;
for (unsigned int i = 0; i < res.size; i++)
{
if (i < op_a.size) {carry += op_a.data[i];}
if (i < op_b.size) {carry += op_b.data[i];}
res.data[i] = static_cast<DataType>(carry);
carry >>= 32;
}
}
template <unsigned int res_bits>
void eval_sub(SizedUInt<res_bits> &res)
{
OverflowDataType carry = 0;
for (unsigned int i = 0; i < res.size; i++)
{
if (i < op_a.size) {carry -= op_a.data[i];}
if (i < op_b.size) {carry -= op_b.data[i];}
res.data[i] = static_cast<DataType>(carry);
carry >>= 32;
}
}
template <unsigned int res_bits>
void eval_mul(SizedUInt<res_bits> &res)
{
res.zero();
for (unsigned int i = 0; i < op_a.size; i++)
{
for (unsigned int j = 0; j < op_b.size; j++)
{
res.add_shifted(i + j, op_a.data[i] * op_b.data[j]);
}
}
}
};
SizedUInt()
{}
void zero()
{
data.fill(0);
}
template <typename SetType>
void set_integral(SetType val)
{
static_assert(std::is_integral<SetType>::value, "set_integral argument must be integral");
assert(val >= 0);
for (unsigned int i = 0; i < size; i++)
{
data[i] = val;
val >>= 32;
}
}
void set_raw(const void *val, unsigned int len_bytes)
{
len_bytes = min(len_bytes, size * sizeof(DataType));
void *dst = static_cast<void*>(&data[0]);
memcpy(dst, val, len_bytes);
memset(dst + len_bytes, 0, size - len_bytes);
}
template <unsigned int other_bits>
Operation<bits, other_bits> operator+(const SizedUInt<other_bits> &other) const
{
return Operation<bits, other_bits>(*this, Operation::OpAdd, other);
}
template <unsigned int other_bits>
Operation<bits, other_bits> operator-(const SizedUInt<other_bits> &other) const
{
return Operation<bits, other_bits>(*this, Operation::OpSub, other);
}
template <unsigned int other_bits>
Operation<bits, other_bits> operator*(const SizedUInt<other_bits> &other) const
{
return Operation<bits, other_bits>(*this, Operation::OpMul, other);
}
template <unsigned int other_bits>
SizedUInt<bits> &operator=(const SizedUInt<other_bits>& other)
{
assert(this != &other);
for (unsigned int i = 0; i < size; i++)
{
data[i] = i < other.size ? other.data[i] : 0;
}
return *this;
}
template <unsigned int op_a_bits, unsigned int op_b_bits>
SizedUInt<bits> &operator=(const Operation<op_a_bits, op_b_bits> other)
{
other.eval(*this);
return *this;
}
private:
std::array<DataType, size> data;
void add_shifted(unsigned int shift, OverflowDataType add)
{
OverflowDataType carry = add;
for (unsigned int i = shift; carry && i < size; i++)
{
carry += data[i];
data[i] = carry;
carry >>= 32;
}
}
};
}
#endif // JWUTIL_SIZEDUINT_H