Skip to content

Commit 0193f27

Browse files
committed
Added new operators to BigInteger, also some code cleanup to match fresh tommath
1 parent fdd011c commit 0193f27

2 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/common/BigInteger.cpp

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace Firebird
7979

8080
BigInteger::BigInteger(const BigInteger& val)
8181
{
82-
CHECK_MP(mp_init_copy(&t, const_cast<mp_int*>(&val.t) ));
82+
CHECK_MP(mp_init_copy(&t, &val.t));
8383
}
8484

8585
BigInteger::~BigInteger()
@@ -89,7 +89,7 @@ namespace Firebird
8989

9090
BigInteger& BigInteger::operator= (const BigInteger& val)
9191
{
92-
CHECK_MP(mp_copy(const_cast<mp_int*>(&val.t), &t));
92+
CHECK_MP(mp_copy(&val.t, &t));
9393
return *this;
9494
}
9595

@@ -102,107 +102,119 @@ namespace Firebird
102102

103103
void BigInteger::assign(unsigned int count, const unsigned char* bytes)
104104
{
105-
CHECK_MP(mp_read_unsigned_bin(&t, bytes, count));
105+
CHECK_MP(mp_from_ubin(&t, bytes, count));
106106
}
107107

108108
BigInteger BigInteger::operator+ (const BigInteger& val) const
109109
{
110110
BigInteger rc;
111-
CHECK_MP(mp_add(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t), &rc.t));
111+
CHECK_MP(mp_add(&t, &val.t, &rc.t));
112112
return rc;
113113
}
114114

115115
BigInteger BigInteger::operator- (const BigInteger& val) const
116116
{
117117
BigInteger rc;
118-
CHECK_MP(mp_sub(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t), &rc.t));
118+
CHECK_MP(mp_sub(&t, &val.t, &rc.t));
119119
return rc;
120120
}
121121

122122
BigInteger BigInteger::operator* (const BigInteger& val) const
123123
{
124124
BigInteger rc;
125-
CHECK_MP(mp_mul(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t), &rc.t));
125+
CHECK_MP(mp_mul(&t, &val.t, &rc.t));
126126
return rc;
127127
}
128128

129129
BigInteger BigInteger::operator/ (const BigInteger& val) const
130130
{
131131
BigInteger rc;
132-
CHECK_MP(mp_div(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t), &rc.t, NULL));
132+
CHECK_MP(mp_div(&t, &val.t, &rc.t, NULL));
133133
return rc;
134134
}
135135

136136
BigInteger BigInteger::operator% (const BigInteger& val) const
137137
{
138138
BigInteger rc;
139-
CHECK_MP(mp_mod(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t), &rc.t));
139+
CHECK_MP(mp_mod(&t, &val.t, &rc.t));
140140
return rc;
141141
}
142142

143143
BigInteger& BigInteger::operator+= (const BigInteger& val)
144144
{
145-
CHECK_MP(mp_add(&t, const_cast<mp_int*>(&val.t), &t));
145+
CHECK_MP(mp_add(&t, &val.t, &t));
146146
return *this;
147147
}
148148

149149
BigInteger& BigInteger::operator-= (const BigInteger& val)
150150
{
151-
CHECK_MP(mp_sub(&t, const_cast<mp_int*>(&val.t), &t));
151+
CHECK_MP(mp_sub(&t, &val.t, &t));
152152
return *this;
153153
}
154154

155155
BigInteger& BigInteger::operator*= (const BigInteger& val)
156156
{
157-
CHECK_MP(mp_mul(&t, const_cast<mp_int*>(&val.t), &t));
157+
CHECK_MP(mp_mul(&t, &val.t, &t));
158158
return *this;
159159
}
160160

161161
BigInteger& BigInteger::operator/= (const BigInteger& val)
162162
{
163-
CHECK_MP(mp_div(&t, const_cast<mp_int*>(&val.t), &t, NULL));
163+
CHECK_MP(mp_div(&t, &val.t, &t, NULL));
164164
return *this;
165165
}
166166

