-
Notifications
You must be signed in to change notification settings - Fork 0
/
Root_lib.cpp
358 lines (278 loc) · 6.05 KB
/
Root_lib.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef ROOT_LIB
#define ROOT_LIB
#include <iostream>
#include <stdint.h>
#include <vector>
#include <bitset>
#include <fstream>
#include <stdint.h>
#include <sstream>
using namespace std;
const unsigned int word_len = 64;
uint32_t one[word_len] = { 0 };
uint32_t var[word_len] = { 0 };
uint32_t buff[word_len] = { 0 };
uint32_t empty_arr[word_len] = { 0 };
uint32_t x[word_len] = { 0 };
std::chrono::high_resolution_clock::time_point begTime;
std::chrono::high_resolution_clock::time_point endTime;
void timeStart()
{
begTime = std::chrono::high_resolution_clock::now();
}
void timeStop()
{
endTime = std::chrono::high_resolution_clock::now();
}
long execTime()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - begTime).count();
}
void randomizer_init()
{
std::srand(std::time(nullptr));
}
void randomizer(uint32_t array[], int n)
{
//std::cout << "Random value on [0 " << RAND_MAX << "]: " << random_variable << '\n';
for (size_t i = 0; i < n; i++)
{
array[i] = std::rand();
}
}
void copy_(uint32_t a[], uint32_t b[], int n)
{
for (int i = 0; i < n; i++)
{
b[i] = a[i];
}
}
void add_word(uint32_t a[], uint32_t b[], uint32_t result[], int len)
{
int carry = 0;
for (int i = len - 1; i >= 0; i--)
{
result[i] = a[i] + b[i] + carry;
carry = (a[i] > result[i] || b[i] > result[i]) ? 1 : 0;
// if (a[i] + b[i] + carry >= 4294967296)
// {
// carry = 1;
// }
// else carry = 0;
}
}
void subtract_word(uint32_t a[], uint32_t b[], uint32_t result[], int len) // len+1
{
int carry = 0;
for (int i = len - 1; i >= 0; i--)
{
result[i] = a[i] - b[i] - carry;
if (b[i] + carry > a[i])
{
carry = 1;
}
else
{
carry = 0;
}
}
}
void shiftR_word(uint32_t array[], int n)
{
if (n == 0)
return;
for (int i = n - 1; i >= 0; i--)
{
// right shift
array[i] = array [i] >> 1;
// carry bit
if (i > 0)
array[i] = array[i] | (array[i - 1] & 0x1) << 31;
}
}
void printAsBit(uint32_t w_number[], ostream& s, int n )
{
std::cout << std::endl;
for (size_t i = 0; i < n; i++)
{
s << std::bitset<32>(w_number[i]);
}
}
bool equals(uint32_t a[], uint32_t b[], int len)
{
for (int i = 0; i < len; i++)
{
// if (a[i] == b[i])
//return true;
if (a[i] != b[i])
return false;
}
return true;
}
bool isZero(uint32_t a[], int len )
{
for (int i = 0; i < len; i++)
{
if (a[i] != 0)
return false;
}
return true;
}
bool isBigger(uint32_t a[], uint32_t b[], int len)
{
for (int i = 0; i < len; i++)
{
if (a[i] > b[i])
{
return true;
}
else
if (a[i] < b[i])
{
return false;
}
}
return false; // because of warning (function is non-void)
}
void root(uint32_t input[], uint32_t result[], int n)
{
//one[0] = 1L << 30;
for (int i = 0; i < n; i++)
{
if(input[i]!=0)
one[i] = 1L << 30;
}
while (isBigger(one, input, n))
{
shiftR_word(one, n);
shiftR_word(one, n);
}
while (!isZero(one, n))
{
copy_(empty_arr, var, n); //zerowanie var
add_word(result, one, var, n); // var = result + one
if (isBigger(input, var, n) || equals (input,var, n ))
{
copy_(empty_arr, buff, n); //zerowanie buff
subtract_word(input, var, buff, n+1);
copy_(buff, input, n); //input -=var
shiftR_word(result, n);
copy_(empty_arr, x, n); // zerowanie x
add_word(result, one, x, n);
copy_(x, result, n);
}
else
{
shiftR_word(result, n);
}
shiftR_word(one, n);
shiftR_word(one, n);
//printAsBit(one,cout,n);
}
}
string square(string num1, int base = 2)
{
string num2 = num1;
int n1 = num1.size();
int n2 = num2.size();
if (n1 == 0 || n2 == 0)
return "0";
// will keep the result number in vector
// in reverse order
vector<int> result(n1 + n2, 0);
// Below two indexes are used to find positions
// in result.
int i_n1 = 0;
int i_n2 = 0;
// Go from right to left in num1
for (int i = n1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1[i] - '0';
// To shift position to left after every
// multiplication of a digit in num2
i_n2 = 0;
// Go from right to left in num2
for (int j = n2 - 1; j >= 0; j--) {
// Take current digit of second number
int n2 = num2[j] - '0';
// Multiply with current digit of first number
// and add result to previously stored result
// at current position.
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
// Carry for next iteration
carry = sum / base;
// Store result
result[i_n1 + i_n2] = sum % base;
i_n2++;
}
// store carry in next cell
if (carry > 0)
result[i_n1 + i_n2] += carry;
// To shift position to left after every
// multiplication of a digit in num1.
i_n1++;
}
// ignore '0's from the right
int i = result.size() - 1;
while (i >= 0 && result[i] == 0)
i--;
// If all were '0's - means either both or
// one of num1 or num2 were '0'
if (i == -1)
return "0";
// generate the result string
string s = "";
while (i >= 0)
s += std::to_string(result[i--]);
return s;
}
void fillZeros(uint32_t a[], int n)
{
for (int i = 0; i < n; i++)
{
a[i]=0;
}
}
long autoTest(uint32_t array[], uint32_t result[], long numberOfTests , int n)
{
long resultTime=0;
for (size_t i = 0; i < numberOfTests; i++)
{
randomizer(array,n);
timeStart();
root(array,result,n);
timeStop();
resultTime+=execTime();
fillZeros(one,word_len);
fillZeros(var,word_len);
fillZeros(buff,word_len);
fillZeros(empty_arr,word_len);
fillZeros(x,word_len);
}
cout << "\nNumber of tests: " << numberOfTests;
cout << "\nExecution time : " << resultTime;
cout << "\nAverage time : " << resultTime/numberOfTests;
cout << "\nFor " << n*32 << "-bit arguments.";
cout << "\nResults in nanoseconds.\n";
return resultTime;
}
string convert(uint32_t array[], int n, string result)
{
std::stringstream ss;
for (int i = 0; i < n; i++)
{
ss << std::bitset<32>(array[i]);
}
ss >> result;
//cout << result;
string result_str = result;
return result_str;
}
string fillStringToFixedSize(string s, int n)
{
string res = s;
for (size_t i = 0; res.length() < n*32; i++)
res.insert(0,"0");
return res;
}
#endif /* ROOT_LIB */