From 2f9ca2a5739a66de2b6aa2ea024e3513c6883d66 Mon Sep 17 00:00:00 2001 From: benibus Date: Sat, 17 Jun 2023 23:21:51 -0400 Subject: [PATCH] Small refactor --- cpp/src/arrow/util/float16.h | 14 +++------- cpp/src/arrow/util/float16_test.cc | 42 ++++++++++++------------------ 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/cpp/src/arrow/util/float16.h b/cpp/src/arrow/util/float16.h index f2db88f3d3ea2..7959a29aa7900 100644 --- a/cpp/src/arrow/util/float16.h +++ b/cpp/src/arrow/util/float16.h @@ -66,23 +66,17 @@ class Float16Base { } void ToLittleEndian(uint8_t* dest) const { - auto value = bit_util::ToLittleEndian(value_); - std::memcpy(dest, &value, sizeof(value)); + Float16Base{bit_util::ToLittleEndian(value_)}.ToBytes(dest); } std::array ToLittleEndian() const { - std::array bytes; - ToLittleEndian(bytes.data()); - return bytes; + return Float16Base{bit_util::ToLittleEndian(value_)}.ToBytes(); } void ToBigEndian(uint8_t* dest) const { - auto value = bit_util::ToBigEndian(value_); - std::memcpy(dest, &value, sizeof(value)); + Float16Base{bit_util::ToBigEndian(value_)}.ToBytes(dest); } std::array ToBigEndian() const { - std::array bytes; - ToBigEndian(bytes.data()); - return bytes; + return Float16Base{bit_util::ToBigEndian(value_)}.ToBytes(); } friend constexpr bool operator==(Float16Base lhs, Float16Base rhs) { diff --git a/cpp/src/arrow/util/float16_test.cc b/cpp/src/arrow/util/float16_test.cc index 4e6bc64d5b6a6..446d89c30a788 100644 --- a/cpp/src/arrow/util/float16_test.cc +++ b/cpp/src/arrow/util/float16_test.cc @@ -95,33 +95,25 @@ template class Float16OperatorTest : public ::testing::Test { public: void TestCompare(const std::vector& test_values) { - // Check all combinations of operands in both directions - for (size_t i = 0; i < test_values.size(); ++i) { - this->TestCompare(test_values, static_cast(i)); - } - } - - void TestCompare(const std::vector& test_values, int offset) { const auto num_values = static_cast(test_values.size()); - ASSERT_TRUE(offset >= 0 && offset < num_values); - int i = 0; - int j = offset; - while (i < num_values) { - ARROW_SCOPED_TRACE(i, ",", j); - - auto a = test_values[i]; - auto b = test_values[j]; - std::pair ret; - - // Results for float16 and float32 should be the same - ret = Operator{}(a, b); - ASSERT_EQ(ret.first, ret.second); - ret = Operator{}(b, a); - ASSERT_EQ(ret.first, ret.second); - - ++i; - j = (j + 1) % num_values; + // Check all combinations of operands in both directions + for (int offset = 0; offset < num_values; ++offset) { + int i = 0; + int j = offset; + while (i < num_values) { + ARROW_SCOPED_TRACE(i, ",", j); + + auto a = test_values[i]; + auto b = test_values[j]; + + // Results for float16 and float32 should be the same + auto ret = Operator{}(a, b); + ASSERT_EQ(ret.first, ret.second); + + ++i; + j = (j + 1) % num_values; + } } } };