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

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 17 23:41:48 PST 2024


AdvenamTacet wrote:

@philnik777 thx for looking at it and fixing my benchmark! It's still big performance hit, but you are right that it's probably unlikely to see it in the wild often.

> - In which cases is it faster or slower?
> - Would it be possible to detect these cases and decide which algorithm to use based on that? (e.g. with a popcount)

My fast tests show that for random numbers or interval of numbers, it's unlikely to have worse performance with this new implementation when  `abs(__a) > 250 && abs(__b) > 250`. Probably we can do something similar with a number chosen more carefully.

> How often do we expect these cases to occur?

On every big interval I tested, implementation from this PR is faster, I did not try to check how often it's slower and how often it's faster. I can try to look at it soon.
Probably we should look at discrete logarithm for a better estimation.

On average, it may have a positive effect, if we are ok with big slowdown in some rare cases.
https://grep.app/search?q=std%3A%3Agcd%28
https://grep.app/search?current=3&q=gcd%28

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


More information about the libcxx-commits mailing list