From da310607b7aa8cbc3786824f2f62eed311890751 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Wed, 20 Jun 2018 19:38:05 +0100 Subject: [PATCH 1/3] Consistently use unsigned in big-int --- src/big-int/bigint.cc | 116 ++++++++++++++++++++------------------- unit/big-int/big-int.cpp | 6 +- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/src/big-int/bigint.cc b/src/big-int/bigint.cc index 978c63e2ad0..33e555bf2d1 100644 --- a/src/big-int/bigint.cc +++ b/src/big-int/bigint.cc @@ -58,13 +58,13 @@ adjust_size (unsigned size) inline int digit_cmp (onedig_t const *a, onedig_t const *b, unsigned n) { - for (int i = n; --i >= 0; ) - { - if (a[i] < b[i]) - return -1; - else if (a[i] > b[i]) - return 1; - } + for(unsigned i = n; i > 0; --i) + { + if(a[i - 1] < b[i - 1]) + return -1; + else if(a[i - 1] > b[i - 1]) + return 1; + } return 0; } @@ -136,12 +136,12 @@ static _fast onedig_t digit_mul (onedig_t *b, unsigned l, onedig_t d) { twodig_t p = 0; - for (int i = l; --i >= 0; ) - { - p += twodig_t (d) * twodig_t (*b); - *b++ = onedig_t (p); - p >>= single_bits; - } + for(unsigned i = l; i > 0; --i) + { + p += twodig_t(d) * twodig_t(*b); + *b++ = onedig_t(p); + p >>= single_bits; + } return onedig_t (p); } @@ -177,13 +177,13 @@ static _fast onedig_t digit_div (onedig_t *b, unsigned l, onedig_t d) { twodig_t r = 0; - for (int i = l; --i >= 0; ) - { - r <<= single_bits; - r |= b[i]; - b[i] = onedig_t (r / d); - r %= d; - } + for(unsigned i = l; i > 0; --i) + { + r <<= single_bits; + r |= b[i - 1]; + b[i - 1] = onedig_t(r / d); + r %= d; + } return onedig_t (r); } @@ -259,17 +259,18 @@ static _fast void digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql) { r += ql; - for (int i = ql; --r, --i >= 0; ) + --r; + for(unsigned i = ql; i > 0; --r, --i) + { + onedig_t qh = guess_q(r + yl, y + yl - 1); + if(multiply_and_subtract(r, y, yl, qh) == 0) { - onedig_t qh = guess_q (r + yl, y + yl - 1); - if (multiply_and_subtract (r, y, yl, qh) == 0) - { - --qh; - add_back (r, y, yl); - } - if (q != 0) - q[i] = qh; + --qh; + add_back(r, y, yl); } + if(q != 0) + q[i - 1] = qh; + } } @@ -502,16 +503,16 @@ BigInt::scan_on (char const *s, onedig_t b) for (char c = *s; c; c = *++s) { // Convert digit. Use 0..9A..Z for singles up to 36. Ignoring case. - c = toupper (c); + c = (char)toupper(c); onedig_t dig; if (c < '0') return s; else if (c <= '9') - dig = c - '0'; + dig = (onedig_t)(c - '0'); else if (c < 'A') return s; else if (c <= 'Z') - dig = c - 'A' + 10; + dig = (onedig_t)(c - 'A' + 10); else return s; if (dig >= b) @@ -585,7 +586,7 @@ BigInt::as_string (char *p, unsigned l, onedig_t b) const if (l == 0) return 0; onedig_t r = digit_div (dig, len, b); - p[--l] = r < 10 ? r + '0' : 'A' + r - 10; + p[--l] = (char)(r < 10 ? r + '0' : 'A' + r - 10); if (dig[len-1] == 0) --len; } @@ -632,7 +633,7 @@ BigInt::dump (unsigned char *p, unsigned n) for (;;) { while (i--) - *p++ = d >> i * CHAR_BIT; + *p++ = (unsigned char)(d >> i * CHAR_BIT); if (t <= digit) break; d = *--t; @@ -683,8 +684,8 @@ BigInt::is_long() const return false; // There is exactly one good signed number n with abs (n) having the // topmost bit set: The most negative number. - for (int l = length - 1; --l >= 0; ) - if (digit[l] != 0) + for(unsigned l = length - 1; l > 0; --l) + if(digit[l - 1] != 0) return false; return true; } @@ -692,18 +693,18 @@ BigInt::is_long() const ullong_t BigInt::to_ulong() const { ullong_t ul = 0; - for (int i = length; --i >= 0; ) - { - ul <<= single_bits; - ul |= digit[i]; - } + for(unsigned i = length; i > 0; --i) + { + ul <<= single_bits; + ul |= digit[i - 1]; + } return ul; } llong_t BigInt::to_long() const { - ullong_t ul = to_ulong(); - return positive ? ul : -llong_t (ul); + llong_t l = llong_t(to_ulong()); + return positive ? l : -l; } @@ -753,7 +754,7 @@ BigInt::compare (llong_t b) const onedig_t dig[small]; unsigned len; - digit_set (-b, dig, len); + digit_set(ullong_t(-b), dig, len); if (length < len) return 1; if (length > len) @@ -905,7 +906,7 @@ BigInt & BigInt::operator+= (llong_t y) { bool pos = y > 0; - ullong_t uy = pos ? y : -y; + ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; unsigned yl; digit_set (uy, yb, yl); @@ -917,7 +918,7 @@ BigInt & BigInt::operator-= (llong_t y) { bool pos = y > 0; - ullong_t uy = pos ? y : -y; + ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; unsigned yl; digit_set (uy, yb, yl); @@ -929,7 +930,7 @@ BigInt & BigInt::operator*= (llong_t y) { bool pos = y > 0; - ullong_t uy = pos ? y : -y; + ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; unsigned yl; digit_set (uy, yb, yl); @@ -941,7 +942,7 @@ BigInt & BigInt::operator/= (llong_t y) { bool pos = y > 0; - ullong_t uy = pos ? y : -y; + ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; unsigned yl; digit_set (uy, yb, yl); @@ -952,7 +953,7 @@ BigInt & BigInt::operator%= (llong_t y) { bool pos = y > 0; - ullong_t uy = pos ? y : -y; + ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; unsigned yl; digit_set (uy, yb, yl); @@ -1078,7 +1079,7 @@ BigInt::div (BigInt const &x, BigInt const &y, BigInt &q, BigInt &r) // This digit_div() transforms the dividend into the quotient. q = y; r.digit[0] = digit_div (q.digit, q.length, y.digit[0]); - r.length = r.digit[0] ? 1 : 0; + r.length = r.digit[0] ? 1u : 0u; } else { @@ -1269,22 +1270,25 @@ BigInt::operator%= (BigInt const &y) unsigned BigInt::floorPow2 () const { - int i = length - 1; // Start on the last value - while (i >= 0 && digit[i] == 0) { + unsigned i = length; // Start on the last value + while(i > 0 && digit[i - 1] == 0) + { --i; // Skip zeros } - if (i < 0) { + if(i == 0) + { return 0; // Special case } twodig_t power = 1; - int count = 0; + unsigned count = 0; - while ((power << 1) <= (twodig_t)digit[i]) { + while((power << 1) <= (twodig_t)digit[i - 1]) + { ++count, power <<= 1; } - return (single_bits * i) + count; + return (single_bits * (i - 1)) + count; } // Not part of original BigInt. diff --git a/unit/big-int/big-int.cpp b/unit/big-int/big-int.cpp index 678318bf238..ab678018a71 100644 --- a/unit/big-int/big-int.cpp +++ b/unit/big-int/big-int.cpp @@ -245,12 +245,12 @@ TEST_CASE("arbitrary precision integers", "[core][big-int][bigint]") REQUIRE(N == M); - REQUIRE(N.floorPow2() == 0); + REQUIRE(N.floorPow2() == 0U); N -= 1; // 0 - REQUIRE(N.floorPow2() == 0); + REQUIRE(N.floorPow2() == 0U); N += 2; // 2 - REQUIRE(N.floorPow2() == 1); + REQUIRE(N.floorPow2() == 1U); } } From 297e4c000f49bce1fe0bed3666434b4baba582ea Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Fri, 7 Feb 2025 13:34:39 +0000 Subject: [PATCH 2/3] Use std::size_t for all size-typed operations in big-int --- src/big-int/bigint-func.cc | 4 +- src/big-int/bigint.cc | 242 ++++++++++++++++++------------------- src/big-int/bigint.hh | 30 ++--- src/util/mp_arith.cpp | 2 +- 4 files changed, 132 insertions(+), 146 deletions(-) diff --git a/src/big-int/bigint-func.cc b/src/big-int/bigint-func.cc index 6ee5210a5c6..8eb293960fc 100644 --- a/src/big-int/bigint-func.cc +++ b/src/big-int/bigint-func.cc @@ -7,9 +7,7 @@ #include "bigint.hh" - -BigInt -pow (BigInt const &x, unsigned y) +BigInt pow(BigInt const &x, std::size_t y) { BigInt a = x; BigInt r = 1; diff --git a/src/big-int/bigint.cc b/src/big-int/bigint.cc index 33e555bf2d1..158e51a540c 100644 --- a/src/big-int/bigint.cc +++ b/src/big-int/bigint.cc @@ -25,13 +25,11 @@ typedef BigInt::onedig_t onedig_t; typedef BigInt::twodig_t twodig_t; static const unsigned small = BigInt::small; -static const int single_bits = sizeof (onedig_t) * CHAR_BIT; +static const std::size_t single_bits = sizeof(onedig_t) * CHAR_BIT; static const twodig_t base = twodig_t (1) << single_bits; static const twodig_t single_max = base - 1; - -inline unsigned -adjust_size (unsigned size) +inline std::size_t adjust_size(std::size_t size) { // Always allocate at least something greater than an ullong_t. if (size <= small) @@ -40,8 +38,8 @@ adjust_size (unsigned size) // Assuming the heap works with a specific granularity G and needs // space B for a few bookkeeping pointers: Prefer allocation sizes // of N * G - B. (Just guesses. May tune that later.) - const unsigned G = 32; - const unsigned B = 8; + const std::size_t G = 32; + const std::size_t B = 8; size *= sizeof (onedig_t); size += B; size += G - 1; @@ -55,10 +53,9 @@ adjust_size (unsigned size) // Compare unsigned digit strings, returns -1/0/+1. -inline int -digit_cmp (onedig_t const *a, onedig_t const *b, unsigned n) +inline int digit_cmp(onedig_t const *a, onedig_t const *b, std::size_t n) { - for(unsigned i = n; i > 0; --i) + for(std::size_t i = n; i > 0; --i) { if(a[i - 1] < b[i - 1]) return -1; @@ -70,13 +67,15 @@ digit_cmp (onedig_t const *a, onedig_t const *b, unsigned n) // Add unsigned digit strings, return carry. Assumes l1 >= l2! -static _fast onedig_t -digit_add (onedig_t const *d1, unsigned l1, - onedig_t const *d2, unsigned l2, - onedig_t *r) // May be same as d1 or d2. +static _fast onedig_t digit_add( + onedig_t const *d1, + std::size_t l1, + onedig_t const *d2, + std::size_t l2, + onedig_t *r) // May be same as d1 or d2. { twodig_t c = 0; - unsigned i = 0; + std::size_t i = 0; while (i < l2) { c += twodig_t (d1[i]) + twodig_t (d2[i]); @@ -101,13 +100,15 @@ digit_add (onedig_t const *d1, unsigned l1, // Subtract unsigned digit strings, return carry. Assumes l1 >= l2! -static _fast void -digit_sub (onedig_t const *d1, unsigned l1, - onedig_t const *d2, unsigned l2, - onedig_t *r) // May be same as d1 or d2. +static _fast void digit_sub( + onedig_t const *d1, + std::size_t l1, + onedig_t const *d2, + std::size_t l2, + onedig_t *r) // May be same as d1 or d2. { twodig_t c = 1; - unsigned i = 0; + std::size_t i = 0; while (i < l2) { c += twodig_t (d1[i]) + single_max - twodig_t (d2[i]); @@ -132,11 +133,10 @@ digit_sub (onedig_t const *d1, unsigned l1, // Multiply unsigned digit string by single digit, replaces argument // with product and returns overflowing digit. -static _fast onedig_t -digit_mul (onedig_t *b, unsigned l, onedig_t d) +static _fast onedig_t digit_mul(onedig_t *b, std::size_t l, onedig_t d) { twodig_t p = 0; - for(unsigned i = l; i > 0; --i) + for(std::size_t i = l; i > 0; --i) { p += twodig_t(d) * twodig_t(*b); *b++ = onedig_t(p); @@ -149,35 +149,36 @@ digit_mul (onedig_t *b, unsigned l, onedig_t d) // Multiply two digit strings. Writes result into a third digit string // which must have the appropriate size and must not be same as one of // the arguments. -static _fast void -digit_mul (onedig_t const *a, unsigned la, - onedig_t const *b, unsigned lb, - onedig_t *r) // Must not be same as a or b. +static _fast void digit_mul( + onedig_t const *a, + std::size_t la, + onedig_t const *b, + std::size_t lb, + onedig_t *r) // Must not be same as a or b. { memset (r, 0, (la + lb) * sizeof (onedig_t)); - for (unsigned i = 0; i < la; i++) + for(std::size_t i = 0; i < la; i++) + { + onedig_t d = a[i]; + twodig_t p = 0; + for(std::size_t j = 0; j < lb; j++) { - onedig_t d = a[i]; - twodig_t p = 0; - for (unsigned j = 0; j < lb; j++) - { - p += r[j] + twodig_t (d) * b[j]; - r[j] = onedig_t (p); - p >>= single_bits; - } - r[lb] = onedig_t (p); - ++r; + p += r[j] + twodig_t(d) * b[j]; + r[j] = onedig_t(p); + p >>= single_bits; } + r[lb] = onedig_t(p); + ++r; + } } // Divide unsigned digit string by single digit, replaces argument // with quotient and returns remainder. -static _fast onedig_t -digit_div (onedig_t *b, unsigned l, onedig_t d) +static _fast onedig_t digit_div(onedig_t *b, std::size_t l, onedig_t d) { twodig_t r = 0; - for(unsigned i = l; i > 0; --i) + for(std::size_t i = l; i > 0; --i) { r <<= single_bits; r |= b[i - 1]; @@ -216,20 +217,20 @@ guess_q (onedig_t const *r, onedig_t const *y) // Multiply divisor with quotient digit and subtract from dividend. // Returns overflow. static _fast onedig_t -multiply_and_subtract (onedig_t *r, onedig_t const *y, unsigned l, onedig_t q) +multiply_and_subtract(onedig_t *r, onedig_t const *y, std::size_t l, onedig_t q) { twodig_t p = 0; twodig_t h = 1; - for (unsigned i = 0; i < l; i++) - { - p += twodig_t (q) * y[i]; - h += r[i]; - h += single_max; - h -= onedig_t (p); - r[i] = onedig_t (h); - p >>= single_bits; - h >>= single_bits; - } + for(std::size_t i = 0; i < l; i++) + { + p += twodig_t(q) * y[i]; + h += r[i]; + h += single_max; + h -= onedig_t(p); + r[i] = onedig_t(h); + p >>= single_bits; + h >>= single_bits; + } h += r[l]; h += single_max; h -= p; @@ -239,28 +240,31 @@ multiply_and_subtract (onedig_t *r, onedig_t const *y, unsigned l, onedig_t q) // Add back divisor digits to dividend, corresponds to a correction of // the guessed quotient digit by -1. -static _fast void -add_back (onedig_t *r, onedig_t const *y, unsigned l) +static _fast void add_back(onedig_t *r, onedig_t const *y, std::size_t l) { twodig_t h = 0; - for (unsigned i = 0; i < l; i++) - { - h += r[i]; - h += y[i]; - r[i] = onedig_t (h); - h >>= single_bits; - } + for(std::size_t i = 0; i < l; i++) + { + h += r[i]; + h += y[i]; + r[i] = onedig_t(h); + h >>= single_bits; + } r[l] = 0; } // Divide two digit strings. Divides r by y/yl. Stores quotient in // q/ql and leaves the remainder in r. Size of r is yl+ql. -static _fast void -digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql) +static _fast void digit_div( + onedig_t *r, + const onedig_t *y, + std::size_t yl, + onedig_t *q, + std::size_t ql) { r += ql; --r; - for(unsigned i = ql; i > 0; --r, --i) + for(std::size_t i = ql; i > 0; --r, --i) { onedig_t qh = guess_q(r + yl, y + yl - 1); if(multiply_and_subtract(r, y, yl, qh) == 0) @@ -276,8 +280,7 @@ digit_div (onedig_t *r, const onedig_t *y, unsigned yl, onedig_t *q, unsigned ql // Newly allocate uninitialized space for specified number of digits. -inline void -BigInt::allocate (unsigned digits) +inline void BigInt::allocate(std::size_t digits) { size = adjust_size (digits); length = 0; @@ -288,8 +291,7 @@ BigInt::allocate (unsigned digits) // Used in assignment: When smaller than specified digits, allocate // anew. Don`t bother to keep the contents. -inline void -BigInt::reallocate (unsigned digits) +inline void BigInt::reallocate(std::size_t digits) { if (digits > size) { @@ -303,13 +305,12 @@ BigInt::reallocate (unsigned digits) // Increase size keeping the contents. -inline void -BigInt::resize (unsigned digits) +inline void BigInt::resize(std::size_t digits) { if (digits > size) { onedig_t *old_digit = digit; - unsigned old_size = size; + std::size_t old_size = size; size = adjust_size (digits); digit = new onedig_t[size]; if (old_digit) @@ -342,8 +343,7 @@ BigInt::adjust() // Store unsigned elementary integer type into string of onedig_t. -inline void -digit_set (ullong_t ul, onedig_t d[small], unsigned &l) +inline void digit_set(ullong_t ul, onedig_t d[small], std::size_t &l) { l = 0; if (ul) @@ -395,11 +395,8 @@ BigInt::~BigInt() } } -BigInt::BigInt (onedig_t *dig, unsigned len, bool pos) - : size (0), - length (len), - digit (dig), - positive (pos) +BigInt::BigInt(onedig_t *dig, std::size_t len, bool pos) + : size(0), length(len), digit(dig), positive(pos) {} BigInt::BigInt() @@ -551,9 +548,7 @@ BigInt::scan (char const *s, onedig_t b) return scan_on (s, b); } - -unsigned -BigInt::digits (onedig_t b) const +std::size_t BigInt::digits(onedig_t b) const { int bits = -1; while (b) @@ -561,15 +556,13 @@ BigInt::digits (onedig_t b) const return (single_bits * length + bits - 1) / bits; } - -char * -BigInt::as_string (char *p, unsigned l, onedig_t b) const +char *BigInt::as_string(char *p, std::size_t l, onedig_t b) const { if (l < 2) return 0; // Not enough room for number. p[--l] = '\0'; // Check for zero. Would otherwise print as empty string. - unsigned len = length; + std::size_t len = length; while (len && digit[len-1] == 0) --len; if (len == 0) @@ -602,8 +595,7 @@ BigInt::as_string (char *p, unsigned l, onedig_t b) const return p + l; } -bool -BigInt::dump (unsigned char *p, unsigned n) +bool BigInt::dump(unsigned char *p, std::size_t n) { // Access most significant digit. onedig_t *t = digit + length; @@ -617,9 +609,9 @@ BigInt::dump (unsigned char *p, unsigned n) } // Determine number m of characters needed. onedig_t d = *--t; - unsigned i = sizeof (onedig_t); + std::size_t i = sizeof(onedig_t); while (--i && (d >> i * CHAR_BIT) == 0); - unsigned m = ++i + (t - digit) * sizeof (onedig_t); + const std::size_t m = ++i + (t - digit) * sizeof(onedig_t); // Fill in leading zeroes. if (m > n) { @@ -642,8 +634,7 @@ BigInt::dump (unsigned char *p, unsigned n) return true; } -void -BigInt::load (unsigned char const *p, unsigned n) +void BigInt::load(unsigned char const *p, std::size_t n) { // Skip leading zeroes. while (n > 0 && *p == 0) @@ -654,7 +645,7 @@ BigInt::load (unsigned char const *p, unsigned n) length = 0; unsigned char const *q = p + n; onedig_t d = 0; - unsigned i = 0; + std::size_t i = 0; for (;;) { if (q <= p) @@ -684,7 +675,7 @@ BigInt::is_long() const return false; // There is exactly one good signed number n with abs (n) having the // topmost bit set: The most negative number. - for(unsigned l = length - 1; l > 0; --l) + for(std::size_t l = length - 1; l > 0; --l) if(digit[l - 1] != 0) return false; return true; @@ -693,7 +684,7 @@ BigInt::is_long() const ullong_t BigInt::to_ulong() const { ullong_t ul = 0; - for(unsigned i = length; i > 0; --i) + for(std::size_t i = length; i > 0; --i) { ul <<= single_bits; ul |= digit[i - 1]; @@ -729,7 +720,7 @@ BigInt::compare (ullong_t b) const if (!positive) return -1; onedig_t dig[small]; - unsigned len; + std::size_t len; digit_set (b, dig, len); if (length < len) return -1; @@ -753,7 +744,7 @@ BigInt::compare (llong_t b) const return 1; onedig_t dig[small]; - unsigned len; + std::size_t len; digit_set(ullong_t(-b), dig, len); if (length < len) return 1; @@ -777,8 +768,7 @@ BigInt::compare (BigInt const &b) const // Auxiliary method for all adding and subtracting. -void -BigInt::add (onedig_t const *dig, unsigned len, bool pos) +void BigInt::add(onedig_t const *dig, std::size_t len, bool pos) { // Make sure the result fits into this, even with carry. resize ((length > len ? length : len) + 1); @@ -787,7 +777,7 @@ BigInt::add (onedig_t const *dig, unsigned len, bool pos) // expect the greater operand first. onedig_t const *d1; onedig_t const *d2; - unsigned l1, l2; + std::size_t l1, l2; bool gt = (length > len || (length == len && digit_cmp (digit, dig, len) >= 0)); if (gt) @@ -830,8 +820,7 @@ BigInt::add (onedig_t const *dig, unsigned len, bool pos) // Auxiliary method for multiplication. -void -BigInt::mul (onedig_t const *dig, unsigned len, bool pos) +void BigInt::mul(onedig_t const *dig, std::size_t len, bool pos) { if (len < 2) { @@ -875,7 +864,7 @@ BigInt::mul (onedig_t const *dig, unsigned len, bool pos) else { // Get a new string of digits for the result. - unsigned old_size = size; + std::size_t old_size = size; size = adjust_size (length + len); onedig_t *r = new onedig_t[size]; @@ -908,7 +897,7 @@ BigInt::operator+= (llong_t y) bool pos = y > 0; ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); add (yb, yl, pos); return *this; @@ -920,7 +909,7 @@ BigInt::operator-= (llong_t y) bool pos = y > 0; ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); add (yb, yl, !pos); return *this; @@ -932,7 +921,7 @@ BigInt::operator*= (llong_t y) bool pos = y > 0; ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); mul (yb, yl, pos); return *this; @@ -944,7 +933,7 @@ BigInt::operator/= (llong_t y) bool pos = y > 0; ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); return *this /= BigInt (yb, yl, pos); } @@ -955,7 +944,7 @@ BigInt::operator%= (llong_t y) bool pos = y > 0; ullong_t uy = pos ? ullong_t(y) : ullong_t(-y); onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); return *this %= BigInt (yb, yl, pos); } @@ -965,7 +954,7 @@ BigInt & BigInt::operator+= (ullong_t uy) { onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); add (yb, yl, true); return *this; @@ -975,7 +964,7 @@ BigInt & BigInt::operator-= (ullong_t uy) { onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); add (yb, yl, false); return *this; @@ -985,7 +974,7 @@ BigInt & BigInt::operator*= (ullong_t uy) { onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); mul (yb, yl, true); return *this; @@ -995,7 +984,7 @@ BigInt & BigInt::operator/= (ullong_t uy) { onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); return *this /= BigInt (yb, yl, true); } @@ -1004,7 +993,7 @@ BigInt & BigInt::operator%= (ullong_t uy) { onedig_t yb[small]; - unsigned yl; + std::size_t yl; digit_set (uy, yb, yl); return *this %= BigInt (yb, yl, true); } @@ -1086,11 +1075,11 @@ BigInt::div (BigInt const &x, BigInt const &y, BigInt &q, BigInt &r) // Copy operands and scale them such that the first divisor // digit is initially no less than half base. This is essential // for guess_q() to work. - unsigned al = x.length; + std::size_t al = x.length; onedig_t *a = (onedig_t *)alloca ((al + 2) * sizeof (onedig_t)); memcpy (a, x.digit, al * sizeof (onedig_t)); - unsigned bl = y.length; + std::size_t bl = y.length; onedig_t *b = (onedig_t *)alloca (bl * sizeof (onedig_t)); memcpy (b, y.digit, bl * sizeof (onedig_t)); @@ -1169,11 +1158,11 @@ BigInt::operator/= (BigInt const &y) else { // Copy and scale as above in div(). - unsigned al = length; + std::size_t al = length; onedig_t *a = (onedig_t *)alloca ((al + 2) * sizeof (onedig_t)); memcpy (a, digit, al * sizeof (onedig_t)); - unsigned bl = y.length; + std::size_t bl = y.length; onedig_t *b = (onedig_t *)alloca (bl * sizeof (onedig_t)); memcpy (b, y.digit, bl * sizeof (onedig_t)); @@ -1239,10 +1228,10 @@ BigInt::operator%= (BigInt const &y) // Scale as above. But do not copy dividend. It is transformed // into the remainder which is the result we want here. resize (length + 2); - unsigned &al = length; + std::size_t &al = length; onedig_t *a = digit; - unsigned bl = y.length; + std::size_t bl = y.length; onedig_t *b = (onedig_t *)alloca (bl * sizeof (onedig_t)); memcpy (b, y.digit, bl * sizeof (onedig_t)); @@ -1267,10 +1256,9 @@ BigInt::operator%= (BigInt const &y) } // Not part of original BigInt. -unsigned -BigInt::floorPow2 () const +std::size_t BigInt::floorPow2() const { - unsigned i = length; // Start on the last value + std::size_t i = length; // Start on the last value while(i > 0 && digit[i - 1] == 0) { --i; // Skip zeros @@ -1281,7 +1269,7 @@ BigInt::floorPow2 () const } twodig_t power = 1; - unsigned count = 0; + std::size_t count = 0; while((power << 1) <= (twodig_t)digit[i - 1]) { @@ -1292,17 +1280,17 @@ BigInt::floorPow2 () const } // Not part of original BigInt. -void -BigInt::setPower2 (unsigned exponent) { - unsigned digitOffset = exponent / single_bits; - unsigned bitOffset = exponent % single_bits; - unsigned digitsNeeded = 1 + digitOffset; +void BigInt::setPower2(std::size_t exponent) +{ + std::size_t digitOffset = exponent / single_bits; + std::size_t bitOffset = exponent % single_bits; + std::size_t digitsNeeded = 1 + digitOffset; reallocate(digitsNeeded); this->length = digitsNeeded; this->positive = true; - unsigned i; + std::size_t i; for (i = 0; i < digitOffset; ++i) { digit[i] = 0; } diff --git a/src/big-int/bigint.hh b/src/big-int/bigint.hh index 4abaca6abaa..d20808a07a7 100644 --- a/src/big-int/bigint.hh +++ b/src/big-int/bigint.hh @@ -129,15 +129,15 @@ public: enum { small = sizeof (ullong_t) / sizeof (onedig_t) }; private: - unsigned size; // Length of digit vector. - unsigned length; // Used places in digit vector. + std::size_t size; // Length of digit vector. + std::size_t length; // Used places in digit vector. onedig_t *digit; // Least significant first. bool positive; // Signed magnitude representation. // Create or resize this. - inline void allocate (unsigned digits); - inline void reallocate (unsigned digits); - inline void resize (unsigned digits); + inline void allocate(std::size_t digits); + inline void reallocate(std::size_t digits); + inline void resize(std::size_t digits); // Adjust length (e.g. after subtraction). inline void adjust(); @@ -148,12 +148,12 @@ private: // Aux methods, only for internal use. inline int ucompare (BigInt const &) const; - void add (onedig_t const *, unsigned, bool) _fast; - void mul (onedig_t const *, unsigned, bool) _fast; + void add(onedig_t const *, std::size_t, bool) _fast; + void mul(onedig_t const *, std::size_t, bool) _fast; // Auxiliary constructor used for temporary or static BigInt. // Sets size=0 which indicates that ~BigInt must not delete[]. - inline BigInt (onedig_t *, unsigned, bool) _fast; + inline BigInt(onedig_t *, std::size_t, bool) _fast; public: ~BigInt() _fast; @@ -183,19 +183,19 @@ public: // Return an upper bound for the number of digits the textual // representation of this might have. - unsigned digits (onedig_t = 10) const _fast; + std::size_t digits(onedig_t = 10) const _fast; // Convert into string, right adjusted in field of specified width. // Returns pointer to start of number or NULL if field too small. - char *as_string (char *, unsigned, onedig_t = 10) const _fasta; + char *as_string(char *, std::size_t, onedig_t = 10) const _fasta; // Convert to/from a binary representation. // Write and read in a compact byte-wise binary form. Effectively // print in base 256 with the most significant digit first. Also // read back from such a representation. Return success. - bool dump (unsigned char *, unsigned) _fast; - void load (unsigned char const *, unsigned) _fast; + bool dump(unsigned char *, std::size_t) _fast; + void load(unsigned char const *, std::size_t) _fast; // Conversions to elementary types. @@ -275,11 +275,11 @@ public: // Returns the largest x such that 2^x <= abs() or 0 if input is 0 // Not part of original BigInt. - unsigned floorPow2 () const _fast; + std::size_t floorPow2() const _fast; // Sets the number to the power of two given by the exponent // Not part of original BigInt. - void setPower2 (unsigned exponent) _fast; + void setPower2(std::size_t exponent) _fast; void swap (BigInt &other) { @@ -293,7 +293,7 @@ public: // Functions on BigInt. Implementations in bigint-func.cc. -BigInt pow (BigInt const &, unsigned) _fast; +BigInt pow(BigInt const &, std::size_t) _fast; BigInt pow (BigInt const &, BigInt const &, BigInt const &modulus) _fast; BigInt sqrt (BigInt const &) _fast; BigInt gcd (const BigInt &, const BigInt &) _fast; diff --git a/src/util/mp_arith.cpp b/src/util/mp_arith.cpp index fd0d3c43939..23135e62607 100644 --- a/src/util/mp_arith.cpp +++ b/src/util/mp_arith.cpp @@ -102,7 +102,7 @@ const std::string integer2binary(const mp_integer &n, std::size_t width) const std::string integer2string(const mp_integer &n, unsigned base) { - unsigned len = n.digits(base) + 2; + std::size_t len = n.digits(base) + 2; std::vector buffer(len); char *s = n.as_string(buffer.data(), len, base); From c7a1826efcdb03ed3ffc691f629b52dbc6daabf5 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Sun, 11 Nov 2018 21:55:51 +0000 Subject: [PATCH 3/3] Use static_cast instead of C-style cast --- src/big-int/bigint.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/big-int/bigint.cc b/src/big-int/bigint.cc index 158e51a540c..07cbb7f7135 100644 --- a/src/big-int/bigint.cc +++ b/src/big-int/bigint.cc @@ -500,7 +500,7 @@ BigInt::scan_on (char const *s, onedig_t b) for (char c = *s; c; c = *++s) { // Convert digit. Use 0..9A..Z for singles up to 36. Ignoring case. - c = (char)toupper(c); + c = static_cast(toupper(c)); onedig_t dig; if (c < '0') return s; @@ -579,7 +579,7 @@ char *BigInt::as_string(char *p, std::size_t l, onedig_t b) const if (l == 0) return 0; onedig_t r = digit_div (dig, len, b); - p[--l] = (char)(r < 10 ? r + '0' : 'A' + r - 10); + p[--l] = static_cast(r < 10 ? r + '0' : 'A' + r - 10); if (dig[len-1] == 0) --len; }