title: Writing cython for both 32-bit and 64-bit?
body: <p>I'm working on project with some cython modules, which contain function definitions like</p>
  
  <pre><code>def f(np.ndarray[int, ndim=1, mode='c'] x):
      _f(&amp;x[0])
  
  cdef _f(int* x):
      ...
  </code></pre>
  
  <p>I'm on a 64-bit machine, so then at run-time I get errors like the following when calling <code>f</code>:</p>
  
  <pre><code>ValueError: Item size of buffer (8 bytes) does not match size of 'int' (4 bytes)
  </code></pre>
  
  <p>The code can be "fixed" to run on my machine by changing to <code>long</code> integers:</p>
  
  <pre><code>def f(np.ndarray[long, ndim=1, mode='c'] x):
      _f(&amp;x[0])
  
  cdef _f(long* x):
      ...
  </code></pre>
  
  <p>The problem is that NumPy integer arrays are defaulting to <code>int32</code> on 32-bit and <code>int64</code> on 64-bit, which correspond to <code>int</code> and <code>long</code>, respectively, in c/cython. (Is that even correct?)</p>
  
  <p><em>So, what is the recommended or standard practice for writing cython that will work with both 32-bit and 64-bit?</em></p>
  
  <ol>
  <li><p>Should I check <code>platform.architecture</code> and put a <code>ctypedef</code> at the top of my .pyx file? Does putting a <code>ctypedef</code> inside an if-statement even work? Something like this:</p>
  
  <pre><code>import platform
  bits, linkage = platform.architechure
  if bits == '64bit':
      ctypedef long myint_t
  elif bits == '32bit':
      ctypedef int myint_t
  
  def f(np.ndarray[myint_t, ndim=1, mode='c'] x):
       _f(x)
  
  cdef _f(long* x):
      ...
  </code></pre></li>
  <li><p>Should I leave the cython library alone force the input array to be 32-bit integers? Something like this:</p>
  
  <pre><code>import numpy as np
  import my_cython_library
  
  data = np.arange(10, dtype='int32')
  my_cython_library.f(data)
  </code></pre></li>
  <li><p>Am I missing something obvious in the cython build or compilation options?</p></li>
  </ol>
  
  <p>I don't like (1) because then I have custom types all around just for basic integers, and I don't like (2) because then I'm using my hardware poorly and specify the type even when it should be obvious. So I'm hoping there is a good option (3).</p>