Skip to content

Commit

Permalink
Add Lzcnt.LeadingZeroCount benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Jul 9, 2018
1 parent d893850 commit caedf06
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Benchmarks/ArrayIsSorted.cs
Expand Up @@ -38,7 +38,7 @@ public bool IsSorted_LINQ()
return ArrayOfInts.OrderBy(i => i).SequenceEqual(ArrayOfInts);
}

/*
/*
[Benchmark]
public bool IsSorted_CppPinvoke()
{
Expand All @@ -47,7 +47,7 @@ public bool IsSorted_CppPinvoke()
[DllImport("NativeLib", CallingConvention = CallingConvention.Cdecl)]
static extern bool is_sorted_avx2_generic(int[] array, int count);
*/
*/

// simple implementations to benchmark against intrinsics:

Expand Down
2 changes: 1 addition & 1 deletion Benchmarks/BenchmarkBase.cs
Expand Up @@ -6,7 +6,7 @@
namespace IntrinsicsPlayground
{
[MemoryDiagnoser]
[MarkdownExporter]
[MarkdownExporter]
public class ArrayBenchmarkBase
{
public int[] ArrayOfInts { get; set; }
Expand Down
54 changes: 54 additions & 0 deletions Benchmarks/Misc/LeadingCountZero.cs
@@ -0,0 +1,54 @@
using System.Runtime.Intrinsics.X86;
using BenchmarkDotNet.Attributes;

namespace IntrinsicsPlayground.Benchmarks.Misc
{
public unsafe class LeadingZeroCount
{
// benchmarking LZCNT against its soft implementation in Decimal https://github.com/dotnet/corert/blob/a4c6faac2c31bffc6f13287c6cb8c6a7bb9667fd/src/System.Private.CoreLib/src/System/Decimal.DecCalc.cs#L2047

[Benchmark]
public int[] LeadingZeroCount_Soft()
{
int[] results = new int[100000];
for (uint i = 1 /* see https://github.com/dotnet/corert/pull/5883#issuecomment-403617647 */ ; i < results.Length; i++)
results[i] = LeadingZeroCount_SoftStatic(i);
return results;
}

[Benchmark(Baseline = true)]
public int[] LeadingZeroCount_HW()
{
int[] results = new int[100000];
for (uint i = 1; i < results.Length; i++)
results[i] = (int)Lzcnt.LeadingZeroCount(i);
return results;
}

public static int LeadingZeroCount_SoftStatic(uint value)
{
int c = 1;
if ((value & 0xFFFF0000) == 0)
{
value <<= 16;
c += 16;
}
if ((value & 0xFF000000) == 0)
{
value <<= 8;
c += 8;
}
if ((value & 0xF0000000) == 0)
{
value <<= 4;
c += 4;
}
if ((value & 0xC0000000) == 0)
{
value <<= 2;
c += 2;
}
return c + ((int)value >> 31);
}
}
}
23 changes: 1 addition & 22 deletions Program.cs
@@ -1,20 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.Intrinsics.X86;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
using IntrinsicsPlayground.Benchmarks;
using IntrinsicsPlayground.Benchmarks.Misc;
using IntrinsicsPlayground.Tests;

namespace IntrinsicsPlayground
{
Expand All @@ -25,16 +12,8 @@ static void Main(string[] args)
if (!Sse41.IsSupported || !Avx2.IsSupported)
throw new NotSupportedException(":(");

// Intrinsics:

BenchmarkRunner.Run<ArrayEqual>();
BenchmarkRunner.Run<ArrayIndexOf>();
BenchmarkRunner.Run<ArrayIsSorted>();
BenchmarkRunner.Run<ArrayMax>();
BenchmarkRunner.Run<ArrayReverse>();
BenchmarkRunner.Run<ArraySum>();
BenchmarkRunner.Run<MatrixMultiply>();
BenchmarkRunner.Run<MatrixNegate>();
BenchmarkRunner.Run<MatrixSum>();

// Sorting:
//BenchmarkRunner.Run<SortingAlreadySortedArray>();
Expand Down

0 comments on commit caedf06

Please sign in to comment.