dotnet/runtime

Optimize Math.Pow(X, C) in JIT

Open

#13,336 opened on Aug 29, 2019

View on GitHub
 (6 comments) (8 reactions) (0 assignees)C# (5,445 forks)batch import
area-CodeGen-coreclrhelp wantedoptimization

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

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