Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-44767: [C++] Fix Float16.To{Little,Big}Endian on big endian machines #44768

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions cpp/src/arrow/util/float16.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ class ARROW_EXPORT Float16 {
}
/// \brief Return the value's bytes in little-endian byte order
constexpr std::array<uint8_t, 2> ToLittleEndian() const {
#if ARROW_LITTLE_ENDIAN
return {uint8_t(bits_ & 0xff), uint8_t(bits_ >> 8)};
#else
return {uint8_t(bits_ >> 8), uint8_t(bits_ & 0xff)};
#endif
}

/// \brief Copy the value's bytes in big-endian byte order
Expand All @@ -125,11 +121,7 @@ class ARROW_EXPORT Float16 {
}
/// \brief Return the value's bytes in big-endian byte order
constexpr std::array<uint8_t, 2> ToBigEndian() const {
#if ARROW_LITTLE_ENDIAN
return {uint8_t(bits_ >> 8), uint8_t(bits_ & 0xff)};
#else
return {uint8_t(bits_ & 0xff), uint8_t(bits_ >> 8)};
#endif
}

constexpr Float16 operator-() const { return FromBits(bits_ ^ 0x8000); }
Expand Down
47 changes: 28 additions & 19 deletions cpp/src/arrow/util/float16_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,44 +323,53 @@ TEST(Float16Test, Compare) {
TEST(Float16Test, ToBytes) {
constexpr auto f16 = Float16::FromBits(0xd01c);
std::array<uint8_t, 2> bytes;
auto load = [&bytes]() { return SafeLoadAs<uint16_t>(bytes.data()); };

constexpr uint8_t expected_high = 0xd0;
constexpr uint8_t expected_low = 0x1c;

// Test native-endian
f16.ToBytes(bytes.data());
ASSERT_EQ(load(), 0xd01c);
#if ARROW_LITTLE_ENDIAN
ASSERT_EQ(bytes[0], expected_low);
ASSERT_EQ(bytes[1], expected_high);
#else
ASSERT_EQ(bytes[0], expected_high);
ASSERT_EQ(bytes[1], expected_low);
#endif
bytes = f16.ToBytes();
ASSERT_EQ(load(), 0xd01c);

#if ARROW_LITTLE_ENDIAN
constexpr uint16_t expected_le = 0xd01c;
constexpr uint16_t expected_be = 0x1cd0;
ASSERT_EQ(bytes[0], expected_low);
ASSERT_EQ(bytes[1], expected_high);
#else
constexpr uint16_t expected_le = 0x1cd0;
constexpr uint16_t expected_be = 0xd01c;
ASSERT_EQ(bytes[0], expected_high);
ASSERT_EQ(bytes[1], expected_low);
#endif

// Test little-endian
f16.ToLittleEndian(bytes.data());
ASSERT_EQ(load(), expected_le);
ASSERT_EQ(bytes[0], expected_low);
ASSERT_EQ(bytes[1], expected_high);
bytes = f16.ToLittleEndian();
ASSERT_EQ(load(), expected_le);
ASSERT_EQ(bytes[0], expected_low);
ASSERT_EQ(bytes[1], expected_high);
// Test big-endian
f16.ToBigEndian(bytes.data());
ASSERT_EQ(load(), expected_be);
ASSERT_EQ(bytes[0], expected_high);
ASSERT_EQ(bytes[1], expected_low);
bytes = f16.ToBigEndian();
ASSERT_EQ(load(), expected_be);
ASSERT_EQ(bytes[0], expected_high);
ASSERT_EQ(bytes[1], expected_low);
}

TEST(Float16Test, FromBytes) {
constexpr uint16_t u16 = 0xd01c;
const auto* data = reinterpret_cast<const uint8_t*>(&u16);
ASSERT_EQ(Float16::FromBytes(data), Float16::FromBits(0xd01c));
const std::array<uint8_t, 2> bytes = {0x1c, 0xd0};
#if ARROW_LITTLE_ENDIAN
ASSERT_EQ(Float16::FromLittleEndian(data), Float16::FromBits(0xd01c));
ASSERT_EQ(Float16::FromBigEndian(data), Float16::FromBits(0x1cd0));
ASSERT_EQ(Float16::FromBytes(bytes.data()), Float16::FromBits(0xd01c));
#else
ASSERT_EQ(Float16::FromLittleEndian(data), Float16(0x1cd0));
ASSERT_EQ(Float16::FromBigEndian(data), Float16(0xd01c));
ASSERT_EQ(Float16::FromBytes(bytes.data()), Float16::FromBits(0x1cd0));
#endif
ASSERT_EQ(Float16::FromLittleEndian(bytes.data()), Float16::FromBits(0xd01c));
ASSERT_EQ(Float16::FromBigEndian(bytes.data()), Float16::FromBits(0x1cd0));
}

} // namespace
Expand Down
Loading