restrict-qualifier

Granularity of restrict qualifier for overlapping pointers, types

六月ゝ 毕业季﹏ 提交于 2019-12-04 14:57:22
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 array object, provided the range of elements accessed through one of the pointers does not overlap with the

When to use restrict and when not to

蹲街弑〆低调 提交于 2019-12-04 08:55:19
问题 I have a general understanding of restrict but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature (currently without restrict ): char const *StringUrlEncode(char const *unencoded, char *encoded, char *encodedEnd); unencoded is my null-terminated source string. The destination buffer is represented by encoded and encodedEnd , where encoded points to

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

馋奶兔 提交于 2019-12-03 10:31:53
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 curiosity, since restrict is part of C since C99, that is 15 years. Read this as well: I'm interested in

When to use restrict and when not to

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:22:43
I have a general understanding of restrict but I'm hoping to clarify some fine points. I have a function that reads a null-terminated string from one buffer and writes out a URL encoded version in another buffer. The function has this signature (currently without restrict ): char const *StringUrlEncode(char const *unencoded, char *encoded, char *encodedEnd); unencoded is my null-terminated source string. The destination buffer is represented by encoded and encodedEnd , where encoded points to the first char in the buffer and encodedEnd points to the first char after the buffer, i.e. the

Can I `__restrict__ this` somehow?

混江龙づ霸主 提交于 2019-12-02 04:29:32
I've been watching Mike Acton's talk on Data-oriented design in C++ in CppCon 2014, and he gives this example: int Foo::Bar(int count) { int value = 0; for (int i = 0; i < count; i++) { if (m_someDataMemberOfFoo) value++ } return value; } And explains how some compilers keep re-reading m_someDataMemberOfFoo in every iteration, perhaps because its value might change due to concurrent access. Regardless of whether it's appropriate for the compiler to do so - can one tell the compiler to specifically ignore any possibility of concurrent access to anything during the execution of some method, so

restrict qualifier on member functions (restrict this pointer)

旧时模样 提交于 2019-12-01 20:16:12
问题 Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here. gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict ) qualifier on a member function, effectively making this a restrict qualified pointer within the function's scope. Where is the beef? Most member functions work on other members, accessing them via this , which is a T* const (and normally unaliased). For

restrict qualifier on member functions (restrict this pointer)

浪尽此生 提交于 2019-12-01 18:20:17
Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here . gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict ) qualifier on a member function, effectively making this a restrict qualified pointer within the function's scope. Where is the beef? Most member functions work on other members, accessing them via this , which is a T* const (and normally unaliased). For this to be possibly aliased, there would need to be a second pointer-to-type in use within the member

C/C++ __restrict type

夙愿已清 提交于 2019-11-30 12:12:34
Is there a way to define using typedef integral/float type which implies no aliasng? something equivalent to (but primitive construct): template < typename T > struct restrict { T* __restrict data; }; as related question, is it possible to ask gcc what it determines alias/no alias of pointer is? Matt B. As noted in the comments, many newer C++ compilers do support the C99 implementation of the restrict type qualifier. Since restrict is not a reserved keyword in C++, the compilers generally use __restrict or __restrict__ . Both GCC and Visual C++ document this nicely, with explicit references

Understanding restrict qualifier by examples

大兔子大兔子 提交于 2019-11-30 11:31:17
The restrict keyword's behavior is defined in C99 by 6.7.3.1: Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T. If D appears inside a block and does not have storage class extern, let B denote the block. If D appears in the list of parameter declarations of a function definition, let B denote the associated block. Otherwise, let B denote the block of main (or the block of whatever function is called at program startup in a freestanding environment). In what follows, a pointer expression E is said to be

Does restrict help in C if a pointer is already marked const?

戏子无情 提交于 2019-11-30 10:59:36
Just wondering: When I add restrict to a pointer, I tell the compiler that the pointer is not an alias for another pointer. Let's assume I have a function like: // Constructed example void foo (float* result, const float* a, const float* b, const size_t size) { for (size_t i = 0; i < size; ++i) { result [i] = a [0] * b [i]; } } If the compiler has to assume that result might overlap with a , it has to refetch a each time. But, as a is marked const , the compiler could also assume that a is fixed, and hence fetching it once is ok. Question is, in a situation like this, what is the recommend way