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

FEXCore/vl64: Fixes int16 encoding #4338

Merged
merged 2 commits into from
Feb 8, 2025
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
25 changes: 19 additions & 6 deletions FEXCore/Source/Utils/variable_length_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ struct vl64 final {

if (vl8_type->Type == vl8_type_header) {
return {vl8_type->Integer, sizeof(vl8_enc)};
} else if (vl16_type->Type == vl16_type_header) {
return {vl16_type->Integer, sizeof(vl16_enc)};
} else if (vl16_type->HighBits.Type == vl16_type_header) {
return {vl16_type->Integer(), sizeof(vl16_enc)};
} else if (vl32_type->Type == vl32_type_header) {
return {vl32_type->Integer, sizeof(vl32_enc)};
}
Expand All @@ -77,8 +77,11 @@ struct vl64 final {
return sizeof(vl8_enc);
} else if (Data >= vl16_min && Data <= vl16_max) {
*vl16_type = {
.Integer = static_cast<int16_t>(Data),
.Type = vl16_type_header,
.HighBits {
.Top = static_cast<int8_t>((Data >> 8) & 0xFF),
.Type = vl16_type_header,
},
.LowBits = static_cast<uint8_t>(Data & 0xFF),
};
return sizeof(vl16_enc);
} else if (Data >= vl32_min && Data <= vl32_max) {
Expand All @@ -105,8 +108,18 @@ struct vl64 final {
static_assert(sizeof(vl8_enc) == 1);

struct vl16_enc {
int16_t Integer : 14;
uint16_t Type : 2;
struct {
int8_t Top : 6;
uint8_t Type : 2;
} HighBits;
uint8_t LowBits;

int64_t Integer() const {
int16_t Value {};
Value |= (HighBits.Top << 8);
Value |= LowBits;
return (Value << 2) >> 2;
}
};
static_assert(sizeof(vl16_enc) == 2);

Expand Down
79 changes: 65 additions & 14 deletions FEXCore/unittests/APITests/vl_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,91 +24,142 @@ TEST_CASE("vl-size") {
CHECK(FEXCore::Utils::vl64::EncodedSize(std::numeric_limits<int64_t>::max()) == 9);
}

TEST_CASE("vl8 - in memory - encode") {
TEST_CASE("vl8 - in memory - encode/decode") {
uint8_t data[1];
REQUIRE(FEXCore::Utils::vl64::Encode(data, 0) == 1);
CHECK(data[0] == 0);
auto Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 1);
CHECK(Dec.Integer == 0);

REQUIRE(FEXCore::Utils::vl64::Encode(data, 63) == 1);
CHECK(data[0] == 0b0011'1111);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 1);
CHECK(Dec.Integer == 63);

REQUIRE(FEXCore::Utils::vl64::Encode(data, -1) == 1);
CHECK(data[0] == 0b0111'1111);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 1);
CHECK(Dec.Integer == -1);

REQUIRE(FEXCore::Utils::vl64::Encode(data, -64) == 1);
CHECK(data[0] == 0b0100'0000);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 1);
CHECK(Dec.Integer == -64);
}

TEST_CASE("vl16 - in memory - encode") {
TEST_CASE("vl16 - in memory - encode/decode") {
uint8_t data[2];

REQUIRE(FEXCore::Utils::vl64::Encode(data, -65) == 2);
CHECK(data[0] == 0b1011'1111);
CHECK(data[1] == 0b1011'1111);
CHECK((uint64_t)data[0] == 0b1011'1111);
CHECK((uint64_t)data[1] == 0b1011'1111);
auto Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 2);
CHECK(Dec.Integer == -65);

REQUIRE(FEXCore::Utils::vl64::Encode(data, -66) == 2);
CHECK(data[0] == 0b1011'1110);
CHECK(data[1] == 0b1011'1111);
CHECK((uint64_t)data[0] == 0b1011'1111);
CHECK((uint64_t)data[1] == 0b1011'1110);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 2);
CHECK(Dec.Integer == -66);

REQUIRE(FEXCore::Utils::vl64::Encode(data, 64) == 2);
CHECK(data[0] == 0b0100'0000);
CHECK(data[1] == 0b1000'0000);
CHECK((uint64_t)data[0] == 0b1000'0000);
CHECK((uint64_t)data[1] == 0b0100'0000);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 2);
CHECK(Dec.Integer == 64);

REQUIRE(FEXCore::Utils::vl64::Encode(data, 8191) == 2);
CHECK(data[0] == 0b1111'1111);
CHECK(data[1] == 0b1001'1111);
CHECK((uint64_t)data[0] == 0b1001'1111);
CHECK((uint64_t)data[1] == 0b1111'1111);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 2);
CHECK(Dec.Integer == 8191);

REQUIRE(FEXCore::Utils::vl64::Encode(data, -8192) == 2);
CHECK(data[0] == 0b0000'0000);
CHECK(data[1] == 0b1010'0000);
CHECK((uint64_t)data[0] == 0b1010'0000);
CHECK((uint64_t)data[1] == 0b0000'0000);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 2);
CHECK(Dec.Integer == -8192);
}

TEST_CASE("vl32 - in memory - encode") {
TEST_CASE("vl32 - in memory - encode/decode") {
uint8_t data[5];
int32_t result {};

REQUIRE(FEXCore::Utils::vl64::Encode(data, 8192) == 5);
CHECK(data[0] == 0b1100'0000);
memcpy(&result, &data[1], sizeof(int32_t));
CHECK(result == 8192);
auto Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 5);
CHECK(Dec.Integer == 8192);

REQUIRE(FEXCore::Utils::vl64::Encode(data, -8193) == 5);
CHECK(data[0] == 0b1100'0000);
memcpy(&result, &data[1], sizeof(int32_t));
CHECK(result == -8193);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 5);
CHECK(Dec.Integer == -8193);

REQUIRE(FEXCore::Utils::vl64::Encode(data, std::numeric_limits<int32_t>::min()) == 5);
CHECK(data[0] == 0b1100'0000);
memcpy(&result, &data[1], sizeof(int32_t));
CHECK(result == std::numeric_limits<int32_t>::min());
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 5);
CHECK(Dec.Integer == std::numeric_limits<int32_t>::min());

REQUIRE(FEXCore::Utils::vl64::Encode(data, std::numeric_limits<int32_t>::max()) == 5);
CHECK(data[0] == 0b1100'0000);
memcpy(&result, &data[1], sizeof(int32_t));
CHECK(result == std::numeric_limits<int32_t>::max());
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 5);
CHECK(Dec.Integer == std::numeric_limits<int32_t>::max());
}

TEST_CASE("vl64 - in memory - encode") {
TEST_CASE("vl64 - in memory - encode/decode") {
uint8_t data[9];
int64_t result {};

REQUIRE(FEXCore::Utils::vl64::Encode(data, static_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1) == 9);
CHECK(data[0] == 0b1110'0000);
memcpy(&result, &data[1], sizeof(int64_t));
CHECK(result == static_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1);
auto Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 9);
CHECK(Dec.Integer == static_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1);

REQUIRE(FEXCore::Utils::vl64::Encode(data, static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1) == 9);
CHECK(data[0] == 0b1110'0000);
memcpy(&result, &data[1], sizeof(int64_t));
CHECK(result == static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1);
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 9);
CHECK(Dec.Integer == static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1);

REQUIRE(FEXCore::Utils::vl64::Encode(data, std::numeric_limits<int64_t>::min()) == 9);
CHECK(data[0] == 0b1110'0000);
memcpy(&result, &data[1], sizeof(int64_t));
CHECK(result == std::numeric_limits<int64_t>::min());
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 9);
CHECK(Dec.Integer == std::numeric_limits<int64_t>::min());

REQUIRE(FEXCore::Utils::vl64::Encode(data, std::numeric_limits<int64_t>::max()) == 9);
CHECK(data[0] == 0b1110'0000);
memcpy(&result, &data[1], sizeof(int64_t));
CHECK(result == std::numeric_limits<int64_t>::max());
Dec = FEXCore::Utils::vl64::Decode(data);
CHECK(Dec.Size == 9);
CHECK(Dec.Integer == std::numeric_limits<int64_t>::max());
}
Loading