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

贡献者指南