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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rGb095aa3f9a99: [libc] Use the new wide integer to hex string facility in LibcTest. (authored by sivachandra).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150901/new/

https://reviews.llvm.org/D150901

Files:
  libc/test/UnitTest/LibcTest.cpp


Index: libc/test/UnitTest/LibcTest.cpp
===================================================================
--- libc/test/UnitTest/LibcTest.cpp
+++ libc/test/UnitTest/LibcTest.cpp
@@ -18,38 +18,25 @@
 
 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; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150901.523542.patch
Type: text/x-patch
Size: 2268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230518/9ef386ee/attachment.bin>


More information about the libc-commits mailing list