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

Code before:
        /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4);
    /* clang-format on */
    const __m256i low_mask = _mm256_set1_epi8(0x0f);
    __m256i acc = _mm256_setzero_si256();

/* Count 32 bytes per iteration. */
#define ITER_32_BYTES                                                             \
    {                                                                             \
        const __m256i vec = _mm256_loadu_si256((const __m256i *)(p + i));         \
        const __m256i lo = _mm256_and_si256(vec, low_mask);                       \
        const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(vec, 4), low_mask); \
        const __m256i popcnt1 = _mm256_shuffle_epi8(lookup, lo);                  \
        const __m256i popcnt2 = _mm256_shuffle_epi8(lookup, hi);                  \
        local = _mm256_add_epi8(local, popcnt1);                                  \
        local = _mm256_add_epi8(local, popcnt2);                                  \
        i += 32;                                                                  \
    }

    /* We divide the array into the following three parts
     *        Part A         Part B       Part C
     * +-----------------+--------------+---------+
     * | 8 * 32bytes * X |  32bytes * Y | Z bytes |
     * +-----------------+--------------+---------+
     */

    /* Parrt A: loop unrolling, processing 8 * 32 bytes per iteration. */
    while (i + 8 * 32 <= count) {
        __m256i local = _mm256_setzero_si256();
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));
    }

    /* Part B: when the remaining data length is less than 8 * 32 bytes,
     * process 32 bytes per iteration. */
    __m256i local = _mm256_setzero_si256();
    while (i + 32 <= count) {
        ITER_32_BYTES;
    }
    acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));

#undef ITER_32_BYTES

    bits += _mm256_extract_epi64(acc, 0);
    bits += _mm256_extract_epi64(acc, 1);

Review comment (suggestion):
```suggestion
    /* Part A: loop unrolling, processing 8 * 32 bytes per iteration. */
```

Code after:
        /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
        /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4);
    /* clang-format on */
    const __m256i low_mask = _mm256_set1_epi8(0x0f);
    __m256i acc = _mm256_setzero_si256();

/* Count 32 bytes per iteration. */
#define ITER_32_BYTES                                                             \
    {                                                                             \
        const __m256i vec = _mm256_loadu_si256((const __m256i *)(p + i));         \
        const __m256i lo = _mm256_and_si256(vec, low_mask);                       \
        const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(vec, 4), low_mask); \
        const __m256i popcnt1 = _mm256_shuffle_epi8(lookup, lo);                  \
        const __m256i popcnt2 = _mm256_shuffle_epi8(lookup, hi);                  \
        local = _mm256_add_epi8(local, popcnt1);                                  \
        local = _mm256_add_epi8(local, popcnt2);                                  \
        i += 32;                                                                  \
    }

    /* We divide the array into the following three parts
     *        Part A         Part B       Part C
     * +-----------------+--------------+---------+
     * | 8 * 32bytes * X |  32bytes * Y | Z bytes |
     * +-----------------+--------------+---------+
     */

    /* Part A: loop unrolling, processing 8 * 32 bytes per iteration. */
    while (i + 8 * 32 <= count) {
        __m256i local = _mm256_setzero_si256();
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        ITER_32_BYTES
        acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));
    }

    /* Part B: when the remaining data length is less than 8 * 32 bytes,
     * process 32 bytes per iteration. */
    __m256i local = _mm256_setzero_si256();
    while (i + 32 <= count) {
        ITER_32_BYTES;
    }
    acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));

#undef ITER_32_BYTES

    bits += _mm256_extract_epi64(acc, 0);