Skip to content
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

Move ConcurrentQueueSegment<T>.RoundUpToPowerOf2 to BitOperations #50070

Merged
merged 1 commit into from
Mar 23, 2021

Conversation

stephentoub
Copy link
Member

Contributes to #43135
cc: @tannergooding

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoub stephentoub added this to the 6.0.0 milestone Mar 23, 2021
@ghost
Copy link

ghost commented Mar 23, 2021

Tagging subscribers to this area: @tannergooding, @pgovind
See info in area-owners.md if you want to be subscribed.

Issue Details

Contributes to #43135
cc: @tannergooding

Author: stephentoub
Assignees: -
Labels:

area-System.Numerics

Milestone: -

internal static uint RoundUpToPowerOf2(uint value)
{
// TODO: https://github.com/dotnet/runtime/issues/43135
// When this is exposed publicly, decide on behavior be for the boundary cases...
Copy link
Member

@tannergooding tannergooding Mar 23, 2021

Choose a reason for hiding this comment

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

For reference, the main two concerns are 0 and overflow (int.MaxValue + 2 through uint.MaxValue).

Given 0 is not a power of two, it makes sense for this to return 1. This is what the lzcnt path returns, but the bithacks implementation returns 0 and needs an additional value += (value == 0) ? 1 : 0.

Likewise, the "logical" solution for overflow cases is to return 0, representing the lowest 32-bits of a theoretically infinitely precise result. This is what the bithacks version returns (unless modified to account for rounding up) but the lzcnt variant ends up shifting by 32 which causes it to return 1 instead.

It would be great if we can figure out trivial additions to get the expected result in both cases without significant overhead.

@stephentoub stephentoub merged commit 8ca6a07 into dotnet:main Mar 23, 2021
@stephentoub stephentoub deleted the roundp2 branch March 23, 2021 10:14
@ghost ghost locked as resolved and limited conversation to collaborators Apr 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants