Repository: valkey-io/valkey
File: src/bitops.c (C)

Code before:
    for (; p + 16 <= e; p += 16) {
        t += vaddvq_u8(vcntq_u8(vld1q_u8(p)));
    }

    /* Handle remaining bytes with scalar fallback */
    if (p < e) {
        size_t r = e - p;
        t += popcountScalar((void *)p, r);
    }

    return t;
}
#endif

/* Count number of bits set in the binary array pointed by 's' and long
 * 'count' bytes. The implementation of this function is required to
 * work with an input string length up to 512 MB or more (server.proto_max_bulk_len) */
long long serverPopcount(void *s, long count) {
#if HAVE_X86_SIMD
    /* If length of s >= 256 bits and the CPU supports AVX2,
     * we prefer to use the SIMD version */
    if (count >= 32) {
        return popcountAVX2(s, count);
    }
#endif
#ifdef __ARM_NEON
    if (count >= 16) {
        return popcountNEON(s, count);
    }
#endif

    return popcountScalar(s, count);
}

/* Return the position of the first bit set to one (if 'bit' is 1) or
 * zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes.
 *
 * The function is guaranteed to return a value >= 0 if 'bit' is 0 since if
 * no zero bit is found, it returns count*8 assuming the string is zero
 * padded on the right. However if 'bit' is 1 it is possible that there is
 * not a single set bit in the bitmap. In this special case -1 is returned. */
long long serverBitpos(void *s, unsigned long count, int bit) {
    unsigned long *l;
    unsigned char *c;
    unsigned long skipval, word = 0, one;
    long long pos = 0; /* Position of bit, to return to the caller. */
    unsigned long j;
    int found;

    /* Process whole words first, seeking for first word that is not
     * all ones or all zeros respectively if we are looking for zeros

Review comment (suggestion):
The other code is guarded with the `__aarch64__` macro, so we should probably use the same here here. I guess there is som 32-bit ARM where `__ARM_NEON` is defined.

```suggestion
#ifdef __aarch64__
```

Code after:
    for (; p + 16 <= e; p += 16) {
        t += vaddvq_u8(vcntq_u8(vld1q_u8(p)));
    }

    /* Handle remaining bytes with scalar fallback */
    if (p < e) {
        size_t r = e - p;
        t += popcountScalar((void *)p, r);
    }

    return t;
}
#endif

/* Count number of bits set in the binary array pointed by 's' and long
 * 'count' bytes. The implementation of this function is required to
 * work with an input string length up to 512 MB or more (server.proto_max_bulk_len) */
long long serverPopcount(void *s, long count) {
#if HAVE_X86_SIMD
    /* If length of s >= 256 bits and the CPU supports AVX2,
     * we prefer to use the SIMD version */
    if (count >= 32) {
        return popcountAVX2(s, count);
    }
#endif
#ifdef __aarch64__
    if (count >= 16) {
        return popcountNEON(s, count);
    }
#endif

    return popcountScalar(s, count);
}

/* Return the position of the first bit set to one (if 'bit' is 1) or
 * zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes.
 *
 * The function is guaranteed to return a value >= 0 if 'bit' is 0 since if
 * no zero bit is found, it returns count*8 assuming the string is zero
 * padded on the right. However if 'bit' is 1 it is possible that there is
 * not a single set bit in the bitmap. In this special case -1 is returned. */
long long serverBitpos(void *s, unsigned long count, int bit) {
    unsigned long *l;
    unsigned char *c;
    unsigned long skipval, word = 0, one;
    long long pos = 0; /* Position of bit, to return to the caller. */
    unsigned long j;
    int found;

    /* Process whole words first, seeking for first word that is not
     * all ones or all zeros respectively if we are looking for zeros