-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbig.h
261 lines (212 loc) · 7.36 KB
/
big.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
252
253
254
255
256
257
258
259
260
261
/**
*
* Copyright (c) 2010-2015 Voidware Ltd. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code as
* defined in and that are subject to the Voidware Public Source Licence version
* 1.0 (the 'Licence'). You may not use this file except in compliance with the
* Licence or with expressly written permission from Voidware. Please obtain a
* copy of the Licence at http://www.voidware.com/legal/vpsl1.txt and read it
* before using this file.
*
* The Original Code and all software distributed under the Licence are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
* OR IMPLIED, AND VOIDWARE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Please see the Licence for the specific language governing rights and
* limitations under the Licence.
*
*/
#ifndef __big_h__
#define __big_h__
#include "mi.h"
struct BigInt;
struct BigInt
{
// constructors
BigInt() { _big = 0; }
BigInt(long v) { _big = createBig(v); }
BigInt(unsigned long v) { _big = createBigu(v); }
BigInt(int v) { _big = createBig((long)v); }
BigInt(unsigned int v) { _big = createBigu((unsigned long)v); }
BigInt(Big* b) : _big(b) {}
BigInt(const BigInt& b) { _big = copyBig(b._big); }
// destructor
~BigInt() { destroyBig(_big); }
void operator=(const BigInt& b)
{
// ASSUME not self!
_purge();
_big = copyBig(b._big);
}
void operator=(Big* b)
{
_purge();
_big = b;
}
void operator=(long v)
{
_purge();
_big = createBig(v);
}
void operator=(unsigned long v)
{
_purge();
_big = createBigu(v);
}
bool valid() const { return _big != 0; }
bool isZero() const { return ::isZero(_big); }
bool isNeg() const { return ::isNeg(_big); }
void operator=(int v) { *this = (long)v; }
void operator=(unsigned int v) { *this = (unsigned long)v; }
// operators
friend BigInt operator+(const BigInt& a, const BigInt& b)
{ return addBig(a._big, b._big); }
friend BigInt operator+(const BigInt& a, int b)
{ return addint(a._big, b); }
friend BigInt operator+(const BigInt& a, unsigned int b)
{ return addint(a._big, b); }
friend BigInt operator-(const BigInt& a, const BigInt& b)
{ return subBig(a._big, b._big); }
friend BigInt operator-(const BigInt& a, int b)
{ return addint(a._big, -b); }
friend BigInt operator-(const BigInt& a, unsigned int b)
{ return subint(a._big, b); }
friend BigInt operator*(const BigInt& a, const BigInt& b)
{ return mulBig(a._big, b._big); }
friend BigInt operator*(const BigInt& a, int b)
{ return mulint(a._big, b); }
friend BigInt operator/(const BigInt& a, const BigInt& b)
{ return divBig(a._big, b._big, 0); }
friend Big* operator%(const BigInt& a, const BigInt& n)
{
Big* r;
destroyBig(divBig(a._big, n._big, &r));
return r;
}
friend Big* mod(const BigInt& a, const BigInt& n)
{
// as %, but does not allow negative
Big* r;
destroyBig(divBig(a._big, n._big, &r));
if (::isNeg(r))
{
Big* m = addBig(r, n._big);
destroyBig(r);
r = m;
}
return r;
}
friend BigInt operator<<(const BigInt& a, unsigned int n)
{
return lshiftn(a._big, n);
}
friend BigInt operator>>(const BigInt& a, unsigned int n)
{
return rshiftn(a._big, n);
}
void operator>>=(unsigned int n) { *this = (*this >> n).give(); }
void operator%=(const BigInt& n) { *this = *this % n; }
void operator+=(const BigInt& b) { *this = (*this + b).give(); }
void operator+=(int b) { *this = (*this + b).give(); }
void operator+=(unsigned int b) { *this = (*this + b).give(); }
void operator-=(const BigInt& b) { *this = (*this - b).give(); }
void operator-=(int b) { *this = (*this - b).give(); }
void operator-=(unsigned int b) { *this = (*this - b).give(); }
void operator*=(const BigInt& b) { *this = (*this * b).give(); }
void operator*=(int b) { *this = (*this * b).give(); }
void operator/=(const BigInt& b) { *this = (*this / b).give(); }
// Comparison
friend bool operator<(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) < 0; }
friend bool operator<=(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) <= 0; }
friend bool operator==(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) == 0; }
friend bool operator!=(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) != 0; }
friend bool operator>(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) > 0; }
friend bool operator>=(const BigInt& a, const BigInt& b)
{ return compare(a._big, b._big) >= 0; }
friend BigInt fabs(const BigInt& b)
{
Big* a = copyBig(b._big);
if (::isNeg(a))
a = negateBig(a);
return a;
}
friend BigInt sqrt(const BigInt& a)
{
return rootBig(a._big);
}
friend unsigned int log2(const BigInt& a) { return log2(a._big); }
friend unsigned int log10(const BigInt& a) { return log10(a._big); }
friend BigInt gcd(const BigInt& a, const BigInt& b)
{
// a, b >= 0
return gcdBig(a._big, b._big);
}
friend BigInt pow(const BigInt& a, unsigned int n)
{
// n >= 0
return powerBig(a._big, n);
}
friend BigInt powMod(const BigInt& a, const BigInt& n,
const BigInt& m)
{
// a^n % m, a < m
return powerBigMod(a._big, n._big, m._big);
}
friend BigInt powMod(const BigInt& a, unsigned int n,
const BigInt& m)
{ return powerBigintMod(a._big, n, m._big); }
friend Big* subMod(const BigInt& a, const BigInt& b, const BigInt& n)
{
Big* v = subBig(a._big, b._big);
if (::isNeg(v))
{
Big* t = addBig(v, n._big);
destroyBig(v);
v = t;
}
return v;
}
friend BigInt sqr(const BigInt& a) { return sqrBig(a._big); }
// invert mod n
friend Big* invert(const BigInt& a, const BigInt& n, BigInt&);
bool isOdd() const { return _big && ISODD(_big); }
bool isEven() const { return !ISODD(_big); }
BCD toBCD() const
{
BCD v;
BigToMF(_big, &v);
return v;
}
unsigned int asUInt() const
{
// will be zero if does not convert
return _big ? bigAsUint(_big) : 0;
}
BCD asBCD() const
{
BCD nf(0);
if (_big) BigToMF(_big, &nf);
return nf;
}
// destructive
Big* give() const
{
Big* p = _big;
((BigInt*)this)->_big = 0; // const cast
return p;
}
void negate() { _big = negateBig(_big); }
private:
void _purge() { destroyBig(_big); _big = 0; }
Big* _big;
};
#endif