This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Simplify strncat.
- From: "Wilco Dijkstra" <wdijkstr at arm dot com>
- To: 'OndÅej BÃlka' <neleai at seznam dot cz>, <eggert at cs dot ucla dot edu>
- Cc: <libc-alpha at sourceware dot org>
- Date: Thu, 18 Dec 2014 14:37:04 -0000
- Subject: Re: [PATCH] Simplify strncat.
- Authentication-results: sourceware.org; auth=none
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;
}
Wilco