-
Notifications
You must be signed in to change notification settings - Fork 5k
Optimize BigInteger.Multiply
by Toom-Cook multiplication
#112876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Tagging subscribers to this area: @dotnet/area-system-numerics |
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
0195ba7
to
1c6a1df
Compare
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
Hey @kzrnm , the current BigInteger is quite the mess. I developed a clean, 64bit, 2s compliment version which is 300% faster. BigInt Fibonacci(1,073,741,824) => 650 seconds My version also uses ToomCook3 (as well as other multiplication algorithms) https://github.com/tecel007/BigInt I would like a Faster BigInteger, but don't have time to integrate it. Seems like you are doing performance stuff... I really do think we need to start fresh, clean slate! KISS |
@tecel007 @IDisposable Please discuss topics unrelated to this Pull Request elsewhere (perhaps #114516). |
https://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication
I updated
BigInteger
multiplication to use the Toom-3 algorithm.The current Karatsuba algorithm has a time complexity of$O(n^{\log_2{3}}) \simeq O(n^{1.58})$ , which is expected to improve to $O(n^{\log_3{5}}) \simeq O(n^{1.46})$ resulting in better performance.
in other languages:
About the Implementation
SquareThreshold
andMultiplyKaratsubaThreshold
.{[MethodImpl(MethodImplOptions.AggressiveInlining)]
to avoid stack consumption when determining the algorithm.Why
MultiplyToom3Threshold
is 256?Based on the benchmark results, I decided to set
MultiplyToom3Threshold
to 256.Benchmark
When the number of digits is small, the preprocessing for algorithm selection is relatively high, leading to a slight regression—for example, a computation that previously took 19 μs now takes 20 μs.
However, as the number of digits increases, performance improves; for instance, a multiplication that used to take 750 μs is now completed in 690 μs.
Code