From: Paul Eggert <eggert@cs.ucla.edu>
To: Ralf Jung <post@ralfj.de>,
Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>,
libc-alpha <libc-alpha@sourceware.org>
Subject: Re: Support for memcpy with equal source and destination
Date: Sat, 25 Nov 2023 09:11:03 -0800 [thread overview]
Message-ID: <9e6eb1ab-9a9d-4b69-ae49-4805ee7cdce8@cs.ucla.edu> (raw)
In-Reply-To: <ec4d8f53-da73-42a6-afc6-95406dc903d3@ralfj.de>
On 2023-11-25 00:20, Ralf Jung wrote:
> For a memcpy that is implemented in C (rather than assembly), I don't
> think it is possible to make this promise (of supporting src==dest) when
> there is "restrict" in the signature.
Actually it is possible, though it costs a conditional branch compared
to the naive approach that generally would work anyway.
Here's a sample implementation. When dest == src this function doesn't
dereference either pointer, and this satisfies the C standard's rules
for 'restrict'.
#include <string.h>
void *
memcpy (void *restrict dest, void const *restrict src, size_t n)
{
if (dest != src)
{
char *d = dest;
char const *s = src;
for (; n != 0; n--)
*d++ = *s++;
}
return dest;
}
When n is zero, this implementation also supports NULL dest or src,
though that's a separate issue.
> when one sees restrict in a signature, it is impossible to tell what the actual constraint is without further documentation: the function needs to say which memory is being accesses through which pointer, and *that* is then where the disjointness comes from.
True, and one could document the new guarantee along those lines.
Writing the documentation could be a bit tricky, though, as one needs to
explain all this stuff clearly without being too prolix.
next prev parent reply other threads:[~2023-11-25 17:11 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-23 12:14 Adhemerval Zanella Netto
2023-11-25 7:48 ` Paul Eggert
2023-11-25 8:20 ` Ralf Jung
2023-11-25 17:11 ` Paul Eggert [this message]
2023-11-27 11:15 ` Ralf Jung
2023-11-27 11:46 ` Alexander Monakov
2023-11-27 12:34 ` Ralf Jung
2023-11-27 14:25 ` Alexander Monakov
2023-11-27 14:38 Wilco Dijkstra
2023-11-27 14:45 ` Ralf Jung
2023-11-27 14:53 ` Zack Weinberg
2023-11-27 15:02 ` enh
2023-11-27 19:28 Aaron Peter Bachmann
2023-11-27 19:39 ` Paul Eggert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9e6eb1ab-9a9d-4b69-ae49-4805ee7cdce8@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
--cc=post@ralfj.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).