Skip to content

Commit da31060

Browse files
committed
Consistently use unsigned in big-int
1 parent 66004dc commit da31060

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

src/big-int/bigint.cc

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ adjust_size (unsigned size)
5858
inline int
5959
digit_cmp (onedig_t const *a, onedig_t const *b, unsigned n)
6060
{
61-
for (int i = n; --i >= 0; )
62-
{
63-
if (a[i] < b[i])
64-
return -1;
65-
else if (a[i] > b[i])
66-
return 1;
67-
}
61+
for(unsigned i = n; i > 0; --i)
62+
{
63+
if(a[i - 1] < b[i - 1])
64+
return -1;
65+
else if(a[i - 1] > b[i - 1])
66+
return 1;
67+
}
6868
return 0;
6969
}
7070

@@ -136,12 +136,12 @@ static _fast onedig_t
136136
digit_mul (onedig_t *b, unsigned l, onedig_t d)
137137
{
138138
twodig_t p = 0;
139-
for (int i = l; --i >= 0; )
140-
{
141-
p += twodig_t (d) * twodig_t (*b);
142-
*b++ = onedig_t (p);
143-
p >>= single_bits;
144-
}
139+
for(unsigned i = l; i > 0; --i)
140+
{
141+
p += twodig_t(d) * twodig_t(*b);
142+
*b++ = onedig_t(p);
143+
p >>= single_bits;
144+
}
145145
return onedig_t (p);
146146
}
147147

@@ -177,13 +177,13 @@ static _fast onedig_t
177177
digit_div (onedig_t *b, unsigned l, onedig_t d)
178178
{
179179
twodig_t r = 0;
180-
for (int i = l; --i >= 0; )
181-
{
182-
r <<= single_bits;
183-
r |= b[i];
184-
b[i] = onedig_t (r / d);
185-
r %= d;
186-
}
180+
for(unsigned i = l; i > 0; --i)
181+
{
182+
r <<= single_bits;
183+
r |= b[i - 1];
184+
b[i - 1] = onedig_t(r / d);
185+
r %= d;
186+
}
187187
return onedig_t (r);
188188
}
189189

@@ -259,17 +259,18 @@ static _fast void
259259
digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql)
260260
{
261261
r += ql;
262-
for (int i = ql; --r, --i >= 0; )
262+
--r;
263+
for(unsigned i = ql; i > 0; --r, --i)
264+
{
265+
onedig_t qh = guess_q(r + yl, y + yl - 1);
266+
if(multiply_and_subtract(r, y, yl, qh) == 0)
263267
{
264-
onedig_t qh = guess_q (r + yl, y + yl - 1);
265-
if (multiply_and_subtract (r, y, yl, qh) == 0)
266-
{
267-
--qh;
268-
add_back (r, y, yl);
269-
}
270-
if (q != 0)
271-
q[i] = qh;
268+
--qh;
269+
add_back(r, y, yl);
272270
}
271+
if(q != 0)
272+
q[i - 1] = qh;
273+
}
273274
}
274275

275276

@@ -502,16 +503,16 @@ BigInt::scan_on (char const *s, onedig_t b)
502503
for (char c = *s; c; c = *++s)
503504
{
504505
// Convert digit. Use 0..9A..Z for singles up to 36. Ignoring case.
505-
c = toupper (c);
506+
c = (char)toupper(c);
506507
onedig_t dig;
507508
if (c < '0')
508509
return s;
509510
else if (c <= '9')
510-
dig = c - '0';
511+
dig = (onedig_t)(c - '0');
511512
else if (c < 'A')
512513
return s;
513514
else if (c <= 'Z')
514-
dig = c - 'A' + 10;
515+
dig = (onedig_t)(c - 'A' + 10);
515516
else
516517
return s;
517518
if (dig >= b)
@@ -585,7 +586,7 @@ BigInt::as_string (char *p, unsigned l, onedig_t b) const
585586
if (l == 0)
586587
return 0;
587588
onedig_t r = digit_div (dig, len, b);
588-
p[--l] = r < 10 ? r + '0' : 'A' + r - 10;
589+
p[--l] = (char)(r < 10 ? r + '0' : 'A' + r - 10);
589590
if (dig[len-1] == 0)
590591
--len;
591592
}
@@ -632,7 +633,7 @@ BigInt::dump (unsigned char *p, unsigned n)
632633
for (;;)
633634
{
634635
while (i--)
635-
*p++ = d >> i * CHAR_BIT;
636+
*p++ = (unsigned char)(d >> i * CHAR_BIT);
636637
if (t <= digit)
637638
break;
638639
d = *--t;
@@ -683,27 +684,27 @@ BigInt::is_long() const
683684
return false;
684685
// There is exactly one good signed number n with abs (n) having the
685686
// topmost bit set: The most negative number.
686-
for (int l = length - 1; --l >= 0; )
687-
if (digit[l] != 0)
687+
for(unsigned l = length - 1; l > 0; --l)
688+
if(digit[l - 1] != 0)
688689
return false;
689690
return true;
690691
}
691692

