KEMBAR78
Unnecessary extra and instruction in uint % 256 · Issue #8833 · dotnet/runtime · GitHub
Skip to content

Unnecessary extra and instruction in uint % 256 #8833

@stephentoub

Description

@stephentoub

Consider:

using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        Mod(123);
        Cast(123);
        And(123);
    }

    [MethodImpl(MethodImplOptions.NoInlining)] static byte Cast(uint i) => (byte)(i);
    [MethodImpl(MethodImplOptions.NoInlining)] static byte And(uint i) => (byte)(i & 0xFF);
    [MethodImpl(MethodImplOptions.NoInlining)] static byte Mod(uint i) => (byte)(i % 256);
}

For Cast and And I get:

       0FB6C1               movzx    rax, cl
       C3                   ret

but for Mod I get:

       81E1FF000000         and      ecx, 255
       0FB6C1               movzx    rax, cl
       C3                   ret

Is there a reason that extra and instruction needs to exist?

category:cq
theme:basic-cq
skill-level:intermediate
cost:medium

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions