dotnet/runtime

Optimize Math.Pow(X, C) in JIT

Open

#13.336 geöffnet am 29. Aug. 2019

Auf GitHub ansehen
 (6 Kommentare) (8 Reaktionen) (0 zugewiesene Personen)C# (5.445 Forks)batch import
area-CodeGen-coreclrhelp wantedoptimization

Repository-Metriken

Stars
 (17.886 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)

Beschreibung

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

Contributor Guide