Skip to content

optimize bitops#802

Merged
bitfaster merged 4 commits into
mainfrom
users/alexpeck/bitops
Jun 17, 2026
Merged

optimize bitops#802
bitfaster merged 4 commits into
mainfrom
users/alexpeck/bitops

Conversation

@bitfaster

@bitfaster bitfaster commented May 4, 2026

Copy link
Copy Markdown
Owner

Bring forward changes from https://github.com/bitfaster/BitFaster.Caching/pull/667/changes.

Original code had a clause specifically for .NET3.1 that had different behavior (see code below). This build target will be removed in the next major release, therefore remove now rather than fixing it.

        public static uint CeilingPowerOfTwo(uint x)
        {
#if NETSTANDARD2_0
            // https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
            --x;
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            x |= x >> 16;
            return x + 1;
#elif NET6_0_OR_GREATER
            return BitOperations.RoundUpToPowerOf2(x);
#else
            return 1u << -BitOperations.LeadingZeroCount(x - 1);
#endif
      }

@coveralls

coveralls commented Jun 16, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 99.212% (+0.001%) from 99.211% — users/alexpeck/bitops into main

@bitfaster bitfaster merged commit f0dfd82 into main Jun 17, 2026
26 of 27 checks passed
@bitfaster bitfaster deleted the users/alexpeck/bitops branch June 17, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants