Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions src/Nethermind.Int256.Benchmark/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,31 @@ public Int256 Subtract_Int256()
[MemoryDiagnoser]
public class AddModUnsinged : UnsignedThreeParamBenchmarkBase
{
[Benchmark(Baseline = true)]
[Benchmark]
public BigInteger AddMod_BigInteger()
{
return ((A.Item1 + B.Item1) % C.Item1);
}

[Benchmark(Baseline = true)]
public UInt256 AddMod_UInt256_Standard()
{
UInt256.AddModStandard(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}

[Benchmark]
public UInt256 AddMod_UInt256()
public UInt256 AddMod_UInt256_Barrett()
{
UInt256.AddMod(A.Item2, B.Item2, C.Item2, out UInt256 res);
UInt256.BarrettPrecompute(C.Item2, out UInt256 mu);
UInt256.AddModBarrett(A.Item2, B.Item2, C.Item2, mu, out UInt256 res);
return res;
}

[Benchmark]
public UInt256 AddMod_UInt256_Optimized()
{
UInt256.AddModOptimized(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}
}
Expand Down Expand Up @@ -208,16 +223,24 @@ public Int256 AddMod_Int256()
[MemoryDiagnoser]
public class SubtractModUnsinged : UnsignedThreeParamBenchmarkBase
{
[Benchmark(Baseline = true)]
[Benchmark]
public BigInteger SubtractMod_BigInteger()
{
return ((A.Item1 - B.Item1) % C.Item1);
}

[Benchmark(Baseline = true)]
public UInt256 SubtractMod_UInt256_Standard()
{
UInt256.SubtractModStandard(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}

[Benchmark]
public UInt256 SubtractMod_UInt256()
public UInt256 SubtractMod_UInt256_Barrett()
{
UInt256.SubtractMod(A.Item2, B.Item2, C.Item2, out UInt256 res);
UInt256.BarrettPrecompute(C.Item2, out UInt256 mu);
UInt256.SubtractModBarrett(A.Item2, B.Item2, C.Item2, mu, out UInt256 res);
return res;
}
}
Expand Down Expand Up @@ -284,16 +307,24 @@ public Int256 Multiply_Int256()
[MemoryDiagnoser]
public class MultiplyModUnsigned : UnsignedThreeParamBenchmarkBase
{
[Benchmark(Baseline = true)]
[Benchmark]
public BigInteger MultiplyMod_BigInteger()
{
return ((A.Item1 * B.Item1) % C.Item1);
}

[Benchmark(Baseline = true)]
public UInt256 MultiplyMod_UInt256_Standard()
{
UInt256.MultiplyModStandard(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}

[Benchmark]
public UInt256 MultiplyMod_UInt256()
public UInt256 MultiplyMod_UInt256_Barrett()
{
UInt256.MultiplyMod(A.Item2, B.Item2, C.Item2, out UInt256 res);
UInt256.BarrettPrecompute(C.Item2, out UInt256 mu);
UInt256.MultiplyModBarrett(A.Item2, B.Item2, C.Item2, mu, out UInt256 res);
return res;
}
}
Expand Down Expand Up @@ -398,16 +429,23 @@ public Int256 Exp_Int256()
[MemoryDiagnoser]
public class ExpModUnsigned : UnsignedThreeParamBenchmarkBase
{
[Benchmark(Baseline = true)]
[Benchmark]
public BigInteger ExpMod_BigInteger()
{
return BigInteger.ModPow(A.Item1, B.Item1, C.Item1);
}

[Benchmark(Baseline = true)]
public UInt256 ExpMod_UInt256_Standard()
{
UInt256.ExpModStandard(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}

[Benchmark]
public UInt256 ExpMod_UInt256()
public UInt256 ExpMod_UInt256_Barrett()
{
UInt256.ExpMod(A.Item2, B.Item2, C.Item2, out UInt256 res);
UInt256.ExpModBarrett(A.Item2, B.Item2, C.Item2, out UInt256 res);
return res;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind.Int256.Benchmark/NoIntrinsicsJobAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace Nethermind.Int256.Benchmark
{
public class NoIntrinsicsJobAttribute : JobConfigBaseAttribute
{
public NoIntrinsicsJobAttribute(RuntimeMoniker runtimeMoniker, int launchCount = -1, int warmupCount = -1, int iterationCount = -1, int invocationCount = -1, string id = null, bool baseline = false)
public NoIntrinsicsJobAttribute(RuntimeMoniker runtimeMoniker, int launchCount = -1, int warmupCount = -1, int iterationCount = -1, int invocationCount = -1, string? id = null, bool baseline = false)
: base(CreateJob(id, launchCount, warmupCount, iterationCount, invocationCount, null, baseline, runtimeMoniker)
.WithEnvironmentVariable("DOTNET_EnableHWIntrinsic", "0"))
{

}

private static Job CreateJob(string id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy, bool baseline, RuntimeMoniker runtimeMoniker = RuntimeMoniker.HostProcess)
private static Job CreateJob(string? id, int launchCount, int warmupCount, int iterationCount, int invocationCount, RunStrategy? runStrategy, bool baseline, RuntimeMoniker runtimeMoniker = RuntimeMoniker.HostProcess)
{
Job job = new Job(id);
int num = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind.Int256.Tests/Convertibles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public static (Type type, BigInteger? min, BigInteger? max)[] ConvertibleTypes =

private static IEnumerable<TestCaseData> GenerateTestCases(IEnumerable<(object, string)> numbers, BigInteger? minValue = null)
{
Type ExpectedException(BigInteger value, BigInteger? min, BigInteger? max) =>
Type? ExpectedException(BigInteger value, BigInteger? min, BigInteger? max) =>
(!min.HasValue || !max.HasValue || (value >= min && value <= max)) && (!minValue.HasValue || value >= minValue)
? null
: typeof(OverflowException);

string ExpectedString(Type type, BigInteger value, BigInteger? min, ref Type expectedException)
string? ExpectedString(Type type, BigInteger value, BigInteger? min, ref Type? expectedException)
{
string expectedString = null;
string? expectedString = null;
if (expectedException is not null && type == typeof(float))
{
expectedString = value < min ? "-∞" : "∞";
Expand All @@ -104,8 +104,8 @@ string ExpectedString(Type type, BigInteger value, BigInteger? min, ref Type exp
foreach ((Type type, BigInteger? min, BigInteger? max) in ConvertibleTypes)
{
BigInteger value = BigInteger.Parse(number.ToString()!);
Type expectedException = ExpectedException(value, min, max);
string expectedString = ExpectedString(type, value, min, ref expectedException);
Type? expectedException = ExpectedException(value, min, max);
string? expectedString = ExpectedString(type, value, min, ref expectedException);
string testName = $"Convert({name}, {type.Name}){(expectedException is not null || expectedString?.Contains('∞') == true ? " over/under flow" : "")}";
yield return new TestCaseData(type, number, expectedException, expectedString) { TestName = testName };
}
Expand Down
23 changes: 10 additions & 13 deletions src/Nethermind.Int256.Tests/UInt256Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@

namespace Nethermind.Int256.Test
{
public abstract class UInt256TestsTemplate<T> where T : IInteger<T>
public abstract class UInt256TestsTemplate<T>(
Func<BigInteger, T> convert,
Func<int, T> convertFromInt,
Func<BigInteger, BigInteger> postprocess,
BigInteger maxValue)
where T : IInteger<T>
{
protected readonly Func<BigInteger, T> convert;
protected readonly Func<int, T> convertFromInt;
protected readonly Func<BigInteger, BigInteger> postprocess;
protected readonly BigInteger maxValue;

protected UInt256TestsTemplate(Func<BigInteger, T> convert, Func<int, T> convertFromInt, Func<BigInteger, BigInteger> postprocess, BigInteger maxValue)
{
this.convert = convert;
this.convertFromInt = convertFromInt;
this.postprocess = postprocess;
this.maxValue = maxValue;
}
protected readonly Func<BigInteger, T> convert = convert;
protected readonly Func<int, T> convertFromInt = convertFromInt;
protected readonly Func<BigInteger, BigInteger> postprocess = postprocess;
protected readonly BigInteger maxValue = maxValue;

[TestCaseSource(typeof(BinaryOps), nameof(BinaryOps.TestCases))]
public virtual void Add((BigInteger A, BigInteger B) test)
Expand Down
Loading