MCPcopy Create free account
hub / github.com/numpy/numpy / npyiter_find_best_axis_ordering

Function npyiter_find_best_axis_ordering

numpy/core/src/multiarray/nditer_constr.c:2277–2413  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2275}
2276
2277static void
2278npyiter_find_best_axis_ordering(NpyIter *iter)
2279{
2280 npy_uint32 itflags = NIT_ITFLAGS(iter);
2281 int idim, ndim = NIT_NDIM(iter);
2282 int iop, nop = NIT_NOP(iter);
2283
2284 npy_intp ax_i0, ax_i1, ax_ipos;
2285 npy_int8 ax_j0, ax_j1;
2286 npy_int8 *perm;
2287 NpyIter_AxisData *axisdata = NIT_AXISDATA(iter);
2288 npy_intp sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
2289 int permuted = 0;
2290
2291 perm = NIT_PERM(iter);
2292
2293 /*
2294 * Do a custom stable insertion sort. Note that because
2295 * the AXISDATA has been reversed from C order, this
2296 * is sorting from smallest stride to biggest stride.
2297 */
2298 for (ax_i0 = 1; ax_i0 < ndim; ++ax_i0) {
2299 npy_intp *strides0;
2300
2301 /* 'ax_ipos' is where perm[ax_i0] will get inserted */
2302 ax_ipos = ax_i0;
2303 ax_j0 = perm[ax_i0];
2304
2305 strides0 = NAD_STRIDES(NIT_INDEX_AXISDATA(axisdata, ax_j0));
2306 for (ax_i1 = ax_i0-1; ax_i1 >= 0; --ax_i1) {
2307 int ambig = 1, shouldswap = 0;
2308 npy_intp *strides1;
2309
2310 ax_j1 = perm[ax_i1];
2311
2312 strides1 = NAD_STRIDES(NIT_INDEX_AXISDATA(axisdata, ax_j1));
2313
2314 for (iop = 0; iop < nop; ++iop) {
2315 if (strides0[iop] != 0 && strides1[iop] != 0) {
2316 if (intp_abs(strides1[iop]) <=
2317 intp_abs(strides0[iop])) {
2318 /*
2319 * Set swap even if it's not ambiguous already,
2320 * because in the case of conflicts between
2321 * different operands, C-order wins.
2322 */
2323 shouldswap = 0;
2324 }
2325 else {
2326 /* Only set swap if it's still ambiguous */
2327 if (ambig) {
2328 shouldswap = 1;
2329 }
2330 }
2331
2332 /*
2333 * A comparison has been done, so it's
2334 * no longer ambiguous

Callers 1

NpyIter_AdvancedNewFunction · 0.85

Calls 1

intp_absFunction · 0.85

Tested by

no test coverage detected