Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves BigInt “special” operations performance by introducing an in-place single-word multiply helper and using it to optimize the leaf cases of the binary-splitting product_range() used by factorial/permutation. It also tightens permutation’s allowable n range to values that fit in a single 32-bit word, and adds tests covering the new behavior and performance-related code paths.
Changes:
- Add
multiply_by_word_inplace()for repeated in-place multiplication by aUInt32. - Optimize
product_range()leaf computation to accumulate via in-place word multiplies instead of allocating intermediate BigInts. - Update permutation’s input constraints (reject
n > 2^32 - 1) and add new tests for factorial/permutation edge cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/decimo/bigint/arithmetics.mojo |
Adds multiply_by_word_inplace() helper used for faster repeated small-factor multiplication. |
src/decimo/bigint/special.mojo |
Uses in-place word multiplication for product_range() leaves; tightens permutation’s n bound to WORD_MAX. |
tests/bigint/test_bigint_special.mojo |
Adds tests to exercise the binary-split factorial path and the updated permutation constraints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
127
to
154
| @@ -128,9 +144,11 @@ def permutation(x: BigInt, k: Int) raises -> BigInt: | |||
| message="Permutation k is too large to compute (must be <= 10^6).", | |||
| function="permutation()", | |||
| ) | |||
| if x > BigInt(Int.MAX): | |||
| if x > BigInt(BigInt.WORD_MAX): | |||
| raise ValueError( | |||
| message="Permutation n is too large to fit in an Int.", | |||
| message=( | |||
| "Permutation n is too large to compute (must be <= 2^32 - 1)." | |||
| ), | |||
| function="permutation()", | |||
| ) | |||
| var n = Int(x) | |||
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.
This PR improves BigInt “special” operations performance by introducing an in-place single-word multiply helper and using it to optimize the leaf cases of the binary-splitting
product_range()used by factorial/permutation. It also tightens permutation’s allowablenrange to values that fit in a single 32-bit word, and adds tests covering the new behavior and performance-related code paths.Changes:
multiply_by_word_inplace()for repeated in-place multiplication by aUInt32.product_range()leaf computation to accumulate via in-place word multiplies instead of allocating intermediate BigInts.n > 2^32 - 1) and add new tests for factorial/permutation edge cases.