Skip to content

refactor: optimize number/uint32/base/muldw implementation for better performance#11702

Open
impawstarlight wants to merge 3 commits intostdlib-js:developfrom
impawstarlight:refactor/optimize-umuldw
Open

refactor: optimize number/uint32/base/muldw implementation for better performance#11702
impawstarlight wants to merge 3 commits intostdlib-js:developfrom
impawstarlight:refactor/optimize-umuldw

Conversation

@impawstarlight
Copy link
Copy Markdown
Contributor

Resolves none.

Description

What is the purpose of this pull request?

This pull request:

  • Optimizes number/uint32/base/muldw implementation to improve performance by moving the isnan check out of the lower level implementation.
  • Utilizes number/uint32/base/mul for computing the lower half of the double word product to reduce unnecessary calculation.
  • Remove NaN tests for the lower level implementation.

Related Issues

Does this pull request have any related issues?

No.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

{{TODO: add disclosure if applicable}}


@stdlib-js/reviewers

…`umul`

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@impawstarlight impawstarlight requested a review from a team April 21, 2026 19:58
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Apr 21, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

stdlib-bot commented Apr 21, 2026

Coverage Report

Package Statements Branches Functions Lines
number/uint32/base/muldw $\color{green}185/185$
$\color{green}+0.00%$
$\color{green}8/8$
$\color{green}+0.00%$
$\color{green}2/2$
$\color{green}+0.00%$
$\color{green}185/185$
$\color{green}+0.00%$

The above coverage report was generated for the changes in this PR.

@kgryte kgryte added difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. review: 5 and removed Needs Review A pull request which needs code review. labels Apr 21, 2026
Comment thread lib/node_modules/@stdlib/number/uint32/base/muldw/lib/assign.js Outdated
* // returns [ 954437176, 1908874354 ]
*/
function umuldw( a, b ) {
if ( isnan( a ) || isnan( b ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@impawstarlight What is the rationale for keeping these checks here but not in assign.js?

Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>

out[ offset ] = ( ( ha*hb ) + w1 + k ) >>> 0; // compute the higher 32 bits and cast to an unsigned 32-bit integer
out[ offset + stride ] = ( ( t << 16 ) + w3) >>> 0; // compute the lower 32 bits and cast to an unsigned 32-bit integer
out[ offset + stride ] = umul( a, b ) >>> 0; // compute the lower 32 bits and cast to an unsigned 32-bit integer
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@impawstarlight I am a bit dense, but how does this manage to produce the same result? Previously, the logic for computing the lower 32 bits doesn't exceed the max uint32, but, here, a*b could, resulting in wraparound, which is a bit counterintuitive to me that it achieves the same result.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ultimately, this boils down to a call to imul, but not obvious to me why imul is faster than a bit shift plus addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Needs Discussion Needs further discussion. review: 4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants