[Numpy-discussion] Inline functions?

Charles R Harris charlesr.harris at gmail.com
Sun Nov 19 22:06:53 CST 2006


I wonder if we couldn't make use of inline functions for some of the bigger
macros? For instance:

#define PyArray_ITER_NEXT(it) {                                     \
        (it)->index++;                                            \
        if ((it)->nd_m1 == 0) {                                   \
                _PyArray_ITER_NEXT1(it);                          \
        }                                                               \
        else if ((it)->contiguous)  (it)->dataptr +=
(it)->ao->descr->elsize; \
        else if ((it)->nd_m1 == 1) {                              \
                _PyArray_ITER_NEXT2(it);                          \
        }                                                               \
        else {                                                          \
                int __npy_i;                                            \
                for (__npy_i = (it)->nd_m1; __npy_i >= 0; __npy_i--)
{          \
                        if ((it)->coordinates[__npy_i]
<                        \
                            (it)->dims_m1[__npy_i])
{                           \

(it)->coordinates[__npy_i]++;                   \
                                (it)->dataptr +=
(it)->strides[__npy_i];        \
                                break;                                  \
                        }                                               \
                        else {                                          \
                                (it)->coordinates[__npy_i] = 0;         \
                                (it)->dataptr -=
(it)->backstrides[__npy_i];    \
                        }                                               \
                }                                                       \
        }                                                               \
}

I admit using a function might slow down the common case, but maybe we could
figure some way of keeping the common case small so it goes inline while
leaving the rest as a function. For instance,

inline PyArray_ITER_NEXT(iter* it)
{
    it->index++;
    if(it->nd_ml == 0 && it->contiguous)
        it->dataptr += it->ao->descr->elsize;
    else
        punt(it);
}

And if *that* isn't inlined we can probable just make the whole thing a
normal function without losing much. I suppose a drawback to using inline
functions is that a reasonably modern C compiler needs to be used that
conforms to C-99.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061119/a9b542ce/attachment-0001.html 


More information about the Numpy-discussion mailing list