c++ - 我如何优化这个 S 曲线函数?

标签 c++ gamma-function

我正在研究生成“S 曲线”的 Gamma 函数。 我需要在实时环境中运行它,所以我需要尽可能加快它的速度。

代码如下:

float Gamma = 2.0f; //Input Variable

float GammaMult = pow(0.5f, 1.0f-Gamma);
if(Input<1.0f && Input>0.0f)
{
    if(Input<0.5f)
    {
        Output = pow(Input,Gamma)*GammaMult;
    }
    else
    {
        Output  = 1.0f-pow(1.0f-Input,Gamma)*GammaMult;
    }
}
else
{
   Output  = Input;
}

有什么方法可以优化这段代码吗?

最佳答案

你可以避免pipeline stalls通过消除 Input<1.0f && Input>0.0f 上的分支如果指令集支持saturation arithmetic或使用最大/最小内在函数,例如x86 MAXSS

您还应该通过舍入饱和的 Input 来消除其他分支.完整算法:

float GammaMult = pow(0.5f, 1.0f-Gamma);
Input = saturate(Input); // saturate via assembly or intrinsics
// Input is now in [0, 1]
Rounded = round(Input); // round via assembly or intrinsics
Coeff = 1 - 2 * Rounded
Output = Rounded + Coeff * pow(Rounded + Coeff * Input,Gamma)*GammaMult;

应该进行四舍五入 via asm/intrinsics as well .

如果您使用此功能,例如如果目标体系结构支持 SIMD,则应考虑对数组的连续值进行矢量化。

关于c++ - 我如何优化这个 S 曲线函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854104/

相关文章:

php - 如何在 C++ 服务器中解码 curl url 编码的字符串

r - 在 ggplot 中绘制具有 Gamma 分布的模型

c++ - 不完全 Gamma 函数算法

c++ - 在另一台计算机上运行 a.out

c++ - fsanitize=undefined 导致链接器错误

c++ - Boost 1.70 io_service 弃用

c++ - SURF 检测后 OpenCV 崩溃

python - Python 中具有 Gamma 函数的累积分布函数

c - R语言源码——C代码中的 `NOMORE_FOR_THREADS`是什么

python - 计算两个 Gamma 分布列表之间的 KL 散度