20 template <
typename UInteger>
30 bool operator< (UInteger
const& number)
const;
32 bool operator> (UInteger
const& number)
const;
39 void Add(UInteger
const& n0, UInteger
const& n1);
40 void Sub(UInteger
const& n0, UInteger
const& n1);
41 void Mul(UInteger
const& n0, UInteger
const& n1);
45 void ShiftLeft(UInteger
const& number, int32_t shift);
57 uint64_t
GetPrefix(int32_t numRequested)
const;
61 template <
typename UInteger>
64 UInteger
const&
self = *(UInteger
const*)
this;
65 int32_t numBits =
self.GetNumBits();
66 if (numBits != number.GetNumBits())
73 auto const&
bits =
self.GetBits();
74 auto const& nBits = number.GetBits();
75 int32_t
const last =
self.GetSize() - 1;
76 for (int32_t i = last; i >= 0; --i)
78 if (
bits[i] != nBits[i])
87 template <
typename UInteger>
93 template <
typename UInteger>
96 UInteger
const&
self = *(UInteger
const*)
this;
97 int32_t nNumBits = number.GetNumBits();
98 auto const& nBits = number.GetBits();
100 int32_t numBits =
self.GetNumBits();
101 if (numBits > 0 && nNumBits > 0)
108 auto const&
bits =
self.GetBits();
109 int bitIndex0 = numBits - 1;
110 int bitIndex1 = nNumBits - 1;
111 int block0 = bitIndex0 / 32;
112 int block1 = bitIndex1 / 32;
113 int numBlockBits0 = 1 + (bitIndex0 % 32);
114 int numBlockBits1 = 1 + (bitIndex1 % 32);
115 uint64_t n0shift =
bits[block0];
116 uint64_t n1shift = nBits[block1];
117 while (block0 >= 0 && block1 >= 0)
129 n0shift =
bits[block0];
134 n1shift = nBits[block1];
146 return block0 < block1;
152 return (nNumBits > 0);
156 template <
typename UInteger>
162 template <
typename UInteger>
168 template <
typename UInteger>
174 template <
typename UInteger>
177 UInteger&
self = *(UInteger*)
this;
178 int32_t n0NumBits = n0.GetNumBits();
179 int32_t n1NumBits = n1.GetNumBits();
183 int numBits = std::max(n0NumBits, n1NumBits) + 1;
184 self.SetNumBits(numBits);
188 int32_t numElements0 = n0.GetSize();
189 int32_t numElements1 = n1.GetSize();
193 (numElements0 >= numElements1 ? n0.GetBits() : n1.GetBits());
195 (numElements0 >= numElements1 ? n1.GetBits() : n0.GetBits());
196 auto numElements = std::minmax(numElements0, numElements1);
199 auto&
bits =
self.GetBits();
200 uint64_t carry = 0, sum;
202 for (i = 0; i < numElements.first; ++i)
204 sum = u0[i] + (
u1[i] + carry);
213 for (; i < numElements.second; ++i)
226 for (; i < numElements.second; ++i)
233 uint32_t firstBitIndex = (numBits - 1) % 32;
234 uint32_t
mask = (1 << firstBitIndex);
235 if ((mask &
self.GetBack()) == 0)
237 self.SetNumBits(--numBits);
241 template <
typename UInteger>
244 UInteger&
self = *(UInteger*)
this;
245 int32_t n0NumBits = n0.GetNumBits();
246 auto const& n0Bits = n0.GetBits();
247 auto const& n1Bits = n1.GetBits();
256 int32_t numElements0 = n0.GetSize();
257 int32_t numElements1 = n1.GetSize();
261 UInteger n2(n0NumBits);
262 auto& n2Bits = n2.GetBits();
264 for (i = 0; i < numElements1; ++i)
266 n2Bits[i] = ~n1Bits[i];
268 for (; i < numElements0; ++i)
274 uint64_t carry = 1, sum;
275 for (i = 0; i < numElements0; ++i)
277 sum = n2Bits[i] + carry;
284 self.SetNumBits(n0NumBits + 1);
288 auto&
bits =
self.GetBits();
289 for (i = 0, carry = 0; i < numElements0; ++i)
291 sum = n2Bits[i] + (n0Bits[i] + carry);
298 for (block = numElements0 - 1; block >= 0; --block)
309 template <
typename UInteger>
312 UInteger&
self = *(UInteger*)
this;
313 int32_t n0NumBits = n0.GetNumBits();
314 int32_t n1NumBits = n1.GetNumBits();
315 auto const& n0Bits = n0.GetBits();
316 auto const& n1Bits = n1.GetBits();
319 int numBits = n0NumBits + n1NumBits;
320 self.SetNumBits(numBits);
321 auto&
bits =
self.GetBits();
324 UInteger product(numBits);
325 auto& pBits = product.GetBits();
328 int32_t
const numElements0 = n0.GetSize();
329 int32_t
const numElements1 = n1.GetSize();
330 int32_t
const numElements =
self.GetSize();
340 uint64_t block0 = n0Bits[0];
342 for (i1 = 0; i1 < numElements1; ++i1)
344 term = block0 * n1Bits[i1] + carry;
346 carry = (term >> 32);
348 if (i1 < numElements)
353 for (i0 = 1; i0 < numElements0; ++i0)
358 for (i1 = 0, i2 = i0; i1 < numElements1; ++i1, ++i2)
360 term = block0 * n1Bits[i1] + carry;
362 carry = (term >> 32);
364 if (i2 < numElements)
371 for (i1 = 0, i2 = i0; i1 < numElements1; ++i1, ++i2)
373 sum = pBits[i2] + (
bits[i2] + carry);
377 if (i2 < numElements)
379 sum = pBits[i2] + carry;
385 uint32_t firstBitIndex = (numBits - 1) % 32;
386 uint32_t
mask = (1 << firstBitIndex);
387 if ((mask &
self.GetBack()) == 0)
389 self.SetNumBits(--numBits);
393 template <
typename UInteger>
396 UInteger&
self = *(UInteger*)
this;
397 int32_t nNumBits = number.GetNumBits();
398 auto const& nBits = number.GetBits();
401 self.SetNumBits(nNumBits + shift);
404 auto&
bits =
self.GetBits();
405 int32_t
const shiftBlock = shift / 32;
406 for (int32_t i = 0; i < shiftBlock; ++i)
412 int32_t
const numInElements = number.GetSize();
413 int32_t
const lshift = shift % 32;
421 int32_t
const rshift = 32 - lshift;
422 uint32_t prev = 0, curr;
423 for (i = shiftBlock, j = 0; j < numInElements; ++i, ++j)
426 bits[i] = (curr << lshift) | (prev >> rshift);
429 if (i <
self.GetSize())
434 bits[i] = (prev >> rshift);
441 for (i = shiftBlock, j = 0; j < numInElements; ++i, ++j)
448 template <
typename UInteger>
451 UInteger&
self = *(UInteger*)
this;
452 auto const& nBits = number.GetBits();
455 int32_t
const numElements = number.GetSize();
456 int32_t
const numM1 = numElements - 1;
457 int32_t firstBitIndex = 32 * numM1 +
GetLeadingBit(nBits[numM1]);
460 int32_t lastBitIndex = -1;
461 for (int32_t block = 0; block < numElements; ++block)
463 uint32_t
value = nBits[block];
472 self.SetNumBits(firstBitIndex - lastBitIndex + 1);
473 auto&
bits =
self.GetBits();
474 int32_t
const numBlocks =
self.GetSize();
477 int32_t
const shiftBlock = lastBitIndex / 32;
478 int32_t rshift = lastBitIndex % 32;
481 int32_t
const lshift = 32 - rshift;
482 int32_t i, j = shiftBlock;
483 uint32_t curr = nBits[j++];
484 for (i = 0; j < numElements; ++i, ++j)
486 uint32_t next = nBits[j];
487 bits[i] = (curr >> rshift) | (next << lshift);
492 bits[i] = (curr >> rshift);
497 for (int32_t i = 0, j = shiftBlock; i < numBlocks; ++i, ++j)
503 return rshift + 32 * shiftBlock;
506 template <
typename UInteger>
509 UInteger
const&
self = *(UInteger
const*)
this;
510 auto const&
bits =
self.GetBits();
513 int32_t bitIndex =
self.GetNumBits() - 1;
514 int32_t blockIndex = bitIndex / 32;
515 uint64_t prefix =
bits[blockIndex];
518 int32_t firstBitIndex = bitIndex % 32;
519 int32_t numBlockBits = firstBitIndex + 1;
523 int32_t targetIndex = 63;
524 prefix <<= targetIndex - firstBitIndex;
525 numRequested -= numBlockBits;
526 targetIndex -= numBlockBits;
528 if (numRequested > 0 && --blockIndex >= 0)
534 uint64_t nextBlock =
bits[blockIndex];
535 nextBlock <<= targetIndex - 31;
540 if (numRequested > 0 && --blockIndex >= 0)
546 nextBlock =
bits[blockIndex];
547 nextBlock >>= 31 - targetIndex;
uint64_t GetPrefix(int32_t numRequested) const
GTE_IMPEXP int32_t GetTrailingBit(uint32_t value)
bool operator==(UInteger const &number) const
GTE_IMPEXP int32_t GetLeadingBit(uint32_t value)
int32_t ShiftRightToOdd(UInteger const &number)
GLsizei const GLfloat * value
void ShiftLeft(UInteger const &number, int32_t shift)
bool operator!=(UInteger const &number) const
void Sub(UInteger const &n0, UInteger const &n1)
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
#define GTE_GET_LO_U64(v)
bool operator>=(UInteger const &number) const
void Add(UInteger const &n0, UInteger const &n1)
bool operator>(UInteger const &number) const
void Mul(UInteger const &n0, UInteger const &n1)
bool operator<=(UInteger const &number) const
bool operator<(UInteger const &number) const