feat(big): add HighBitPos

This commit is contained in:
fallenoak 2023-02-01 21:08:15 -06:00
parent e9d3284c70
commit 6e62f0a604
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
3 changed files with 92 additions and 0 deletions

View file

@ -68,6 +68,33 @@ void FromUnsigned(BigBuffer& buffer, uint32_t value) {
buffer.SetCount(1);
}
uint32_t HighBitPos(const BigBuffer& buffer) {
uint32_t index = buffer.Count();
if (index == 0) {
return 0;
}
while (buffer[--index] == 0) {
if (index == 0) {
return 0;
}
}
uint32_t mask = 0x80000000;
uint32_t bitIndex = 32;
while (bitIndex > 0) {
bitIndex--;
if (buffer[index] & mask) {
return (index * 32) + bitIndex;
}
mask >>= 1;
}
return 0;
}
uint64_t MakeLarge(uint32_t low, uint32_t high) {
return low + (static_cast<uint64_t>(high) << 32);
}