Hi,

Recently a colleague of mine presented me with a surprising find, one which I do not fully understand. Maybe someone can shed some light on this.

The background behind the following example is that I try to code in a certain style which I consider "modern and safe". One of the "best practices" there is to always define variables locally.

Another one, that might look quirky, is to make a temporary variable a const if it is not supposed to change any more. The same goes for parameters to functions.

Then, the colleague comes along and tells me that my code runs slower, than the code that does not call temporaries const and also defines them in the "old C style" at the beginning of the functions.

First of all, I can see no reason why it should do.

Ok. Now the meaty bit. First mine:

#include <cmath> using namespace std; void f (float * const __restrict__ t, float const * const __restrict__ x, float const * const __restrict__ v, int const n) { #pragma simd for (int i = 0; i < n; ++i) { float const t02 = float(i); float const x2 = x[i] * x[i]; float const v2 = v[i] * v[i]; t[i] = sqrt(t02 + x2 / v2); } }

And then his version:

#include <cmath> using namespace std; void f (float * __restrict__ t, float * __restrict__ x, float * __restrict__ v, int n) { int i; float t02, x2, v2; #pragma simd for (i = 0; i < n; ++i) { t02 = float(i); x2 = x[i] * x[i]; v2 = v[i] * v[i]; t[i] = sqrt(t02 + x2 / v2); } }

Now, I did not yet benchmark it, but I ran both through the icpc compiler version 14.0.3 on x86-64 Linux and I got surprisingly differences.

Can anyone explain the differences in the cmpq and lea?

Surely the compiler should create the same machine code from both C++ codes? Btw: g++ 4.7.2 does.

Thank you!

Best regards

Andreas