- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我做了一个我认为不错的定点平方根算法
:
template<int64_t M, int64_t P>
typename enable_if<M + P == 32, FixedPoint<M, P>>::type sqrt(FixedPoint<M, P> f)
{
if (f.num == 0)
return 0;
//Reduce it to the 1/2 to 2 range (based around FixedPoint<2, 30> to avoid left/right shift branching)
int64_t num{ f.num }, faux_half{ 1 << 29 };
ptrdiff_t mag{ 0 };
while (num < (faux_half)) {
num <<= 2;
++mag;
}
int64_t res = (M % 2 == 0 ? SQRT_32_EVEN_LOOKUP : SQRT_32_ODD_LOOKUP)[(num >> (30 - 4)) - (1LL << 3)];
res >>= M / 2 + mag - 1; //Finish making an excellent guess
for (int i = 0; i < 2; ++i)
// \ | /
// \ | /
// _| V L
res = (res + (int64_t(f.num) << P) / res) >> 1; //Use Newton's method to improve greatly on guess
// 7 A r
// / | \
// / | \
// The Infamous Time Eater
return FixedPoint<M, P>(res, true);
}
然而,在分析之后(在 Release模式下)我发现这里的除法占据了这个算法花费的时间的 83%。我可以通过用乘法代替除法来加速 6 倍,但这是错误的。不幸的是,我发现整数除法比乘法慢得多。有什么办法可以优化吗?
如果需要这张表。
const array<int32_t, 24> SQRT_32_EVEN_LOOKUP = {
0x2d413ccd, //magic numbers calculated by taking 0.5 + 0.5 * i / 8 from i = 0 to 23, multiplying by 2^30, and converting to hex
0x30000000,
0x3298b076,
0x3510e528,
0x376cf5d1,
0x39b05689,
0x3bddd423,
0x3df7bd63,
0x40000000,
0x41f83d9b,
0x43e1db33,
0x45be0cd2,
0x478dde6e,
0x49523ae4,
0x4b0bf165,
0x4cbbb9d6,
0x4e623850,
0x50000000,
0x5195957c,
0x532370b9,
0x54a9fea7,
0x5629a293,
0x57a2b749,
0x59159016
};
SQRT_32_ODD_LOOKUP
只是 SQRT_32_EVEN_LOOKUP
除以 sqrt(2)
。
最佳答案
重新发明轮子,真的,但不是很好。正确的解决方案是使用 NR 计算 1/sqrt(x)
,然后乘以一次得到 x/sqrt(x)
- 只需检查 x= =0
放在前面。
之所以如此好,是因为 y=1/sqrt(x)
的 NR 步骤只是 y = (3-x*y*y)*y/2
。这就是所有直接的乘法。
关于c++ - 优化定点平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36077485/
当我在定点 Z3Py 中启用解释生成选项时,我收到包含以下消息的核心转储。 Error setting 'DL_GENERATE_EXPLANATIONS', reason: unknown opti
我正在开发一些代码,可以从 HW 获取浮点或定点数据。目前我们将其作为 float 。 底层API都是定点的。所以我们必须将数据作为定点传回。我们使用的算法是 Cholesky。我想知道为什么我们必须
我有一个关于在 MATLAB 中为 Texas Instruments TMS320C64xx DSP 编写算法的问题: 我在 MATLAB 中草率地实现了我的过滤器。我的目标是使用 MATLAB E
我需要将 float 转换为Q31定点,Q31表示1个符号位,0位表示整数部分,31位表示小数部分。这意味着 Q31 只能表示 [-1,0.9999] 范围内的数字。 根据定义,从浮点转换为定点时,会
我正在使用第 3 方定点 antilog() 函数来计算分贝 out_mag = 10^( in_db/20 ) 的幅度。 antilog() 采用 Q6.25 格式作为输入,并在输出时提供 Q16.
我想将一个定点数(Q31/int32 表示具有 31 个小数位的小数)除以另一个 Q31/int32。我想计算z = y/x,知道abs(x)>abs(y)。因此,z<1,因此可以表示为另一个Q31/
我是一名优秀的程序员,十分优秀!