快速上浮到INT转换(截断) [英] Fast float to int conversion (truncate)

查看:169
本文介绍了快速上浮到INT转换(截断)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来截断浮动 INT 在一个快速和便携式(IEEE 754)的方式。其原因是,因为在这个函数的50%的时间被花费在铸造

 浮动fm_sinf(浮X){
    常量一浮= 0.00735246819687011731341356165096815f;
    常量浮动B = -0.16528911397014738207016302002888890f;
    常量浮C = 0.99969198629596757779830113868360584f;    浮R,X 2;
    时int k;    / *带中的x范围* /
    K =(int)的(F_1_PI * X + copysignf(0.5F中,x)); / *< - 50%时间是花在投* /    点¯x - = K * F_PI;    / *如果x是一个奇怪的圆周率算,我们必须翻转* /
    R = 1 - 2 *(K&安培; 1); / *把戏R =(K%2)== 0? 1:-1; * /    X2 = X * X;    返回R * X *(C + X2 *(B + A * X2));
}


解决方案

我发现了一个快截方式按SREE Kotay其中规定正是我所需要的最优化。

I'm looking for a way to truncate a float into an int in a fast and portable (IEEE 754) way. The reason is because in this function 50% of the time is spent in the cast:

float fm_sinf(float x) {
    const float a =  0.00735246819687011731341356165096815f;
    const float b = -0.16528911397014738207016302002888890f;
    const float c =  0.99969198629596757779830113868360584f;

    float r, x2;
    int k;

    /* bring x in range */
    k = (int) (F_1_PI * x + copysignf(0.5f, x)); /* <-- 50% of time is spent in cast */

    x -= k * F_PI;

    /* if x is in an odd pi count we must flip */
    r = 1 - 2 * (k & 1); /* trick for r = (k % 2) == 0 ? 1 : -1; */

    x2 = x * x;

    return r * x*(c + x2*(b + a*x2));
}

I found a fast truncate method by Sree Kotay which provides exactly the optimization that I needed.

这篇关于快速上浮到INT转换(截断)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