-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify BigInteger.Multiply #98587
Simplify BigInteger.Multiply #98587
Conversation
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsIn my previous pull request #92208, I split BigIntegerCalculator.Multiply into two methods: MultiplyNearLength and MultiplyFarLength. The reason for this division is that BenchmarkBenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);
public class Tests
{
public IEnumerable<object> GetMultiplyArgs()
{
Random random = new Random(227);
var lengths = new int[] { 1000, 10000, 100000, 1000000 };
for (int i = lengths.Length - 1; i >= 0; i--)
{
var largeBytes = MakeBytes(random, lengths[i]);
var large = new BigInteger(largeBytes, isUnsigned: true);
{
var smallBytes = MakeBytes(random, lengths[i] / 2);
var small = new BigInteger(smallBytes, isUnsigned: true);
yield return new Data($"{largeBytes.Length:D7}-{smallBytes.Length:D7}", large, small);
}
for (int j = i; j >= 0; j--)
{
var smallBytes = MakeBytes(random, lengths[j]);
var small = new BigInteger(smallBytes, isUnsigned: true);
yield return new Data($"{largeBytes.Length:D7}-{smallBytes.Length:D7}", large, small);
}
}
static byte[] MakeBytes(Random random, int length)
{
var bytes = new byte[length];
random.NextBytes(bytes);
return bytes;
}
}
public record Data(string Name, BigInteger Large, BigInteger Small)
{
public override string ToString() => Name;
}
[Benchmark]
[ArgumentsSource(nameof(GetMultiplyArgs))]
public BigInteger Multiply(Data data)
{
return data.Large * data.Small;
}
}
|
80d294c
to
763ebdd
Compare
763ebdd
to
f7eaf3b
Compare
f7eaf3b
to
7d4f903
Compare
7d4f903
to
08ec62a
Compare
08ec62a
to
95c7edc
Compare
95c7edc
to
f085edc
Compare
Co-authored-by: Tanner Gooding <tagoo@outlook.com>
In my previous pull request #92208, I split BigIntegerCalculator.Multiply into two methods: MultiplyNearLength and MultiplyFarLength.
However, this division resulted in a lot of code duplication.
The reason for this division is that
coreLength
can be greater thanbits.Length - n
. Actually,core.Slice(0, ActualLength(core))
seems to be sufficient sinceActualLength(core))
is never greater thanbits.Length - n
.https://github.com/kzrnm/dotnet-runtime/blob/f7eaf3b17fb4bd753b3404f0c9977c9693a2b480/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.SquMul.cs#L282-L326
Benchmark