[PATCH] Simplify strncat.
Carlos O'Donell
carlos@redhat.com
Fri Dec 19 08:22:00 GMT 2014
On 12/18/2014 09:37 AM, Wilco Dijkstra wrote:
> OndÅej BÃlka wrote:
>>> On Tue, Dec 16, 2014 at 12:50:38PM -0800, Paul Eggert wrote:
>>> Thanks, this is much better than worrying about how to pacify GCC.
>>> The code could be made a bit shorter and clearer with mempcpy, and
>>> there's no longer any need to distinguish between s and s1, so I
>>> suggest the following minor rewrite, which shrinks the code size by
>>> another 26 bytes (16%) on my x86-64 platform.
>>
>>> char *
>>> STRNCAT (char *s1, const char *s2, size_t n)
>>> {
>>> char *s1_end = mempcpy (s1 + strlen (s1), s2, __strnlen (s2, n));
>>> *s1_end = '\0';
>>> return s1;
>>> }
>> That looks better (with minor fix s/mempcpy/__mempcpy/), anybody objects
>> using this instead?
>
> I don't think mempcpy is better, few targets define it (ARM, MIPS, AARCH64
> don't for example), so it means an extra call and return. Mempcpy is also
> non-standard and rarely used, so would not be cache resident even if you
> have an optimized implementation.
>
> This has the same advantages as Paul's version (fewer callee-saves):
>
> char *
> STRNCAT (char *s1, const char *s2, size_t n)
> {
> char *s;
> n = strnlen (s2, n);
> s = s1 + strlen (s1);
> s[n] = '\0';
> memcpy (s, s2, n);
> return s1;
> }
Agreed.
Cheers,
Carlos.
More information about the Libc-alpha
mailing list