Questions tagged [restrict-qualifier]

Restrict is a keyword that could applied to a pointer to an object. It makes this pointer the one and only way to access the data of that object.

The restrict-qualifier tells the compiler that if the memory accessed by the restrict-qualified pointer is modified, no other pointers will access it.

The compiler may use this hint to optimize the restrict-qualified pointers in such a way, that would otherwise cause incorrect or undefined behavior.

C++ supports restrict keyword for compatibility with C. However, a C++ compiler will ignore the restrict keyword, because in C++ "restrict" is a valid variable name. C++ compilers, though, have a special __restrict__ qualifier.

In-detail information along with examples can be found on the IBM website.

128 questions
2
votes
1 answer

Does GCC C++ compiler take into account __restrict - statements?

I've have investigating the effect of __restricting certain pointers in a C++-code, when compiling it via the GCC-compiler. It turned that not only the run-time remains quite the same, but the executable doesn't seem to have changed, the size in…
shuhalo
  • 5,732
  • 12
  • 43
  • 60
2
votes
1 answer

Can a const * __restrict__ increase cuda register usage?

Because my pointers are all pointing to non-overlapping memory I've went all out and replaced my pointers passed to kernels (and their inlined functions) to be restricted, and to made them const too, where ever possible. This however increased the…
ikku100
  • 809
  • 1
  • 7
  • 16
2
votes
3 answers

Is C++ pointer aliasing a threat if the pointers are exactly the same?

Consider this function intended for vectorization: void AddSqr(float* restrict dst, float* restrict src, int cnt) { for (int i=0; i
mrzacek mrzacek
  • 308
  • 2
  • 12
2
votes
0 answers

__restrict in g++ and MSVC with Array Syntax

I'm in the process of updating performance critical libraries to use restrict, as implemented in C++11 by g++ and MSVC with the keyword __restrict. There are a lot of routines and functions that look something like: void f(float a[],float b[]); In…
geometrian
  • 14,775
  • 10
  • 56
  • 132
2
votes
2 answers

In which cases will the restrict qualifier applied to a return value have an effect?

If I have a member function declared like so: double* restrict data(){ return m_data; // array member variable } can the restrict keyword do anything? Apparently, with g++ (x86 architecture) it cannot, but are there other…
Dave
  • 7,555
  • 8
  • 46
  • 88
2
votes
1 answer

Confusing adapting code to use restrict qualifier

I'm trying to adapt the following version of the stpcpy function to use restrict-qualified pointers as its arguments and internally, but I'm not sure if simply adding the qualifier would result introduce undefined behavior. #define ALIGN…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1
vote
1 answer

Is it valid to use restrict on read-only objects shared between threads in C?

In C, when i have a shared object between many threads, but i can guarantee that it will not be modified, can i restrict-qualify a pointer to it? Of course, in each individual thread the usual requirement, that the pointer provides exclusive access…
Finn
  • 37
  • 5
1
vote
0 answers

`restrict` ignored unless function manually inlined

With GCC 9.4.0, the code static inline double k(double const *restrict a) { return a[-1] + a[0] + a[1]; } static void f(double const *restrict a, double *restrict b, int n) { for (int i = 0; i < n; ++i) { b[i] = k(a); …
user3708067
  • 543
  • 5
  • 12
1
vote
1 answer

How to use C keyword restrict to decorate return pointer correctly?

In some documents, I learned that we shall use 'restrict' to decorate function parameters or memory allocation statements. Like this: void(int*restrict paraA){} int*restrict A = (int*)malloc(10 * sizeof(int); And I'm confused about whether…
One Loser
  • 13
  • 2
1
vote
1 answer

An explanation on rules and interpretations of CVR type qualifications in C

I was going through const, volatile and restrict type qualifier pages on cppreference. I had lots of doubts and confusions about the explanation/examples given there. This was an example given there: char *p = 0; const char *pp = p; // OK char…
1
vote
2 answers

Usage of restrict on two pointers pointing to same array

I'm having quite hard time making proper usage of restrict keyword in C. I wrote this function to in-place reverse a string: void my_strrev(char *restrict str, const size_t len) { char *restrict strr = str + len - 1; char temp; while…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
1
vote
1 answer

restrict qualifier placement with pointers to pointers

Here are a couple of C and POSIX functions that need to fetch some data from or put some data into a buffer and tell the caller how much, so they take a pointer to the starting buffer address and write the adjusted pointer there on return: size_t…
1
vote
2 answers

Assigning a non-restricted pointer to a restricted pointer

I am recently implementing a function (my_copy()) with restrict pointers as arguments: #include #include void my_copy(int n, int * restrict p, int * restrict q) { if (q == NULL) { q = calloc(n, sizeof(int)); } …
Eric Stdlib
  • 1,292
  • 1
  • 18
  • 32
1
vote
2 answers

What are the semantics of assigning restrict pointer to a non-restrict one?

Is the following hypothetical code correct (are the assumptions in comments hold)? Or does it have UB? #define N 1 // what if it's 0? void foo(int *x, int * restrict y) { *x = 42; *y = 0; // compiler can assume *x is still 42. { int *a…
Dan M.
  • 3,818
  • 1
  • 23
  • 41
1
vote
1 answer

Tell c++ compiler that the argument is not aliased

One of the big differences between C/C++ and Fortran when it comes to speed is that the former languages use pointers which can be aliased and therefore a compiler needs to load in the data at each loop iteration while Fortran's allocatable does not…
ATK
  • 1,296
  • 10
  • 26
1 2 3
8 9