Skip to content

Commit

Permalink
Optimize Log2FloorNonZero with LZCNT
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Dec 24, 2018
1 parent 4f46f1b commit 873680d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion BrotliSharpLib/BrotliSharpLib.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net20;net35;net40;net45;net451;net452;net462;netstandard1.3;netstandard1.6;netstandard2.0;netcoreapp1.0;netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>net20;net35;net40;net45;net451;net452;net462;netstandard1.3;netstandard1.6;netstandard2.0;netcoreapp1.0;netcoreapp1.1;netcoreapp3.0</TargetFrameworks>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1570;1701</NoWarn>
Expand Down
10 changes: 10 additions & 0 deletions BrotliSharpLib/Encode/FastLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using size_t = BrotliSharpLib.Brotli.SizeT;

namespace BrotliSharpLib {
Expand Down Expand Up @@ -106,7 +107,16 @@ private static double FastLog2(size_t v) {
return Math.Log(v) * LOG_2_INV;
}

#if AGGRESSIVE_INLINING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private static uint Log2FloorNonZero(size_t n) {
#if NETCOREAPP3_0
if (System.Runtime.Intrinsics.X86.Lzcnt.IsSupported)
{
return 31u ^ (uint)System.Runtime.Intrinsics.X86.Lzcnt.LeadingZeroCount((uint)n | 1);
}
#endif
size_t m = n;
uint result = 0;
while ((m >>= 1) != 0) result++;
Expand Down

0 comments on commit 873680d

Please sign in to comment.