dotnet/runtime

Optimize Math.Pow(X, C) in JIT

Open

#13,336 建立於 2019年8月29日

在 GitHub 查看
 (6 留言) (8 反應) (0 負責人)C# (5,445 fork)batch import
area-CodeGen-coreclrhelp wantedoptimization

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

I noticed that it's quite popular to use constants with Math.Pow, e.g.

double z = Math.Pow(x, 2);
// can be optimized to just
double z = x * x;

Codegen:

C.Test1(Double)
    L0000: vzeroupper
    L0003: vmovsd xmm1, qword [rip+0x15]
    L000b: mov rax, 0x7ffec7f7cca0
    L0015: jmp rax

C.Test2(Double)
    L0000: vzeroupper
    L0003: vmulsd xmm0, xmm0, xmm0
    L0007: ret

E.g. Burgers benchmark uses it a lot https://github.com/dotnet/performance/blob/master/src/benchmarks/micro/coreclr/Burgers/Burgers.cs#L22

I am currently converting Math methods in Mono to LLVM intrinsic and noticed LLVM's InstCombine actually does it, see https://github.com/mono/mono/pull/16561

category:cq theme:floating-point skill-level:intermediate cost:medium

貢獻者指南