5 ms·
It's not subjective. Intel's guidelines for writing vectorizable code: Prefer array notation to the use of pointers. C programs in particular impose very few r
by danieldk 12y ago
It's not subjective. Intel's guidelines for writing vectorizable code:
Prefer array notation to the use of pointers. C programs in particular impose very few restrictions on the use of pointers; aliased pointers may lead to unexpected
dependencies. Without help, the compiler often cannot tell whether it is safe to vectorize code containing pointers.
Source: https://software.intel.com/sites/default/files/8c/a9/CompilerAutovectorizationGuide.pdf https://software.intel.com/sites/default/files/8c/a9/Compile... (Section 5.1)
- chii 12y agoyep - signal your intentions as specifically to the compiler as possible, so that it can generate as optimized output as possible. Array access is more specific and intentional than pointer arithmetic.
- asveikau 12y agoWhat are the odds this turns out to be practical advice for this example? Are you going to write all your loops like that because someone at Intel told you it was a good idea, or are you going to measure it when it's seen to be a problem?
- danieldk 12y agoWhat are the odds this turns out to be practical advice for this example? High. E.g. gcc 4.8.2 vectorizes this on my machine (-march=native -mtune=native -O3 -ffast-math -ftree-vectorizer-verbose=1): for (size_t i = 0; i < n; ++i) { sum += a[i]; sum += b[i]; } (a and b have the same length) But doesn't vectorize: for (double *p = a; p < a + n; ++p) { sum += *p; sum += *(b + (p-a)); } (Yes, you can make an extra pointer for b, but just for the sake of the example.) tl;dr: your compiler writer probably knows better than you.
- asveikau 12y agoThe poster did not sum values. They used the loop to call into printf. I'm guessing they didn't also use those particular compiler options... What you have here sounds like a lot of tuning that does not apply equally for all scenarios.