3

I would like to optimize my C++ code for an ARM device, which lacks a floating point unit. Most of my functions use floats, and I am considering changing them to fixed-point.

Are there real benefits in the case of ARM devices, or are compilers smart enough to do this themselves?

For example, would this be more efficient on an ARM device

//unsigned char const* input
unsigned int a, b;
a= *input++ << 12; 
b= *input++ << 12; 
a*=1024;    //0.25 shifted 12 bits
b*=1024;    //0.25 shifted 12 bits
*output++ = (a+b) >> 24;

than doing

float a,b;
a= *input++;
b= *input++;
a=a/4;
b=a/4;
*output++ = a+b;
4

4 回答 4

5

我在 gp2x 手持设备上做了一些编码,它有一个 200Mhz 的 ARM 处理器。我正在做 3D 图形,并且使用浮点数进行数学运算肯定会太慢。

即使仅对每个顶点计算使用浮点数学,性能也比使用定点慢得多。这是编译器使用浮点仿真库的“soft-fp”。当让内核处理浮点仿真时,性能更加糟糕。

我开发了一个 C++定点库,它提供了一个带有重载运算符的类,人们可以使用它来代替浮点数。

于 2012-05-25T11:15:37.980 回答
4

不,编译器肯定不会将浮点数的操作转换为定点,因为语义有很大不同。浮点数具有更大的范围,它们支持 NaN 和无穷大,并且舍入行为完全不同。

因此,如果您的目标处理器没有 FPU,您肯定会看到使用定点的巨大好处(如果这些计算占您运行时间的很大一部分)。不过,您必须处理与定点相关的所有内务管理。

于 2012-05-25T11:28:56.130 回答
3

我不能专门为 ARM 代码说话,但模拟浮点并不便宜。有前导 1 检测、动态移位、舍入和特殊情况处理(NaN、inf 等)。这与您在那里的定点代码完全不同。

于 2012-05-25T10:51:02.903 回答
2

Fixed point is typically more efficient in all cases where the floating point doesn't actually float, even on devices with FPUs. For, example if you are using long integers to represent a space to the nearest millimetre. Savings are typically realised in computational speed, space used where you'd need a double rather than a float to get the same millimetre range as you'd get out of a long, and output where you can avoid functions like sprintf etc...

So yes, fixed point does make sense for many contexts.

于 2012-05-25T11:56:31.947 回答