mirror of
https://github.com/redis/redis.git
synced 2026-04-22 19:37:30 -04:00
This PR replaces the manual `popcount64()` implementation with `__builtin_popcountll()` for computing Hamming distance in binary vectors, when the underlying hardware supports the `POPCNT` instruction. The built-in version simplifies the code and enables the compiler to emit a single `POPCNT` instruction on supported CPUs, which is significantly faster than the manual bitwise method. You can verify the difference here: [https://godbolt.org/z/TxWMcE8M3](https://godbolt.org/z/TxWMcE8M3) — the manual version generates a long sequence of instructions (approximately 34 on modern HW) vs 1 instruction (popcnt) when using __builtin_popcountll() ## Portability across platforms This change maintains full portability across platforms and compilers. The use of `__builtin_popcountll()` is guarded by the `HAVE_POPCNT` macro, which is defined only when the compiler supports the target("popcnt") attribute. At runtime, we also check `__builtin_cpu_supports("popcnt")` to ensure the hardware provides support for the instruction. If not available, the implementation safely falls back to the original manual `popcount64()` logic. --------- Co-authored-by: debing.sun <debing.sun@redis.com>