[libcxx-commits] [libcxx] [libc++] Implement std::gcd using the binary version (PR #77747)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 17 22:30:51 PST 2024


philnik777 wrote:

@AdvenamTacet You always have to be careful when writing benchmarks. If you look at the generated code, you see that Clang was able to constant-fold the native version and you only benchmark moving `0x1` into memory. While we should also consider this, it's not exactly a common case. If you avoid that the numbers look much better: https://quick-bench.com/q/KxLGz_xNDsVCEd3UUgBD6R9oPlY.
Much more importantly: I can produce similarly bad cases for just about any optimziation in the library. The question is how likely it is that this is an actual problem in the wild. I'd guess that it's not very common to calculate the gcd of `(2^n)-1` and 1.
Given that, there are a few questions to answer:
- In which cases is it faster or slower?
- How often do we expect these cases to occur?
- Would it be possible to detect these cases and decide which algorithm to use based on that? (e.g. with a `popcount`)
- How much of a difference does it make if we don't have perfect branch prediction?


https://github.com/llvm/llvm-project/pull/77747


More information about the libcxx-commits mailing list