Yes, the author is wrong there; restrict is valid and useful in that memcpy example.
It is true though that restrict doesn't solve all of C's aliasing problems though. For example:
#include <stdlib.h>
struct array_3d {
float * restrict data;
size_t xmin, ymin, zmin;
size_t xmax, ymax, zmax;
size_t allocated;
};
static inline float *
array_3d_elem_addr(struct array_3d *a3d, size_t x, size_t y, size_t z) {
return a3d->data + z +
y * (a3d->zmax - a3d->zmin) +
x * (a3d->zmax - a3d->zmin) * (a3d->ymax - a3d->ymin);
}
void array_3d_add(struct array_3d *restrict dst, struct array_3d *restrict src) {
for (size_t x = dst->xmin; x != dst->xmax; ++x)
for (size_t y = dst->ymin; y != dst->ymax; ++y)
for (size_t z = dst->zmin; z != dst->zmax; ++z)
*array_3d_elem_addr(dst, x, y, z) += *array_3d_elem_addr(src, x, y, z);
}
No amount of restrict keywords can tell the compiler that src->data and dst->data don't alias here (putting restrict on the 'data' member declaration inside array_3d doesn't mean anything here, because of the way restrict is defined).
GCC manages to vectorize this loop, but only conditionally, with runtime alias checks. That's ok for this trivial example, but it isn't always viable in more complex examples. It's also worth noting that there are ways to rewrite this code so that it does fully vectorize, though again it's helped by the example being so trivial. The main point is that in Fortran, the alias rules are strong by default, without the programmer having to do acrobatics.
Comments
Yes, the author is wrong there; restrict is valid and useful in that memcpy example.
It is true though that restrict doesn't solve all of C's aliasing problems though. For example:
No amount of restrict keywords can tell the compiler that src->data and dst->data don't alias here (putting restrict on the 'data' member declaration inside array_3d doesn't mean anything here, because of the way restrict is defined).GCC manages to vectorize this loop, but only conditionally, with runtime alias checks. That's ok for this trivial example, but it isn't always viable in more complex examples. It's also worth noting that there are ways to rewrite this code so that it does fully vectorize, though again it's helped by the example being so trivial. The main point is that in Fortran, the alias rules are strong by default, without the programmer having to do acrobatics.
Edit: formatting fixes
Also note that this restriction is in C itself. This is how restrict is defined in the C standard. It's not specific to GCC or any other compiler.