Skip to content

<__msvc_int128.hpp>: optimize _Signed128::operator* with a branchless multiply - #6406

Open
KKoishi_ (Koishi-Satori) wants to merge 4 commits into
microsoft:mainfrom
Koishi-Satori:main
Open

<__msvc_int128.hpp>: optimize _Signed128::operator* with a branchless multiply#6406
KKoishi_ (Koishi-Satori) wants to merge 4 commits into
microsoft:mainfrom
Koishi-Satori:main

Conversation

@Koishi-Satori

@Koishi-Satori KKoishi_ (Koishi-Satori) commented Aug 18, 2026

Copy link
Copy Markdown

Resolves issue #6405.

std::_Signed128::operator* in <__msvc_int128.hpp> perform sign-normalizes before multiplying, but it is unnecessary and will cause performance issue.

The low 128 bits of a two's-complement product equal the unsigned product, so the sign handling is unnecessary. I replaced it with the same computation _Base128::_Multiply already performs.

I've tried to implement a version without sign handling, the new version has same behavior and will not break any ABI. The new version without sign-normalizing is more faster than current version in <__msvc_int128.hpp>.

This PR also adds a signed128_mul benchmark comparing the new implementation against a faithful replica of the old one.

Benchmark

On MSVC x64, Release (/O2), measured on a GitHub Actions hosted runner (shared 4-vCPU VM; absolute numbers vary between runs):

Benchmark Old (sign-normalizing) New (branchless) Speedup
bm_signed128_mul (independent multiplies) 9.66 ns 6.94 ns ~1.39x
bm_signed128_mul_horner (5 dependent multiplies) 80.9 ns 9.15 ns ~8.8x

The first case measures multiplication throughput. The second evaluates a degree-5 polynomial with a Horner chain, where every multiply depends on the previous result; this is the same computation shape used by fixed-point math functions such as log2 and sin. Removing the sign normalization shortens the serial dependency chain of each multiply (and eliminates its branches), so latency-bound code benefits far more than the throughput microbenchmarksuggests.

@Koishi-Satori
KKoishi_ (Koishi-Satori) requested a review from a team as a code owner August 18, 2026 09:20
Copilot AI balanced review requested due to automatic review settings August 18, 2026 09:20
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Aug 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Optimizes _Signed128 multiplication by avoiding sign normalization and adds a microbenchmark to compare new vs. old behavior/performance.

Changes:

  • Replaced _Signed128::operator* implementation with a direct low-128-bit product using _UMul128 + cross terms.
  • Added a new Google Benchmark program to compare the new multiplication vs. the previous implementation.
  • Registered the new benchmark target in the benchmarks CMake list.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
stl/inc/__msvc_int128.hpp Reworks _Signed128 multiplication implementation to compute the low 128 bits directly.
benchmarks/src/signed128_mul.cpp Adds a benchmark comparing new vs. old signed-128 multiplication.
benchmarks/CMakeLists.txt Adds the new signed128_mul benchmark target.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread stl/inc/__msvc_int128.hpp Outdated
Comment thread stl/inc/__msvc_int128.hpp Outdated
Comment thread benchmarks/src/signed128_mul.cpp Outdated
Comment thread benchmarks/src/signed128_mul.cpp Outdated
Comment thread benchmarks/src/signed128_mul.cpp Outdated
@Koishi-Satori

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Comment thread stl/inc/__msvc_int128.hpp Outdated
Copilot AI review requested due to automatic review settings August 21, 2026 09:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

stl/inc/__msvc_int128.hpp:1377

  • Optional: this reimplements _Base128::_Multiply verbatim (defined at lines 422–427), creating a second multiplication core that can drift when the shared implementation changes. The unsigned operator already delegates to that helper at lines 1025–1026; returning its result here removes the sign-normalization branches while preserving a single implementation.
        _Signed128 _Result;
        _Result._Word[0] = _UMul128(_Left._Word[0], _Right._Word[0], _Result._Word[1]);
        _Result._Word[1] += _Left._Word[1] * _Right._Word[0];
        _Result._Word[1] += _Left._Word[0] * _Right._Word[1];

Comment thread stl/inc/__msvc_int128.hpp Outdated
Co-authored-by: statementreply <statementreply@gmail.com>
Copilot AI review requested due to automatic review settings August 23, 2026 01:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please provide your benchmark numbers.

See Benchmarking the STL on the Wiki or follow other PR precedents.

Comment thread benchmarks/src/signed128_mul.cpp Outdated
Comment thread benchmarks/src/signed128_mul.cpp Outdated
Comment thread benchmarks/src/signed128_mul.cpp Outdated
@github-project-automation github-project-automation Bot moved this from Initial Review to Work In Progress in STL Code Reviews Aug 24, 2026
@StephanTLavavej

This comment was marked as resolved.

@azure-pipelines

This comment was marked as resolved.

Copilot AI review requested due to automatic review settings August 24, 2026 15:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@Koishi-Satori

Copy link
Copy Markdown
Author

Please provide your benchmark numbers.

See Benchmarking the STL on the Wiki or follow other PR precedents.

I've added benchmark results in PR description.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Work In Progress to Initial Review in STL Code Reviews Aug 24, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

/azp run STL-CI

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

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

Labels

performance Must go faster

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

7 participants