Use wraparound and boundscheck directives for memory view slices.
authorRobert Bradshaw <robertwb@gmail.com>
Thu, 14 Feb 2013 06:24:43 +0000 (22:24 -0800)
committerRobert Bradshaw <robertwb@gmail.com>
Thu, 14 Feb 2013 06:24:43 +0000 (22:24 -0800)
Cython/Compiler/ExprNodes.py
Cython/Compiler/MemoryView.py
Cython/Utility/MemoryView_C.c

index 3ad7414..ba3087e 100755 (executable)
@@ -3358,7 +3358,8 @@ class IndexNode(ExprNode):
         buffer_entry.generate_buffer_slice_code(code, self.original_indices,
                                                 self.result(),
                                                 have_gil=have_gil,
-                                                have_slices=have_slices)
+                                                have_slices=have_slices,
+                                                directives=code.globalstate.directives)
 
     def generate_memoryviewslice_setslice_code(self, rhs, code):
         "memslice1[...] = memslice2 or memslice1[:] = memslice2"
index bd5ff0f..6181f95 100644 (file)
@@ -273,7 +273,7 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry):
         return bufp
 
     def generate_buffer_slice_code(self, code, indices, dst, have_gil,
-                                   have_slices):
+                                   have_slices, directives):
         """
         Slice a memoryviewslice.
 
@@ -359,6 +359,8 @@ class MemoryViewSliceBufferEntry(Buffer.BufferEntry):
                                      "All preceding dimensions must be "
                                      "indexed and not sliced")
 
+                wraparound = int(directives['wraparound'])
+                boundscheck = int(directives['boundscheck'])
                 d = locals()
                 code.put(load_slice_util("SliceIndex", d))
 
index 942c9b6..6fdced4 100644 (file)
@@ -759,10 +759,10 @@ if (unlikely(__pyx_memoryview_slice_memviewslice(
     Py_ssize_t __pyx_tmp_idx = {{idx}};
     Py_ssize_t __pyx_tmp_shape = {{src}}.shape[{{dim}}];
     Py_ssize_t __pyx_tmp_stride = {{src}}.strides[{{dim}}];
-    if (__pyx_tmp_idx < 0)
+    if ({{wraparound}} && (__pyx_tmp_idx < 0))
         __pyx_tmp_idx += __pyx_tmp_shape;
 
-    if (__pyx_tmp_idx < 0 || __pyx_tmp_idx >= __pyx_tmp_shape) {
+    if ({{boundscheck}} && (__pyx_tmp_idx < 0 || __pyx_tmp_idx >= __pyx_tmp_shape)) {
         {{if not have_gil}}
             #ifdef WITH_THREAD
             PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();