bigintcpp

bigintcpp Commit Details


Date:2013-08-11 14:50:00 (11 years 4 months ago)
Author:Natalie Adams
Branch:default
Commit:6aca31b7bfa0
Message:initial commit

Changes:
Abigint.cpp (full)
Abigint.h (full)
Amain.cpp (full)

File differences

bigint.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
#include <iostream>
#include "bigint.h"
using namespace std;
int bigint::sizeofchar(char* chr_arr)
{
char chr = '1';
int i = 0;
while(chr > 47 && chr < 58)
{
chr = chr_arr[i];
++i;
}
return i-1;
}
bigint::bigint()
{
#if __DEBUG
cout << "Init big int" << endl;
#endif
}
void bigint::testHalfAddr(int xbit, int ybit)
{
this->xbit = xbit;
this->ybit = ybit;
halfaddr();
}
void bigint::testFullAddr(int xbit, int ybit, int cbit)
{
this->xbit = xbit;
this->ybit = ybit;
this->incbit = cbit;
fulladdr();
}
int bigint::getSbit()
{
return this->sbit;
}
int bigint::getCbit()
{
return this->cbit;
}
void bigint::halfaddr()
{
int cbit;
int sbit;
int xbit;
int ybit;
xbit = this->xbit;
ybit = this->ybit;
int notcbit;
__asm{
moveax, xbit
andeax, ybit
mov cbit, eax;cbit = xbit AND ybit
movebx, cbit
movnotcbit, ebx
notnotcbit;notcbit = NOT cbit
moveax, xbit
oreax, ybit
andeax, notcbit
movsbit, eax;sbit = (xbit OR ybit) AND notcbit
}
this->cbit = cbit;
this->sbit = sbit;
}
void bigint::fulladdr()
{
int cbit;
int cbit1;
int sbit;
int xbit = this->xbit;
int ybit = this->ybit;
int incbit = this->incbit;
halfaddr();
cbit = this->cbit;
sbit = this->sbit;
__asm {
movebx, cbit
mov cbit1, ebx;cbit1 = cbit
mov ebx, sbit
mov xbit, ebx;xbit = sbit
mov ebx, incbit
mov ybit, ebx;ybit = incbit
}
this->xbit = xbit;
this->ybit = ybit;
halfaddr();
cbit = this->cbit;
__asm {
;call halfaddr;halfaddr(xbit, ybit)
mov ebx, cbit
or ebx, cbit1
mov cbit, ebx;cbit = cbit OR cbit1
}
this->cbit = cbit;
}
int bigint::length()
{
int sze = 0;
int lng = this->num.size();
int * arr = this->getNumber();
__asm {
mov ecx, lng
mov eax, lng
mov edx, 4
mul edx
sub eax, 4
mov ebx, eax
mov eax, arr
mov edx, 0
lpcnt:
cmp edx, [eax+ebx]
jne lpcnt2
dec ebx
dec ebx
dec ebx
dec ebx ;is dec 4 faster than sub 4?
;who knows?
loop lpcnt
lpcnt2:
mov ecx, 4
mov eax, ebx
div ecx
inc eax
mov sze, eax
}
/*for(int i = this->num.size() -1; i > 0; i--)
{
if (num[i] != 0)
{
size = i;
break;
}
}*/
delete arr;
return sze;
}
int* bigint::getNumber()
{
int* tmp; //I hope the caller remembers to delete this! ):
tmp = new int[this->num.size()+2];
for(int i = 0; i < (int)this->num.size(); i++)
tmp[i] = this->num[i];
return tmp;
}
int bigint::isnum(int n)
{
if (n > 0)
return n;
else
return 0;
}
bigint bigint::operator+(bigint& num)
{
bigint ret;
int* src_n = this->getNumber();
int* src2_n = num.getNumber();
int nsize = 0;
//int lng = (this->length() > num.length()) ? (num.length()) : (this->length());
//int lngarr = (this->length() < num.length()) ? (num.length()) : (this->length());
//int * dest = new int[lngarr+1];
int carry = 0;
if (this->length() > num.length())
{
for (int i = 0; i < num.length() + 1; ++i)
{
src_n[i] = src_n[i] + this->isnum(src2_n[i]) + carry;
carry = 0;
if (src_n[i] >= 10)
{
src_n[i] -= 10;
carry = 1;
}
}
int n = this->length();
if (this->length() < num.length())
src2_n[n] += carry;
else
src2_n[n] = carry;
//src_n[n] += carry;
char * tmp;
tmp = new char[this->num.size()+1];
int x = 0;
if (carry)
nsize = this->num.size();
else
nsize = this->num.size() - 1;
for (int i = nsize; i >= 0; --i) //reverse, reverse!
{
tmp[x] = src_n[i] + 48;
++x;
}
ret.equals(tmp);
delete tmp;
//this->num.clear();
} else {
for (int i = 0; i < this->length() + 1; ++i)
{
src2_n[i] = this->isnum(src_n[i]) + src2_n[i] + carry;
carry = 0;
if (src2_n[i] >= 10)
{
src2_n[i] -= 10;
carry = 1;
}
}
int n = num.length();
if (this->length() > num.length())
src2_n[n] += carry;
else
src2_n[n] = carry;
char * tmp;
tmp = new char[num.num.size()+1];
int x = 0;
if (carry)
nsize = num.num.size();
else
nsize = num.num.size() - 1;
for (int i = nsize; i >= 0; --i) //reverse, reverse!
{
tmp[x] = src2_n[i] + 48;
++x;
}
ret.equals(tmp);
delete tmp;
//num.num.clear();
}
//dest[lng] += carry;
//for (int i = 0; i < (int)num.num.size(); i++)
//cout << src2_n[i];
//delete dest;
delete [] src_n;
delete [] src2_n;
return ret;
}
void bigint::equals(char* cn)
{
this->num.clear();
//cout << this->sizeofchar(chr_arr) << endl;
//int nDigits = this->sizeofchar(chr_arr);
int i;
//pushback(num);
//we have to do this in reverse
__asm {
;push num
push cn
mov ecx, this
call bigint::sizeofchar
mov ecx, eax
mov i, ecx
dec i
lpprep:
/*
We need to use assembly here to convert a ASCII number to an actual number
*/
;char_tmp[0] = chr_arr[i];
;lea ax, char_tmp[0]
mov eax, cn
sub edx, edx
mov ebx, i
mov dl, [eax+ebx]
sub dl, 48
;mov num, edx
//ints are 4?
;mov eax, edx
//need to push ecx BEFORE you push the number, right?
push ecx
push edx
mov ecx, this
callbigint::pushback
pop ecx
dec i
;push ecx
//num = atoi(char_tmp[0]);
//pushback(num);
;pop ecx
;push ecx
loop lpprep
;pop ecx
}
/*for(int i = nDigits-1; i >= 0; --i)
{
char_tmp[0] = chr_arr[i];
num = atoi(char_tmp);
this->num.push_back(num);
}*/
//return *this;
}
ostream& operator<<(ostream& os,bigint& bi)
{
int size = bi.num.size();
int* asm_bi;
asm_bi = new int[size];
for (int x = 0; x < size; ++x)
asm_bi[x] = bi.num[x];
int arry[] = {2,0};
int n1;
int nDigits = bi.length();
//int i;
__asm {
mov ecx, nDigits
mov eax, nDigits
mov edx, 4
mul edx
mov ebx, eax
sub ebx, 4
push ebx
lpout:
mov eax, asm_bi
pop ebx
mov edx, [eax+ebx]
sub ebx, 4
mov n1, edx
push ebx
push ecx
}
os << n1;
__asm {
pop ecx
loop lpout
pop ebx
}
delete[] asm_bi;
asm_bi = NULL;
return os;
}
void bigint::pushback(int n)
{
this->num.push_back(n);
}
bigint::bigint(char* chr_arr)
{
this->equals(chr_arr);
}
bigint.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
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
/*
Theory:
The largest digit number this library can handle in 32bit is 4,294,967,295
The largest digit number this library can handle in 64bit is 18,446,744,073,709,551,615
(Source: wikipedia)
*/
#define VERSION "1.0-0"
class bigint {
private:
std::vector<int> num;
int sizeofchar(char* chr_arr);
int cbit;
int sbit;
int ybit;
int xbit;
int incbit;
void halfaddr();
void fulladdr();
friend std::ostream& operator<<(std::ostream& os,bigint& bi);
public:
void pushback(int n);
void equals(char* cn);
bigint operator+(bigint& num);
int isnum(int n);
int* getNumber();
int length();
void testHalfAddr(int xbit, int ybit);
void testFullAddr(int xbit, int ybit, int cbit);
int getSbit();
int getCbit();
bigint();
bigint(char* chr_arr);
};
#endif
main.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
#include <iostream>
#include "bigint.h"
#define _WINDOWS
#if defined(_WINDOWS)
#include <windows.h>
#include "psapi.h"
#endif
using namespace std;
int main()
{
HANDLE hProc = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS_EX info;
info.cb = sizeof(info);
bigint n1("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
//n1.equals("1234");
bigint n2("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
BOOL okay = GetProcessMemoryInfo(hProc, (PROCESS_MEMORY_COUNTERS*)&info, info.cb);
n2 = n2 + n1;
cout << n2 << endl;
cout << (float)info.WorkingSetSize/1024/1024 << endl;
return 0;
}

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.42496s using 14 queries.