24#ifndef MYSQL_SERIALIZATION_VARIABLE_LENGTH_INTEGERS_H
25#define MYSQL_SERIALIZATION_VARIABLE_LENGTH_INTEGERS_H
50 const std::unsigned_integral
auto &data) {
61 int bits_in_number = std::bit_width(data);
62 return ((bits_in_number * 575) >> 12) + 1;
69 auto sign_mask = data >> (
sizeof(data) * 8 - 1);
90 const std::unsigned_integral
auto &data) {
91 uint64_t data_cpy = data;
93 stream[0] = ((1 << (byte_count - 1)) - 1) |
94 static_cast<uint8_t
>(data_cpy << byte_count);
96 if (byte_count == 1) {
101 data_cpy >>= (8 - byte_count + ((byte_count + 7) >> 4));
104 memcpy(&stream[1], &data_cpy, byte_count - 1);
111 const std::signed_integral
auto &data) {
114 auto sign_mask = (data >> (
sizeof(data) * 8 - 1));
115 uint64_t data_cpy = (data ^ sign_mask);
117 data_cpy = (data_cpy << 1) | (sign_mask & 1);
124 const std::unsigned_integral
auto &data) {
131 const std::signed_integral
auto &data) {
141template <
typename Type>
143 std::size_t stream_bytes,
Type &data)
144 requires std::unsigned_integral<Type>
146 if (stream_bytes == 0) {
149 uint8_t first_byte = stream[0];
150 std::size_t num_bytes = std::countr_one(first_byte) + 1;
151 if (num_bytes > stream_bytes) {
154 Type data_cpy = first_byte >> num_bytes;
155 if (num_bytes == 1) {
159 uint64_t data_tmp = 0;
160 memcpy(&data_tmp, &stream[1], num_bytes - 1);
164 data_tmp <<= (8 - num_bytes + ((num_bytes + 7) >> 4));
165 if (data_tmp > std::numeric_limits<Type>::max()) {
168 data_cpy |= data_tmp;
174template <
typename Type>
176 std::size_t stream_bytes,
Type &data)
177 requires std::signed_integral<Type>
179 using Type_unsigned = std::make_unsigned_t<Type>;
180 Type_unsigned data_tmp = 0;
181 std::size_t num_bytes =
185 Type_unsigned sign_mask = -
static_cast<Type>(data_tmp & 1);
187 data_tmp = data_tmp >> 1;
189 data_tmp = data_tmp ^ sign_mask;
190 data =
Type(data_tmp);
196 std::unsigned_integral
auto &data) {
202 std::signed_integral
auto &data) {
uint64_t htole64(uint64_t x)
Converting a 64 bit integer from host's byte order to little-endian byte order.
Definition: byte_order_helpers.h:70
uint64_t le64toh(uint64_t x)
Converting a 64 bit integer from little-endian byte order to host byteorder.
Definition: byte_order_helpers.h:50
Functions for reading and storing in machine-independent format.
Definition: variable_length_integers.h:43
size_t get_size_integer_varlen_unsigned(const std::unsigned_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:49
size_t write_varlen_bytes_signed(unsigned char *stream, const std::signed_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:110
size_t read_varlen_bytes_unsigned(const unsigned char *stream, std::size_t stream_bytes, Type &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:142
size_t write_varlen_bytes(unsigned char *stream, const std::unsigned_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:123
size_t get_size_integer_varlen(const std::unsigned_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:75
size_t read_varlen_bytes_signed(const unsigned char *stream, std::size_t stream_bytes, Type &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:175
size_t read_varlen_bytes(const unsigned char *stream, std::size_t stream_bytes, std::unsigned_integral auto &data)
Reads variable-length integer from the stream.
Definition: variable_length_integers.h:195
size_t get_size_integer_varlen_signed(const std::signed_integral auto &data)
Calculates the number of bytes necessary to store data.
Definition: variable_length_integers.h:67
size_t write_varlen_bytes_unsigned(unsigned char *stream, const std::unsigned_integral auto &data)
Writes variable-length integer to the stream.
Definition: variable_length_integers.h:89
Type
Definition: resource_group_basic_types.h:33