3 * @brief Arbitrary sized integer definitions
5 * @see add_digits_asm.s
6 * @see sub_digits_asm.s
7 * @see mul_digits_asm.s
27 /** Absolute value hackery **/
28 template <> Arbint Tabs(const Arbint & a)
35 Arbint::Arbint(int64_t i) : m_digits(1), m_sign(i < 0)
37 m_digits[0] = llabs(i);
40 Arbint::Arbint(unsigned n, digit_t d0, ...) : m_digits(n), m_sign(false)
46 for (unsigned i = 1; i < n; ++i)
48 m_digits[i] = va_arg(ap, digit_t);
53 Arbint::Arbint(const Arbint & cpy) : m_digits(cpy.m_digits), m_sign(cpy.m_sign)
58 Arbint::Arbint(const vector<digit_t> & digits) : m_digits(digits), m_sign(false)
63 Arbint & Arbint::operator=(const Arbint & cpy)
65 m_digits = cpy.m_digits;
72 m_digits.resize(1, 0L);
75 unsigned Arbint::Shrink()
78 if (m_digits.size() <= 1)
82 while (m_digits.size() > 1 && m_digits[m_digits.size()-1] == 0L)
91 void Arbint::GrowDigit(digit_t new_msd)
93 static unsigned total_grows = 0;
94 static unsigned biggest_arbint = 1;
95 m_digits.push_back(new_msd);
97 if (m_digits.size() > biggest_arbint)
99 biggest_arbint = m_digits.size();
100 Warn("New biggest Arbint of size %u", m_digits.size());
102 //Warn("Arbint grows digit (%.16lx), this->m_digits.size() = %u, total grown = %u", new_msd, m_digits.size(), ++total_grows);
103 //if (total_grows++ > 10000)
105 // Fatal("Too many GrowDigit calls!");
109 Arbint & Arbint::operator*=(const Arbint & mul)
111 vector<digit_t> new_digits(m_digits.size(), 0L);
112 new_digits.reserve(new_digits.size()+mul.m_digits.size());
113 for (unsigned i = 0; i < mul.m_digits.size(); ++i)
115 vector<digit_t> step(m_digits.size()+i, 0L);
116 memcpy(step.data()+i, m_digits.data(), sizeof(digit_t)*m_digits.size());
118 digit_t overflow = mul_digits((digit_t*)step.data()+i, mul.m_digits[i], m_digits.size());
121 step.push_back(overflow);
123 new_digits.resize(max(new_digits.size(), step.size()), 0L);
124 digit_t carry = add_digits((digit_t*)new_digits.data(), step.data(), step.size());
132 m_digits.swap(new_digits);
133 m_sign = !(m_sign == mul.m_sign);
138 void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) const
147 /* may break things (even more that is)
148 else if (div.m_digits.size() == 1)
150 result.m_digits.resize(m_digits.size(), 0L);
151 remainder = Arbint(div_digits((digit_t*)&m_digits[0], div.m_digits[0], m_digits.size(), result.m_digits.data()));
152 result.m_sign = !(m_sign == div.m_sign);
156 for (int i = 8*sizeof(digit_t)*m_digits.size(); i >= 0; --i)
162 remainder.BitClear(0);
163 if (remainder >= div)
169 result.m_sign = !(m_sign == div.m_sign);
173 Arbint & Arbint::operator+=(const Arbint & add)
175 if (m_sign == add.m_sign)
177 // -a + -b == -(a + b)
178 return AddBasic(add);
183 // -a + b == -(a - b)
197 Arbint & Arbint::operator-=(const Arbint & sub)
199 if (m_sign == sub.m_sign)
200 return SubBasic(sub);
201 return AddBasic(sub);
204 Arbint & Arbint::AddBasic(const Arbint & add)
206 while (m_digits.size() < add.m_digits.size())
209 Debug("Size is %u, add's size is %u", m_digits.size(), add.m_digits.size());
212 //m_digits.resize(add.m_digits.size()+1,0L);
214 digit_t carry = add_digits((digit_t*)m_digits.data(),
215 (digit_t*)add.m_digits.data(), add.m_digits.size());
218 Debug("Grow carry %lu", carry);
225 Arbint & Arbint::SubBasic(const Arbint & sub)
228 while (sub.m_digits.size() > m_digits.size())
235 Debug("START sub was %c%s, I am %c%s", SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
238 digit_t borrow = sub_digits((digit_t*)m_digits.data(),
239 (digit_t*)sub.m_digits.data(), sub.m_digits.size());
243 Debug("SUB_DIGITS -> sub was %c%s, I am %c%s, borrow is %lu", SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str(), borrow);
246 //TODO: Write ASM to do this bit?
249 if (sub.m_digits.size() < m_digits.size())
251 m_digits[m_digits.size()-1] -= borrow;
256 for (unsigned i = 0; i < m_digits.size(); ++i)
257 m_digits[i] = (~m_digits[i]);
258 vector<digit_t> one_digits(m_digits.size(), 0L);
260 add_digits((digit_t*)m_digits.data(), (digit_t*)one_digits.data(), m_digits.size());
265 Debug("END -> sub was %c%s, I am %c%s", sub.SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
270 Debug("SHRUNK -> sub was %c%s, I am %c%s", sub.SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
276 string Arbint::Str(const string & base) const
281 reverse(s.begin(), s.end());
285 bool Arbint::IsZero() const
287 for (unsigned i = m_digits.size()-1; i > 0; --i)
289 if (m_digits[i] != 0L) return false;
291 return (m_digits[0] == 0L);
294 bool Arbint::operator==(const Arbint & equ) const
296 if (m_sign != equ.m_sign)
298 unsigned min_size = m_digits.size();
299 const Arbint * larger = &equ;
300 if (m_digits.size() > equ.m_digits.size())
302 min_size = equ.m_digits.size();
306 if (memcmp(m_digits.data(), equ.m_digits.data(), sizeof(digit_t)*min_size) != 0)
309 for (unsigned i = min_size; i < larger->m_digits.size(); ++i)
311 if (larger->m_digits[i] != 0L)
317 bool Arbint::operator<(const Arbint & less) const
321 return (cpy.m_sign && !cpy.IsZero());
324 string Arbint::DigitStr() const
327 //ss << std::hex << std::setfill('0');
328 for (unsigned i = 0; i < m_digits.size(); ++i)
330 if (i != 0) ss << ',';
331 //ss << std::setw(2*sizeof(digit_t)) << static_cast<digit_t>(m_digits[i]);
332 ss << static_cast<digit_t>(m_digits[i]);
337 Arbint & Arbint::operator>>=(unsigned amount)
339 // Shift by whole number of digits
340 unsigned whole = amount/(8*sizeof(digit_t));
341 unsigned old_size = m_digits.size();
343 if (whole >= old_size)
345 m_digits.resize(1,0L);
349 memmove(m_digits.data(), m_digits.data()+whole, sizeof(digit_t)*(old_size-whole));
350 m_digits.resize(old_size-whole, 0L);
352 // Shift by partial amount
353 amount = amount %(8*sizeof(digit_t));
357 digit_t underflow = 0L;
358 for (int i = (int)(m_digits.size()-1); i >= 0; --i)
360 unsigned shl = (8*sizeof(digit_t)-amount);
361 digit_t next_underflow = (m_digits[i] << shl);
362 //digit_t mask_upper = ~(0L >> amount);
363 m_digits[i] = (m_digits[i] >> amount);// & mask_upper;
364 m_digits[i] |= underflow;
365 underflow = next_underflow;
371 Arbint & Arbint::operator<<=(unsigned amount)
373 // Shift by whole number of digits
374 unsigned whole = amount/(8*sizeof(digit_t));
375 unsigned old_size = m_digits.size();
376 for (unsigned i = 0; i < whole; ++i)
378 //Debug("i = %u, whole = %u", i, whole);
379 GrowDigit(0L);//m_digits.resize(m_digits.size() + whole);
381 memmove(m_digits.data()+whole, m_digits.data(), sizeof(digit_t)*old_size);
382 memset(m_digits.data(), 0L, whole*sizeof(digit_t));
387 amount = amount % (8*sizeof(digit_t));
391 //Debug("Shift by %u from %u", amount, whole);
392 digit_t overflow = 0L;
393 for (unsigned i = whole; i < m_digits.size(); ++i)
395 //Debug("Digit is %.16lx", m_digits[i]);
396 unsigned shr = (8*sizeof(digit_t)-amount);
397 //Debug("shr is %u", shr);
398 digit_t next_overflow = (m_digits[i] >> shr);
399 //Debug("Next overflow %.16lx", next_overflow);
400 m_digits[i] <<= amount;
401 //Debug("Before overflow %.16lx", m_digits[i]);
402 m_digits[i] |= overflow;
403 overflow = next_overflow;
406 m_digits.push_back(overflow);
411 bool Arbint::GetBit(unsigned i) const
413 unsigned digit = i/(8*sizeof(digit_t));
414 if (digit >= m_digits.size())
417 i = i % (8*sizeof(digit_t));
419 return (m_digits[digit] & (1L << i));
423 void Arbint::BitClear(unsigned i)
425 unsigned digit = i/(8*sizeof(digit_t));
426 if (digit >= m_digits.size())
428 i = i % (8*sizeof(digit_t));
429 m_digits[digit] &= ~(1L << i);
432 void Arbint::BitSet(unsigned i)
434 unsigned digit = i/(8*sizeof(digit_t));
435 while (m_digits.size() < digit+1)
437 //Debug("Grow BitSet Size %u, digit %u", m_digits.size(), digit);
441 i = i % (8*sizeof(digit_t));
442 m_digits[digit] |= (1L << i);