From 8bf2c814d64c8475415c1c9ba81ffaae84039275 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 7 Oct 2015 12:23:20 -0400 Subject: [PATCH] Add BitCount LongExtensions --- .../Http/FrameHeaders.Generated.cs | 364 +----------------- .../Infrastructure/LongExtensions.cs | 18 + .../KnownHeaders.cs | 1 + 3 files changed, 22 insertions(+), 361 deletions(-) create mode 100644 src/Microsoft.AspNet.Server.Kestrel/Infrastructure/LongExtensions.cs diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/FrameHeaders.Generated.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/FrameHeaders.Generated.cs index 9666fad29b..5ca0e5fc12 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/FrameHeaders.Generated.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/FrameHeaders.Generated.cs @@ -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) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/LongExtensions.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/LongExtensions.cs new file mode 100644 index 0000000000..0ac6a718d4 --- /dev/null +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/LongExtensions.cs @@ -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); + } + } +} diff --git a/tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode/KnownHeaders.cs b/tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode/KnownHeaders.cs index 0fc30fa0de..6dbad61a7c 100644 --- a/tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode/KnownHeaders.cs +++ b/tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode/KnownHeaders.cs @@ -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