Scalar Toom multiplication for fmpz_poly - #2813
Merged
Merged
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
fmpz_poly_mul_toom_scalarandfmpz_poly_mulmid_toom_scalar, "scalar" meaning Toom evaluation/interpolation done on the scalar level rather than recursively on polynomials. Supports full products of length up to 20, limited by 64-bit precomputed lookup tables. Pure middle products use transposed Newton interpolation; general truncated products internally pad to a full product or a pure middle product, whichever is shorter.Developed using Claude Opus 5.
Nice feature of the algorithm: full products write the output in-place with zero scratch space, so this is extremely memory-efficient.
Performance comparison below of
fmpz_poly_mulmid_toom_scalaralongsidefmpz_poly_mulmid_classical_fft_small. The numbers show the speedup compared to the currentfmpz_poly_mul/fmpz_poly_mullow/fmpz_poly_mulmid, i.e. >1.000 means winning over the current default algorithm."mul" is n x n -> 2n-1, "mullow" is n x n -> n, "mulmid" is 2n x n -> 2n
This shows that Toom generally beats or matches FFT caching for full products, while FFT caching generally wins for low products; for middle products the winner varies with the bit size.
Either way, there is now a lot to gain in
fmpz_poly_muland variants by switching to these algorithms for short polynomials with large coefficients. However, this will have to wait until there is better Karatsuba and maybe recursive Toom to compare with.