Skip to content

Commit f037b96

Browse files
lemireDaniel Lemire
andauthored
Fixing Avx2 tests (#38)
--------- Co-authored-by: Daniel Lemire <dlemire@lemire.me>
1 parent 8d78bba commit f037b96

File tree

6 files changed

+536
-316
lines changed

6 files changed

+536
-316
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ dotnet test
3030

3131
To see which tests are running, we recommend setting the verbosity level:
3232

33+
```
34+
dotnet test -v=normal
35+
```
36+
37+
More details could be useful:
3338
```
3439
dotnet test -v d
3540
```
@@ -89,7 +94,6 @@ dotnet build
8994
We recommend you use `dotnet format`. E.g.,
9095

9196
```
92-
cd test
9397
dotnet format
9498
```
9599

benchmark/Benchmark.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ public class Speed : IColumn
2727
{
2828
public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
2929
{
30+
if (summary is null || benchmarkCase is null || benchmarkCase.Parameters is null)
31+
{
32+
return "N/A";
33+
}
3034
var ourReport = summary.Reports.First(x => x.BenchmarkCase.Equals(benchmarkCase));
3135
var fileName = (string)benchmarkCase.Parameters["FileName"];
32-
long length = new System.IO.FileInfo(fileName).Length;
33-
if (ourReport.ResultStatistics is null)
36+
if (ourReport is null || ourReport.ResultStatistics is null)
3437
{
3538
return "N/A";
3639
}
40+
long length = new System.IO.FileInfo(fileName).Length;
3741
var mean = ourReport.ResultStatistics.Mean;
3842
return $"{(length / ourReport.ResultStatistics.Mean):#####.00}";
3943
}
@@ -57,8 +61,8 @@ public string GetValue(Summary summary, BenchmarkCase benchmarkCase)
5761
[Config(typeof(Config))]
5862
public class RealDataBenchmark
5963
{
60-
61-
private class Config : ManualConfig
64+
#pragma warning disable CA1812
65+
private sealed class Config : ManualConfig
6266
{
6367
public Config()
6468
{
@@ -67,6 +71,7 @@ public Config()
6771

6872
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
6973
{
74+
#pragma warning disable CA1303
7075
Console.WriteLine("ARM64 system detected.");
7176
AddFilter(new AnyCategoriesFilter(["arm64", "scalar", "runtime"]));
7277

@@ -75,21 +80,25 @@ public Config()
7580
{
7681
if (Vector512.IsHardwareAccelerated && System.Runtime.Intrinsics.X86.Avx512Vbmi.IsSupported)
7782
{
83+
#pragma warning disable CA1303
7884
Console.WriteLine("X64 system detected (Intel, AMD,...) with AVX-512 support.");
7985
AddFilter(new AnyCategoriesFilter(["avx512", "avx", "sse", "scalar", "runtime"]));
8086
}
8187
else if (Avx2.IsSupported)
8288
{
89+
#pragma warning disable CA1303
8390
Console.WriteLine("X64 system detected (Intel, AMD,...) with AVX2 support.");
8491
AddFilter(new AnyCategoriesFilter(["avx", "sse", "scalar", "runtime"]));
8592
}
8693
else if (Ssse3.IsSupported)
8794
{
95+
#pragma warning disable CA1303
8896
Console.WriteLine("X64 system detected (Intel, AMD,...) with Sse4.2 support.");
8997
AddFilter(new AnyCategoriesFilter(["sse", "scalar", "runtime"]));
9098
}
9199
else
92100
{
101+
#pragma warning disable CA1303
93102
Console.WriteLine("X64 system detected (Intel, AMD,...) without relevant SIMD support.");
94103
AddFilter(new AnyCategoriesFilter(["scalar", "runtime"]));
95104
}
@@ -131,13 +140,13 @@ public Config()
131140
@"data/turkish.utf8.txt",
132141
@"data/vietnamese.utf8.txt")]
133142
public string? FileName;
134-
public byte[] allLinesUtf8 = new byte[0];
143+
private byte[] allLinesUtf8 = Array.Empty<byte>();
135144

136145

137146
public unsafe delegate byte* Utf8ValidationFunction(byte* pUtf8, int length);
138147
public unsafe delegate byte* DotnetRuntimeUtf8ValidationFunction(byte* pUtf8, int length, out int utf16CodeUnitCountAdjustment, out int scalarCountAdjustment);
139148

140-
public void RunUtf8ValidationBenchmark(byte[] data, Utf8ValidationFunction validationFunction)
149+
private void RunUtf8ValidationBenchmark(byte[] data, Utf8ValidationFunction validationFunction)
141150
{
142151
unsafe
143152
{
@@ -152,7 +161,7 @@ public void RunUtf8ValidationBenchmark(byte[] data, Utf8ValidationFunction valid
152161
}
153162
}
154163

155-
public void RunDotnetRuntimeUtf8ValidationBenchmark(byte[] data, DotnetRuntimeUtf8ValidationFunction validationFunction)
164+
private void RunDotnetRuntimeUtf8ValidationBenchmark(byte[] data, DotnetRuntimeUtf8ValidationFunction validationFunction)
156165
{
157166
unsafe
158167
{
@@ -229,18 +238,18 @@ public unsafe void SIMDUtf8ValidationRealDataArm64()
229238

230239
[Benchmark]
231240
[BenchmarkCategory("avx")]
232-
public unsafe void SIMDUtf8ValidationRealDataAvx2()
233-
{
234-
if (allLinesUtf8 != null)
235-
{
241+
public unsafe void SIMDUtf8ValidationRealDataAvx2()
242+
{
243+
if (allLinesUtf8 != null)
244+
{
236245
RunUtf8ValidationBenchmark(allLinesUtf8, (byte* pInputBuffer, int inputLength) =>
237246
{
238247
int dummyUtf16CodeUnitCountAdjustment, dummyScalarCountAdjustment;
239248
// Call the method with additional out parameters within the lambda.
240249
// You must handle these additional out parameters inside the lambda, as they cannot be passed back through the delegate.
241250
return SimdUnicode.UTF8.GetPointerToFirstInvalidByteAvx2(pInputBuffer, inputLength, out dummyUtf16CodeUnitCountAdjustment, out dummyScalarCountAdjustment);
242251
});
243-
}
252+
}
244253
}
245254

246255
[Benchmark]

src/Ascii.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public unsafe static class Ascii
2525

2626
public static bool IsAscii(this string s)
2727
{
28+
if (s == null) return true;
2829
foreach (var c in s)
2930
{
3031
if (!c.IsAscii()) return false;

0 commit comments

Comments
 (0)