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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]