快速浮点到int转换(截断)

orl*_*rlp 9 c floating-point optimization truncate

我正在寻找一种方式来截断floatint以快速和便携式(IEEE 754)的方式.原因是因为在这个函数中,50%的时间花在了演员身上:

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));
}
Run Code Online (Sandbox Code Playgroud)

orl*_*rlp 3

我发现了Sree Kotay 的快速截断方法,它提供了我所需要的优化。