Repository: scipy/scipy
File: scipy/linalg/src/_common_array_utils.h (C/C++)

Code before:
            swap_cf_z(src, dst, r2, c, n);
            swap_cf_z(src + r2, dst+(r2)*n, r-r2, c, n);
        } else {  // Nope
            c2 = c/2;
            swap_cf_z(src, dst, r, c2, n);
            swap_cf_z(src+(c2)*n, dst+c2, r, c-c2, n);
        }
    }
}

/*
 * Function:  isschur/isschurf
 * ---------------------------
 * checks whether a square array is (quasi)upper triangular and if any 2x2 blocks
 * are present whether these block are in the form of [[a, b], [c, a]] where b*c < 0.
 * This is to check that the 2x2 blocks have actually complex eigenvalues needed
 * for various algorithms.
 *
 *  data: the float/double array to be checked (in Fortran-contiguous layout)
 *  n: the size of the array
 *
 *  returns: an int value indicating it is Schur (1) or not (0).
 *
 * For complex arrays, being Schur is equivalent to being upper triangular.
 */
static inline int
isschurf(const float* restrict data, const Py_ssize_t n)
{
    float prev_a, prev_b;
    int isSchur = 1;
    int expect_zero = 0;
    for (Py_ssize_t j = 0; j < n; j++) {
        if (data[j*n + j+1] == 0.0f) {
            // Subdiagonal is zero
            if (expect_zero) {
                // Previous column had a non-zero entry and this marks the 2x2
                // block. Check the [[a, b], [c, a]] form with b*c < 0.
                if ((prev_a == data[j*n + j]) && (prev_b*data[j*n + j-1] < 0.0))
                {
                    expect_zero = 0; // All good, reset the flag
                } else {
                    // Broken 2x2 form
                    return 0;
                }
            }
            expect_zero = 0;
        } else {
            // Subdiagonal is non-zero. Either first column of 2x2 or not Schur
            if (expect_zero)
            {
                // Already nonzero in the previous col so not Schur.

Review comment (bug):
Here and elsewhere--did you intentionally remove the `inline` designation, or was it just intended to be removed so you could have better call stack access while developing/debugging? Naturally, the tests do pass locally when I restore it.

Code after:
            swap_cf_z(src, dst, r2, c, n);
            swap_cf_z(src + r2, dst+(r2)*n, r-r2, c, n);
        } else {  // Nope
            c2 = c/2;
            swap_cf_z(src, dst, r, c2, n);
            swap_cf_z(src+(c2)*n, dst+c2, r, c-c2, n);
        }
    }
}

/*
 * Function:  isschur/isschurf
 * ---------------------------
 * checks whether a square array is (quasi)upper triangular and if any 2x2 blocks
 * are present whether these block are in the form of [[a, b], [c, a]] where b*c < 0.
 * This is to check that the 2x2 blocks have actually complex eigenvalues needed
 * for various algorithms.
 *
 *  data: the float/double array to be checked (in Fortran-contiguous layout)
 *  n: the size of the array
 *
 *  returns: an int value indicating it is Schur (1) or not (0).
 *
 * For complex arrays, being Schur is equivalent to being upper triangular.
 */
static int
isschurf(const float* restrict data, const Py_ssize_t n)
{
    int in_block = 0;
    float block_diag, block_offdiag;

    for (Py_ssize_t k = 0; k < n - 1; k++) {
        // Below-subdiagonal entries in column k must be zero
        for (Py_ssize_t i = k + 2; i < n; i++) {
            if (data[k*n + i] != 0.0f) { return 0; }
        }

        if (in_block) {
            // Second column of 2x2 block: validate using current column only
            if (block_diag != data[k*n + k]) { return 0; }
            if (block_offdiag * data[k*n + k - 1] >= 0.0f) { return 0; }
            if (data[k*n + k + 1] != 0.0f) { return 0; }
            in_block = 0;
        } else if (data[k*n + k + 1] != 0.0f) {
            // First column of 2x2 block: save and mark
            block_diag = data[k*n + k];
            block_offdiag = data[k*n + k + 1];
            in_block = 1;
        }
    }
    // Validate if last block was 2x2.