Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

[NO MERGE] BitOps analysis CoreFX (WIP) #34917

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/src/System/Net/WebSockets/ManagedWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ private static unsafe int ApplyMask(Span<byte> toMask, int mask, int maskIndex)
Debug.Assert(maskIndex < sizeof(int));

int maskShift = maskIndex * 8;
int shiftedMask = (int)(((uint)mask >> maskShift) | ((uint)mask << (32 - maskShift)));
int shiftedMask = BitOps.RotateRight(mask, maskShift);

// Try to use SIMD. We can if the number of bytes we're trying to mask is at least as much
// as the width of a vector and if the width is an even multiple of the mask.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ internal static bool AreBytesEqual(byte[] src, byte[] tgt)

internal static void ClearBit(ref int value, uint bitmask)
{
value = (int)(((uint)value) & ((uint)(~bitmask)));
BitOps.ClearBit(ref value, (int)bitmask);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Motivation to use uint for declared offset param

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to do some more analysis of the original code to make sure that the refactor is functionally equivalent.

}

internal static void SetBit(ref int value, uint bitmask)
{
value = (int)(((uint)value) | ((uint)bitmask));
BitOps.InsertBit(ref value, (uint)bitmask);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to do some more analysis of the original code to make sure that the refactor is functionally equivalent.

}

// {0xa2, 0x3f,...} --> "a23f..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,17 @@ internal static class BitArithmetic
{
internal static int CountBits(int v)
{
return CountBits(unchecked((uint)v));
return BitOps.PopCount(v);
}

internal static int CountBits(uint v)
{
unchecked
{
v = v - ((v >> 1) & 0x55555555u);
v = (v & 0x33333333u) + ((v >> 2) & 0x33333333u);
return (int)((v + (v >> 4) & 0xF0F0F0Fu) * 0x1010101u) >> 24;
}
return BitOps.PopCount(v);
}

internal static int CountBits(ulong v)
{
const ulong Mask01010101 = 0x5555555555555555UL;
const ulong Mask00110011 = 0x3333333333333333UL;
const ulong Mask00001111 = 0x0F0F0F0F0F0F0F0FUL;
const ulong Mask00000001 = 0x0101010101010101UL;
v = v - ((v >> 1) & Mask01010101);
v = (v & Mask00110011) + ((v >> 2) & Mask00110011);
return (int)(unchecked(((v + (v >> 4)) & Mask00001111) * Mask00000001) >> 56);
return BitOps.PopCount(v);
}

internal static uint Align(uint position, uint alignment)
Expand Down