You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "HeartLinked (via GitHub)" <gi...@apache.org> on 2025/08/27 02:49:47 UTC
[PR] feat: implement endian conversion utilities [iceberg-cpp]
HeartLinked opened a new pull request, #196:
URL: https://github.com/apache/iceberg-cpp/pull/196
Implements cross-platform endianness conversion utilities in `src/iceberg/util/endian.h` with full support for integer and floating-point types, including comprehensive test coverage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2313153652
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
Review Comment:
I'll delete this case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2304689587
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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>;
Review Comment:
> What about char? There is a `sizeof(T) <= 1` branch, so maybe treat bool the same.
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2313145340
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
+ 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) {
+ 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) {
+ 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}));
+
+ 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}));
Review Comment:
A macro argument containing commas must be wrapped in extra parentheses to prevent the preprocessor from misinterpreting it as multiple separate arguments.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2303302487
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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>;
Review Comment:
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 .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#issuecomment-3241380249
@wgtmac I've updated the code to address the feedback from the review. Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2303033173
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
+ uint64_t int_representation = std::bit_cast<uint64_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ }
+ }
+ }
Review Comment:
You are right, I've added static_assert to provide clearer compile-error information.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2302931867
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
+ uint64_t int_representation = std::bit_cast<uint64_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ }
+ }
+ }
Review Comment:
I don't understand your concern. This is a template method that only accepts types where std::is_arithmetic_v && !std::same_as<T, bool> evaluates to true. Given these constraints, I believe failure is impossible for such types.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2302944137
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
+ uint64_t int_representation = std::bit_cast<uint64_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ }
+ }
+ }
Review Comment:
Since we have C++23, so, maybe: https://en.cppreference.com/w/cpp/types/floating-point.html
:-)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2303021854
##########
test/CMakeLists.txt:
##########
@@ -89,7 +89,8 @@ add_iceberg_test(util_test
formatter_test.cc
config_test.cc
visit_type_test.cc
- string_utils_test.cc)
+ string_utils_test.cc
+ endian_test.cc)
Review Comment:
Please sort them alphabetically.
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
Review Comment:
There four functions are essentially same. Could we write our own `ByteSwap` function and reuse it by those functions?
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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>;
Review Comment:
Why excluding `bool`?
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
Review Comment:
```suggestion
} else if constexpr (std::is_integral_v<T>) {
```
Can we unnest the else branch?
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ // We only support 32-bit and 64-bit floats.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
+ uint64_t int_representation = std::bit_cast<uint64_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else {
+ static_assert(false, "Unsupported floating-point size for endian conversion.");
+ }
+ }
+ }
+}
+
+/// \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 {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ // We only support 32-bit and 64-bit floats.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
Review Comment:
```suggestion
return std::bit_cast<T>(std::byteswap(std::bit_cast<uint32_t>(value)));
```
We can use one-liner here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko merged PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "HeartLinked (via GitHub)" <gi...@apache.org>.
HeartLinked commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2313189879
##########
src/iceberg/util/endian.h:
##########
@@ -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) {
+ 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)) {
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2302904642
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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 Convert a value to little-endian format.
+template <EndianConvertible T>
+constexpr T ToLittleEndian(T value) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ if constexpr (std::is_integral_v<T>) {
+ return std::byteswap(value);
+ } else if constexpr (std::is_floating_point_v<T>) {
+ // For floats, use the bit_cast -> byteswap -> bit_cast pattern.
+ if constexpr (sizeof(T) == sizeof(uint32_t)) {
+ uint32_t int_representation = std::bit_cast<uint32_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
+ uint64_t int_representation = std::bit_cast<uint64_t>(value);
+ int_representation = std::byteswap(int_representation);
+ return std::bit_cast<T>(int_representation);
+ }
+ }
+ }
Review Comment:
A un-matched should compile-error rather than do-nothing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2304101907
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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>;
Review Comment:
What about char? There is a `sizeof(T) <= 1` branch, so maybe treat bool the same.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: implement endian conversion utilities [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #196:
URL: https://github.com/apache/iceberg-cpp/pull/196#discussion_r2306077067
##########
test/endian_test.cc:
##########
@@ -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
Review Comment:
```suggestion
```
The comment is redundant.
##########
src/iceberg/util/endian.h:
##########
@@ -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) {
+ 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)) {
Review Comment:
Should we add `if constexpr (sizeof(T) == sizeof(uint16_t))` to be future-proof?
There are std::float16_t and std::bfloat16_t.
##########
src/iceberg/util/endian.h:
##########
@@ -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) {
Review Comment:
It seems that you have already supported bool type here.
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
+ 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) {
Review Comment:
I don't think we need this case.
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
Review Comment:
There are a lot of duplicate lines here. Can we use a simple macro for this?
```
#define EXPECT_ROUNDTRIP(T value) \
EXPECT_EQ(FromLittleEndian(ToLittleEndian(value)), value); \
EXPECT_EQ(FromBigEndian(ToBigEndian(value)), value);
```
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
+ 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) {
+ 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) {
+ 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}));
+
+ 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}));
Review Comment:
```suggestion
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});
```
##########
src/iceberg/util/endian.h:
##########
@@ -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) {
+ 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)) {
+ 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) {
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
+ return value;
+ } else {
+ return ByteSwap(value);
+ }
Review Comment:
```suggestion
if constexpr (std::endian::native == std::endian::little) {
return value;
} else {
return ByteSwap(value);
}
```
Doesn't it look better?
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
+ 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) {
+ 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) {
Review Comment:
```suggestion
TEST(EndianTest, ByteWiseValidation) {
```
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
Review Comment:
Is there any difference with the above case?
##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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>
+
+/// \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>;
Review Comment:
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?
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
Review Comment:
Or we can use template function to leverage parameterized test to improve readability.
##########
test/endian_test.cc:
##########
@@ -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
+TEST(EndianTest, RoundTripPreservesValue) {
+ EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
+ 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) {
+ 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) {
+ 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) {
+ 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}));
Review Comment:
```suggestion
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});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org