692693
ullong_t BigInt::to_ulong() const
693694
{
694695
ullong_t ul = 0;
695-
for (int i = length; --i >= 0; )
696-
{
697-
ul <<= single_bits;
698-
ul |= digit[i];
699-
}
696+
for(unsigned i = length; i > 0; --i)
697+
{
698+
ul <<= single_bits;
699+
ul |= digit[i - 1];
700+
}
700701
return ul;
701702
}
702703

703704
llong_t BigInt::to_long() const
704705
{
705-
ullong_t ul = to_ulong();
706-
return positive ? ul : -llong_t (ul);
706+
llong_t l = llong_t(to_ulong());
707+
return positive ? l : -l;
707708
}
708709

709710

@@ -753,7 +754,7 @@ BigInt::compare (llong_t b) const
753754

754755
onedig_t dig[small];
755756
unsigned len;
756-
digit_set (-b, dig, len);
757+
digit_set(ullong_t(-b), dig, len);
757758
if (length < len)
758759
return 1;
759760
if (length > len)
@@ -905,7 +906,7 @@ BigInt &
905906
BigInt::operator+= (llong_t y)
906907
{
907908
bool pos = y > 0;
908-
ullong_t uy = pos ? y : -y;
909+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
909910
onedig_t yb[small];
910911
unsigned yl;
911912
digit_set (uy, yb, yl);
@@ -917,7 +918,7 @@ BigInt &
917918
BigInt::operator-= (llong_t y)
918919
{
919920
bool pos = y > 0;
920-
ullong_t uy = pos ? y : -y;
921+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
921922
onedig_t yb[small];
922923
unsigned yl;
923924
digit_set (uy, yb, yl);
@@ -929,7 +930,7 @@ BigInt &
929930
BigInt::operator*= (llong_t y)
930931
{
931932
bool pos = y > 0;
932-
ullong_t uy = pos ? y : -y;
933+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
933934
onedig_t yb[small];
934935
unsigned yl;
935936
digit_set (uy, yb, yl);
@@ -941,7 +942,7 @@ BigInt &
941942
BigInt::operator/= (llong_t y)
942943
{
943944
bool pos = y > 0;
944-
ullong_t uy = pos ? y : -y;
945+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
945946
onedig_t yb[small];
946947
unsigned yl;
947948
digit_set (uy, yb, yl);
@@ -952,7 +953,7 @@ BigInt &
952953
BigInt::operator%= (llong_t y)
953954
{
954955
bool pos = y > 0;
955-
ullong_t uy = pos ? y : -y;
956+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
956957
onedig_t yb[small];
957958
unsigned yl;
958959
digit_set (uy, yb, yl);
@@ -1078,7 +1079,7 @@ BigInt::div (BigInt const &x, BigInt const &y, BigInt &q, BigInt &r)
10781079
// This digit_div() transforms the dividend into the quotient.
10791080
q = y;
10801081
r.digit[0] = digit_div (q.digit, q.length, y.digit[0]);
1081-
r.length = r.digit[0] ? 1 : 0;
1082+
r.length = r.digit[0] ? 1u : 0u;
10821083
}
10831084
else
10841085
{
@@ -1269,22 +1270,25 @@ BigInt::operator%= (BigInt const &y)
12691270
unsigned
12701271
BigInt::floorPow2 () const
12711272
{
1272-
int i = length - 1; // Start on the last value
1273-
while (i >= 0 && digit[i] == 0) {
1273+
unsigned i = length; // Start on the last value
1274+
while(i > 0 && digit[i - 1] == 0)
1275+
{
12741276
--i; // Skip zeros
12751277
}
1276-
if (i < 0) {
1278+
if(i == 0)
1279+
{
12771280
return 0; // Special case
12781281
}
12791282

12801283
twodig_t power = 1;
1281-
int count = 0;
1284+
unsigned count = 0;
12821285

1283-
while ((power << 1) <= (twodig_t)digit[i]) {
1286+
while((power << 1) <= (twodig_t)digit[i - 1])
1287+
{
12841288
++count, power <<= 1;
12851289
}
12861290

1287-
return (single_bits * i) + count;
1291+
return (single_bits * (i - 1)) + count;
12881292
}
12891293

12901294
// Not part of original BigInt.

unit/big-int/big-int.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ TEST_CASE("arbitrary precision integers", "[core][big-int][bigint]")
245245

246246
REQUIRE(N == M);
247247

248-
REQUIRE(N.floorPow2() == 0);
248+
REQUIRE(N.floorPow2() == 0U);
249249

250250
N -= 1; // 0
251-
REQUIRE(N.floorPow2() == 0);
251+
REQUIRE(N.floorPow2() == 0U);
252252

253253
N += 2; // 2
254-
REQUIRE(N.floorPow2() == 1);
254+
REQUIRE(N.floorPow2() == 1U);
255255
}
256256
}

0 commit comments

Comments
 (0)