Skip to content

fix(bits): terminate unsigned multiplication and preserve high-bit products - #2242

Open
Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/unsigned-multiply-high-bit
Open

Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/unsigned-multiply-high-bit

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

multiplyUnsigned(1, 0xffffffff) never returns because the signed right shift keeps extending the high bit. Use a zero-fill shift so every unsigned 32-bit multiplier reaches zero.

Accumulate a numerically doubled multiplicand instead of using a signed left shift, which would otherwise return incorrect negative or truncated partial products after fixing termination. For example, multiplyUnsigned(2, 0xffffffff) now returns 8589934590.

Fixes #2232.

Validation: the original high-bit case times out in an isolated 500 ms VM. The fix passes 20,001 comparisons against BigInt products within the safe-integer range, all 178 suites / 593 tests, full coverage, and ESLint. Six committed regressions cover zero, high bits, both operand orders, and products exceeding 32 bits. Results remain JavaScript Numbers, with the usual precision limits beyond safe integers.

AI-assisted contribution.

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.

multiplyUnsigned hangs for multiplier >= 2^31 (>>= sign-extends)

1 participant