-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Optimize BigInteger.Multiply
by Toom-Cook multiplication
#112876
Open
kzrnm
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
kzrnm:BigIntegerToom3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+847
−222
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tagging subscribers to this area: @dotnet/area-system-numerics |
This was referenced Feb 24, 2025
Open
IDisposable
reviewed
Feb 27, 2025
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
IDisposable
reviewed
Feb 27, 2025
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Outdated
Show resolved
Hide resolved
huoyaoyuan
reviewed
Feb 28, 2025
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
This was referenced Feb 28, 2025
IDisposable
reviewed
Feb 28, 2025
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs
Show resolved
Hide resolved
0195ba7
to
1c6a1df
Compare
This was referenced Feb 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-System.Numerics
community-contribution
Indicates that the PR has been added by a community member
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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