Skip to content

Commit 98984a6

Browse files
committed
feat: implement endian conversion utilities
1 parent b7fadf5 commit 98984a6

File tree

3 files changed

+247
-1
lines changed

3 files changed

+247
-1
lines changed

src/iceberg/util/endian.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
25+
/// \file iceberg/util/endian.h
26+
/// \brief Endianness conversion utilities
27+
28+
namespace iceberg {
29+
30+
/// \brief Concept for values that can be written in little-endian format.
31+
template <typename T>
32+
concept EndianConvertible = std::is_arithmetic_v<T> && !std::same_as<T, bool>;
33+
34+
/// \brief Convert a value to little-endian format.
35+
template <EndianConvertible T>
36+
constexpr T ToLittleEndian(T value) {
37+
if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
38+
return value;
39+
} else {
40+
if constexpr (std::is_integral_v<T>) {
41+
return std::byteswap(value);
42+
} else if constexpr (std::is_floating_point_v<T>) {
43+
// For floats, use the bit_cast -> byteswap -> bit_cast pattern.
44+
if constexpr (sizeof(T) == sizeof(uint32_t)) {
45+
uint32_t int_representation = std::bit_cast<uint32_t>(value);
46+
int_representation = std::byteswap(int_representation);
47+
return std::bit_cast<T>(int_representation);
48+
} else if constexpr (sizeof(T) == sizeof(uint64_t)) {
49+
uint64_t int_representation = std::bit_cast<uint64_t>(value);
50+
int_representation = std::byteswap(int_representation);
51+
return std::bit_cast<T>(int_representation);
52+
}
53+
}
54+
}
55+
}
56+
57+
/// \brief Convert a value from little-endian format.
58+
template <EndianConvertible T>
59+
constexpr T FromLittleEndian(T value) {
60+
if constexpr (std::endian::native == std::endian::little || sizeof(T) <= 1) {
61+
return value;
62+
} else {
63+
if constexpr (std::is_integral_v<T>) {
64+
return std::byteswap(value);
65+
} else if constexpr (std::is_floating_point_v<T>) {
66+
// For floats, use the bit_cast -> byteswap -> bit_cast pattern.
67+
if constexpr (sizeof(T) == sizeof(uint32_t)) {
68+
uint32_t int_representation = std::bit_cast<uint32_t>(value);
69+
int_representation = std::byteswap(int_representation);
70+
return std::bit_cast<T>(int_representation);
71+
} else if constexpr (sizeof(T) == sizeof(uint64_t)) {
72+
uint64_t int_representation = std::bit_cast<uint64_t>(value);
73+
int_representation = std::byteswap(int_representation);
74+
return std::bit_cast<T>(int_representation);
75+
}
76+
}
77+
}
78+
}
79+
80+
/// \brief Convert a value to big-endian format.
81+
template <EndianConvertible T>
82+
constexpr T ToBigEndian(T value) {
83+
if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) {
84+
return value;
85+
} else {
86+
if constexpr (std::is_integral_v<T>) {
87+
return std::byteswap(value);
88+
} else if constexpr (std::is_floating_point_v<T>) {
89+
if constexpr (sizeof(T) == sizeof(uint32_t)) {
90+
uint32_t int_representation = std::bit_cast<uint32_t>(value);
91+
int_representation = std::byteswap(int_representation);
92+
return std::bit_cast<T>(int_representation);
93+
} else if constexpr (sizeof(T) == sizeof(uint64_t)) {
94+
uint64_t int_representation = std::bit_cast<uint64_t>(value);
95+
int_representation = std::byteswap(int_representation);
96+
return std::bit_cast<T>(int_representation);
97+
}
98+
}
99+
}
100+
}
101+
102+
/// \brief Convert a value from big-endian format.
103+
template <EndianConvertible T>
104+
constexpr T FromBigEndian(T value) {
105+
if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) {
106+
return value;
107+
} else {
108+
if constexpr (std::is_integral_v<T>) {
109+
return std::byteswap(value);
110+
} else if constexpr (std::is_floating_point_v<T>) {
111+
if constexpr (sizeof(T) == sizeof(uint32_t)) {
112+
uint32_t int_representation = std::bit_cast<uint32_t>(value);
113+
int_representation = std::byteswap(int_representation);
114+
return std::bit_cast<T>(int_representation);
115+
} else if constexpr (sizeof(T) == sizeof(uint64_t)) {
116+
uint64_t int_representation = std::bit_cast<uint64_t>(value);
117+
int_representation = std::byteswap(int_representation);
118+
return std::bit_cast<T>(int_representation);
119+
}
120+
}
121+
}
122+
}
123+
124+
} // namespace iceberg

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ add_iceberg_test(util_test
8989
formatter_test.cc
9090
config_test.cc
9191
visit_type_test.cc
92-
string_utils_test.cc)
92+
string_utils_test.cc
93+
endian_test.cc)
9394

