restrict

__restrict vis-a-vis a function optimization behavior of popular compilers

我们两清 提交于 2019-12-06 13:37:25
Consider the following function: int bar(const int* __restrict x, void g()) { int result = *x; g(); result += *x; return result; } Do we need to read twice from x because of the call to g() ? Or is the __restrict ion enough to guarantee the invocation of g() does not access/does not alter the value at address x ? At this link we see the most popular compilers have to say about this (GodBolt; language standard C99, platform AMD64): clang 7.0: Restriction respected. GCC 8.3: No restriction. MSVC 19.16: No restriction. Is clang rightly optimizing the second read away, or isn't it? I'm asking both

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

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

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

Does C99/C11 restrict type qualifier imply anything for functions without definition?

孤人 提交于 2019-12-04 10:25:10
Suppose we have a function declaration for which we do not have access to its definition: void f(int * restrict p, int * restrict q, int * restrict r); Since we do not know how the pointers will be accessed, we cannot know if a call will trigger undefined behavior or not -- even if we are passing the same pointer, like the example at 6.7.3.1.10 explains: The function parameter declarations: void h(int n, int * restrict p, int * restrict q, int * restrict r) { int i; for (i = 0; i < n; i++) p[i] = q[i] + r[i]; } illustrate how an unmodified object can be aliased through two restricted pointers.

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

C Primer Plus 第12章 12.7 ANSI C的类型限定词

て烟熏妆下的殇ゞ 提交于 2019-12-02 18:10:48
您已经知道一个变量是以它的类型与存储类表征的。C90增加了两个属性:不变性和易变性。这些属性是通过关键字const和volatile声明的,这样就创建了受限类型(qualified type)。C99标准添加了第三个限定词resrict,用以方便编译器优化。 C99授予类型限定词一个新属性:它们现在是幂等的(idempotent)!这听起来像一个强大的功能,其实只意味着可以在一个声明 中不止一次地使用同一限定词,多余的将被忽略掉: const const const int n = 6; //相当于const int n = 6; 例如,这使下列序列可以被接受: typedef const int zip; const zip q=8; 12.7.1 类型限定词const 回顾一下,如果变量声明中带有关键字const,则不能通过赋值、增量或减量运算来修改该变量的值。在与ANSI 编译器中,下面的代码将产生一个错误信息: const int nochange; //把m限定为常量 nochange = 12; //不允许 然而,可以初始化一个const变量。因此,下面的代码是正确的: const int nochange = 12; //可以 上面的声明使nochange成为一个只读变量。在初始化以后,不可以再改变它。 例如,可以用关键字const创建一组程序不可以改变的数据:

SQL Server - CHECK constraint on a column where values come from another table

為{幸葍}努か 提交于 2019-12-02 13:10:33
问题 How does one put a CHECK constraint on a column such that its range of acceptable values come from another table, without hardcoding? Here's a simplified example: OneManyTable RoleID TaskID 10 Val1 10 Val2 20 Val1 20 Val2 MetaDataTable pkID Class Value 1 A Val1 2 A Val2 3 B Val3 4 B Val4 I want to put a CHECK Constraint on OneManyTable.TaskID column such that acceptable values come from another tables's column, i.e. from MetadataTable.Value where MetadataTable.class= 'A' I already tried

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