Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benibus committed Jun 18, 2023
1 parent 899ac72 commit 2f9ca2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
14 changes: 4 additions & 10 deletions cpp/src/arrow/util/float16.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t, 2> ToLittleEndian() const {
std::array<uint8_t, 2> 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<uint8_t, 2> ToBigEndian() const {
std::array<uint8_t, 2> bytes;
ToBigEndian(bytes.data());
return bytes;
return Float16Base{bit_util::ToBigEndian(value_)}.ToBytes();
}

friend constexpr bool operator==(Float16Base lhs, Float16Base rhs) {
Expand Down
42 changes: 17 additions & 25 deletions cpp/src/arrow/util/float16_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,25 @@ template <typename Operator>
class Float16OperatorTest : public ::testing::Test {
public:
void TestCompare(const std::vector<TestValue>& 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<int>(i));
}
}

void TestCompare(const std::vector<TestValue>& test_values, int offset) {
const auto num_values = static_cast<int>(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<bool, bool> 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;
}
}
}
};
Expand Down

0 comments on commit 2f9ca2a

Please sign in to comment.