9495
if(ICEBERG_BUILD_BUNDLE)
9596
add_iceberg_test(avro_test

test/endian_test.cc

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
// test round trip preserves value
31+
TEST(EndianTest, RoundTripPreservesValue) {
32+
EXPECT_EQ(FromLittleEndian(ToLittleEndian<uint16_t>(0x1234)), 0x1234);
33+
EXPECT_EQ(FromBigEndian(ToBigEndian<uint32_t>(0xDEADBEEF)), 0xDEADBEEF);
34+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<uint64_t>::max())),
35+
std::numeric_limits<uint64_t>::max());
36+
EXPECT_EQ(FromBigEndian(ToBigEndian<uint32_t>(0)), 0);
37+
38+
EXPECT_EQ(FromBigEndian(ToBigEndian<int16_t>(-1)), -1);
39+
EXPECT_EQ(FromLittleEndian(ToLittleEndian<int32_t>(-0x12345678)), -0x12345678);
40+
EXPECT_EQ(FromBigEndian(ToBigEndian(std::numeric_limits<int64_t>::min())),
41+
std::numeric_limits<int64_t>::min());
42+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<int16_t>::max())),
43+
std::numeric_limits<int16_t>::max());
44+
45+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(3.14f)), 3.14f);
46+
EXPECT_EQ(FromBigEndian(ToBigEndian(2.718281828459045)), 2.718281828459045);
47+
48+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(std::numeric_limits<float>::infinity())),
49+
std::numeric_limits<float>::infinity());
50+
EXPECT_EQ(FromBigEndian(ToBigEndian(-std::numeric_limits<float>::infinity())),
51+
-std::numeric_limits<float>::infinity());
52+
EXPECT_TRUE(std::isnan(
53+
FromLittleEndian(ToLittleEndian(std::numeric_limits<float>::quiet_NaN()))));
54+
EXPECT_EQ(FromBigEndian(ToBigEndian(0.0f)), 0.0f);
55+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(-0.0f)), -0.0f);
56+
57+
EXPECT_EQ(FromBigEndian(ToBigEndian(std::numeric_limits<double>::infinity())),
58+
std::numeric_limits<double>::infinity());
59+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(-std::numeric_limits<double>::infinity())),
60+
-std::numeric_limits<double>::infinity());
61+
EXPECT_TRUE(
62+
std::isnan(FromBigEndian(ToBigEndian(std::numeric_limits<double>::quiet_NaN()))));
63+
EXPECT_EQ(FromLittleEndian(ToLittleEndian(0.0)), 0.0);
64+
EXPECT_EQ(FromBigEndian(ToBigEndian(-0.0)), -0.0);
65+
}
66+
67+
// test constexpr evaluation
68+
TEST(EndianTest, ConstexprEvaluation) {
69+
static_assert(FromBigEndian(ToBigEndian<uint16_t>(0x1234)) == 0x1234);
70+
static_assert(FromLittleEndian(ToLittleEndian<uint32_t>(0x12345678)) == 0x12345678);
71+
static_assert(FromBigEndian(ToBigEndian<int64_t>(-1)) == -1);
72+
73+
static_assert(ToBigEndian<uint8_t>(0xFF) == 0xFF);
74+
static_assert(FromLittleEndian<int8_t>(-1) == -1);
75+
76+
static_assert(FromLittleEndian(ToLittleEndian(3.14f)) == 3.14f);
77+
static_assert(FromBigEndian(ToBigEndian(2.718)) == 2.718);
78+
}
79+
80+
// test platform dependent behavior
81+
TEST(EndianTest, PlatformDependentBehavior) {
82+
uint32_t test_value = 0x12345678;
83+
84+
if constexpr (std::endian::native == std::endian::little) {
85+
EXPECT_EQ(ToLittleEndian(test_value), test_value);
86+
EXPECT_EQ(FromLittleEndian(test_value), test_value);
87+
EXPECT_NE(ToBigEndian(test_value), test_value);
88+
} else if constexpr (std::endian::native == std::endian::big) {
89+
EXPECT_EQ(ToBigEndian(test_value), test_value);
90+
EXPECT_EQ(FromBigEndian(test_value), test_value);
91+
EXPECT_NE(ToLittleEndian(test_value), test_value);
92+
}
93+
94+
EXPECT_EQ(ToLittleEndian<uint8_t>(0xAB), 0xAB);
95+
EXPECT_EQ(ToBigEndian<uint8_t>(0xAB), 0xAB);
96+
}
97+
98+
// test specific byte pattern validation
99+
TEST(EndianTest, SpecificBytePatternValidation) {
100+
uint32_t original_int = 0x12345678;
101+
uint32_t little_endian_int = ToLittleEndian(original_int);
102+
uint32_t big_endian_int = ToBigEndian(original_int);
103+
104+
auto little_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_int);
105+
auto big_int_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_int);
106+
107+
EXPECT_EQ(little_int_bytes, (std::array<uint8_t, 4>{0x78, 0x56, 0x34, 0x12}));
108+
EXPECT_EQ(big_int_bytes, (std::array<uint8_t, 4>{0x12, 0x34, 0x56, 0x78}));
109+
110+
float original_float = 3.14f;
111+
float little_endian_float = ToLittleEndian(original_float);
112+
float big_endian_float = ToBigEndian(original_float);
113+
114+
auto little_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_float);
115+
auto big_float_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_float);
116+
117+
EXPECT_EQ(little_float_bytes, (std::array<uint8_t, 4>{0xC3, 0xF5, 0x48, 0x40}));
118+
EXPECT_EQ(big_float_bytes, (std::array<uint8_t, 4>{0x40, 0x48, 0xF5, 0xC3}));
119+
}
120+
121+
} // namespace iceberg

0 commit comments

Comments
 (0)