Add BitCount LongExtensions

This commit is contained in:
Ben Adams 2015-10-07 12:23:20 -04:00
parent 816dabb009
commit 8bf2c814d6
3 changed files with 22 additions and 361 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Server.Kestrel.Extensions;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNet.Server.Kestrel.Http
@ -589,214 +590,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
protected override int GetCountFast()
{
var count = MaybeUnknown?.Count ?? 0;
if (((_bits & 1L) != 0))
{
++count;
}
if (((_bits & 2L) != 0))
{
++count;
}
if (((_bits & 4L) != 0))
{
++count;
}
if (((_bits & 8L) != 0))
{
++count;
}
if (((_bits & 16L) != 0))
{
++count;
}
if (((_bits & 32L) != 0))
{
++count;
}
if (((_bits & 64L) != 0))
{
++count;
}
if (((_bits & 128L) != 0))
{
++count;
}
if (((_bits & 256L) != 0))
{
++count;
}
if (((_bits & 512L) != 0))
{
++count;
}
if (((_bits & 1024L) != 0))
{
++count;
}
if (((_bits & 2048L) != 0))
{
++count;
}
if (((_bits & 4096L) != 0))
{
++count;
}
if (((_bits & 8192L) != 0))
{
++count;
}
if (((_bits & 16384L) != 0))
{
++count;
}
if (((_bits & 32768L) != 0))
{
++count;
}
if (((_bits & 65536L) != 0))
{
++count;
}
if (((_bits & 131072L) != 0))
{
++count;
}
if (((_bits & 262144L) != 0))
{
++count;
}
if (((_bits & 524288L) != 0))
{
++count;
}
if (((_bits & 1048576L) != 0))
{
++count;
}
if (((_bits & 2097152L) != 0))
{
++count;
}
if (((_bits & 4194304L) != 0))
{
++count;
}
if (((_bits & 8388608L) != 0))
{
++count;
}
if (((_bits & 16777216L) != 0))
{
++count;
}
if (((_bits & 33554432L) != 0))
{
++count;
}
if (((_bits & 67108864L) != 0))
{
++count;
}
if (((_bits & 134217728L) != 0))
{
++count;
}
if (((_bits & 268435456L) != 0))
{
++count;
}
if (((_bits & 536870912L) != 0))
{
++count;
}
if (((_bits & 1073741824L) != 0))
{
++count;
}
if (((_bits & 2147483648L) != 0))
{
++count;
}
if (((_bits & 4294967296L) != 0))
{
++count;
}
if (((_bits & 8589934592L) != 0))
{
++count;
}
if (((_bits & 17179869184L) != 0))
{
++count;
}
if (((_bits & 34359738368L) != 0))
{
++count;
}
if (((_bits & 68719476736L) != 0))
{
++count;
}
if (((_bits & 137438953472L) != 0))
{
++count;
}
if (((_bits & 274877906944L) != 0))
{
++count;
}
if (((_bits & 549755813888L) != 0))
{
++count;
}
if (((_bits & 1099511627776L) != 0))
{
++count;
}
return count;
return _bits.BitCount() + (MaybeUnknown?.Count ?? 0);
}
protected override StringValues GetValueFast(string key)
@ -5623,159 +5417,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
protected override int GetCountFast()
{
var count = MaybeUnknown?.Count ?? 0;
if (((_bits & 1L) != 0))
{
++count;
}
if (((_bits & 2L) != 0))
{
++count;
}
if (((_bits & 4L) != 0))
{
++count;
}
if (((_bits & 8L) != 0))
{
++count;
}
if (((_bits & 16L) != 0))
{
++count;
}
if (((_bits & 32L) != 0))
{
++count;
}
if (((_bits & 64L) != 0))
{
++count;
}
if (((_bits & 128L) != 0))
{
++count;
}
if (((_bits & 256L) != 0))
{
++count;
}
if (((_bits & 512L) != 0))
{
++count;
}
if (((_bits & 1024L) != 0))
{
++count;
}
if (((_bits & 2048L) != 0))
{
++count;
}
if (((_bits & 4096L) != 0))
{
++count;
}
if (((_bits & 8192L) != 0))
{
++count;
}
if (((_bits & 16384L) != 0))
{
++count;
}
if (((_bits & 32768L) != 0))
{
++count;
}
if (((_bits & 65536L) != 0))
{
++count;
}
if (((_bits & 131072L) != 0))
{
++count;
}
if (((_bits & 262144L) != 0))
{
++count;
}
if (((_bits & 524288L) != 0))
{
++count;
}
if (((_bits & 1048576L) != 0))
{
++count;
}
if (((_bits & 2097152L) != 0))
{
++count;
}
if (((_bits & 4194304L) != 0))
{
++count;
}
if (((_bits & 8388608L) != 0))
{
++count;
}
if (((_bits & 16777216L) != 0))
{
++count;
}
if (((_bits & 33554432L) != 0))
{
++count;
}
if (((_bits & 67108864L) != 0))
{
++count;
}
if (((_bits & 134217728L) != 0))
{
++count;
}
if (((_bits & 268435456L) != 0))
{
++count;
}
if (((_bits & 536870912L) != 0))
{
++count;
}
return count;
return _bits.BitCount() + (MaybeUnknown?.Count ?? 0);
}
protected override StringValues GetValueFast(string key)

View File

@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Server.Kestrel.Extensions
{
public static class LongExtensions
{
public static int BitCount(this long value)
{
// Parallel bit count for a 64-bit integer
var v = (ulong)value;
v = v - ((v >> 1) & 0x5555555555555555);
v = (v & 0x3333333333333333) + ((v >> 2) & 0x3333333333333333);
v = (v + (v >> 4) & 0x0f0f0f0f0f0f0f0f);
return (int)((v * 0x0101010101010101) >> 56);
}
}
}

View File

@ -167,6 +167,7 @@ namespace Microsoft.AspNet.Server.Kestrel.GeneratedCode
return $@"
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Server.Kestrel.Extensions;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNet.Server.Kestrel.Http