Skip to content

[integer] Add multiply_by_word_inplace() and use it for product_range()#256

Merged
forfudan merged 2 commits into
mainfrom
dev
Jul 1, 2026
Merged

[integer] Add multiply_by_word_inplace() and use it for product_range()#256
forfudan merged 2 commits into
mainfrom
dev

Conversation

@forfudan

@forfudan forfudan commented Jun 27, 2026

Copy link
Copy Markdown
Owner

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 a UInt32.
  • 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a UInt32.
  • 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 thread src/decimo/bigint/arithmetics.mojo
Comment thread src/decimo/bigint/special.mojo Outdated
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)
@forfudan forfudan merged commit bcb8f24 into main Jul 1, 2026
11 checks passed
@forfudan forfudan deleted the dev branch July 1, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants