Re: What is this noalias thing Dennis Ritchie is railing about ?



"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.


.



Relevant Pages

  • Re: What does restrict mean?
    ... void * restrict pSrc, size_t size) ... return pDest; ... doesn't give the compiler any information at all to say if it overlaps ... comparison of the pointers in MemFoo cannot be eliminated as is. ...
    (comp.lang.c)
  • Re: What does restrict mean?
    ... but what does the keyword 'restrict' mean? ... The reason that memcpy has the restrict keyword and memmove doesn't ... will be made from s2 or pointers derived from s2. ...
    (comp.lang.c)
  • Re: Questions about memmove
    ... Is there ever a good reason to use memcpy instead of memmove? ... The memcpy function may be more efficient since it doesn't have ... structures (which may contain pointers). ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: General remarks on restrict
    ... But when a function add a restrict qualification to its parameters (in ... And if I have two pointers that can be used to access the same lvalue, ... it is the hypothetical constraint that k should not be ...
    (comp.std.c)
  • Re: What is this noalias thing Dennis Ritchie is railing about ?
    ... Take a look at the C99 prototypes for memcpy and memmove: ... It's possible that s1 and/or pointers derived from s1, ... This allows restrict to specify exactly what was ...
    (comp.lang.c)