Skip to content

Remove unsafe code from System.Numerics.BigIntegerCalculator.ShiftRot#127847

Draft
EgorBo wants to merge 1 commit intodotnet:mainfrom
EgorBo:reduce-unsafe-bigint-shift
Draft

Remove unsafe code from System.Numerics.BigIntegerCalculator.ShiftRot#127847
EgorBo wants to merge 1 commit intodotnet:mainfrom
EgorBo:reduce-unsafe-bigint-shift

Conversation

@EgorBo
Copy link
Copy Markdown
Member

@EgorBo EgorBo commented May 6, 2026

Note

This PR is AI-generated.

+5b asm diffs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 6, 2026 00:05
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

@EgorBo
Copy link
Copy Markdown
Member Author

EgorBo commented May 6, 2026

@MihuBot

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 refactors System.Runtime.Numerics' internal BigIntegerCalculator shift/rotate implementation to remove direct LoadUnsafe/StoreUnsafe usage from the SIMD paths, which sit underneath BigInteger shift and rotate operations.

Changes:

  • Reworks LeftShiftSelf to use sliced spans plus Vector128/256/512.Create and CopyTo instead of ref+offset-based vector loads/stores.
  • Applies the same span-based vector load/store pattern to RightShiftSelf.
  • Changes loop bookkeeping from integer offsets to shrinking Span<nuint> windows while keeping the existing carry/scalar fallback structure.

@EgorBo
Copy link
Copy Markdown
Member Author

EgorBo commented May 6, 2026

Note

AI-generated benchmark.

@EgorBot -arm -amd

using System;
using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

public class Bench
{
    private BigInteger _value;

    [Params(128, 1024, 8192)]
    public int Bits { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        var rng = new Random(42);
        byte[] bytes = new byte[Bits / 8];
        rng.NextBytes(bytes);
        bytes[^1] &= 0x7F; // ensure positive
        _value = new BigInteger(bytes);
    }

    [Benchmark]
    public BigInteger LeftShift() => _value << 7;

    [Benchmark]
    public BigInteger RightShift() => _value >> 7;
}

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants