Re: What is this noalias thing Dennis Ritchie is railing about ?
- From: "Charlie Gordon" <news@xxxxxxxxxxx>
- Date: Thu, 13 Sep 2007 11:28:30 +0200
"Richard Bos" <rlb@xxxxxxxxxxxxxxxxxxxxxx> a écrit dans le message de news:
46e7e64e.784836508@xxxxxxxxxxxxxxxxx
Francine.Neary@xxxxxxxxxxxxxx wrote:
On Sep 11, 12:19 am, pete <pfil...@xxxxxxxxxxxxxx> wrote:
That's not what restrict is all about.
Take a look at the C99 prototypes for memcpy and memmove:
void * memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void * s1, const void * s2, size_t n);
"restrict" means that all accesses to the object pointed to by s1,
will be made by pointers which were derived from s1.
In this version of memcpy:
void *memcpy(void *restrict s1, const void *restrict s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;
while (n-- != 0) {
*p1++ = *p2++;
}
return s1;
}
all accesses to the object pointed to by s1,
are made through various values of p1, but p1 is derived from s1.
In memmove, because the objects may overlap,
It's possible that s1 and/or pointers derived from s1,
may point to the same addresses
as pointed to by s2 and/or pointers derived from s2.
Wow, what a pig's breakfast!
Why a pig's breakfast? This allows restrict to specify exactly what was
written in words in the previous Standard: that the two pointers to
memcpy() must not point to overlapping areas, and that those to
memmove() may. This is actually the most useful application of restrict
I've found in the Standard.
This wording is not precise enough: you can pass pointers to the same object
to memcpy, but the areas accessed through these pointers by memcpy should
not overlap.
Using the restrict keyword on the function declaration is just a weak hint
the the programmer about potential constraints on the arguments. It does
not give any information to the compiler.
Using restrict on the function definition allows the compiler to produce
optimized code for instance with fewer memory loads: anything accessed
through a restricted pointer is guaranteed by the programmer to not have
been modified through another pointer within the same scope. This guarantee
is a consequence of documented constraints that callers of the function must
fulfill.
--
Chqrlie.
.
- References:
- What is this noalias thing Dennis Ritchie is railing about ?
- From: Spiros Bousbouras
- Re: What is this noalias thing Dennis Ritchie is railing about ?
- From: Martin Wells
- Re: What is this noalias thing Dennis Ritchie is railing about ?
- From: pete
- Re: What is this noalias thing Dennis Ritchie is railing about ?
- From: Francine . Neary
- Re: What is this noalias thing Dennis Ritchie is railing about ?
- From: Richard Bos
- What is this noalias thing Dennis Ritchie is railing about ?
- Prev by Date: Re: Anyone have any ideas on this one..... (OT)
- Next by Date: Re: 010 is 8 and not 10 :(
- Previous by thread: Re: What is this noalias thing Dennis Ritchie is railing about ?
- Next by thread: Re: What is this noalias thing Dennis Ritchie is railing about ?
- Index(es):
Relevant Pages
|