dotnet/runtime

Optimize Math.Pow(X, C) in JIT

Open

#13.336 aperta il 29 ago 2019

Vedi su GitHub
 (6 commenti) (8 reazioni) (0 assegnatari)C# (5445 fork)batch import
area-CodeGen-coreclrhelp wantedoptimization

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

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

Guida contributor