title: Generating SIMD instructions from Cython code
body: <p>I need to get an overview of the performance one can get from using Cython in high performance numerical code. One of the thing I am interested in is to find out if an optimizing C compiler can vectorize code generated by Cython. So I decided to write the following small example:</p>
  
  <pre><code>import numpy as np
  cimport numpy as np
  cimport cython
  
  @cython.boundscheck(False)
  @cython.wraparound(False)
  cpdef int f(np.ndarray[int, ndim = 1] f):
      cdef int array_length =  f.shape[0]
      cdef int sum = 0
      cdef int k
      for k in range(array_length):
          sum += f[k]
      return sum
  </code></pre>
  
  <p>I know that there are Numpy functions that does the job, but I would like to have an easy code in order to understand what is possible with Cython. It turns out that the code generated with:</p>
  
  <pre><code>from distutils.core import setup
  from Cython.Build import cythonize
  
  setup(ext_modules = cythonize("sum.pyx"))
  </code></pre>
  
  <p>and called with:</p>
  
  <pre><code>python setup.py build_ext --inplace
  </code></pre>
  
  <p>generates a C code which look likes this for the loop:</p>
  
  <pre><code>for (__pyx_t_2 = 0; __pyx_t_2 &lt; __pyx_t_1; __pyx_t_2 += 1) {
    __pyx_v_sum = __pyx_v_sum + (*(int *)((char *) 
      __pyx_pybuffernd_f.rcbuffer-&gt;pybuffer.buf +
      __pyx_t_2 * __pyx_pybuffernd_f.diminfo[0].strides)));
  }
  </code></pre>
  
  <p>The main problem with this code is that the compiler does not know at compile time that <code>__pyx_pybuffernd_f.diminfo[0].strides</code> is such that the elements of the array are close together in memory. Without that information, the compiler cannot vectorize efficiently.</p>
  
  <p>Is there a way to do such a thing from Cython?</p>