squall/storm/big/Ops.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

2023-01-23 22:10:24 -06:00
#include "storm/big/Ops.hpp"
uint32_t ExtractLowPart(uint64_t& value) {
auto low = static_cast<uint32_t>(value);
value >>= 32;
return low;
}
uint32_t ExtractLowPartSx(uint64_t& value) {
auto low = static_cast<uint32_t>(value);
value >>= 32;
if (value >= 0x80000000) {
reinterpret_cast<uint32_t*>(&value)[0] = value;
reinterpret_cast<uint32_t*>(&value)[1] = -1;
}
return low;
}
2023-01-29 20:37:09 -06:00
void FromBinary(BigBuffer& buffer, const void* data, uint32_t bytes) {
buffer.Clear();
for (uint32_t i = 0; i < bytes; i++) {
auto byte = static_cast<const uint8_t*>(data)[i];
auto v7 = (i & 3) ? buffer[i / 4] : 0;
buffer[i / 4] = v7 + (byte << (8 * (i & 3)));
}
}
void FromUnsigned(BigBuffer& buffer, uint32_t value) {
buffer[0] = value;
buffer.SetCount(1);
}
2023-01-23 22:10:24 -06:00
uint64_t MakeLarge(uint32_t low, uint32_t high) {
return low + (static_cast<uint64_t>(high) << 32);
}
void ToBinaryAppend(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
for (uint32_t i = 0; i < buffer.Count() * 4; i++) {
auto byte = buffer[i / 4] >> (8 * (i & 3));
if (byte || (i / 4) + 1 < buffer.Count()) {
*output.New() = byte;
}
}
}
void ToBinary(TSGrowableArray<uint8_t>& output, const BigBuffer& buffer) {
output.SetCount(0);
ToBinaryAppend(output, buffer);
}