TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
internal::BigInteger Class Reference

#include <biginteger.h>

Public Types

typedef uint64_t Type
 

Public Member Functions

 BigInteger (const BigInteger &rhs)
 
 BigInteger (uint64_t u)
 
 BigInteger (const char *decimals, size_t length)
 
BigIntegeroperator= (uint64_t u)
 
BigIntegeroperator+= (uint64_t u)
 
BigIntegeroperator*= (uint64_t u)
 
BigIntegeroperator*= (uint32_t u)
 
BigIntegeroperator<<= (size_t shift)
 
bool operator== (const BigInteger &rhs) const
 
bool operator== (const Type rhs) const
 
BigIntegerMultiplyPow5 (unsigned exp)
 
bool Difference (const BigInteger &rhs, BigInteger *out) const
 
int Compare (const BigInteger &rhs) const
 
size_t GetCount () const
 
Type GetDigit (size_t index) const
 
bool IsZero () const
 

Private Member Functions

void AppendDecimal64 (const char *begin, const char *end)
 
void PushBack (Type digit)
 

Static Private Member Functions

static uint64_t ParseUint64 (const char *begin, const char *end)
 
static uint64_t MulAdd64 (uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
 

Private Attributes

Type digits_ [kCapacity]
 
size_t count_
 

Static Private Attributes

static const size_t kBitCount = 3328
 
static const size_t kCapacity = kBitCount / sizeof(Type)
 
static const size_t kTypeBit = sizeof(Type) * 8
 

Member Typedef Documentation

Constructor & Destructor Documentation

internal::BigInteger::BigInteger ( const BigInteger rhs)
inline
31  : count_(rhs.count_) {
32  std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
33  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
Type
Type of JSON value.
Definition: rapidjson.h:642
internal::BigInteger::BigInteger ( uint64_t  u)
inlineexplicit
35  : count_(1) {
36  digits_[0] = u;
37  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
internal::BigInteger::BigInteger ( const char *  decimals,
size_t  length 
)
inline
39  : count_(1) {
41  digits_[0] = 0;
42  size_t i = 0;
43  const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
44  while (length >= kMaxDigitPerIteration) {
45  AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
46  length -= kMaxDigitPerIteration;
47  i += kMaxDigitPerIteration;
48  }
49 
50  if (length > 0)
51  AppendDecimal64(decimals + i, decimals + i + length);
52  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
void AppendDecimal64(const char *begin, const char *end)
Definition: biginteger.h:214
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
float length(float v)
Definition: vectorMath.h:208

+ Here is the call graph for this function:

Member Function Documentation

void internal::BigInteger::AppendDecimal64 ( const char *  begin,
const char *  end 
)
inlineprivate
214  {
215  uint64_t u = ParseUint64(begin, end);
216  if (IsZero())
217  *this = u;
218  else {
219  unsigned exp = static_cast<unsigned>(end - begin);
220  (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
221  }
222  }
Quat exp(const Quat &q)
Definition: Quat.h:729
bool IsZero() const
Definition: biginteger.h:211
static uint64_t ParseUint64(const char *begin, const char *end)
Definition: biginteger.h:229
unsigned __int64 uint64_t
Definition: stdint.h:90
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:152

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int internal::BigInteger::Compare ( const BigInteger rhs) const
inline
198  {
199  if (count_ != rhs.count_)
200  return count_ < rhs.count_ ? -1 : 1;
201 
202  for (size_t i = count_; i-- > 0;)
203  if (digits_[i] != rhs.digits_[i])
204  return digits_[i] < rhs.digits_[i] ? -1 : 1;
205 
206  return 0;
207  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273

+ Here is the caller graph for this function:

bool internal::BigInteger::Difference ( const BigInteger rhs,
BigInteger out 
) const
inline
176  {
177  int cmp = Compare(rhs);
178  RAPIDJSON_ASSERT(cmp != 0);
179  const BigInteger *a, *b; // Makes a > b
180  bool ret;
181  if (cmp < 0) { a = &rhs; b = this; ret = true; }
182  else { a = this; b = &rhs; ret = false; }
183 
184  Type borrow = 0;
185  for (size_t i = 0; i < a->count_; i++) {
186  Type d = a->digits_[i] - borrow;
187  if (i < b->count_)
188  d -= b->digits_[i];
189  borrow = (d > a->digits_[i]) ? 1 : 0;
190  out->digits_[i] = d;
191  if (d != 0)
192  out->count_ = i + 1;
193  }
194 
195  return ret;
196  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:198
BigInteger(const BigInteger &rhs)
Definition: biginteger.h:31
size_t count_
Definition: biginteger.h:274
Type
Type of JSON value.
Definition: rapidjson.h:642

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t internal::BigInteger::GetCount ( ) const
inline
209 { return count_; }
size_t count_
Definition: biginteger.h:274
Type internal::BigInteger::GetDigit ( size_t  index) const
inline
210 { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
bool internal::BigInteger::IsZero ( ) const
inline
211 { return count_ == 1 && digits_[0] == 0; }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273

+ Here is the caller graph for this function:

static uint64_t internal::BigInteger::MulAdd64 ( uint64_t  a,
uint64_t  b,
uint64_t  k,
uint64_t outHigh 
)
inlinestaticprivate
239  {
240 #if defined(_MSC_VER) && defined(_M_AMD64)
241  uint64_t low = _umul128(a, b, outHigh) + k;
242  if (low < k)
243  (*outHigh)++;
244  return low;
245 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
246  __extension__ typedef unsigned __int128 uint128;
247  uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
248  p += k;
249  *outHigh = static_cast<uint64_t>(p >> 64);
250  return static_cast<uint64_t>(p);
251 #else
252  const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
253  uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
254  x1 += (x0 >> 32); // can't give carry
255  x1 += x2;
256  if (x1 < x2)
257  x3 += (static_cast<uint64_t>(1) << 32);
258  uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
259  uint64_t hi = x3 + (x1 >> 32);
260 
261  lo += k;
262  if (lo < k)
263  hi++;
264  *outHigh = hi;
265  return lo;
266 #endif
267  }
unsigned __int64 uint64_t
Definition: stdint.h:90

+ Here is the caller graph for this function:

BigInteger& internal::BigInteger::MultiplyPow5 ( unsigned  exp)
inline
152  {
153  static const uint32_t kPow5[12] = {
154  5,
155  5 * 5,
156  5 * 5 * 5,
157  5 * 5 * 5 * 5,
158  5 * 5 * 5 * 5 * 5,
159  5 * 5 * 5 * 5 * 5 * 5,
160  5 * 5 * 5 * 5 * 5 * 5 * 5,
161  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
162  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
163  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
164  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
165  5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
166  };
167  if (exp == 0) return *this;
168  for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
169  for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
170  if (exp > 0) *this *= kPow5[exp - 1];
171  return *this;
172  }
Quat exp(const Quat &q)
Definition: Quat.h:729
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
unsigned int uint32_t
Definition: stdint.h:80

+ Here is the caller graph for this function:

BigInteger& internal::BigInteger::operator*= ( uint64_t  u)
inline
77  {
78  if (u == 0) return *this = 0;
79  if (u == 1) return *this;
80  if (*this == 1) return *this = u;
81 
82  uint64_t k = 0;
83  for (size_t i = 0; i < count_; i++) {
84  uint64_t hi;
85  digits_[i] = MulAdd64(digits_[i], u, k, &hi);
86  k = hi;
87  }
88 
89  if (k > 0)
90  PushBack(k);
91 
92  return *this;
93  }
void PushBack(Type digit)
Definition: biginteger.h:224
static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t *outHigh)
Definition: biginteger.h:239
size_t count_
Definition: biginteger.h:274
unsigned __int64 uint64_t
Definition: stdint.h:90
Type digits_[kCapacity]
Definition: biginteger.h:273

+ Here is the call graph for this function:

BigInteger& internal::BigInteger::operator*= ( uint32_t  u)
inline
95  {
96  if (u == 0) return *this = 0;
97  if (u == 1) return *this;
98  if (*this == 1) return *this = u;
99 
100  uint64_t k = 0;
101  for (size_t i = 0; i < count_; i++) {
102  const uint64_t c = digits_[i] >> 32;
103  const uint64_t d = digits_[i] & 0xFFFFFFFF;
104  const uint64_t uc = u * c;
105  const uint64_t ud = u * d;
106  const uint64_t p0 = ud + k;
107  const uint64_t p1 = uc + (p0 >> 32);
108  digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
109  k = p1 >> 32;
110  }
111 
112  if (k > 0)
113  PushBack(k);
114 
115  return *this;
116  }
void PushBack(Type digit)
Definition: biginteger.h:224
size_t count_
Definition: biginteger.h:274
unsigned __int64 uint64_t
Definition: stdint.h:90
Type digits_[kCapacity]
Definition: biginteger.h:273

+ Here is the call graph for this function:

BigInteger& internal::BigInteger::operator+= ( uint64_t  u)
inline
60  {
61  Type backup = digits_[0];
62  digits_[0] += u;
63  for (size_t i = 0; i < count_ - 1; i++) {
64  if (digits_[i] >= backup)
65  return *this; // no carry
66  backup = digits_[i + 1];
67  digits_[i + 1] += 1;
68  }
69 
70  // Last carry
71  if (digits_[count_ - 1] < backup)
72  PushBack(1);
73 
74  return *this;
75  }
void PushBack(Type digit)
Definition: biginteger.h:224
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
Type
Type of JSON value.
Definition: rapidjson.h:642

+ Here is the call graph for this function:

BigInteger& internal::BigInteger::operator<<= ( size_t  shift)
inline
118  {
119  if (IsZero() || shift == 0) return *this;
120 
121  size_t offset = shift / kTypeBit;
122  size_t interShift = shift % kTypeBit;
123  RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
124 
125  if (interShift == 0) {
126  std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
127  count_ += offset;
128  }
129  else {
130  digits_[count_] = 0;
131  for (size_t i = count_; i > 0; i--)
132  digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
133  digits_[offset] = digits_[0] << interShift;
134  count_ += offset;
135  if (digits_[count_])
136  count_++;
137  }
138 
139  std::memset(digits_, 0, offset * sizeof(Type));
140 
141  return *this;
142  }
static const size_t kCapacity
Definition: biginteger.h:270
bool IsZero() const
Definition: biginteger.h:211
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
static const size_t kTypeBit
Definition: biginteger.h:271
Type
Type of JSON value.
Definition: rapidjson.h:642

+ Here is the call graph for this function:

BigInteger& internal::BigInteger::operator= ( uint64_t  u)
inline
54  {
55  digits_[0] = u;
56  count_ = 1;
57  return *this;
58  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
bool internal::BigInteger::operator== ( const BigInteger rhs) const
inline
144  {
145  return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
146  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
Type
Type of JSON value.
Definition: rapidjson.h:642
bool internal::BigInteger::operator== ( const Type  rhs) const
inline
148  {
149  return count_ == 1 && digits_[0] == rhs;
150  }
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273
static uint64_t internal::BigInteger::ParseUint64 ( const char *  begin,
const char *  end 
)
inlinestaticprivate
229  {
230  uint64_t r = 0;
231  for (const char* p = begin; p != end; ++p) {
232  RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
233  r = r * 10 + (*p - '0');
234  }
235  return r;
236  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
unsigned __int64 uint64_t
Definition: stdint.h:90

+ Here is the caller graph for this function:

void internal::BigInteger::PushBack ( Type  digit)
inlineprivate
224  {
226  digits_[count_++] = digit;
227  }
static const size_t kCapacity
Definition: biginteger.h:270
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
size_t count_
Definition: biginteger.h:274
Type digits_[kCapacity]
Definition: biginteger.h:273

+ Here is the caller graph for this function:

Member Data Documentation

size_t internal::BigInteger::count_
private
Type internal::BigInteger::digits_[kCapacity]
private
const size_t internal::BigInteger::kBitCount = 3328
staticprivate
const size_t internal::BigInteger::kCapacity = kBitCount / sizeof(Type)
staticprivate
const size_t internal::BigInteger::kTypeBit = sizeof(Type) * 8
staticprivate

The documentation for this class was generated from the following file: