Remove unsafe code from System.Numerics.BigIntegerCalculator.ShiftRot#127847
Draft
EgorBo wants to merge 1 commit intodotnet:mainfrom
Draft
Remove unsafe code from System.Numerics.BigIntegerCalculator.ShiftRot#127847EgorBo wants to merge 1 commit intodotnet:mainfrom
EgorBo wants to merge 1 commit intodotnet:mainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Member
Author
Contributor
There was a problem hiding this comment.
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
LeftShiftSelfto use sliced spans plusVector128/256/512.CreateandCopyToinstead 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.
Member
Author
|
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;
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR is AI-generated.
+5b asm diffs