[PATCH 3/3] resolv: Optimize inet_ntop

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Wed Jun 4 17:30:44 GMT 2025



On 04/06/25 14:08, Florian Weimer wrote:
> * Adhemerval Zanella Netto:
> 
>>> The words copy shouldn't really be needed, GCC can usually combine two
>>> byte accesses into single 16-bit loads.
>>
>> There is no need for the words array in fact, we can ntohs when
>> accessing the s6_addr16 for source (and on most chips it should be
>> lowered efficiently).
> 
> I believe this introduces an alignment requirement that the inet_ntop
> specification does not actually provide.

Sure, and we can just use a wrapper to handle this correctly:

static inline uint16_t
in6_addr_addr16 (const struct in6_addr *src, int idx)
{
  const struct { uint16_t x; } __attribute__((__packed__)) *pptr =
    (typeof(pptr))(&src->s6_addr16[idx]);
  return ntohs (pptr->x);
}

It uses byte-load on architectures that does not allow unaligned
access (like sparc).  It also generates slight better code than
the usual temporary plus memcpy we do for other uses on sparc
(it does not spill store-byte for the temporary).


More information about the Libc-alpha mailing list