Skip to content

Commit 68b7ed4

Browse files
committed
feat: implement endian conversion utilities
1 parent 8369803 commit 68b7ed4

File tree

3 files changed

+186
-18
lines changed

3 files changed

+186
-18
lines changed

src/iceberg/util/endian.h

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#pragma once
2121

22-
#include <array>
2322
#include <bit>
2423
#include <concepts>
2524

@@ -30,46 +29,95 @@ namespace iceberg {
3029

3130
/// \brief Concept for values that can be written in little-endian format.
3231
template <typename T>
33-
concept EndianConvertible = std::is_arithmetic_v<T>;
34-
35-
/// \brief Concept for values that can be written in big-endian format,
36-
template <typename T>
37-
concept BigEndianWritable = std::same_as<T, std::array<uint8_t, 16>>;
32+
concept EndianConvertible = std::is_arithmetic_v<T> && !std::same_as<T, bool>;
3833

3934
/// \brief Convert a value to little-endian format.
4035
template <EndianConvertible T>
41-
T ToLittleEndian(T value) {
42-
if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
43-
return std::byteswap(value);
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+
}
4454
}
45-
return value;
4655
}
4756

4857
/// \brief Convert a value from little-endian format.
4958
template <EndianConvertible T>
50-
T FromLittleEndian(T value) {
51-
if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
52-
return std::byteswap(value);
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+
}
5377
}
54-
return value;
5578
}
5679

80+
/// \brief Convert a value to big-endian format.
5781
template <EndianConvertible T>
5882
constexpr T ToBigEndian(T value) {
5983
if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) {
6084
return value;
6185
} else {
62-
return std::byteswap(value);
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+
}
6399
}
64100
}
65101

66-
/// \brief Convert a value from big-endian format to native.
102+
/// \brief Convert a value from big-endian format.
67103
template <EndianConvertible T>
68104
constexpr T FromBigEndian(T value) {
69105
if constexpr (std::endian::native == std::endian::big || sizeof(T) <= 1) {
70106
return value;
71107
} else {
72-
return std::byteswap(value);
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+
}
73121
}
74122
}
75123

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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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_value = 0x12345678;
101+
102+
uint32_t little_endian_val = ToLittleEndian(original_value);
103+
uint32_t big_endian_val = ToBigEndian(original_value);
104+
105+
auto little_bytes = std::bit_cast<std::array<uint8_t, 4>>(little_endian_val);
106+
auto big_bytes = std::bit_cast<std::array<uint8_t, 4>>(big_endian_val);
107+
108+
EXPECT_EQ(little_bytes[0], 0x78);
109+
EXPECT_EQ(little_bytes[1], 0x56);
110+
EXPECT_EQ(little_bytes[2], 0x34);
111+
EXPECT_EQ(little_bytes[3], 0x12);
112+
113+
EXPECT_EQ(big_bytes[0], 0x12);
114+
EXPECT_EQ(big_bytes[1], 0x34);
115+
EXPECT_EQ(big_bytes[2], 0x56);
116+
EXPECT_EQ(big_bytes[3], 0x78);
117+
}
118+
119+
} // namespace iceberg

0 commit comments

Comments
 (0)