KEMBAR78
Faster Math.Max/Min on arm64 by EgorBo · Pull Request #65584 · dotnet/runtime · GitHub
Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 18, 2022

Math.Max and Math.Min do a lot of stuff for float and double to match IEEE754:2018 maximum/minimum. But we can at least rely on ARM's fmin/fmax, e.g.:

double Foo(double a, double b) => Math.Max(a, b);   

Current codegen on arm64 (I am using R2R):

; Method Foo(double,double):double:this
G_M2729_IG01:
            stp     fp, lr, [sp,#-32]!
            mov     fp, sp
G_M2729_IG02:
            fcmp    d0, d1
            beq     G_M2729_IG06
G_M2729_IG03:
            fcmp    d0, d0
            bne     G_M2729_IG05
            fcmp    d1, d0
            blo     G_M2729_IG04
            b       G_M2729_IG08
G_M2729_IG04:
            fmov    d1, d0
            b       G_M2729_IG08
G_M2729_IG05:
            fmov    d1, d0
            b       G_M2729_IG08
G_M2729_IG06:
            str     d1, [fp,#24]
            ldr     x0, [fp,#24]
            cmp     x0, #0
            blt     G_M2729_IG07
            b       G_M2729_IG08
G_M2729_IG07:
            fmov    d1, d0
G_M2729_IG08:
            mov     v0.8b, v1.8b
G_M2729_IG09:
            ldp     fp, lr, [sp],#32
            ret     lr
; Total bytes of code: 88

New codegen:

; Method Foo(double,double):double:this
G_M2729_IG01:
            stp     fp, lr, [sp,#-16]!
            mov     fp, sp
G_M2729_IG02:
            fmax    d0, d0, d1
G_M2729_IG03:
            ldp     fp, lr, [sp],#16
            ret     lr
; Total bytes of code: 20

@ghost ghost assigned EgorBo Feb 18, 2022
@ghost ghost added the area-System.Numerics label Feb 18, 2022
@ghost
Copy link

ghost commented Feb 18, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

An issue from twitter

Math.Max/Min do a lot of stuff for floating point, e.g.:
image

Let's see if this passes our tests around edge cases values (e.g. min(+0.0, -0.0))
e.g. https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime.Extensions/tests/System/Math.cs#L1228

Author: EgorBo
Assignees: EgorBo
Labels:

area-System.Numerics

Milestone: -

EgorBo and others added 2 commits February 18, 2022 22:39
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
EgorBo and others added 4 commits February 19, 2022 19:44
Co-authored-by: Tanner Gooding <tagoo@outlook.com>
Co-authored-by: Tanner Gooding <tagoo@outlook.com>
@EgorBo
Copy link
Member Author

EgorBo commented Feb 19, 2022

@tannergooding Thanks, addressed your feedback

@EgorBo EgorBo merged commit 7345109 into dotnet:main Feb 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants