A
Andreas Kahari
gc wrote: said:Does this mean that code of the following type is disallowed too:
void
test(char * restrict s)
{
char *p=s;/* p points at s*/
}
That is, as far as I understand, not allowed.
gc wrote: said:Does this mean that code of the following type is disallowed too:
void
test(char * restrict s)
{
char *p=s;/* p points at s*/
}
Really!
void *memcpy(void *restrict dest,
const void *restrict src, size_t n) {
// Please show us how you you would know
// when dest and src overlap.
return dest;
}
Alan said:I'm not implementing memcpy - that's been done.
When I write
#define BUFSIZE 40
char a[BUFSIZE];
char b[BUFSIZE];
...
memcpy(a, b, BUFSIZE);
I'm reasonably sure the buffers don't overlap.
Perhaps you didn't write what you actually meant,
or perhaps I misread. If the latter, my apologies.
Suppose, instead, that someone modifies your code:
#define BUFSIZE 40
char c[BUFFSIZE + 20];
for (int j = 0; j < BUFFSIZE + 20; ++j)
c[j] = j;
char* a = c + 20;
char* b = c;
. . .
memcpy(a, b, BUFSIZE);
Would you expect your C compiler [or lint] to detect this bug
and issue an appropriate diagnostic message?
Evidently, you are saying that a C 99 compiler is permitted
to do a better job of optimizing this definition:
void *memcpy(void *restrict s1,
const void *restrict s2, size_t n) {
for (int j = 0; j < n; ++j)
((char*)s1)[j] = ((char*)s2)[j];
return s1;
}
than it is allowed to do for this definition:
void *memcpy(void *s1,
const void *s2, size_t n) {
for (int j = 0; j < n; ++j)
((char*)s1)[j] = ((char*)s2)[j];
return s1;
}
Further, you appear to be saying that
the appearance of the restrict keyword in the
declaration of memcpy (what you call the prototype)
serves no purpose except for documentation
as the ANSI/ISO C99 standard does *not*
require the compiler to detect any aliases
and issue a diagnostic for it when memcpy is invoked.
Thanks!
Does this mean that code of the following type is disallowed too:
void
test(char * restrict s)
{
char *p=s;/* p points at s*/
..........
}
The "restrict" qualifier basically makes the same guarantee that
Fortran makes for *all* its subroutine and function parameters.
This enables all the optimizations the Fortran compiler folks have
come up with over the years. On machines like Crays and Convexes,
such optimizations can make functions run anywhere from three to
300 times faster. This makes them quite desirable, at least to
certain groups of programmers -- but at the same time, qualifying
pointers in this way is rather dangerous, since the programmer can
get it wrong, and in general, the compiler cannot diagnose such
mistakes.
What is the current state of compilers when it comes to 'restrict'?
Which compilers actually use it?
Intel C implements restrict. The keyword is ignored by default; there is aDaniel said:[snip (an, as usual nice, explanation of restrict)]
The "restrict" qualifier basically makes the same guarantee that
Fortran makes for *all* its subroutine and function parameters.
This enables all the optimizations the Fortran compiler folks have
come up with over the years. On machines like Crays and Convexes,
such optimizations can make functions run anywhere from three to
300 times faster. This makes them quite desirable, at least to
certain groups of programmers -- but at the same time, qualifying
pointers in this way is rather dangerous, since the programmer can
get it wrong, and in general, the compiler cannot diagnose such
mistakes.
What is the current state of compilers when it comes to 'restrict'?
Which compilers actually use it? How much does restrict help current
compiler optimizations? Since the basic situation is the same as the
Fortran case, one would expect that the C compiler writers fairly
easily should be able to incorporate roughly the same optimizations
as in the Fortran case? Is this happening?
Daniel Vallstrom
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.