[libc-commits] [libc] b095aa3 - [libc] Use the new wide integer to hex string facility in LibcTest.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Thu May 18 13:48:40 PDT 2023


Author: Siva Chandra Reddy
Date: 2023-05-18T20:48:21Z
New Revision: b095aa3f9a99bb69818532d41989eaeb6bc883ac

URL: https://github.com/llvm/llvm-project/commit/b095aa3f9a99bb69818532d41989eaeb6bc883ac
DIFF: https://github.com/llvm/llvm-project/commit/b095aa3f9a99bb69818532d41989eaeb6bc883ac.diff

LOG: [libc] Use the new wide integer to hex string facility in LibcTest.

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

Added: 
    

Modified: 
    libc/test/UnitTest/LibcTest.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/UnitTest/LibcTest.cpp b/libc/test/UnitTest/LibcTest.cpp
index ac67b997a9f2..edf543b33b16 100644
--- a/libc/test/UnitTest/LibcTest.cpp
+++ b/libc/test/UnitTest/LibcTest.cpp
@@ -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; }


        


More information about the libc-commits mailing list