123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- #pragma once
- #include <Mathematics/BitHacks.h>
- #include <algorithm>
- namespace WwiseGTE
- {
- template <typename UInteger>
- class UIntegerALU32
- {
- public:
-
-
-
-
- bool operator==(UInteger const& number) const
- {
- UInteger const& self = *(UInteger const*)this;
- int32_t numBits = self.GetNumBits();
- if (numBits != number.GetNumBits())
- {
- return false;
- }
- if (numBits > 0)
- {
- auto const& bits = self.GetBits();
- auto const& nBits = number.GetBits();
- int32_t const last = self.GetSize() - 1;
- for (int32_t i = last; i >= 0; --i)
- {
- if (bits[i] != nBits[i])
- {
- return false;
- }
- }
- }
- return true;
- }
- bool operator!=(UInteger const& number) const
- {
- return !operator==(number);
- }
- bool operator< (UInteger const& number) const
- {
- UInteger const& self = *(UInteger const*)this;
- int32_t nNumBits = number.GetNumBits();
- auto const& nBits = number.GetBits();
- int32_t numBits = self.GetNumBits();
- if (numBits > 0 && nNumBits > 0)
- {
-
-
-
-
-
-
- auto const& bits = self.GetBits();
- int bitIndex0 = numBits - 1;
- int bitIndex1 = nNumBits - 1;
- int block0 = bitIndex0 / 32;
- int block1 = bitIndex1 / 32;
- int numBlockBits0 = 1 + (bitIndex0 % 32);
- int numBlockBits1 = 1 + (bitIndex1 % 32);
- uint64_t n0shift = bits[block0];
- uint64_t n1shift = nBits[block1];
- while (block0 >= 0 && block1 >= 0)
- {
-
- uint32_t value0 = (uint32_t)((n0shift << (32 - numBlockBits0)) & 0x00000000FFFFFFFFull);
- uint32_t value1 = (uint32_t)((n1shift << (32 - numBlockBits1)) & 0x00000000FFFFFFFFull);
-
-
- if (--block0 >= 0)
- {
- n0shift = bits[block0];
- value0 |= (uint32_t)((n0shift >> numBlockBits0) & 0x00000000FFFFFFFFull);
- }
- if (--block1 >= 0)
- {
- n1shift = nBits[block1];
- value1 |= (uint32_t)((n1shift >> numBlockBits1) & 0x00000000FFFFFFFFull);
- }
- if (value0 < value1)
- {
- return true;
- }
- if (value0 > value1)
- {
- return false;
- }
- }
- return block0 < block1;
- }
- else
- {
-
-
- return nNumBits > 0;
- }
- }
- bool operator<=(UInteger const& number) const
- {
- return operator<(number) || operator==(number);
- }
- bool operator> (UInteger const& number) const
- {
- return !operator<=(number);
- }
- bool operator>=(UInteger const& number) const
- {
- return !operator<(number);
- }
-
-
-
-
- void Add(UInteger const& n0, UInteger const& n1)
- {
- UInteger& self = *(UInteger*)this;
- int32_t n0NumBits = n0.GetNumBits();
- int32_t n1NumBits = n1.GetNumBits();
-
-
- int numBits = std::max(n0NumBits, n1NumBits) + 1;
- self.SetNumBits(numBits);
- self.SetBack(0);
-
- int32_t numElements0 = n0.GetSize();
- int32_t numElements1 = n1.GetSize();
-
- auto const& u0 = (numElements0 >= numElements1 ? n0.GetBits() : n1.GetBits());
- auto const& u1 = (numElements0 >= numElements1 ? n1.GetBits() : n0.GetBits());
- auto numElements = std::minmax(numElements0, numElements1);
-
- auto& bits = self.GetBits();
- uint64_t carry = 0, sum;
- int32_t i;
- for (i = 0; i < numElements.first; ++i)
- {
- sum = u0[i] + (u1[i] + carry);
- bits[i] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- carry = (sum >> 32);
- }
-
-
- if (carry > 0)
- {
- for (; i < numElements.second; ++i)
- {
- sum = u0[i] + carry;
- bits[i] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- carry = (sum >> 32);
- }
- if (carry > 0)
- {
- bits[i] = (uint32_t)(carry & 0x00000000FFFFFFFFull);
- }
- }
- else
- {
- for (; i < numElements.second; ++i)
- {
- bits[i] = u0[i];
- }
- }
-
- uint32_t firstBitIndex = (numBits - 1) % 32;
- uint32_t mask = (1 << firstBitIndex);
- if ((mask & self.GetBack()) == 0)
- {
- self.SetNumBits(--numBits);
- }
- }
- void Sub(UInteger const& n0, UInteger const& n1)
- {
- UInteger& self = *(UInteger*)this;
- int32_t n0NumBits = n0.GetNumBits();
- auto const& n0Bits = n0.GetBits();
- auto const& n1Bits = n1.GetBits();
-
-
-
-
-
-
-
-
- int32_t numElements0 = n0.GetSize();
- int32_t numElements1 = n1.GetSize();
-
-
- UInteger n2;
- n2.SetNumBits(n0NumBits);
- auto& n2Bits = n2.GetBits();
- int32_t i;
- for (i = 0; i < numElements1; ++i)
- {
- n2Bits[i] = ~n1Bits[i];
- }
- for (; i < numElements0; ++i)
- {
- n2Bits[i] = ~0u;
- }
-
- uint64_t carry = 1, sum;
- for (i = 0; i < numElements0; ++i)
- {
- sum = n2Bits[i] + carry;
- n2Bits[i] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- carry = (sum >> 32);
- }
-
-
- self.SetNumBits(n0NumBits + 1);
- self.SetBack(0);
-
- auto & bits = self.GetBits();
- for (i = 0, carry = 0; i < numElements0; ++i)
- {
- sum = n2Bits[i] + (n0Bits[i] + carry);
- bits[i] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- carry = (sum >> 32);
- }
-
- int32_t block;
- for (block = numElements0 - 1; block >= 0; --block)
- {
- if (bits[block] > 0)
- {
- break;
- }
- }
- if (block >= 0)
- {
- self.SetNumBits(32 * block + BitHacks::GetLeadingBit(bits[block]) + 1);
- }
- else
- {
- self.SetNumBits(0);
- }
- }
- void Mul(UInteger const& n0, UInteger const& n1)
- {
- UInteger& self = *(UInteger*)this;
- int32_t n0NumBits = n0.GetNumBits();
- int32_t n1NumBits = n1.GetNumBits();
- auto const& n0Bits = n0.GetBits();
- auto const& n1Bits = n1.GetBits();
-
- int numBits = n0NumBits + n1NumBits;
- self.SetNumBits(numBits);
- auto& bits = self.GetBits();
-
- UInteger product;
- product.SetNumBits(numBits);
- auto& pBits = product.GetBits();
-
- int32_t const numElements0 = n0.GetSize();
- int32_t const numElements1 = n1.GetSize();
- int32_t const numElements = self.GetSize();
-
- int32_t i0, i1, i2;
- uint64_t term, sum;
-
-
-
-
- uint64_t block0 = n0Bits[0];
- uint64_t carry = 0;
- for (i1 = 0; i1 < numElements1; ++i1)
- {
- term = block0 * n1Bits[i1] + carry;
- bits[i1] = (uint32_t)(term & 0x00000000FFFFFFFFull);
- carry = (term >> 32);
- }
- if (i1 < numElements)
- {
- bits[i1] = (uint32_t)(carry & 0x00000000FFFFFFFFull);
- }
- for (i0 = 1; i0 < numElements0; ++i0)
- {
-
- block0 = n0Bits[i0];
- carry = 0;
- for (i1 = 0, i2 = i0; i1 < numElements1; ++i1, ++i2)
- {
- term = block0 * n1Bits[i1] + carry;
- pBits[i2] = (uint32_t)(term & 0x00000000FFFFFFFFull);
- carry = (term >> 32);
- }
- if (i2 < numElements)
- {
- pBits[i2] = (uint32_t)(carry & 0x00000000FFFFFFFFull);
- }
-
- carry = 0;
- for (i1 = 0, i2 = i0; i1 < numElements1; ++i1, ++i2)
- {
- sum = pBits[i2] + (bits[i2] + carry);
- bits[i2] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- carry = (sum >> 32);
- }
- if (i2 < numElements)
- {
- sum = pBits[i2] + carry;
- bits[i2] = (uint32_t)(sum & 0x00000000FFFFFFFFull);
- }
- }
-
- uint32_t firstBitIndex = (numBits - 1) % 32;
- uint32_t mask = (1 << firstBitIndex);
- if ((mask & self.GetBack()) == 0)
- {
- self.SetNumBits(--numBits);
- }
- }
-
-
- void ShiftLeft(UInteger const& number, int32_t shift)
- {
- UInteger& self = *(UInteger*)this;
- int32_t nNumBits = number.GetNumBits();
- auto const& nBits = number.GetBits();
-
- self.SetNumBits(nNumBits + shift);
-
- auto& bits = self.GetBits();
- int32_t const shiftBlock = shift / 32;
- for (int32_t i = 0; i < shiftBlock; ++i)
- {
- bits[i] = 0;
- }
-
- int32_t const numInElements = number.GetSize();
- int32_t const lshift = shift % 32;
- int32_t i, j;
- if (lshift > 0)
- {
-
-
-
-
- int32_t const rshift = 32 - lshift;
- uint32_t prev = 0, curr;
- for (i = shiftBlock, j = 0; j < numInElements; ++i, ++j)
- {
- curr = nBits[j];
- bits[i] = (curr << lshift) | (prev >> rshift);
- prev = curr;
- }
- if (i < self.GetSize())
- {
-
-
-
- bits[i] = (prev >> rshift);
- }
- }
- else
- {
-
-
- for (i = shiftBlock, j = 0; j < numInElements; ++i, ++j)
- {
- bits[i] = nBits[j];
- }
- }
- }
-
-
-
-
- int32_t ShiftRightToOdd(UInteger const& number)
- {
- UInteger& self = *(UInteger*)this;
- auto const& nBits = number.GetBits();
-
- int32_t const numElements = number.GetSize();
- int32_t const numM1 = numElements - 1;
- int32_t firstBitIndex = 32 * numM1 + BitHacks::GetLeadingBit(nBits[numM1]);
-
- int32_t lastBitIndex = -1;
- for (int32_t block = 0; block < numElements; ++block)
- {
- uint32_t value = nBits[block];
- if (value > 0)
- {
- lastBitIndex = 32 * block + BitHacks::GetTrailingBit(value);
- break;
- }
- }
-
- self.SetNumBits(firstBitIndex - lastBitIndex + 1);
- auto& bits = self.GetBits();
- int32_t const numBlocks = self.GetSize();
-
- int32_t const shiftBlock = lastBitIndex / 32;
- int32_t rshift = lastBitIndex % 32;
- if (rshift > 0)
- {
- int32_t const lshift = 32 - rshift;
- int32_t i, j = shiftBlock;
- uint32_t curr = nBits[j++];
- for (i = 0; j < numElements; ++i, ++j)
- {
- uint32_t next = nBits[j];
- bits[i] = (curr >> rshift) | (next << lshift);
- curr = next;
- }
- if (i < numBlocks)
- {
- bits[i] = (curr >> rshift);
- }
- }
- else
- {
- for (int32_t i = 0, j = shiftBlock; i < numBlocks; ++i, ++j)
- {
- bits[i] = nBits[j];
- }
- }
- return rshift + 32 * shiftBlock;
- }
-
-
-
-
-
- int32_t RoundUp()
- {
- UInteger const& self = *(UInteger const*)this;
- UInteger rounded;
- rounded.Add(self, UInteger(1u));
- return ShiftRightToOdd(rounded);
- }
-
-
-
-
-
- uint64_t GetPrefix(int32_t numRequested) const
- {
- UInteger const& self = *(UInteger const*)this;
- auto const& bits = self.GetBits();
-
- int32_t bitIndex = self.GetNumBits() - 1;
- int32_t blockIndex = bitIndex / 32;
- uint64_t prefix = bits[blockIndex];
-
-
- int32_t firstBitIndex = bitIndex % 32;
- int32_t numBlockBits = firstBitIndex + 1;
-
-
- int32_t targetIndex = 63;
- prefix <<= targetIndex - firstBitIndex;
- numRequested -= numBlockBits;
- targetIndex -= numBlockBits;
- if (numRequested > 0 && --blockIndex >= 0)
- {
-
-
-
-
- uint64_t nextBlock = bits[blockIndex];
- nextBlock <<= targetIndex - 31;
- prefix |= nextBlock;
- numRequested -= 32;
- targetIndex -= 32;
- if (numRequested > 0 && --blockIndex >= 0)
- {
-
-
-
-
-
- nextBlock = bits[blockIndex];
- nextBlock >>= 31 - targetIndex;
- prefix |= nextBlock;
- }
- }
- return prefix;
- }
- };
- }
|