* Counts the number of True values in a raw boolean array. This * is a low-overhead function which does no heap allocations. * * Returns -1 on error. */
| 2583 | * Returns -1 on error. |
| 2584 | */ |
| 2585 | static NPY_GCC_OPT_3 npy_intp |
| 2586 | count_nonzero_int(int ndim, char *data, const npy_intp *ashape, const npy_intp *astrides, int elsize) |
| 2587 | { |
| 2588 | assert(elsize <= 8); |
| 2589 | int idim; |
| 2590 | npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS]; |
| 2591 | npy_intp coord[NPY_MAXDIMS]; |
| 2592 | |
| 2593 | // Use raw iteration with no heap memory allocation |
| 2594 | if (PyArray_PrepareOneRawArrayIter( |
| 2595 | ndim, ashape, |
| 2596 | data, astrides, |
| 2597 | &ndim, shape, |
| 2598 | &data, strides) < 0) { |
| 2599 | return -1; |
| 2600 | } |
| 2601 | |
| 2602 | // Handle zero-sized array |
| 2603 | if (shape[0] == 0) { |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
| 2607 | NPY_BEGIN_THREADS_DEF; |
| 2608 | NPY_BEGIN_THREADS_THRESHOLDED(shape[0]); |
| 2609 | |
| 2610 | #define NONZERO_CASE(LEN, SFX) \ |
| 2611 | case LEN: \ |
| 2612 | NPY_RAW_ITER_START(idim, ndim, coord, shape) { \ |
| 2613 | count += count_nonzero_##SFX(data, strides[0], shape[0]); \ |
| 2614 | } NPY_RAW_ITER_ONE_NEXT(idim, ndim, coord, shape, data, strides); \ |
| 2615 | break |
| 2616 | |
| 2617 | npy_intp count = 0; |
| 2618 | switch(elsize) { |
| 2619 | NONZERO_CASE(1, u8); |
| 2620 | NONZERO_CASE(2, u16); |
| 2621 | NONZERO_CASE(4, u32); |
| 2622 | NONZERO_CASE(8, u64); |
| 2623 | } |
| 2624 | #undef NONZERO_CASE |
| 2625 | |
| 2626 | NPY_END_THREADS; |
| 2627 | return count; |
| 2628 | } |
| 2629 | /* |
| 2630 | * Counts the number of True values in a raw boolean array. This |
| 2631 | * is a low-overhead function which does no heap allocations. |
no test coverage detected