pow(x,y)
进行非常快速的计算。希望它只限于一个精确的域,但如果它不够快,它也需要足够的内存效率。
就像我说的那样,它在x的短范围内介于0和1之间,y介于1和2之间。因此,它必须在整个范围内足够精确,以便在递归调用时具有轻微的缩小(和不要拖延数字)
如果你们遇到过这样的事情或有建议......
答案 0 :(得分:3)
将pow(x,y)
替换为
exp(y*log(x))
这有时也是C库实现,但是对于许多整数输入给出了稍微失真的结果。因此,pow(x,y)
实现通常具有用指数0和1,整数幂来捕获琐碎幂的开销,并且进行一些其他转换以获得最精确的结果。减少这种开销可能已经足够加速了。
答案 1 :(得分:1)
超过x
和{}的有限间隔。 p
最佳方法是运行exp2()
和log2()
的两个优化近似值。由于exp2()
minimax多项式的误差将以指数方式复合,但p*log2(x) ≤ 0
的输入始终为0 < x ≤ 1
,因此误差将很好。注意:在x = 0, log2(0) = -∞
。以下是powf()
例程的示例代码,以及1 ≤ p ≤ 2
和log2()
的多个幂exp2
和多阶minimax近似的误差图,希望能够满足您的绝对误差耐受性。
float aPowf(float x, float p){
union { float f; uint32_t u; } L2x, e2e;
float pL2, pL2r, pL2i, E2;
if(x == 0) return(0.0f);
/* Calculate log2(x) Approximation */
L2x.f = x;
pL2 = (uint8_t)(L2x.u >> 23) - 127; // log2(m*2^e) = log2(m) + e
L2x.u = (L2x.u & ~(0xFF << 23)) | (0x7F << 23);
// Approximate log2(x) over 1 <= x < 2, use fma() fused multiply accumulate function for efficient evaluation
// pL2 += -0xf.4fb9dp-4 + L2x.f; // 4.303568e-2
// pL2 += -0x1.acc4cap0 + L2x.f * (0x2.065084p0 + L2x.f * (-0x5.847fe8p-4)); // 4.9397e-3
// pL2 += -0x2.2753ccp0 + L2x.f * (0x3.0c426p0 + L2x.f * (-0x1.0d47dap0 + L2x.f * 0x2.88306cp-4)); // 6.3717e-4
pL2 += -0x2.834a9p0 + L2x.f * (0x4.11f1d8p0 + L2x.f * (-0x2.1ee4fcp0 + L2x.f * (0xa.52841p-4 + L2x.f * (-0x1.4e4cf6p-4)))); // 8.761e-5
// pL2 += -0x2.cce408p0 + L2x.f * (0x5.177808p0 + L2x.f * (-0x3.8cfd5cp0 + L2x.f * (0x1.a19084p0 + L2x.f * (-0x6.aa30dp-4 + L2x.f * 0xb.7cb75p-8)))); // 1.2542058e-5
// pL2 += -0x3.0a3514p0 + L2x.f * (0x6.1cbb88p0 + L2x.f * (-0x5.5737a8p0 + L2x.f * (0x3.490a04p0 + L2x.f * (-0x1.442ae8p0 + L2x.f * (0x4.66497p-4 + L2x.f * (-0x6.925fe8p-8)))))); // 1.8533e-6
// pL2 += -0x3.3eb71cp0 + L2x.f * (0x7.2194ep0 + L2x.f * (-0x7.7cf968p0 + L2x.f * (0x5.c642f8p0 + L2x.f * (-0x2.faeb44p0 + L2x.f * (0xf.9e012p-4 + L2x.f * (-0x2.ef86f8p-4 + L2x.f * 0x3.dc524p-8)))))); // 2.831e-7
// pL2 += -0x3.6c382p0 + L2x.f * (0x8.23b47p0 + L2x.f * (-0x9.f803dp0 + L2x.f * (0x9.3b4f3p0 + L2x.f * (-0x5.f739ep0 + L2x.f * (0x2.9cb704p0 + L2x.f * (-0xb.d395dp-4 + L2x.f * (0x1.f3e2p-4 + L2x.f * (-0x2.49964p-8)))))))); // 4.7028674e-8
pL2 *= p;
// if(pL2 <= -128) return(0.0f);
/* Calculate exp2(p*log2(x)) */
pL2i = floorf(pL2);
pL2r = pL2 - pL2i;
e2e.u = ((int)pL2i + 127) << 23;
// Approximate exp2(x) over 0 <= x < 1, use fma() fused multiply accumulate function for efficient evaluation.
// E2 = 0xf.4fb9dp-4 + pL2r; // 4.303568e-2
// E2 = 0x1.00a246p0 + pL2r * (0xa.6aafdp-4 + pL2r * 0x5.81078p-4); // 2.4761e-3
// E2 = 0xf.ff8fcp-4 + pL2r * (0xb.24b0ap-4 + pL2r * (0x3.96e39cp-4 + pL2r * 0x1.446bc8p-4)); // 1.0705e-4
E2 = 0x1.00003ep0 + pL2r * (0xb.1663cp-4 + pL2r * (0x3.ddbffp-4 + pL2r * (0xd.3b9afp-8 + pL2r * 0x3.81ade8p-8))); // 3.706393e-6
// E2 = 0xf.ffffep-4 + pL2r * (0xb.1729bp-4 + pL2r * (0x3.d79b5cp-4 + pL2r * (0xe.4d721p-8 + pL2r * (0x2.49e21p-8 + pL2r * 0x7.c5b598p-12)))); // 1.192e-7
// E2 = 0x1.p0 + pL2r * (0xb.17215p-4 + pL2r * (0x3.d7fb5p-4 + pL2r * (0xe.34192p-8 + pL2r * (0x2.7a7828p-8 + pL2r * (0x5.15bd08p-12 + pL2r * 0xe.48db2p-16))))); // 2.9105833e-9
// E2 = 0x1.p0 + pL2r * (0xb.17218p-4 + pL2r * (0x3.d7f7acp-4 + pL2r * (0xe.35916p-8 + pL2r * (0x2.761acp-8 + pL2r * (0x5.7e9f9p-12 + pL2r * (0x9.70c6ap-16 + pL2r * 0x1.666008p-16)))))); // 8.10693e-11
// E2 = 0x1.p0 + pL2r * (0xb.17218p-4 + pL2r * (0x3.d7f7b8p-4 + pL2r * (0xe.35874p-8 + pL2r * (0x2.764dccp-8 + pL2r * (0x5.76b95p-12 + pL2r * (0xa.15ca6p-16 + pL2r * (0xf.94e0dp-20 + pL2r * 0x1.cc690cp-20))))))); // 3.9714478e-11
return(E2 * e2e.f);
}
一旦选择了适当的极小极大近似值,请确保使用融合乘法累加运算fma()[这是单周期指令]来实现horner多项式求值。
进一步提高精度可以通过引入LUT进行一系列范围缩小来提高对数函数的精度。