Skip to content

Performance: Consider using float.{Min/Max}Native instead of comparison+branch logic #406

Description

@BoyBaykiller

Hi, just wanted to let you know about places where float.MinNative/float.MaxNative could perhaps be used over the branchful
a < b ? a : b (or similar) logic to maybe get some perf:

https://gist.github.com/BoyBaykiller/78738c2e26770f19e954e2361bd49198


To give some context, I was prototyping an optimization in RyuJIT for patterns like:

float Min(float a, float b)
{
    return a < b ? a : b;
}

where we currently emit a compare+branch:

G_M000_IG02: 
       vucomiss xmm1, xmm0
       jbe      SHORT G_M000_IG04
 
G_M000_IG03:
       ret      
 
G_M000_IG04:
       vmovaps  xmm0, xmm1
 
G_M000_IG05: 
       ret      

to instead use the native x64-specific vminss/vmaxss instruction:

G_M21498_IG02: 
       vminss   xmm0, xmm0, xmm1

G_M21498_IG03:
       ret   

And the link above shows the automated assembly differences it produced in methods from this project.

Note that my prototyped optimization won't make it into the JIT.
float.{Min/Max}Native is the way to get good perf accross all platforms, assuming potentially different edge-case handling can be tolerated.
On x64 it will use the same vminss/vmaxss

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions