I am reading the source code of Python
's numpy
library, and found the following snippets. It seems to perform element-wise operations on vectors (numpy.ndarray
). For example, numpy.multiply([1,2,3],[4,5,6])
will get the result [4,10,18]
#define BASE_UNARY_LOOP(tin, tout, op) \
UNARY_LOOP { \
const tin in = *(tin *)ip1; \
tout * out = (tout *)op1; \
op; \
}
#define UNARY_LOOP_FAST(tin, tout, op) \
do { \
/* condition allows compiler to optimize the generic macro */ \
if (IS_UNARY_CONT(tin, tout)) { \
if (args[0] == args[1]) { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
else { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
} \
else { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
} \
while (0)
It looks very weird to me, especially the comment inside UNARY_LOOP_FAST
.
What is going on here by using if A then X else X
logic to optimize?