Reputation: 22245
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;
Upvotes: 3
Views: 694
Reputation: 22922
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.
Upvotes: 2
Reputation: 7363
I've done some coding on the gp2x handheld device which had an ARM processor at 200Mhz. I was doing 3D graphics and using floats for the math operations would have been too slow for sure.
Even when only using floating point math for the per vertex calculations the performance was considerably slower than using fixed point. This was with "soft-fp" where the compiler used the floating point emulation library. When letting the kernel handle the floating point emulation the performance just sucked even more.
I've developed a C++ Fixed Point Libray that provices a class with overloaded operators that one could use instead of a float.
Upvotes: 5
Reputation: 24140
No, the compiler certainly won't convert operations on floats to fixed-point, as the semantics are considerably different. Floats have a greater range, they support NaNs and infinities, and the roundoff behaviour is quite different.
So if the processor you're targeting doesn't have an FPU, you'll definitely see a big benefit in using fixed-point (if those computations make up a significant part of your running time). You'll have to deal with all of the housekeeping associated with fixed-point, though.
Upvotes: 4
Reputation: 272487
I can't speak for ARM code specifically, but emulating floating-point is not cheap. There's leading-1 detection, dynamic shifts, rounding, and special-case handling (NaN, inf, etc.). That's quite different to the fixed-point code that you have there.
Upvotes: 3