-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
234 lines (173 loc) · 5.18 KB
/
Copy pathmain.cpp
File metadata and controls
234 lines (173 loc) · 5.18 KB
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
#include <typeinfo>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
//Note, this function is not optimized and is for illustration purposes
// see void multiply_any(int digits, LowType *a, LowType *b, LowType *c) which handles zeros more like multiplication in actual practice
void multiply_uint32_t(int digits, uint32_t *a, uint32_t *b, uint32_t *c)
{
uint32_t carry;
for (int place_c=0; place_c < digits*2; place_c++) c[place_c] = 0;
//for each digit in a
for (int place_a=0; place_a<digits; place_a++) {
int place_b;
carry = 0;
//multiply with the digits in b
for (place_b = 0; place_b < digits; place_b++) {
uint64_t v = carry + a[place_a] * (uint64_t) b[place_b] + c[place_a + place_b];
uint32_t low = (uint32_t) v; //store the low half
uint32_t high = (uint32_t) v>>32; //store the high half;
c[place_a + place_b] = low;
carry = high;
}
//process the carry
for (; carry != 0 && place_b < digits * 2; place_b++)
{
uint64_t v = carry + c[place_a + place_b];
uint32_t low = (uint32_t) v; //store the low half
uint32_t high = (uint32_t) v>>32; //store the high half;
c[place_a + place_b] = low;
carry = high;
}
}
}
template<typename LowType, typename HighType>
void multiply_any(int digits, LowType *a, LowType *b, LowType *c)
{
LowType carry;
for (int place_c=0; place_c < digits*2; place_c++) c[place_c] = 0;
//for each digit in a
for (int place_a=0; place_a<digits; place_a++) {
int place_b;
carry = 0;
if (a[place_a] ==0) continue;
//multiply with the digits in b
for (place_b = 0; place_b < digits; place_b++) {
HighType v;
if (b[place_b] != 0) {
v = carry + a[place_a] * (HighType) b[place_b] + c[place_a + place_b];;
} else {
v = carry + c[place_a + place_b];
}
carry = (LowType) (v >> (sizeof(LowType)*8));
c[place_a + place_b] = (LowType) v;
}
//process the carry
for (; carry != 0 && place_b < digits * 2; place_b++)
{
HighType v = carry + c[place_a + place_b];
c[place_a + place_b] = (LowType) v;
carry = (LowType) (v >> (sizeof(LowType)*8));
}
}
}
class Node
{
public:
const std::type_info *ti;
char name[1024];
Node *Next = nullptr;
Node(const std::type_info *ti, const char *name)
{
this->ti = ti;
int i;
for (i=0; i<1023; i++)
{
if (name[i] == 0) break;
this->name[i] = name[i];
}
this->name[i] = 0;
}
const char *GetName(const std::type_info *t)
{
for (Node *N = this; N != nullptr; N = N->Next)
if (N->ti == t) return N->name;
return nullptr;
}
};
Node Head(NULL, "");
template<typename LowType, typename HighType>
void multiply_uint64_t(uint64_t a, uint64_t b)
{
const int digits = sizeof(uint64_t) / sizeof(LowType);
LowType _a[digits];
LowType _b[digits];
LowType _c[digits*2];
uint64_t a_q = a;
uint64_t b_q = b;
const int shift = sizeof(LowType)<<3;
for (int digit=0; digit < digits; digit++)
{
_a[digit] = (LowType) a_q;
_b[digit] = (LowType) b_q;
a_q >>= shift;
b_q >>= shift;
}
multiply_any<LowType,HighType>(digits, _a, _b, _c);
char FormatString[1024];
sprintf(FormatString, "%s%ldX", "%0", sizeof(LowType)*2);
printf("%s: ", Head.GetName(&typeid(LowType)));
fflush(stdout);
for (int i=digits-1; i>=0; i--)
{
printf(FormatString, (int) _a[i]);
fflush(stdout);
}
printf(" * ");
fflush(stdout);
for (int i=digits-1; i>=0; i--)
{
printf(FormatString, (int) _b[i]);
fflush(stdout);
}
printf(" = ");
fflush(stdout);
for (int i=(digits<<1)-1; i>=0; i--)
{
printf(FormatString, (int) _c[i]);
fflush(stdout);
}
printf("\n");
fflush(stdout);
}
void test(uint64_t a, uint64_t b)
{
multiply_uint64_t<uint8_t, uint16_t>(a, b);
multiply_uint64_t<uint16_t, uint32_t>(a, b);
multiply_uint64_t<uint32_t, uint64_t>(a, b);
}
uint64_t Getuint64_t()
{
uint64_t R = 0;
for (int i=0; i < 8; i++)
{
((unsigned char *)&R)[i] = rand() % 0xFF;
}
return R;
}
int main(int argc, char **argv) {
printf("%X\n", 0xFF * 0xFF);
uint64_t a = 0x1000000010000001ul;
uint64_t b = 0x2000000000000002ul;
Node *Cur = &Head;
Node *q;
q = new Node(&typeid(uint8_t), "uint8_t");
Cur = Cur->Next = q;
q = new Node(&typeid(uint16_t), "uint16_t");
Cur = Cur->Next = q;
q = new Node(&typeid(uint32_t), "uint32_t");
Cur = Cur->Next = q;
q = new Node(&typeid(uint64_t), "uint64_t");
Cur = Cur->Next = q;
test(a,b);
srand(0);
printf("run carry\n");
test(0xFFFFFFFFFFFFFFFFul, 0xFFFFFFFFFFFFFFFFul);
for (int i=0; i<4; i++) {
printf("run %d\n", i);
a = Getuint64_t();
b = Getuint64_t();
test(a, b);
}
return 0;
}