Skip to content

IntrospectiveSortUtilities.FloorLog2 method is misleadingly named. #24169

@cpsusie

Description

@cpsusie

coreclr/src/mscorlib/src/System/Collections/Generic/ArraySortHelper.cs

The method FloorLog2 does not actually return FloorLog2. It also does not return CeilingLog2. It returns FloorLog2 + 1 unless x is 0, where 0 is returned.

Examples (where x is a signed 32 bit integer and f(x) does what FloorLog2 actually does).

x => f(x)

x = 1: f(x) = 1
x = 2 : f(x) = 2
x = 3 : f(x) = 2
x = 4 : f(x) = 3
x = 7 : f(x) = 3

x = 8 : f(x) = 4
x = 16 : f(x) = 5
x = 100 : f(x) = 7

It appears that the method does what it is supposed to do-- the introsort routine uses and it works for that purpose. The method, however, is poorly named: it should be renamed to something like FloorLog2PlusOne or at least commented to indicate what actually does.

I came across this when translating the IntroSort routines into another language but replacing the FloorLog2 method with a method that calculates FloorLog2 which I already had on hand in the target language.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.CollectionsenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions