From 7e816a5fcd04f877ff39a1d6a4eafe55ef0c1d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 4 Apr 2022 19:44:18 +0200 Subject: [PATCH] BitHelper with uint division and without Math.DivRem for even better codegen AggressiveInlining isn't needed anymore, also the JIT recognizes the _span field, so no local Span is needed here. --- .../src/System/Collections/Generic/BitHelper.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index d45546abd3aa0..ee71f1002a958 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.CompilerServices; - namespace System.Collections.Generic { internal ref struct BitHelper @@ -21,22 +19,19 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { - (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); - Span span = _span; - if (bitArrayIndex < (uint)span.Length) + uint bitArrayIndex = (uint)bitPosition / IntSize; + if (bitArrayIndex < (uint)_span.Length) { - span[(int)bitArrayIndex] |= 1 << (int)position; + _span[(int)bitArrayIndex] |= (1 << (int)((uint)bitPosition % IntSize)); } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsMarked(int bitPosition) { - (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); - Span span = _span; + uint bitArrayIndex = (uint)bitPosition / IntSize; return - bitArrayIndex < (uint)span.Length && - (span[(int)bitArrayIndex] & (1 << (int)position)) != 0; + bitArrayIndex < (uint)_span.Length && + (_span[(int)bitArrayIndex] & ((int)((uint)bitPosition % IntSize))) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow.