[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 21:54:54 PST 2024


AdvenamTacet wrote:

Until now, I was not really thinking how this algorithm works, but I finally looked at it closer and I don't think we should upstream it.

The idea is based on:
```
If power of two divides both numbers, we can push it out.
- gcd( 2^x * a, 2^x * b) = 2^x * gcd(a, b)

If and only if exactly one number is even, we can divide that number by  that power. 
- if a, b are odd, then gcd(2^x * a, b) = gcd(a, b)

And standard gcd algorithm where instead of modulo, minus is used.
```

Based on that knowledge, it's possible to create an example where the new GCD is hundred times slower. https://quick-bench.com/q/4_kHmlvAN7nwcDaMqk3_6BSGaRM

Moreover, we still can build a test like that, even if we exclude very small values. This algorithm works well for random values, but unfortunately not for all values. https://quick-bench.com/q/vUh_PAKAip6GLvnH3FvgnmzdK74

Potential performance hit is so big that I don't think we can upstream it.

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


More information about the libcxx-commits mailing list