2014-06-11 05:26 AM
I have an array of unsigned short numbers from sensors. The data will be multiplied with an array of floating point numbers.
To use FPU provided by STM32F407, I need to convert the unsigned short numbers into floating point numbers. There are only libraries converting signed short arrays into floating point arrays. Could I ask if there are any efficient way of converting an unsigned short array into a floating point array? Or the conversion has to be done with a for loop?Any comments will be appreciated.2014-06-12 08:14 PM
The DSP library converts short into float in this way:
while(blkCnt > 0u)
{
/* C = (float32_t) A / 32768 */
/* convert from q15 to float and then store the results in the destination buffer */
*pDst++ = ((float32_t) * pIn++ / 3270f);
*pDst++ = ((float32_t) * pIn++ / 3270f);
*pDst++ = ((float32_t) * pIn++ / 3270f);
*pDst++ = ((float32_t) * pIn++ / 3270f);
/* Decrement the loop counter */
blkCnt--;
}
1. Why four outputs are computed at a time?
2. Why a division by 32768 is performed instead of a single cast?
Answers:
1. For loop-unrolling purpose, decrease the number of branch control hazards.
2. No clue yet..
2014-06-12 11:41 PM
2014-06-13 01:42 AM
Thank you very much...
So I guess a simple casting is sufficient to convert unsigned short to float?2014-06-13 01:52 AM
So I guess a simple casting is sufficient to convert unsigned short to float?
Yes, the rest will do the compiler. But for efficiency, you may need to use
-mfloat-abi=hard
. And second, you can do the conversion in a cast while doing the first operation, e.g. scaling, and not separately. And when using preprocessor constants, be aware of the difference between float constants and double constants - double is C default.2014-06-13 01:59 AM
If you are going for efficiency, why don't you simply try and then have a look at the assembled output? While you are given solid good recommendations here by Ivan and fm, much depends on your toolchain and its particular settings anyway.
JW