167167
BigInteger& BigInteger::operator%= (const BigInteger& val)
168168
{
169-
CHECK_MP(mp_mod(&t, const_cast<mp_int*>(&val.t), &t));
169+
CHECK_MP(mp_mod(&t, &val.t, &t));
170170
return *this;
171171
}
172172

173173
bool BigInteger::operator== (const BigInteger& val) const
174174
{
175-
return mp_cmp(const_cast<mp_int*>(&t), const_cast<mp_int*>(&val.t)) == 0;
175+
return mp_cmp(&t, &val.t) == MP_EQ;
176+
}
177+
178+
bool BigInteger::operator< (int val) const
179+
{
180+
BigInteger bVal;
181+
mp_set_i32(&bVal.t, val);
182+
return mp_cmp(&t, &bVal.t) == MP_LT;
183+
}
184+
185+
bool BigInteger::operator> (int val) const
186+
{
187+
BigInteger bVal;
188+
mp_set_i32(&bVal.t, val);
189+
return mp_cmp(&t, &bVal.t) == MP_GT;
176190
}
177191

178192
void BigInteger::getBytes(Firebird::UCharBuffer& bytes) const
179193
{
180-
CHECK_MP(mp_to_unsigned_bin(const_cast<mp_int*>(&t), bytes.getBuffer(length())));
194+
size_t len = length();
195+
size_t written = 0;
196+
CHECK_MP(mp_to_ubin(&t, bytes.getBuffer(len), len, &written));
197+
fb_assert(written == len);
181198
}
182199

183200
unsigned int BigInteger::length() const
184201
{
185-
int rc = mp_unsigned_bin_size(const_cast<mp_int*>(&t));
186-
if (rc < 0)
187-
{
188-
check(rc, "mp_unsigned_bin_size(&t)");
189-
}
190-
return static_cast<unsigned int>(rc);
202+
return static_cast<unsigned int>(mp_ubin_size(&t));
191203
}
192204

193205
void BigInteger::getText(string& str, unsigned int radix) const
194206
{
195207
int size;
196-
CHECK_MP(mp_radix_size(const_cast<mp_int*>(&t), radix, &size));
208+
CHECK_MP(mp_radix_size(&t, radix, &size));
197209
str.resize(size - 1, ' ');
198-
CHECK_MP(mp_toradix(const_cast<mp_int*>(&t), str.begin(), radix));
210+
size_t written = 0;
211+
CHECK_MP(mp_to_radix(&t, str.begin(), size, &written, radix));
199212
}
200213

201214
BigInteger BigInteger::modPow(const BigInteger& pow, const BigInteger& mod) const
202215
{
203216
BigInteger rc;
204-
CHECK_MP(mp_exptmod(const_cast<mp_int*>(&t), const_cast<mp_int*>(&pow.t),
205-
const_cast<mp_int*>(&mod.t), &rc.t));
217+
CHECK_MP(mp_exptmod(&t, &pow.t, &mod.t, &rc.t));
206218
return rc;
207219
}
208220

src/common/BigInteger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class BigInteger
4444
BigInteger(unsigned int count, const unsigned char* bytes);
4545
explicit BigInteger(const Firebird::UCharBuffer& val);
4646
BigInteger(const BigInteger& val);
47-
// BigInteger(int numBits, Random& r);
4847
~BigInteger();
4948

5049
BigInteger& operator= (const BigInteger& val);
@@ -64,6 +63,8 @@ class BigInteger
6463
BigInteger& operator%= (const BigInteger& val);
6564

6665
bool operator== (const BigInteger& val) const;
66+
bool operator< (int val) const;
67+
bool operator> (int val) const;
6768

6869
void getBytes(Firebird::UCharBuffer& bytes) const;
6970
unsigned int length() const;

0 commit comments

Comments
 (0)