Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Richard_H_6
Beginner
88 Views

Whether it's fine to use __restrict keyword in this specific example?

It's the same example as in 6.7.3.1 of C99 spec.

EXAMPLE 3 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. In particular, if a and b are disjoint arrays, a call of the form h(100, a, b, b) has defined behavior, because array b is not modified within function h.

In short, it is explicitly mentioned it's defined behavior if b is not modified within function h. However, whether is it undefined behavior if a call of the form h(100, a, a, b)?

A little bit more backgrounds why I want to make it clear. There are some basic functions which we want to use in in-place or out-of-place manner. In order to reduce the effort, it is desired if we don't need to provide both h(int n, int * restrict p, int * restrict q, int * restrict r) and h_inplace(int n, int * restrict p, int * restrict q). From current observation, it seems icc and msvc can give correct result even if we call it as the form of h(100, a, a, b). However, we definitely don't want to have the risk if it is undefined behavior (which means it may be wrong from other compilers or future versions of icc and msvc). What do you think?

0 Kudos
3 Replies
TimP
Black Belt
88 Views

The rules about repeated restricted pointers have been discussed more thoroughly in the fortran analogue. When an operand is modified through one of those pointers, reference through the other may pick up the operand either before or after modification. Thus the restrict qualifier could produce undefined behavior.

It's reasonable to expect __restrict and restrict to work the same in compilers which implement both, although I've never seen anyone promising this. They have effect only for modified operands.

I have dealt with customers who insist they have never been bitten by violating the aliasing rules and refuse to take the measures to use the repeated pointers safely.

Richard_H_6
Beginner
88 Views

Hi Tim, could you give me the link which has discussions for fortran?

Thanks,

Richard

TimP
Black Belt
88 Views

Individual Fortran compilers have switches such as ifort -assume:dummy_aliases to allow for violation of Fortran aliasing or pointers passed from c which violate restrict or strict aliasing. It may be frustrating trying to look up discussion of safety limits under Fortran aliasing rules.
Reply