Skip to content

Optimize BitOps #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 20 additions & 16 deletions BitFaster.Caching/BitOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
x |= x >> 16;
return x + 1;
#else
return 1u << -BitOperations.LeadingZeroCount(x - 1);
return BitOperations.RoundUpToPowerOf2(x);

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / infer

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / infer

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / mac

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / mac

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-macos

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-macos

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 47 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'
#endif
}

Expand All @@ -66,7 +66,7 @@
x |= x >> 32;
return x + 1;
#else
return 1ul << -BitOperations.LeadingZeroCount(x - 1);
return BitOperations.RoundUpToPowerOf2(x);

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / infer

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / infer

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / mac

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / mac

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-linux

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-macos

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-macos

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'

Check failure on line 69 in BitFaster.Caching/BitOps.cs

View workflow job for this annotation

GitHub Actions / bench-win

'BitOperations' does not contain a definition for 'RoundUpToPowerOf2'
#endif
}

Expand Down Expand Up @@ -113,14 +113,16 @@
public static int BitCount(uint x)
{
#if NETSTANDARD2_0
var count = 0;
while (x != 0)
{
count++;
x &= x - 1; //walking through all the bits which are set to one
}
const uint c1 = 0x_55555555u;
const uint c2 = 0x_33333333u;
const uint c3 = 0x_0F0F0F0Fu;
const uint c4 = 0x_01010101u;

return count;
x -= (x >> 1) & c1;
x = (x & c2) + ((x >> 2) & c2);
x = (((x + (x >> 4)) & c3) * c4) >> 24;

return (int)x;
#else
return BitOperations.PopCount(x);
#endif
Expand All @@ -144,14 +146,16 @@
public static int BitCount(ulong x)
{
#if NETSTANDARD2_0
var count = 0;
while (x != 0)
{
count++;
x &= x - 1; //walking through all the bits which are set to one
}
const ulong c1 = 0x_55555555_55555555ul;
const ulong c2 = 0x_33333333_33333333ul;
const ulong c3 = 0x_0F0F0F0F_0F0F0F0Ful;
const ulong c4 = 0x_01010101_01010101ul;

x -= (x >> 1) & c1;
x = (x & c2) + ((x >> 2) & c2);
x = (((x + (x >> 4)) & c3) * c4) >> 56;

return count;
return (int)x;
#else
return BitOperations.PopCount(x);
#endif
Expand Down
Loading