Gcc C / C ++ does not allow pointer smoothing

Having read recently that the main reason fortran is faster than c / C ++ in numerical computing is because there is no pointer overlay.

Apparently, the use of the restrict or __restrict__ keywords allows in each case to indicate the absence of pointer aliases for this memory element.

The icc compiler apparently has the -fno-alias option, which allows us to globally assume that no overlap exists. On gcc, there is -fno-strict-aliasing , which applies only to a subset of all smoothing situations.

Is there a gcc option, or are there cases where anti-aliasing is not expected when using certain optimization flags?

+8
gcc pointers aliasing
source share
1 answer

GCC has the -fstrict-aliasing option, which allows you to optimize aliases globally and expects you to get nothing in an illegal way. This optimization is enabled for -O2 and -O3 , I believe.

C ++ has well-defined alias rules, although standardized code will not conflict with a strict alias. In particular, this means that you are not allowed to access one variable with a pointer to another type:

 float f; int * p = reinterpret_cast<int*>(&f); // uh-oh *p = 0x3FF00000; // breaks strict aliasing 

An important exception to this rule is that you can always access any variable with a pointer to char . (This is necessary for serialization through I / O.)

Alias โ€‹โ€‹rules do not help the compiler to find out if any pointers of the same aliases are to each other. Consider this:

 void add(float * a, float * b, float * c) { *c = *a + *b; } 

Here the compiler cannot know if c points to a different memory than a or b , and this should be careful. I think restrict here matters, essentially promising that float * restrict c means no one is an alias of c .

+14
source share

All Articles