restrict-qualifier

MSVC++ restrict keyword and local variables

假装没事ソ 提交于 2019-12-10 17:46:40
问题 I've read a number of posts on the restrict keyword. But virtually every example I can find seem to refer to input parameters only to a function and, perhaps a single value. I need to clarify my understanding. I've found a function that looks like it totally violates the rules of the keyword with both an input parameter and a local variable. This function is called with a void* to a buffer and the pointer is declared as _restrict (this is Microsoft Visual C++). Yet later in the function, a

using restrict qualifier with C99 variable length arrays (VLAs)

混江龙づ霸主 提交于 2019-12-10 15:15:08
问题 I am exploring how different implementations of simple loops in C99 auto-vectorize based upon the function signature. Here is my code: /* #define PRAGMA_SIMD _Pragma("simd") */ #define PRAGMA_SIMD #ifdef __INTEL_COMPILER #define ASSUME_ALIGNED(a) __assume_aligned(a,64) #else #define ASSUME_ALIGNED(a) #endif #ifndef ARRAY_RESTRICT #define ARRAY_RESTRICT #endif void foo1(double * restrict a, const double * restrict b, const double * restrict c) { ASSUME_ALIGNED(a); ASSUME_ALIGNED(b); ASSUME

Should I use __restrict on references?

十年热恋 提交于 2019-12-08 19:00:03
问题 In the program I am coding, one of my function declarations goes like this: bool parse( const sentence & __restrict sentence ) { // whatever } When I compile the code with Microsoft Visual Studio 2010 Express, the compiler complains: warning C4227: anachronism used : qualifiers on reference are ignored However, this page of GCC’s documentation says: In addition to allowing restricted pointers, you can specify restricted references, which indicate that the reference is not aliased in the local

Restrict Keyword and Pointers inside structs

久未见 提交于 2019-12-08 14:43:58
问题 By using the restrict keyword like this: int f(int* restrict a, int* restrict b); I can instruct the compiler that arrays a and b do not overlap. Say I have a structure: struct s{ (...) int* ip; }; and write a function that takes two struct s objects: int f2(struct s a, struct s b); How can I similarly instruct the compiler in this case that a.ip and b.ip do not overlap? 回答1: You can also use restrict inside a structure. struct s { /* ... */ int * restrict ip; }; int f2(struct s a, struct s b

Is there an efficient way to make reference to constants actually const instead of read only?

纵然是瞬间 提交于 2019-12-07 07:08:45
问题 Let's look at the following C++ code: #include <iostream> int main() { int z = 2; class A { public: const int & x; A(const int & x) : x(x) {} void show(){ std::cout << "x=" << this->x << std::endl ; } } a(z); a.show(); z = 3; a.show(); } The program prints: 2 and 3 It clearly shows that while inside class A x can't be modified, it merely means it's read only, because I can change it's value from outside. Of course I can make it a copy stored inside class A, but I'm wondering if there is (or

Using restrict with arrays?

ぐ巨炮叔叔 提交于 2019-12-06 21:22:37
问题 Is there a way to tell a C99 compiler that the only way I am going to access given array is by using myarray[index] ? Say something like this: int heavy_calcualtions(float* restrict range1, float* restrict range2) { float __I promise I won't alias this__ tmpvalues[1000] = {0}; .... heavy calculations using range1, range2 and tmpvalues; .... } By using restrict I promised that I won't alias range1 and range2 but how do I do the same thing for array declared inside my function ? 回答1: Although

Granularity of restrict qualifier for overlapping pointers, types

大兔子大兔子 提交于 2019-12-06 08:42:50
问题 The whole point of restrict is to promise accesses through one pointer don't alias another. That said, there are examples where overlapping memory addresses wouldn't imply aliasing. For example: int* arr_ptr0 = &arr[0]; int* arr_ptr1 = &arr[1]; for (int i=0;i<10;++i) { *arr_ptr0 = *arr_ptr1; arr_ptr0 += 2; arr_ptr1 += 2; } The thing is, these pointers actually do point to overlapping memory! For this particular example, guides like this say, e.g.: It is valid . . . to point into the same

errors as i use the restrict qualifier

那年仲夏 提交于 2019-12-05 10:14:50
When I compile the following program I get errors : gcc tester.c -o tester tester.c: In function ‘main’: tester.c:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_X’ tester.c:7:17: error: ‘ptr_X’ undeclared (first use in this function) tester.c:7:17: note: each undeclared identifier is reported only once for each function it appears in tester.c:10:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_Y’ tester.c:10:17: error: ‘ptr_Y’ undeclared (first use in this function) #include <stdio.h> int main() { int x = 10; int y = 20; int *restrict ptr_X;

C++ restrict Semantics

不羁岁月 提交于 2019-12-05 08:34:49
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 . This seems to be the most-standard-extension, so I'll use restrict and __restrict interchangeably. restrict is a C99 keyword, but nevertheless compilers have defined important uses for it in C++. This post intends to be a "question" asking about what each C++-specific use is and what it means, followed by a CW answer answering it. Feel free to add/check/edit. So: "Help! What do these C++ uses of the restrict keyword mean?" Qualifying this

Why is the restrict keyword not part of C++?

不打扰是莪最后的温柔 提交于 2019-12-04 15:15:16
问题 The title says it all. I am curious why is the restrict keyword not part of C++ ? I don't know much about C++, and I'm still not able to find anything online that would give a reason blocking this. Does anyone know what terrible things would happen, if a C++ standard would use this keyword similarly to the way C does? Is it just not needed at all? More explanation: It is not about using it, perhaps I will not have any benefit from this keyword in my whole life. This question is only about