什么是 do_cos_slow.isra?

What is do_cos_slow.isra?

我写了一个简单的代码来测试 prof

double bar_compute (double d) {
    double t = std::abs(d);
    t += std::sqrt(d);
    t += std::cos(d);

    return t;
}

// Do some computation n times
double foo_compute(unsigned n) {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(0.0, 1.0);

    double total = 0;
    for (int i=0; i<n; i++) {
        double d = dist(mt);
        total += bar_compute(d);
    }
    return total;
}

当我 运行 prof 并查看输出时

  56.14%  runcode  libm-2.23.so       [.] __cos_avx                                                                                                                                
  27.34%  runcode  runcode            [.] _Z11foo_computej                                                                                                                         
  13.92%  runcode  runcode            [.] _Z11bar_computed                                                                                                                         
   0.86%  runcode  libm-2.23.so       [.] do_cos_slow.isra.1                                                                                                                       
   0.44%  runcode  runcode            [.] cos@plt                                                                                                                                  
   0.41%  runcode  libm-2.23.so       [.] sloww1                                                                                                                                   
   0.35%  runcode  libm-2.23.so       [.] __dubcos                                                                                                                                 
   0.17%  runcode  ld-2.23.so         [.] _dl_lookup_symbol_x             

do_cos_slow.israsloww1是什么意思?

我可以使用更快的 cos 版本吗?不然怎么会叫慢呢?

do_cos_slow 来自其在 glibc/sysdeps/ieee754/dbl-64/s_sin.c. It is called do_cos_slow because it is more precise than the function it is based on do_cos as per the comment above its declaration on Line 164.

中的声明

.isra是因为该函数是经过IPA SRA优化后的版本Stack Overflow Answer, What does the GCC function suffix “isra” mean?

sloww1 是一个根据上面的注释计算 sin(x+dx) 的函数。

关于更快的cos版本,我不确定是否有更快的版本,但是如果你更新你的glibc或提供libm的libc实现,至少glibc 2.28,那么你会得到Wilco的结果Dijkstra 删除了这些慢速路径函数并重构了 dosincos,从而提高了速度。

来自commit message

Refactor the sincos implementation - rather than rely on odd partial inlining
of preprocessed portions from sin and cos, explicitly write out the cases.
This makes sincos much easier to maintain and provides an additional 16-20%
speedup between 0 and 2^27.  The overall speedup of sincos is 48% over this range.
Between 0 and PI it is 66% faster.

您可以尝试的其他替代方案是其他 libc 或 libm 实现,或其他 cos 实现,包括 avx_mathfun or avx_mathfun with some fixes for newer GCC or supersimd