Skip to content

Commit

Permalink
[libc] Use the new wide integer to hex string facility in LibcTest.
Browse files Browse the repository at this point in the history
The old code, which has regressed over many cleanups, has been replaced
with the new wide integer to hex string facility.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D150901
  • Loading branch information
Siva Chandra Reddy committed May 18, 2023
1 parent afe78db commit b095aa3
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions libc/test/UnitTest/LibcTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,25 @@ namespace testing {

namespace internal {

// When the value is UInt128 or __uint128_t, show its hexadecimal digits.
// We cannot just use a UInt128 specialization as that resolves to only
// one type, UInt<128> or __uint128_t. We want both overloads as we want to
// be able to unittest UInt<128> on platforms where UInt128 resolves to
// UInt128.
// When the value is UInt128, __uint128_t or wider, show its hexadecimal digits.
template <typename T>
cpp::enable_if_t<cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, cpp::string>
describeValueUInt(T Value) {
cpp::enable_if_t<cpp::is_integral_v<T> && cpp::is_unsigned_v<T> &&
(sizeof(T) > sizeof(uint64_t)),
cpp::string>
describeValue(T Value) {
static_assert(sizeof(T) % 8 == 0, "Unsupported size of UInt");
cpp::string S(sizeof(T) * 2, '0');

constexpr char HEXADECIMALS[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
const size_t Size = S.size();
for (size_t I = 0; I < Size; I += 2, Value >>= 8) {
unsigned char Mod = static_cast<unsigned char>(Value) & 0xFF;
S[Size - I] = HEXADECIMALS[Mod & 0x0F];
S[Size - (I + 1)] = HEXADECIMALS[Mod & 0x0F];
}

return "0x" + S;
char buf[IntegerToString::hex_bufsize<T>()];
IntegerToString::hex(Value, buf, false);
return "0x" + cpp::string(buf, sizeof(buf));
}

// When the value is of integral type, just display it as normal.
// When the value is of a standard integral type, just display it as normal.
template <typename ValType>
cpp::enable_if_t<cpp::is_integral_v<ValType>, cpp::string>
cpp::enable_if_t<cpp::is_integral_v<ValType> &&
sizeof(ValType) <= sizeof(uint64_t),
cpp::string>
describeValue(ValType Value) {
if constexpr (sizeof(ValType) <= sizeof(uint64_t)) {
return cpp::to_string(Value);
} else {
return describeValueUInt(Value);
}
return cpp::to_string(Value);
}

cpp::string describeValue(cpp::string Value) { return Value; }
Expand Down

0 comments on commit b095aa3

Please sign in to comment.