-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1] Add BitStreamWriter and BitStreamReader from Zserio C++ runtime
* add also BitSizeOfCalculator
- Loading branch information
Showing
14 changed files
with
4,162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#include <array> | ||
#include <limits> | ||
|
||
#include "zserio/BitSizeOfCalculator.h" | ||
#include "zserio/CppRuntimeException.h" | ||
|
||
namespace zserio | ||
{ | ||
|
||
static const std::array<uint64_t, 2> VARIN16_MAX_VALUES = { | ||
(UINT64_C(1) << (6)) - 1, | ||
(UINT64_C(1) << (6 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 4> VARINT32_MAX_VALUES = { | ||
(UINT64_C(1) << (6)) - 1, | ||
(UINT64_C(1) << (6 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 8> VARINT64_MAX_VALUES = { | ||
(UINT64_C(1) << (6)) - 1, | ||
(UINT64_C(1) << (6 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 2> VARUINT16_MAX_VALUES = { | ||
(UINT64_C(1) << (7)) - 1, | ||
(UINT64_C(1) << (7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 4> VARUINT32_MAX_VALUES = { | ||
(UINT64_C(1) << (7)) - 1, | ||
(UINT64_C(1) << (7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 8> VARUINT64_MAX_VALUES = { | ||
(UINT64_C(1) << (7)) - 1, | ||
(UINT64_C(1) << (7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 9> VARINT_MAX_VALUES = { | ||
(UINT64_C(1) << (6)) - 1, | ||
(UINT64_C(1) << (6 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
static const std::array<uint64_t, 9> VARUINT_MAX_VALUES = { | ||
(UINT64_C(1) << (7)) - 1, | ||
(UINT64_C(1) << (7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, | ||
UINT64_MAX, | ||
}; | ||
|
||
static const std::array<uint64_t, 5> VARSIZE_MAX_VALUES = { | ||
(UINT64_C(1) << (7)) - 1, | ||
(UINT64_C(1) << (7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (7 + 7 + 7 + 7)) - 1, | ||
(UINT64_C(1) << (2 + 7 + 7 + 7 + 8)) - 1, | ||
}; | ||
|
||
template <std::size_t SIZE> | ||
static size_t bitSizeOfVarIntImpl( | ||
uint64_t value, const std::array<uint64_t, SIZE>& maxValues, const char* varIntName) | ||
{ | ||
size_t byteSize = 1; | ||
for (uint64_t maxValue : maxValues) | ||
{ | ||
if (value <= maxValue) | ||
{ | ||
break; | ||
} | ||
byteSize++; | ||
} | ||
|
||
if (byteSize > maxValues.size()) | ||
{ | ||
throw CppRuntimeException("BitSizeOfCalculator: Value '") | ||
<< value << "' is out of range for " << varIntName << "!"; | ||
} | ||
|
||
return byteSize * 8; | ||
} | ||
|
||
template <typename T> | ||
static uint64_t convertToAbsValue(T value) | ||
{ | ||
return static_cast<uint64_t>((value < 0) ? -value : value); | ||
} | ||
|
||
size_t bitSizeOfVarInt16(int16_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARIN16_MAX_VALUES, "varint16"); | ||
} | ||
|
||
size_t bitSizeOfVarInt32(int32_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARINT32_MAX_VALUES, "varint32"); | ||
} | ||
|
||
size_t bitSizeOfVarInt64(int64_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARINT64_MAX_VALUES, "varint64"); | ||
} | ||
|
||
size_t bitSizeOfVarUInt16(uint16_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(value, VARUINT16_MAX_VALUES, "varuint16"); | ||
} | ||
|
||
size_t bitSizeOfVarUInt32(uint32_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(value, VARUINT32_MAX_VALUES, "varuint32"); | ||
} | ||
|
||
size_t bitSizeOfVarUInt64(uint64_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(value, VARUINT64_MAX_VALUES, "varuint64"); | ||
} | ||
|
||
size_t bitSizeOfVarInt(int64_t value) | ||
{ | ||
if (value == INT64_MIN) | ||
{ | ||
return 8; // INT64_MIN is stored as -0 | ||
} | ||
|
||
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARINT_MAX_VALUES, "varint"); | ||
} | ||
|
||
size_t bitSizeOfVarUInt(uint64_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(value, VARUINT_MAX_VALUES, "varuint"); | ||
} | ||
|
||
size_t bitSizeOfVarSize(uint32_t value) | ||
{ | ||
return bitSizeOfVarIntImpl(value, VARSIZE_MAX_VALUES, "varsize"); | ||
} | ||
|
||
size_t bitSizeOfBytes(Span<const uint8_t> bytesValue) | ||
{ | ||
const size_t bytesSize = bytesValue.size(); | ||
|
||
// the bytes consists of varsize for size followed by the bytes | ||
return bitSizeOfVarSize(convertSizeToUInt32(bytesSize)) + bytesSize * 8; | ||
} | ||
|
||
size_t bitSizeOfString(std::string_view stringValue) | ||
{ | ||
const size_t stringSize = stringValue.size(); | ||
|
||
// the string consists of varsize for size followed by the UTF-8 encoded string | ||
return bitSizeOfVarSize(convertSizeToUInt32(stringSize)) + stringSize * 8; | ||
} | ||
|
||
} // namespace zserio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#ifndef ZSERIO_BITSIZEOF_CALCULATOR_H_INC | ||
#define ZSERIO_BITSIZEOF_CALCULATOR_H_INC | ||
|
||
#include <cstddef> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include "zserio/BitBuffer.h" | ||
#include "zserio/BitPositionUtil.h" | ||
#include "zserio/SizeConvertUtil.h" | ||
#include "zserio/Span.h" | ||
#include "zserio/Types.h" | ||
|
||
namespace zserio | ||
{ | ||
|
||
/** | ||
* Calculates bit size of Zserio varint16 type. | ||
* | ||
* \param value Varint16 value. | ||
* | ||
* \return Bit size of the current varint16 value. | ||
*/ | ||
size_t bitSizeOfVarInt16(int16_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varint32 type. | ||
* | ||
* \param value Varint32 value. | ||
* | ||
* \return Bit size of the current varint32 value. | ||
*/ | ||
size_t bitSizeOfVarInt32(int32_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varint64 type. | ||
* | ||
* \param value Varint64 value. | ||
* | ||
* \return Bit size of the current varint64 value. | ||
*/ | ||
size_t bitSizeOfVarInt64(int64_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varuint16 type. | ||
* | ||
* \param value Varuint16 value. | ||
* | ||
* \return Bit size of the current varuint16 value. | ||
*/ | ||
size_t bitSizeOfVarUInt16(uint16_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varuint32 type. | ||
* | ||
* \param value Varuint32 value. | ||
* | ||
* \return Bit size of the current varuint32 value. | ||
*/ | ||
size_t bitSizeOfVarUInt32(uint32_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varuint64 type. | ||
* | ||
* \param value Varuint64 value. | ||
* | ||
* \return Bit size of the current varuint64 value. | ||
*/ | ||
size_t bitSizeOfVarUInt64(uint64_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varint type. | ||
* | ||
* \param value Varint value. | ||
* | ||
* \return Bit size of the current varint value. | ||
*/ | ||
size_t bitSizeOfVarInt(int64_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varuint type. | ||
* | ||
* \param value Varuint value. | ||
* | ||
* \return Bit size of the current varuint value. | ||
*/ | ||
size_t bitSizeOfVarUInt(uint64_t value); | ||
|
||
/** | ||
* Calculates bit size of Zserio varsize type. | ||
* | ||
* \param value Varsize value. | ||
* | ||
* \return Bit size of the current varsize value. | ||
*/ | ||
size_t bitSizeOfVarSize(uint32_t value); | ||
|
||
/** | ||
* Calculates bit size of bytes. | ||
* | ||
* \param bytesValue Span representing the bytes value. | ||
* | ||
* \return Bit size of the given bytes value. | ||
*/ | ||
size_t bitSizeOfBytes(Span<const uint8_t> bytesValue); | ||
|
||
/** | ||
* Calculates bit size of the string. | ||
* | ||
* \param stringValue String view for which to calculate bit size. | ||
* | ||
* \return Bit size of the given string. | ||
*/ | ||
size_t bitSizeOfString(std::string_view stringValue); | ||
|
||
/** | ||
* Calculates bit size of the bit buffer. | ||
* | ||
* \param bitBuffer Bit buffer for which to calculate bit size. | ||
* | ||
* \return Bit size of the given bit buffer. | ||
*/ | ||
template <typename ALLOC> | ||
size_t bitSizeOfBitBuffer(const BasicBitBuffer<ALLOC>& bitBuffer) | ||
{ | ||
const size_t bitBufferSize = bitBuffer.getBitSize(); | ||
|
||
// bit buffer consists of varsize for bit size followed by the bits | ||
return bitSizeOfVarSize(convertSizeToUInt32(bitBufferSize)) + bitBufferSize; | ||
} | ||
|
||
} // namespace zserio | ||
|
||
#endif // ifndef ZSERIO_BITSIZEOF_CALCULATOR_H_INC |
Oops, something went wrong.