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 m_digits.push_back(new_msd);
95 Warn("Arbint grows digit (%.16lx), this->m_digits.size() = %u, total grown = %u", new_msd, m_digits.size(), ++total_grows);
96 if (total_grows++ > 10000)
98 Fatal("Too many GrowDigit calls!");
102 Arbint & Arbint::operator*=(const Arbint & mul)
104 vector<digit_t> new_digits(m_digits.size(), 0L);
105 new_digits.reserve(new_digits.size()+mul.m_digits.size());
106 for (unsigned i = 0; i < mul.m_digits.size(); ++i)
108 vector<digit_t> step(m_digits.size()+i, 0L);
109 memcpy(step.data()+i, m_digits.data(), sizeof(digit_t)*m_digits.size());
111 digit_t overflow = mul_digits((digit_t*)step.data()+i, mul.m_digits[i], m_digits.size());
114 step.push_back(overflow);
116 new_digits.resize(max(new_digits.size(), step.size()), 0L);
117 digit_t carry = add_digits((digit_t*)new_digits.data(), step.data(), step.size());
125 m_digits.swap(new_digits);
126 m_sign = !(m_sign == mul.m_sign);
131 void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) const
140 /* may break things (even more that is)
141 else if (div.m_digits.size() == 1)
143 result.m_digits.resize(m_digits.size(), 0L);
144 remainder = Arbint(div_digits((digit_t*)&m_digits[0], div.m_digits[0], m_digits.size(), result.m_digits.data()));
145 result.m_sign = !(m_sign == div.m_sign);
149 for (int i = 8*sizeof(digit_t)*m_digits.size(); i >= 0; --i)
155 remainder.BitClear(0);
156 if (remainder >= div)
162 result.m_sign = !(m_sign == div.m_sign);
166 Arbint & Arbint::operator+=(const Arbint & add)
168 if (m_sign == add.m_sign)
170 // -a + -b == -(a + b)
171 return AddBasic(add);
176 // -a + b == -(a - b)
190 Arbint & Arbint::operator-=(const Arbint & sub)
192 if (m_sign == sub.m_sign)
193 return SubBasic(sub);
194 return AddBasic(sub);
197 Arbint & Arbint::AddBasic(const Arbint & add)
199 while (m_digits.size() < add.m_digits.size())
202 Debug("Size is %u, add's size is %u", m_digits.size(), add.m_digits.size());
205 //m_digits.resize(add.m_digits.size()+1,0L);
207 digit_t carry = add_digits((digit_t*)m_digits.data(),
208 (digit_t*)add.m_digits.data(), add.m_digits.size());
211 Debug("Grow carry %lu", carry);
218 Arbint & Arbint::SubBasic(const Arbint & sub)
221 while (sub.m_digits.size() > m_digits.size())
228 Debug("START sub was %c%s, I am %c%s", SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
231 digit_t borrow = sub_digits((digit_t*)m_digits.data(),
232 (digit_t*)sub.m_digits.data(), sub.m_digits.size());
236 Debug("SUB_DIGITS -> sub was %c%s, I am %c%s", SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
239 //TODO: Write ASM to do this bit?
243 for (unsigned i = 0; i < m_digits.size(); ++i)
244 m_digits[i] = (~m_digits[i]);
245 vector<digit_t> one_digits(m_digits.size(), 0L);
247 add_digits((digit_t*)m_digits.data(), (digit_t*)one_digits.data(), m_digits.size());
251 Debug("END -> sub was %c%s, I am %c%s", sub.SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
256 Debug("SHRUNK -> sub was %c%s, I am %c%s", sub.SignChar(), sub.DigitStr().c_str(), SignChar(), DigitStr().c_str());
262 string Arbint::Str(const string & base) const
267 reverse(s.begin(), s.end());
271 bool Arbint::IsZero() const
273 for (unsigned i = m_digits.size()-1; i > 0; --i)
275 if (m_digits[i] != 0L) return false;
277 return (m_digits[0] == 0L);
280 bool Arbint::operator==(const Arbint & equ) const
282 if (m_sign != equ.m_sign)
284 unsigned min_size = m_digits.size();
285 const Arbint * larger = &equ;
286 if (m_digits.size() > equ.m_digits.size())
288 min_size = equ.m_digits.size();
292 if (memcmp(m_digits.data(), equ.m_digits.data(), sizeof(digit_t)*min_size) != 0)
295 for (unsigned i = min_size; i < larger->m_digits.size(); ++i)
297 if (larger->m_digits[i] != 0L)
303 bool Arbint::operator<(const Arbint & less) const
307 return (cpy.m_sign && !cpy.IsZero());
310 string Arbint::DigitStr() const
313 //ss << std::hex << std::setfill('0');
314 for (unsigned i = 0; i < m_digits.size(); ++i)
316 if (i != 0) ss << ',';
317 //ss << std::setw(2*sizeof(digit_t)) << static_cast<digit_t>(m_digits[i]);
318 ss << static_cast<digit_t>(m_digits[i]);
323 Arbint & Arbint::operator>>=(unsigned amount)
325 // Shift by whole number of digits
326 unsigned whole = amount/(8*sizeof(digit_t));
327 unsigned old_size = m_digits.size();
329 if (whole >= old_size)
331 m_digits.resize(1,0L);
335 memmove(m_digits.data(), m_digits.data()+whole, sizeof(digit_t)*(old_size-whole));
336 m_digits.resize(old_size-whole, 0L);
338 // Shift by partial amount
339 amount = amount %(8*sizeof(digit_t));
343 digit_t underflow = 0L;
344 for (int i = (int)(m_digits.size()-1); i >= 0; --i)
346 unsigned shl = (8*sizeof(digit_t)-amount);
347 digit_t next_underflow = (m_digits[i] << shl);
348 //digit_t mask_upper = ~(0L >> amount);
349 m_digits[i] = (m_digits[i] >> amount);// & mask_upper;
350 m_digits[i] |= underflow;
351 underflow = next_underflow;
357 Arbint & Arbint::operator<<=(unsigned amount)
359 // Shift by whole number of digits
360 unsigned whole = amount/(8*sizeof(digit_t));
361 unsigned old_size = m_digits.size();
362 for (unsigned i = 0; i < whole; ++i)
365 GrowDigit(0L);//m_digits.resize(m_digits.size() + whole);
367 memmove(m_digits.data()+whole, m_digits.data(), sizeof(digit_t)*old_size);
368 memset(m_digits.data(), 0L, whole*sizeof(digit_t));
373 amount = amount % (8*sizeof(digit_t));
377 //Debug("Shift by %u from %u", amount, whole);
378 digit_t overflow = 0L;
379 for (unsigned i = whole; i < m_digits.size(); ++i)
381 //Debug("Digit is %.16lx", m_digits[i]);
382 unsigned shr = (8*sizeof(digit_t)-amount);
383 //Debug("shr is %u", shr);
384 digit_t next_overflow = (m_digits[i] >> shr);
385 //Debug("Next overflow %.16lx", next_overflow);
386 m_digits[i] <<= amount;
387 //Debug("Before overflow %.16lx", m_digits[i]);
388 m_digits[i] |= overflow;
389 overflow = next_overflow;
392 m_digits.push_back(overflow);
397 bool Arbint::GetBit(unsigned i) const
399 unsigned digit = i/(8*sizeof(digit_t));
400 if (digit >= m_digits.size())
403 i = i % (8*sizeof(digit_t));
405 return (m_digits[digit] & (1L << i));
409 void Arbint::BitClear(unsigned i)
411 unsigned digit = i/(8*sizeof(digit_t));
412 if (digit >= m_digits.size())
414 i = i % (8*sizeof(digit_t));
415 m_digits[digit] &= ~(1L << i);
418 void Arbint::BitSet(unsigned i)
420 unsigned digit = i/(8*sizeof(digit_t));
421 while (m_digits.size() < digit+1)
423 Debug("Grow BitSet Size %u, digit %u", m_digits.size(), digit);
427 i = i % (8*sizeof(digit_t));
428 m_digits[digit] |= (1L << i);