-
Notifications
You must be signed in to change notification settings - Fork 70
feat: implement endian conversion utilities #196
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,94 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <bit> | ||
| #include <concepts> | ||
| #include <cstdint> | ||
|
|
||
| /// \file iceberg/util/endian.h | ||
| /// \brief Endianness conversion utilities | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief Concept for values that can be converted to/from another endian format. | ||
| template <typename T> | ||
| concept EndianConvertible = std::is_arithmetic_v<T> && !std::same_as<T, bool>; | ||
|
|
||
| /// \brief Byte-swap a value. For floating-point types, only support 32-bit and 64-bit | ||
| /// floats. | ||
| template <EndianConvertible T> | ||
| constexpr T ByteSwap(T value) { | ||
| if constexpr (sizeof(T) <= 1) { | ||
HeartLinked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return value; | ||
| } else if constexpr (std::is_integral_v<T>) { | ||
| return std::byteswap(value); | ||
| } else if constexpr (std::is_floating_point_v<T>) { | ||
| if constexpr (sizeof(T) == sizeof(uint32_t)) { | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return std::bit_cast<T>(std::byteswap(std::bit_cast<uint32_t>(value))); | ||
| } else if constexpr (sizeof(T) == sizeof(uint64_t)) { | ||
| return std::bit_cast<T>(std::byteswap(std::bit_cast<uint64_t>(value))); | ||
| } else { | ||
| static_assert(false, "Unsupported floating-point size for endian conversion."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// \brief Convert a value to little-endian format. | ||
| template <EndianConvertible T> | ||
| constexpr T ToLittleEndian(T value) { | ||
HeartLinked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) { | ||
| return value; | ||
| } else { | ||
| return ByteSwap(value); | ||
| } | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// \brief Convert a value from little-endian format. | ||
| template <EndianConvertible T> | ||
| constexpr T FromLittleEndian(T value) { | ||
| if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) { | ||
| return value; | ||
| } else { | ||
| return ByteSwap(value); | ||
| } | ||
| } | ||
|
|
||
| /// \brief Convert a value to big-endian format. | ||
| template <EndianConvertible T> | ||
| constexpr T ToBigEndian(T value) { | ||
| if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) { | ||
| return value; | ||
| } else { | ||
| return ByteSwap(value); | ||
| } | ||
| } | ||
|
|
||
| /// \brief Convert a value from big-endian format. | ||
| template <EndianConvertible T> | ||
| constexpr T FromBigEndian(T value) { | ||
| if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) { | ||
| return value; | ||
| } else { | ||
| return ByteSwap(value); | ||
| } | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
This file contains hidden or 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 hidden or 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,121 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/util/endian.h" | ||
|
|
||
| #include <array> | ||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| // test round trip preserves value | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| TEST(EndianTest, RoundTripPreservesValue) { | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234); | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| EXPECT_EQ(FromBigEndian(ToBigEndian<uint32_t>(0xDEADBEEF)), 0xDEADBEEF); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<uint64_t>::max())), | ||
| std::numeric_limits<uint64_t>::max()); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian<uint32_t>(0)), 0); | ||
|
|
||
| EXPECT_EQ(FromBigEndian(ToBigEndian<int16_t>(-1)), -1); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian<int32_t>(-0x12345678)), -0x12345678); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian(std::numeric_limits<int64_t>::min())), | ||
| std::numeric_limits<int64_t>::min()); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<int16_t>::max())), | ||
| std::numeric_limits<int16_t>::max()); | ||
|
|
||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(3.14f)), 3.14f); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian(2.718281828459045)), 2.718281828459045); | ||
|
|
||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<float>::infinity())), | ||
| std::numeric_limits<float>::infinity()); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian(-std::numeric_limits<float>::infinity())), | ||
| -std::numeric_limits<float>::infinity()); | ||
| EXPECT_TRUE(std::isnan( | ||
| FromLittleEndian(ToLittleEndian(std::numeric_limits<float>::quiet_NaN())))); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian(0.0f)), 0.0f); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(-0.0f)), -0.0f); | ||
|
|
||
| EXPECT_EQ(FromBigEndian(ToBigEndian(std::numeric_limits<double>::infinity())), | ||
| std::numeric_limits<double>::infinity()); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(-std::numeric_limits<double>::infinity())), | ||
| -std::numeric_limits<double>::infinity()); | ||
| EXPECT_TRUE( | ||
| std::isnan(FromBigEndian(ToBigEndian(std::numeric_limits<double>::quiet_NaN())))); | ||
| EXPECT_EQ(FromLittleEndian(ToLittleEndian(0.0)), 0.0); | ||
| EXPECT_EQ(FromBigEndian(ToBigEndian(-0.0)), -0.0); | ||
| } | ||
|
|
||
| // test constexpr evaluation | ||
| TEST(EndianTest, ConstexprEvaluation) { | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static_assert(FromBigEndian(ToBigEndian<uint16_t>(0x1234)) == 0x1234); | ||
| static_assert(FromLittleEndian(ToLittleEndian<uint32_t>(0x12345678)) == 0x12345678); | ||
| static_assert(FromBigEndian(ToBigEndian<int64_t>(-1)) == -1); | ||
|
|
||
| static_assert(ToBigEndian<uint8_t>(0xFF) == 0xFF); | ||
| static_assert(FromLittleEndian<int8_t>(-1) == -1); | ||
|
|
||
| static_assert(FromLittleEndian(ToLittleEndian(3.14f)) == 3.14f); | ||
| static_assert(FromBigEndian(ToBigEndian(2.71)) == 2.71); | ||
| } | ||
|
|
||
| // test platform dependent behavior | ||
| TEST(EndianTest, PlatformDependentBehavior) { | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint32_t test_value = 0x12345678; | ||
|
|
||
| if constexpr (std::endian::native == std::endian::little) { | ||
| EXPECT_EQ(ToLittleEndian(test_value), test_value); | ||
| EXPECT_EQ(FromLittleEndian(test_value), test_value); | ||
| EXPECT_NE(ToBigEndian(test_value), test_value); | ||
| } else if constexpr (std::endian::native == std::endian::big) { | ||
| EXPECT_EQ(ToBigEndian(test_value), test_value); | ||
| EXPECT_EQ(FromBigEndian(test_value), test_value); | ||
| EXPECT_NE(ToLittleEndian(test_value), test_value); | ||
| } | ||
|
|
||
| EXPECT_EQ(ToLittleEndian<uint8_t>(0xAB), 0xAB); | ||
| EXPECT_EQ(ToBigEndian<uint8_t>(0xAB), 0xAB); | ||
| } | ||
|
|
||
| // test specific byte pattern validation | ||
| TEST(EndianTest, SpecificBytePatternValidation) { | ||
HeartLinked marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint32_t original_int = 0x12345678; | ||
| uint32_t little_endian_int = ToLittleEndian(original_int); | ||
| uint32_t big_endian_int = ToBigEndian(original_int); | ||
|
|
||
| auto little_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_int); | ||
| auto big_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_int); | ||
|
|
||
| EXPECT_EQ(little_int_bytes, (std::array<uint8_t, 4>{0x78, 0x56, 0x34, 0x12})); | ||
| EXPECT_EQ(big_int_bytes, (std::array<uint8_t, 4>{0x12, 0x34, 0x56, 0x78})); | ||
HeartLinked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| float original_float = 3.14f; | ||
| float little_endian_float = ToLittleEndian(original_float); | ||
| float big_endian_float = ToBigEndian(original_float); | ||
|
|
||
| auto little_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_float); | ||
| auto big_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_float); | ||
|
|
||
| EXPECT_EQ(little_float_bytes, (std::array<uint8_t, 4>{0xC3, 0xF5, 0x48, 0x40})); | ||
| EXPECT_EQ(big_float_bytes, (std::array<uint8_t, 4>{0x40, 0x48, 0xF5, 0xC3})); | ||
HeartLinked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace iceberg | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why excluding
bool?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sizeof(bool) is 1 byte, and endianness is irrelevant for single-byte data types; a byte-swap is a no-op.
Also, according to the Iceberg specification, a boolean is represented as 0x00 for false and any non-zero byte for true. This definition is inherently independent of endianness, which is different from multi-byte types like int or double, where the specification explicitly defines the value as being stored in a particular endian format .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about char? There is a
sizeof(T) <= 1branch, so maybe treat bool the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, but I think performing an endian conversion on a bool is semantically less reasonable. The main benefit of the current design is that it explicitly catches this kind of logical error at compile time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't support bool here, users may need to special case bool to avoid passing it to
ByteSwap. For simplicity, should we support bool as well?