39 unsigned _add(uint32_t *
const m,
40 uint32_t
const *
const m1,
41 uint32_t
const *
const m2,
47 unsigned const size =
std::min(size1, size2);
48 for (; i < size; ++i) {
49 sum =
static_cast<uint64_t
>(m1[i]) + (sum >> 32) +
50 static_cast<uint64_t
>(m2[i]);
51 m[i] =
static_cast<uint32_t
>(sum);
53 for (; i < size1; ++i) {
54 sum =
static_cast<uint64_t
>(m1[i]) + (sum >> 32);
55 m[i] =
static_cast<uint32_t
>(sum);
57 for (; i < size2; ++i) {
58 sum =
static_cast<uint64_t
>(m2[i]) + (sum >> 32);
59 m[i] =
static_cast<uint32_t
>(sum);
61 uint32_t carry =
static_cast<uint32_t
>(sum >> 32);
72 unsigned _sub(uint32_t *
const m,
73 uint32_t
const *
const m1,
74 uint32_t
const *
const m2,
86 for (; i < size2; ++i) {
87 diff =
static_cast<int64_t
>(m1[i]) + (diff >> 32) -
88 static_cast<int64_t
>(m2[i]);
89 m[i] =
static_cast<uint32_t
>(diff);
92 for (; diff < 0; ++i) {
93 diff =
static_cast<int64_t
>(m1[i]) - 1;
94 m[i] =
static_cast<uint32_t
>(diff);
99 while (m1[i - 1] == 0) { --i; }
103 for (; i < size1; ++i) {
112 unsigned _mul(uint32_t *
const m,
113 uint32_t
const *
const m1,
114 uint32_t
const *
const m2,
115 unsigned const size1,
116 unsigned const size2)
125 unsigned const size = size1 + size2;
126 for (
unsigned i = size1; i < size; ++i) {
129 for (
unsigned i = size1; i > 0; --i) {
130 uint64_t digit = m1[i - 1];
131 uint64_t carry =
static_cast<uint64_t
>(m2[0]) * digit;
133 m[i - 1] =
static_cast<uint32_t
>(carry);
135 for (; j < size2; ++j) {
136 carry =
static_cast<uint64_t
>(m2[j]) * digit +
137 static_cast<uint64_t>(m[i + j - 1]) +
139 m[i + j - 1] =
static_cast<uint32_t
>(carry);
142 for (; carry != 0; ++j) {
143 carry +=
static_cast<uint64_t
>(m[i + j - 1]);
144 m[i + j - 1] =
static_cast<uint32_t
>(carry);
150 return m[size - 1] == 0 ? size - 1 : size;
170 _checkCapacity(
std::max(_size, b._size) + 1);
171 if (_sign == b._sign) {
173 _size = _add(_digits, _digits, b._digits, _size, b._size);
178 if (_size > b._size) {
179 _size = _sub(_digits, _digits, b._digits, _size, b._size);
180 }
else if (_size < b._size) {
181 _size = _sub(_digits, b._digits, _digits, b._size, _size);
188 for (; i > 0 && _digits[i - 1] == b._digits[i - 1]; --i) {}
191 }
else if (_digits[i - 1] > b._digits[i - 1]) {
192 _size = _sub(_digits, _digits, b._digits, i, i);
194 _size = _sub(_digits, b._digits, _digits, i, i);
215 if (_sign == 0 || n == 0) {
220 unsigned const z = (n >> 5);
221 unsigned const s = (n & 0x1f);
222 unsigned const size = _size +
z;
223 _checkCapacity(size + 1);
227 for (
unsigned i = _size; i != 0; --i) {
228 _digits[i + z - 1] = _digits[i - 1];
230 for (
unsigned i = z; i != 0; --i) {
235 uint32_t low, high = 0;
236 for (
unsigned i = _size; i != 0; --i, high = low) {
237 low = _digits[i - 1];
238 _digits[i +
z] = (high << s) | (low >> (32 -
s));
240 _digits[
z] = high <<
s;
241 for (
unsigned i = z; i != 0; --i) {
244 _size = (_digits[size] == 0) ? size: size + 1;
255 _checkCapacity(_size + b._size);
256 if (_size >= b._size) {
257 _size = _mul(_digits, _digits, b._digits, _size, b._size);
259 _size = _mul(_digits, b._digits, _digits, b._size, _size);
BigInteger & multiply(BigInteger const &b)
multiply multiplies this integer by b.
void negate()
negate multiplies this integer by -1.
BigInteger & multiplyPow2(unsigned n)
multiplyPow2 multiplies this integer by 2ⁿ.
BigInteger is an arbitrary precision signed integer class.
A base class for image defects.
void setToZero()
setToZero sets this integer to zero.
This file declares a class for arbitrary precision integers.
BigInteger & subtract(BigInteger const &b)
subtract subtracts b from this integer.
BigInteger & add(BigInteger const &b)
add adds b to this integer.