cancel
Showing results for 
Search instead for 
Did you mean: 

Convert an unsigned short array to a float array efficiently by FPU

cyqdtc
Associate II
Posted on June 11, 2014 at 14:26

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. 
5 REPLIES 5
cyqdtc
Associate II
Posted on June 13, 2014 at 05:14

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..
ivani
Associate II
Posted on June 13, 2014 at 08:41

2. Because q15 is not an integer but fractional number with 15 bits for the fractional part.

cyqdtc
Associate II
Posted on June 13, 2014 at 10:42

Thank you very much... 

So I guess a simple casting is sufficient to convert unsigned short to float?

frankmeyer9
Associate II
Posted on June 13, 2014 at 10:52

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.

Posted on June 13, 2014 at 10:59

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