Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite encoding functions #1358

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 33 additions & 54 deletions src/common/encoding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,44 +99,30 @@
void EncodeFixed8(char *buf, uint8_t value) { buf[0] = static_cast<char>(value & 0xff); }

void EncodeFixed16(char *buf, uint16_t value) {
if (BYTE_ORDER == BIG_ENDIAN) {
memcpy(buf, &value, sizeof(value));
} else {
buf[0] = static_cast<char>((value >> 8) & 0xff);
buf[1] = static_cast<char>(value & 0xff);
if constexpr (BYTE_ORDER == LITTLE_ENDIAN) {
value = __builtin_bswap16(value);
}
__builtin_memcpy(buf, &value, sizeof(value));
}

void EncodeFixed32(char *buf, uint32_t value) {
if (BYTE_ORDER == BIG_ENDIAN) {
memcpy(buf, &value, sizeof(value));
} else {
buf[0] = static_cast<char>((value >> 24) & 0xff);
buf[1] = static_cast<char>((value >> 16) & 0xff);
buf[2] = static_cast<char>((value >> 8) & 0xff);
buf[3] = static_cast<char>(value & 0xff);
if constexpr (BYTE_ORDER == LITTLE_ENDIAN) {
value = __builtin_bswap32(value);
}
__builtin_memcpy(buf, &value, sizeof(value));
}

void EncodeFixed64(char *buf, uint64_t value) {
if (BYTE_ORDER == BIG_ENDIAN) {
memcpy(buf, &value, sizeof(value));
} else {
buf[0] = static_cast<char>((value >> 56) & 0xff);
buf[1] = static_cast<char>((value >> 48) & 0xff);
buf[2] = static_cast<char>((value >> 40) & 0xff);
buf[3] = static_cast<char>((value >> 32) & 0xff);
buf[4] = static_cast<char>((value >> 24) & 0xff);
buf[5] = static_cast<char>((value >> 16) & 0xff);
buf[6] = static_cast<char>((value >> 8) & 0xff);
buf[7] = static_cast<char>(value & 0xff);
if constexpr (BYTE_ORDER == LITTLE_ENDIAN) {
value = __builtin_bswap64(value);
}
__builtin_memcpy(buf, &value, sizeof(value));
}

void PutFixed8(std::string *dst, uint8_t value) {
char buf[1];
buf[0] = static_cast<char>(value & 0xff);
dst->append(buf, 1);
EncodeFixed8(buf, value);
dst->append(buf, sizeof(buf));
}

void PutFixed16(std::string *dst, uint16_t value) {
Expand All @@ -159,7 +145,9 @@ void PutFixed64(std::string *dst, uint64_t value) {

void PutDouble(std::string *dst, double value) {
uint64_t u64 = 0;
memcpy(&u64, &value, sizeof(value));

__builtin_memcpy(&u64, &value, sizeof(value));

auto ptr = &u64;
if ((*ptr >> 63) == 1) {
// signed bit would be zero
Expand All @@ -168,6 +156,7 @@ void PutDouble(std::string *dst, double value) {
// signed bit would be one
*ptr |= 0x8000000000000000;
}

PutFixed64(dst, *ptr);
}

Expand Down Expand Up @@ -217,50 +206,40 @@ bool GetDouble(rocksdb::Slice *input, double *value) {
}

uint16_t DecodeFixed16(const char *ptr) {
if (BYTE_ORDER == BIG_ENDIAN) {
uint16_t value = 0;
memcpy(&value, ptr, sizeof(value));
return value;
} else {
return ((static_cast<uint16_t>(static_cast<uint8_t>(ptr[1]))) |
(static_cast<uint16_t>(static_cast<uint8_t>(ptr[0])) << 8));
}
uint16_t value = 0;

__builtin_memcpy(&value, ptr, sizeof(value));

return BYTE_ORDER == LITTLE_ENDIAN ? __builtin_bswap16(value) : value;
}

uint32_t DecodeFixed32(const char *ptr) {
if (BYTE_ORDER == BIG_ENDIAN) {
uint32_t value = 0;
memcpy(&value, ptr, sizeof(value));
return value;
} else {
return ((static_cast<uint32_t>(static_cast<uint8_t>(ptr[3]))) |
(static_cast<uint32_t>(static_cast<uint8_t>(ptr[2])) << 8) |
(static_cast<uint32_t>(static_cast<uint8_t>(ptr[1])) << 16) |
(static_cast<uint32_t>(static_cast<uint8_t>(ptr[0])) << 24));
}
uint32_t value = 0;

__builtin_memcpy(&value, ptr, sizeof(value));

return BYTE_ORDER == LITTLE_ENDIAN ? __builtin_bswap32(value) : value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PragmaTwice Is there a possibility to use if constexpr here with the condition? Or it wouldn't change anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if constexpr is just a constraint in this case. Compiler will eliminate constant conditions.

}

uint64_t DecodeFixed64(const char *ptr) {
if (BYTE_ORDER == BIG_ENDIAN) {
uint64_t value = 0;
memcpy(&value, ptr, sizeof(value));
return value;
} else {
uint64_t hi = DecodeFixed32(ptr);
uint64_t lo = DecodeFixed32(ptr + 4);
return (hi << 32) | lo;
}
uint64_t value = 0;

__builtin_memcpy(&value, ptr, sizeof(value));

return BYTE_ORDER == LITTLE_ENDIAN ? __builtin_bswap64(value) : value;
}

double DecodeDouble(const char *ptr) {
uint64_t decoded = DecodeFixed64(ptr);

if ((decoded >> 63) == 0) {
decoded ^= 0xffffffffffffffff;
} else {
decoded &= 0x7fffffffffffffff;
}

double value = 0;
memcpy(&value, &decoded, sizeof(value));
__builtin_memcpy(&value, &decoded, sizeof(value));
return value;
}

Expand Down
2 changes: 0 additions & 2 deletions src/common/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@ uint64_t DecodeFixed64(const char *ptr);
double DecodeDouble(const char *ptr);
char *EncodeVarint32(char *dst, uint32_t v);
void PutVarint32(std::string *dst, uint32_t v);
const char *GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *value);
const char *GetVarint32Ptr(const char *p, const char *limit, uint32_t *value);
bool GetVarint32(rocksdb::Slice *input, uint32_t *value);