dotnet/runtime

Unnecessary sign-extension for some never-negative expressions

Open

#119,680 opened on Sep 13, 2025

View on GitHub
 (1 comment) (1 reaction) (0 assignees)C# (17,886 stars) (5,445 forks)batch import
area-CodeGen-coreclrhelp wanted

Description

nint M1(uint mask) {
    return BitOperations.TrailingZeroCount(mask) / sizeof(ushort);
}

nint M2(uint mask) {
    return (nint)(uint.TrailingZeroCount(mask) / sizeof(ushort));
}
C:M1(uint):nint:this (FullOpts):
       xor      eax, eax
       tzcnt    eax, esi
       shr      eax, 1
       cdqe     
       ret      

C:M2(uint):nint:this (FullOpts):
       xor      eax, eax
       tzcnt    eax, esi
       shr      eax, 1
       ret    

godbolt.org

Contributor guide

Unnecessary sign-extension for some never-negative expressions · dotnet/runtime#119680 | Good First Issue