[libc-commits] [libc] [libc] Add inet_ntop (PR #204143)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Jun 25 06:46:12 PDT 2026


================
@@ -61,5 +68,160 @@ cpp::optional<in_addr_t> inet_addr(const char *cp) {
   return Endian::to_big_endian(result);
 }
 
+namespace {
+
+size_t ipv4_num_bytes(cpp::span<const uint8_t> src) {
+  size_t result = 8; // four digits, three dots and '\0'
+  for (unsigned i = 0; i < 4; ++i)
+    result += (src[i] >= 10) + (src[i] >= 100);
+  return result;
+}
+
+size_t ipv4_to_str_unchecked(cpp::span<const uint8_t> src,
+                             cpp::span<char> dst) {
+  size_t pos = 0;
+  for (unsigned i = 0; i < 4; ++i) {
+    uint8_t val = src[i];
+    if (val >= 100) {
+      uint8_t cent = val / 100;
+      val -= cent * 100;
+      uint8_t dec = val / 10;
+      dst[pos++] = '0' + cent;
+      dst[pos++] = '0' + dec;
+      dst[pos++] = '0' + val % 10;
+    } else if (val >= 10) {
+      uint8_t dec = val / 10;
+      dst[pos++] = '0' + dec;
+      dst[pos++] = '0' + val % 10;
+    } else {
+      dst[pos++] = '0' + val;
+    }
+    dst[pos++] = i < 3 ? '.' : '\0';
+  }
+  return pos;
+}
+
+size_t ipv6_to_str_unchecked(const struct in6_addr &src, cpp::span<char> dst) {
+  // Find the longest run of zeroes to compress to "::"
+  struct Run {
+    unsigned start = 0;
+    unsigned len = 0;
+  };
+  Run best, current;
+  for (unsigned i = 0; i < 8; ++i) {
+    uint16_t val = src.s6_addr16[i];
+    if (val == 0) {
+      ++current.len;
+    } else {
+      // In case of ties, the first sequence wins.
+      if (current.len > best.len)
+        best = current;
+      current = {i + 1, 0};
+    }
+  }
+  if (current.len > best.len)
+    best = current;
+
+  bool is_mapped =
+      best.start == 0 &&
+      (best.len == 6 || (best.len == 5 && src.s6_addr16[5] == 0xffff));
+  unsigned num_words = is_mapped ? 6 : 8;
+
+  char *pos = dst.data();
+  auto append_word = [&](unsigned i) {
+    uint16_t word = Endian::from_big_endian(src.s6_addr16[i]);
+    static constexpr char DIGITS[] = "0123456789abcdef";
----------------
labath wrote:

I'm not sure what the TODO is for. Is it that you'd like a version of int_to_b36_char which does not have a huge intermediate representation (and so behaves better during inlining)? The function which has a big "do not optimize me" warning on top of it?
https://github.com/llvm/llvm-project/blob/c834d1acf130b8d98d818b60ab129f780855461f/libc/src/__support/ctype_utils.h#L19-L27

I mean, if that is what you're asking for, then I sort of agree with you. I think this case could be a good argument for a hand-optimized version (well, either that, or for improving the inliner), but I want to double check as that comment is rather strong...

https://github.com/llvm/llvm-project/pull/204143


More information about the libc-commits mailing list