Skip to content

Expose RoundUpToPowerOf2 from BitOperations and optimize it #43135

Closed
@Sergio0694

Description

@Sergio0694

Background and Motivation

Rounding a number to the closest >= power of two is a relatively common operation to perform, especially for developers implementing custom collection type that require a power of two to be used in one of the internal data structures (for reference, CoreCLR uses this method in the ConcurrentQueue type (here). This proposal has two main points:

  • Expose this API from the BitOperations class
  • Optimize it using intrinsics

Proposed API

namespace System.Numerics
{
    public static class BitOperations
    {
+        public static uint RoundUpToPowerOf2(uint i);
+        public static ulong RoundUpToPowerOf2(ulong i);
    }
}

Usage Examples

I'm using this same method in a couple places in the Microsoft.Toolkit.HighPerformance package:

  • In the StringPool type, to initialize the internal buckets for the cached string-s (to get a fast % op)
  • In the ArrayPoolBufferWriter<T> type, to round up the requested size to ArrayPool<T> and avoid repeated new[] allocations

Within CoreCLR, there's also that ConcurrentQueue usage example I mentioned above.

Details

The implementation would include vectorized paths like LeadingZeroCount does, checking for Lzcnt, ArmBase and then X86Base, or alternatively it would use software fallback currently (always) used within ConcurrentQueue.

Notes

If the API proposal is approved I'd be happy to help out and make a PR for this 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.Numericshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions