NumPower / numpower

PHP extension for efficient scientific computing and array manipulation with GPU support
https://numpower.org
Other
185 stars 4 forks source link

NDArray::all not working without AVX2 #16

Closed henrique-borba closed 1 year ago

henrique-borba commented 1 year ago

Update the code so NDArray::all works with other instruction sets or vanilla looping.

double
NDArray_All(NDArray *a) {
    __m256d zero = _mm256_set1_pd(0.0);
    int i;
    double *array = NDArray_DDATA(a);
    for (i = 0; i < NDArray_NUMELEMENTS(a) - 3; i += 4) {
        __m256d elements = _mm256_loadu_pd(&array[i]);
        __m256d comparison = _mm256_cmp_pd(elements, zero, _CMP_NEQ_OQ);

        // Perform horizontal OR operation on comparison results
        int mask = _mm256_movemask_pd(comparison);
        if (mask != 0x0F) {
            return 0;  // At least one element is zero
        }
    }

    // Check remaining elements (if any)
    for (; i < NDArray_NUMELEMENTS(a); i++) {
        if (array[i] == 0.0) {
            return 0;  // Element is zero
        }
    }

    return 1;  // All elements are non-zero
}
henrique-borba commented 1 year ago

Solved by 7b63f5f922792aa471095096723c36ca894c4565