Suppose I have large array which I calculate an index into and pass to a second function. As a simple example, something like:
void foo(float* array, float c, unsigned int n)
{
for (unsigned int i = 0; i < n; ++i)
array[i] *= c;
}
void bar(float* restrict array, float* restrict array2, unsigned int m, unsigned int n)
{
for (unsigned int i = 0; i < m; ++i)
foo(&array[i * n], array2[i], n);
}
Is this breaking the rules for restrict in bar() where you pass the address of part of the array to foo(), even if you never really use the alias for a part of the array within bar()?
In the C programming language (after 99 standard), a new keyword is introduced known as restrict. restrict keyword is mainly used in pointer declarations as a type qualifier for pointers. It doesn't add any new functionality. It is only a way for programmer to inform about an optimization that compiler can make.
The restrict qualifier can be used in the declaration of a structure member. A compiler can assume, when an identifier is declared that provides a means of access to an object of that structure type, that the member provides the sole initial means of access to an object of the type specified in the member declaration.
(All citations refer to N1256, which is C99 plus technical corrigenda (TC3).)
The formal definition of restrict is given in §6.7.3.1. I quote the most important subclause below. P is a restrict-qualified pointer to type T whose scope is a block B. A pointer expression E is said to be based on P if it depends on the value of P itself, not the value that P points to.
During each execution of
B, letLbe any lvalue that has&Lbased on P. IfLis used to access the value of the objectXthat it designates, andXis also modified (by any means), then the following requirements apply:
Tshall not be const-qualified.- Every other lvalue used to access the value of
Xshall also have its address based onP.- Every access that modifies
Xshall be considered also to modifyP, for the purposes of this subclause.- If
Pis assigned the value of a pointer expressionEthat is based on another restricted pointer objectP2, associated with blockB2, then either the execution ofB2shall begin before the execution ofB, or the execution ofB2shall end prior to the assignment.If these requirements are not met, then the behavior is undefined.
Let's look at what the rules have to say about accesses to parts of bar's array in foo. We start with array, a restrict-qualified pointer declared in the parameter list of bar. For clarity, I will alpha-convert the parameters of foo:
void foo(float* b, float c, unsigned int n) { /*modify b[i]*/ }
The storage pointed to by array is also modified through b. That's ok with the second bullet point as &array[i*n] is equivalent to array+(i*n) (see §6.5.3.2).
If b was restrict-qualified, then we would have to check the fourth bullet point with P←b, B←foo, P2←array, B2←bar. Since B is nested inside B2 (functions behave as if inlined here, see §6.7.3.1.11), the first condition is met. There is also one instanciation of the third bullet point (the access to b[i] in foo) which is not a problem.
However b is not restrict-qualified. According to §6.3.2.3.2, “For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to the q-qualified version of the type; the values stored in the original and converted pointers shall compare equal”. Therefore the conversion from array+(i*n) to b is well-defined and has the obvious meaning, so the program's behavior is defined. Furthermore, since b is not restrict-qualified, it doesn't need to obey any linearity condition. For example, the following foo is legal in combination with bar:
void qux(float *v, float *w) {
v[0] += w[0];
}
void foo(float* b, float c, unsigned int n)
{
qux(b,b);
}
ADDED: To address your specific concern “in bar() where you pass the address of part of the array to foo()”, this is a non-issue: restrict applies to the pointer, not the array, and you can perform arithmetic on it (bullet point 2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With