Skip to content

Commit 7595047

Browse files
authored
feat: implement endian conversion utilities (#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.
1 parent 7a4ef57 commit 7595047

File tree

3 files changed

+187
-3
lines changed

3 files changed

+187
-3
lines changed

src/iceberg/util/endian.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <bit>
23+
#include <concepts>
24+
#include <cstdint>
25+
26+
/// \file iceberg/util/endian.h
27+
/// \brief Endianness conversion utilities
28+
29+
namespace iceberg {
30+
31+
/// \brief Concept for values that can be converted to/from another endian format.
32+
template <typename T>
33+
concept EndianConvertible = std::is_arithmetic_v<T>;
34+
35+
/// \brief Byte-swap a value. For floating-point types, only support 32-bit and 64-bit
36+
/// floats.
37+
template <EndianConvertible T>
38+
constexpr T ByteSwap(T value) {
39+
if constexpr (sizeof(T) <= 1) {
40+
return value;
41+
} else if constexpr (std::is_integral_v<T>) {
42+
return std::byteswap(value);
43+
} else if constexpr (std::is_floating_point_v<T>) {
44+
if constexpr (sizeof(T) == sizeof(uint16_t)) {
45+
return std::bit_cast<T>(std::byteswap(std::bit_cast<uint16_t>(value)));
46+
} else if constexpr (sizeof(T) == sizeof(uint32_t)) {
47+
return std::bit_cast<T>(std::byteswap(std::bit_cast<uint32_t>(value)));
48+
} else if constexpr (sizeof(T) == sizeof(uint64_t)) {
49+
return std::bit_cast<T>(std::byteswap(std::bit_cast<uint64_t>(value)));
50+
} else {
51+
static_assert(false, "Unsupported floating-point size for endian conversion.");
52+
}
53+
}
54+
}
55+
56+
/// \brief Convert a value to little-endian format.
57+
template <EndianConvertible T>
58+
constexpr T ToLittleEndian(T value) {
59+
if constexpr (std::endian::native == std::endian::little) {
60+
return value;
61+
} else {
62+
return ByteSwap(value);
63+
}
64+
}
65+
66+
/// \brief Convert a value from little-endian format.
67+
template <EndianConvertible T>
68+
constexpr T FromLittleEndian(T value) {
69+
if constexpr (std::endian::native == std::endian::little) {
70+
return value;
71+
} else {
72+
return ByteSwap(value);
73+
}
74+
}
75+
76+
/// \brief Convert a value to big-endian format.
77+
template <EndianConvertible T>
78+
constexpr T ToBigEndian(T value) {
79+
if constexpr (std::endian::native == std::endian::big) {
80+
return value;
81+
} else {
82+
return ByteSwap(value);
83+
}
84+
}
85+
86+
/// \brief Convert a value from big-endian format.
87+
template <EndianConvertible T>
88+
constexpr T FromBigEndian(T value) {
89+
if constexpr (std::endian::native == std::endian::big) {
90+
return value;
91+
} else {
92+
return ByteSwap(value);
93+
}
94+
}
95+
96+
} // namespace iceberg

test/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ add_iceberg_test(json_serde_test
8686

8787
add_iceberg_test(util_test
8888
SOURCES
89-
formatter_test.cc
9089
config_test.cc
91-
visit_type_test.cc
92-
string_util_test.cc)
90+
endian_test.cc
91+
formatter_test.cc
92+
string_util_test.cc
93+
visit_type_test.cc)
9394

9495
if(ICEBERG_BUILD_BUNDLE)
9596
add_iceberg_test(avro_test

test/endian_test.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "iceberg/util/endian.h"
21+
22+
#include <array>
23+
#include <cmath>
24+
#include <limits>
25+
26+
#include <gtest/gtest.h>
27+
28+
namespace iceberg {
29+
30+
#define EXPECT_ROUNDTRIP(value) \
31+
do { \
32+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(value)), value); \
33+
EXPECT_EQ(FromBigEndian(ToBigEndian(value)), value); \
34+
} while (false)
35+
36+
TEST(EndianTest, RoundTripPreservesValue) {
37+
EXPECT_ROUNDTRIP(static_cast<uint16_t>(0x1234));
38+
EXPECT_ROUNDTRIP(static_cast<uint32_t>(0xDEADBEEF));
39+
EXPECT_ROUNDTRIP(std::numeric_limits<uint64_t>::max());
40+
EXPECT_ROUNDTRIP(static_cast<uint32_t>(0));
41+
42+
EXPECT_ROUNDTRIP(static_cast<int16_t>(-1));
43+
EXPECT_ROUNDTRIP(static_cast<int32_t>(-0x12345678));
44+
EXPECT_ROUNDTRIP(std::numeric_limits<int64_t>::min());
45+
EXPECT_ROUNDTRIP(std::numeric_limits<int16_t>::max());
46+
47+
EXPECT_ROUNDTRIP(3.14f);
48+
EXPECT_ROUNDTRIP(2.718281828459045);
49+
EXPECT_ROUNDTRIP(0.0f);
50+
EXPECT_ROUNDTRIP(-0.0f);
51+
EXPECT_ROUNDTRIP(0.0);
52+
EXPECT_ROUNDTRIP(-0.0);
53+
54+
EXPECT_ROUNDTRIP(std::numeric_limits<float>::infinity());
55+
EXPECT_ROUNDTRIP(-std::numeric_limits<float>::infinity());
56+
EXPECT_ROUNDTRIP(std::numeric_limits<double>::infinity());
57+
EXPECT_ROUNDTRIP(-std::numeric_limits<double>::infinity());
58+
59+
EXPECT_TRUE(std::isnan(
60+
FromLittleEndian(ToLittleEndian(std::numeric_limits<float>::quiet_NaN()))));
61+
EXPECT_TRUE(
62+
std::isnan(FromBigEndian(ToBigEndian(std::numeric_limits<double>::quiet_NaN()))));
63+
}
64+
65+
TEST(EndianTest, ByteWiseValidation) {
66+
uint32_t original_int = 0x12345678;
67+
uint32_t little_endian_int = ToLittleEndian(original_int);
68+
uint32_t big_endian_int = ToBigEndian(original_int);
69+
70+
auto little_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_int);
71+
auto big_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_int);
72+
73+
EXPECT_EQ(little_int_bytes, (std::array<uint8_t, 4>{0x78, 0x56, 0x34, 0x12}));
74+
EXPECT_EQ(big_int_bytes, (std::array<uint8_t, 4>{0x12, 0x34, 0x56, 0x78}));
75+
76+
float original_float = 3.14f;
77+
float little_endian_float = ToLittleEndian(original_float);
78+
float big_endian_float = ToBigEndian(original_float);
79+
80+
auto little_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_float);
81+
auto big_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_float);
82+
83+
EXPECT_EQ(little_float_bytes, (std::array<uint8_t, 4>{0xC3, 0xF5, 0x48, 0x40}));
84+
EXPECT_EQ(big_float_bytes, (std::array<uint8_t, 4>{0x40, 0x48, 0xF5, 0xC3}));
85+
}
86+
87+
} // namespace iceberg

0 commit comments

Comments
 (0)