Skip to content

Commit a323dd2

Browse files
committed
Consistently use unsigned in big-int
1 parent 62ec461 commit a323dd2

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/big-int/bigint-test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ run_clisp_tests (char const *fn)
268268
// =====================================================================
269269

270270
static BigInt
271-
sqrt (int n, int dig)
271+
sqrt (int n, unsigned dig)
272272
{
273273
BigInt N (n);
274274
N *= pow (BigInt (100), dig);

src/big-int/bigint.cc

+34-33
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ adjust_size (unsigned size)
5757
inline int
5858
digit_cmp (onedig_t const *a, onedig_t const *b, unsigned n)
5959
{
60-
for (int i = n; --i >= 0; )
60+
for (unsigned i = n; i > 0; --i)
6161
{
62-
if (a[i] < b[i])
62+
if (a[i - 1] < b[i - 1])
6363
return -1;
64-
else if (a[i] > b[i])
64+
else if (a[i - 1] > b[i - 1])
6565
return 1;
6666
}
6767
return 0;
@@ -135,7 +135,7 @@ static _fast onedig_t
135135
digit_mul (onedig_t *b, unsigned l, onedig_t d)
136136
{
137137
twodig_t p = 0;
138-
for (int i = l; --i >= 0; )
138+
for (unsigned i = l; i > 0; --i)
139139
{
140140
p += twodig_t (d) * twodig_t (*b);
141141
*b++ = onedig_t (p);
@@ -176,11 +176,11 @@ static _fast onedig_t
176176
digit_div (onedig_t *b, unsigned l, onedig_t d)
177177
{
178178
twodig_t r = 0;
179-
for (int i = l; --i >= 0; )
179+
for (unsigned i = l; i > 0; --i)
180180
{
181181
r <<= single_bits;
182-
r |= b[i];
183-
b[i] = onedig_t (r / d);
182+
r |= b[i - 1];
183+
b[i - 1] = onedig_t (r / d);
184184
r %= d;
185185
}
186186
return onedig_t (r);
@@ -258,7 +258,8 @@ static _fast void
258258
digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql)
259259
{
260260
r += ql;
261-
for (int i = ql; --r, --i >= 0; )
261+
--r;
262+
for (unsigned i = ql; i > 0; --r, --i)
262263
{
263264
onedig_t qh = guess_q (r + yl, y + yl - 1);
264265
if (multiply_and_subtract (r, y, yl, qh) == 0)
@@ -267,7 +268,7 @@ digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql
267268
add_back (r, y, yl);
268269
}
269270
if (q != 0)
270-
q[i] = qh;
271+
q[i - 1] = qh;
271272
}
272273
}
273274

@@ -501,16 +502,16 @@ BigInt::scan_on (char const *s, onedig_t b)
501502
for (char c = *s; c; c = *++s)
502503
{
503504
// Convert digit. Use 0..9A..Z for singles up to 36. Ignoring case.
504-
c = toupper (c);
505+
c = (char)toupper (c);
505506
onedig_t dig;
506507
if (c < '0')
507508
return s;
508509
else if (c <= '9')
509-
dig = c - '0';
510+
dig = (onedig_t)(c - '0');
510511
else if (c < 'A')
511512
return s;
512513
else if (c <= 'Z')
513-
dig = c - 'A' + 10;
514+
dig = (onedig_t)(c - 'A' + 10);
514515
else
515516
return s;
516517
if (dig >= b)
@@ -584,7 +585,7 @@ BigInt::as_string (char *p, unsigned l, onedig_t b) const
584585
if (l == 0)
585586
return 0;
586587
onedig_t r = digit_div (dig, len, b);
587-
p[--l] = r < 10 ? r + '0' : 'A' + r - 10;
588+
p[--l] = (char)(r < 10 ? r + '0' : 'A' + r - 10);
588589
if (dig[len-1] == 0)
589590
--len;
590591
}
@@ -631,7 +632,7 @@ BigInt::dump (unsigned char *p, unsigned n)
631632
for (;;)
632633
{
633634
while (i--)
634-
*p++ = d >> i * CHAR_BIT;
635+
*p++ = (unsigned char)(d >> i * CHAR_BIT);
635636
if (t <= digit)
636637
break;
637638
d = *--t;
@@ -682,27 +683,27 @@ BigInt::is_long() const
682683
return false;
683684
// There is exactly one good signed number n with abs (n) having the
684685
// topmost bit set: The most negative number.
685-
for (int l = length - 1; --l >= 0; )
686-
if (digit[l] != 0)
686+
for (unsigned l = length - 1; l > 0; --l)
687+
if (digit[l - 1] != 0)
687688
return false;
688689
return true;
689690
}
690691

691692
ullong_t BigInt::to_ulong() const
692693
{
693694
ullong_t ul = 0;
694-
for (int i = length; --i >= 0; )
695+
for (unsigned i = length; i > 0; --i)
695696
{
696697
ul <<= single_bits;
697-
ul |= digit[i];
698+
ul |= digit[i - 1];
698699
}
699700
return ul;
700701
}
701702

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

708709

@@ -752,7 +753,7 @@ BigInt::compare (llong_t b) const
752753

753754
onedig_t dig[small];
754755
unsigned len;
755-
digit_set (-b, dig, len);
756+
digit_set (ullong_t(-b), dig, len);
756757
if (length < len)
757758
return 1;
758759
if (length > len)
@@ -904,7 +905,7 @@ BigInt &
904905
BigInt::operator+= (llong_t y)
905906
{
906907
bool pos = y > 0;
907-
ullong_t uy = pos ? y : -y;
908+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
908909
onedig_t yb[small];
909910
unsigned yl;
910911
digit_set (uy, yb, yl);
@@ -916,7 +917,7 @@ BigInt &
916917
BigInt::operator-= (llong_t y)
917918
{
918919
bool pos = y > 0;
919-
ullong_t uy = pos ? y : -y;
920+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
920921
onedig_t yb[small];
921922
unsigned yl;
922923
digit_set (uy, yb, yl);
@@ -928,7 +929,7 @@ BigInt &
928929
BigInt::operator*= (llong_t y)
929930
{
930931
bool pos = y > 0;
931-
ullong_t uy = pos ? y : -y;
932+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
932933
onedig_t yb[small];
933934
unsigned yl;
934935
digit_set (uy, yb, yl);
@@ -940,7 +941,7 @@ BigInt &
940941
BigInt::operator/= (llong_t y)
941942
{
942943
bool pos = y > 0;
943-
ullong_t uy = pos ? y : -y;
944+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
944945
onedig_t yb[small];
945946
unsigned yl;
946947
digit_set (uy, yb, yl);
@@ -951,7 +952,7 @@ BigInt &
951952
BigInt::operator%= (llong_t y)
952953
{
953954
bool pos = y > 0;
954-
ullong_t uy = pos ? y : -y;
955+
ullong_t uy = pos ? ullong_t(y) : ullong_t(-y);
955956
onedig_t yb[small];
956957
unsigned yl;
957958
digit_set (uy, yb, yl);
@@ -1077,7 +1078,7 @@ BigInt::div (BigInt const &x, BigInt const &y, BigInt &q, BigInt &r)
10771078
// This digit_div() transforms the dividend into the quotient.
10781079
q = y;
10791080
r.digit[0] = digit_div (q.digit, q.length, y.digit[0]);
1080-
r.length = r.digit[0] ? 1 : 0;
1081+
r.length = r.digit[0] ? 1u : 0u;
10811082
}
10821083
else
10831084
{
@@ -1268,22 +1269,22 @@ BigInt::operator%= (BigInt const &y)
12681269
unsigned
12691270
BigInt::floorPow2 () const
12701271
{
1271-
int i = length - 1; // Start on the last value
1272-
while (i >= 0 && digit[i] == 0) {
1272+
unsigned i = length; // Start on the last value
1273+
while (i > 0 && digit[i - 1] == 0) {
12731274
--i; // Skip zeros
12741275
}
1275-
if (i < 0) {
1276+
if (i == 0) {
12761277
return 0; // Special case
12771278
}
12781279

12791280
twodig_t power = 1;
1280-
int count = 0;
1281+
unsigned count = 0;
12811282

1282-
while ((power << 1) <= (twodig_t)digit[i]) {
1283+
while ((power << 1) <= (twodig_t)digit[i - 1]) {
12831284
++count, power <<= 1;
12841285
}
12851286

1286-
return (single_bits * i) + count;
1287+
return (single_bits * (i - 1)) + count;
12871288
}
12881289

12891290
// Not part of original BigInt.

0 commit comments

Comments
 (0)