3 ms·
> For what it's worth, this is largely unnecessary nowadays. Not true, compilers remain extremely conservative in their default codegen, we’re talking pre-neha
by masklinn 4y ago
> For what it's worth, this is largely unnecessary nowadays.
Not true, compilers remain extremely conservative in their default codegen, we’re talking pre-nehalem (SSE2 at the top end).
I just checked on godbolt, by default u32::count_ones compiled with rustc 1.62 with -O generates 15 lines of assembly hand-counting the bits in the word.
Adding `-C target-cpu=x86-64-v2` reduces that to a single popcnt.
Even if you don’t care for SSE3+, there’s a handful of really useful instructions which are not part of the baseline x86 profile, especially for bit-twiddling and endianness manipulation: popcnt, lzcnt (v3), movbe (v3), BMI1 and BMI2 (v3)
- mcbain 4y agoMy vote is that pext and pdep are the highlight from BMI2.
- planede 4y agoJust be aware that even though these instructions are supported on pre-zen3 AMD, they are pretty much emulated and not very fast. Otherwise I agree that these are awesome, I know a fast Hilbert-curve implementation using those.
- klodolph 4y agoYes, there’s the annoying tendency of extensions to be slow on the first generation of processors that support them, and then faster afterwards.
- planede 4y agoWhile that's true, the PDEP/PEXT numbers are a bit on the extreme side on AMD early generations. On Zen1 and Zen2 these instructions have latency and rthroughput of 18 and 19 clock cycles respectively. On Zen3 it's a perfectly sensible 3 cycle latency and 1 cycle rthroughput [1]. I'm not aware of an other extensions with such a slow implementation in first generations. [1] https://www.agner.org/optimize/instruction_tables.pdf https://www.agner.org/optimize/instruction_tables.pdf It's unfortunate that PDEP and PEXT is part of BMI2. AMD probably wanted to push BMI2 for the other instructions and they had to include emulated PDEP and PEXT before they had a proper implementation for those.