Fortify cython functions against segfaults (without killing perf)
Following discussion in #2892, and segfaults in #3011, #2775, #2778 and others.

manually insert needed checks before inner loop, leaving @cython.boundscheck(False)
inplace.
