3

what is the proper way to give __restrict__qualifier to two-dimensional array reference? for example:

void function(double (&)[3][3]);

as far as I can tell, g++ compiles the following (but no performance difference):

void function(double (& __restrict__)[3][3]);

is that correct?

full segment:

template<class A, class B, class C>
static void
multiply(const A (&a)[L][L], const B (&b)[L][L],
        C (&c)[L][L]) {
// C (&__restrict__ c)[L][L]) {
    for (size_t j = 0; j < L; ++j) {
        // C __restrict__ *cj = c[j];
        for (size_t k = 0; k < L; ++k) {
            double b_jk = b[j][k];
            for (size_t i = 0; i < L; ++i) {
                c[j][i] += a[k][i]*b_jk;
                // cj[i] += a[k][i]*b_jk;
            }
        }
    }
}
Jakuje
  • 24,773
  • 12
  • 69
  • 75
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • There are no references in C, so I removed the C tag. What compiler supports this `__restrict__` in C++ (or C, for that matter)? It's not a part of standard C++. – James McNellis Feb 16 '11 at 22:32
  • What James, said. References are C++ only and `restrict` (no underscores) is C only. If you are trying to use a C or C++ extension you should state your environment. – CB Bailey Feb 16 '11 at 22:32
  • @James gcc, xlc, icc. http://stackoverflow.com/questions/776283/what-does-the-restrict-keyword-mean-in-c – Anycorn Feb 16 '11 at 22:33

1 Answers1

0

__restrict__ can only be used with pointers. It's possible to simulate a two-dimensional array using a pointer, though. Like some_array[x*w + y] instead of some_array[x][y], where some_array is defined as double *some_array[w]. I think that should work...

But honestly, why do you even need restrict?

rmrf
  • 9
  • 2
  • Of course, you'll have to take care of allocating enough space for the pointer. – rmrf Feb 16 '11 at 22:40
  • 1
    because C++ assumes alias in pointers and as far as I can tell in this particular case. – Anycorn Feb 16 '11 at 22:41
  • ACTUALLY you can have restricted references. It just depends on your compiler. GCC will accept the `__restrict__` specifier on references: http://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html – RamblingMad Apr 22 '14 at 21:28
  • `__restrict` or `__restrict__` is perfect valid for references. If often use `__restrict` in combination with `__builtin_assume_aligned` to hint the compiler that it can vectorize and avoid generating assembly for both 8-byte and 16-byte aligned arguments – Jens Munk Apr 01 '17 at 16:20