On the use of C language restrict keyword

  • 2020-04-01 21:35:42
  • OfStack

C99 has added restrict modified Pointers:

A pointer modified by restrict is the only method that initially accesses the object to which the pointer points,
Objects can only be accessed if the second pointer is based on the first.
Access to objects is restricted to pointer expressions based on expressions modified by restrict.

Pointers modified by restrict are mainly used for function parameters, or to point to memory space allocated by malloc().
The restrict data type does not change the semantics of the program.
The compiler can better optimize certain types of routines by making the assumption that restrict-modified Pointers are the only way to access objects.


Restrict is introduced by the c99 standard and can only be used to restrict and constrain Pointers,
The pointer is the only and initial way to access a data object.
That is, it tells the compiler that all operations that modify the contents of the pointer to memory must be modified by the pointer,
It cannot be modified by other methods (other variables or Pointers). And the nice thing about that is,
Can help the compiler to do better optimization code, produce more efficient assembly code

Int *, restrict the PTR,

The memory unit that PTR points to can only be accessed by PTR. Any other Pointers to the same memory unit are undefined.
To be blunt, an invalid pointer.

Restrict came about because of an inherent flaw in the C language itself,
C programmers should actively avoid this defect, and the compiler will be cooperative in optimizing your code.

Example:


int ar[10];
int * restrict restar=(int *)malloc(10*sizeof(int));
int *par=ar;
for(n=0;n<10;n++)
{
    par[n]+=5;
    restar[n]+=5;
    ar[n]*=2;
    par[n]+=3;
    restar[n]+=3;
}

Since restar is the only and initial way to access allocated memory, the compiler can optimize the above operations on restar:
Restar [n] + = 8;


Par is not the only way to access array ar, so the following optimization cannot be done:
Par [n] + = 8;


Because ar[n]*=2 was changed before par[n]+=3.
With the keyword restrict, the compiler can safely optimize.

The keyword restrict has two readers.
One is the compiler, which tells the compiler that it is free to make assumptions about optimization.
Another reader is the user, who tells the user to use only the parameters that qualify.

In general, the compiler has no way to check that you are following this restriction, and you are risking yourself if you defy it.
To help the compiler determine memory dependencies,
You can qualify a pointer, reference, or array
Have the restrict keyword.
The restrict keyword is a type qualifier that may be
Pointers, references, and arrays.
Its use represents a guarantee by the programmer
That within the scope of the pointer declaration
The object pointer to can be activated only by that pointer.

Any violation of this guarantee renders the program undefined.
This practice helps the compiler optimize certain sections of code
Because aliasing information can be more easily determined.

 

Use of the restrict type qualifier with Pointers


void func1(int * restrict a, int * restrict b)
{
  
}

In the example that follows, the restrict keyword is
Used to tell the compiler that the function func1 is
Never called with the Pointers a and b pointing
To objects that overlap in memory.
You are promising that accesses through a and b
Will never conflict; This means that a write through one pointer
You cannot affect a read from any other pointer.
The precise semantics of The restrict keyword are
Described in the 1999 version of the ISO C standard.

Use of the restrict type qualifier with arrays


void func2(int c[restrict], int d[restrict])
{
  int i;
  for(i = 0; i < 64; i++)
  {
    c[i] += d[i];
    d[i] += 1;
  }
}

This example illustrates using the restrict keyword when passing functionality to a function.
Here, the arrays c and d should not overlap, nor should c and d point to the same array.


Related articles: