A (standard) C
and (nonstandard) C++ keyword
implemented only on certain compilers, which most of us will probably never need to use.
One of the excuses given by die-hard FORTRAN
programmers is that "FORTRAN does number crunching
faster than C++". This is only partly true.
FORTRAN's prowess comes from the fact that certain matrix
s can be optimized to a high degree if it can be guaranteed that a value in area of memory wont be written over before it needs to be read from. The only way to guarantee this is to guarantee that areas of memory will never be "aliased" during the operation. In Standardese
, no two lvalue
s can reference the same area of memory. You can also think of it as "each area of memory can never have more than one pointer to it at the same time". As it turns out, variables are non-alias-able by default in FORTRAN, but C and C++ had no way to guarantee this. The roles of "limitation
" and "feature
" have reversed themselves!
However, these algorithms were initially only practical when hardwired
into specialized hardware
of certain high-performace computers, such as certain models of Cray
computers. This made the need for restricting aliasing a platform dependency
So, the answer was for Cray and other supercomputer manufacturers to introduce a nonstandard storage qualifier
guaranteeing no aliasing into their C and C++ compilers, specified by the nonstandard keyword restrict
is in the same class of keywords as const
but can only be applied to pointer
Theoretically, then, a matrix multiplication
void mamul (double *restrict a, double * restrict b, double restrict *c, int m, int n, int p);
could be forwarded to the specialized hardware for high-performance number crunching (actually, I have no idea if matrix multiplication is one of the highly optimizable algorithms; it's just a guess on my part).
Of course any algorithm that can be implemented in hardware can be implemented in software, and the idea of preventing aliasing spread beyond the supercomputer world. restrict
was codified into the C9X
standard for C, and appears as a nonstandard extension of many C++ compilers.
See also http://www.cuj.com/articles/1999/9907/9907d/9907d.htm?topic=articles