Skip to content

Optimize Math.Min/Max byte-overloads #65255

@deeprobin

Description

@deeprobin

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKBuIGYACMxgYUYG8bGfmngAnhhiMAsgEsAdgApBwxtjSM5I4AEpO3XtuIB2MdgwALAHQSZi5WoDcWngF87jJw2VCR5gErYA7rPcKSipWmtTaOvrS2IwAPAC8IQD8CowgyrZhvI6ZPC78AaLYCP7ylsHqoeG8egbGZkVRQTZO2dp5zORIbvKFCN5+wWUBFVw54TVRjAB8CRXJ0WnAGdrZ9kA=

The raw implementation achieves a smaller codegen than the Math.Min/Max methods.

AggresiveInlining and AggresiveOptimization do not change this fact.

Min codegen

C.Min(Byte, Byte)
    L0000: movzx eax, dl
    L0003: movzx edx, r8b
    L0007: cmp eax, edx
    L0009: jle short L000d
    L000b: jmp short L000f
    L000d: mov edx, eax
    L000f: mov eax, edx
    L0011: ret

C.MinRaw(Byte, Byte)
    L0000: movzx eax, dl
    L0003: movzx edx, r8b
    L0007: cmp eax, edx
    L0009: jle short L000e
    L000b: mov eax, edx
    L000d: ret
    L000e: ret

Diff

  movzx eax, dl
  movzx edx, r8b
  cmp eax, edx
- jle short L000d
- jmp short L000f
- mov edx, eax
+ jle short L000e
  mov eax, edx
  ret
+ ret

Max codegen

C.Max(Byte, Byte)
    L0000: movzx eax, dl
    L0003: movzx ecx, r8b
    L0007: cmp eax, ecx
    L0009: jl short L000f
    L000b: movzx eax, dl
    L000e: ret
    L000f: movzx eax, r8b
    L0013: jmp short L000e

C.MaxRaw(Byte, Byte)
    L0000: movzx eax, cl
    L0003: movzx edx, dl
    L0006: cmp eax, edx
    L0008: jge short L000d
    L000a: mov eax, edx
    L000c: ret
    L000d: ret

Diff

- movzx eax, dl
- movzx ecx, r8b
- cmp eax, ecx
- jl short L000f
- movzx eax, dl
+ movzx eax, cl
+ movzx edx, dl
+ cmp eax, edx
+ jge short L000d
+ mov eax, edx
+ ret
  ret
- movzx eax, r8b
- jmp short L000e

category:cq
theme:basic-cq

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions