cancel
Showing results for 
Search instead for 
Did you mean: 

Forcing float on a matlab generated code

igal
Associate II
Posted on January 22, 2014 at 16:25

Hi all,

I use matlab to generate c code and have this code compile in IAR for STM32F4 as a library. So I have no ST libraries added just the c code and h files from matlab.

Matlab uses sqrt, sin functions etc. declared at math.h, these functions use double and I'm looking for a way to force the compiler to use float (so the FPU will be used), I can't go through the entire code and switc every function used for the float equivalent. is there a simple way to do so?

Thank you

#math.h
10 REPLIES 10
frankmeyer9
Associate II
Posted on January 22, 2014 at 19:25

At least Crossworks has a project option ''treat DOUBLE as FLOAT'', not sure about other IDEs.

However, I'm convinced no one will ''automatically'' translate sqrt ()/sin() calls to sqrtf()/sinf() etc.

Perhaps your IDE/Editor supports refactoring.

A much better approach would be to force your matlab model to use float instead of the wasteful double. (Don't ask me, I'm not a mathlab user ...).

igal
Associate II
Posted on January 23, 2014 at 09:47

thanks. but as far as I know Matlab doesn't work with float only double

chen
Associate II
Posted on January 23, 2014 at 10:51

Hi

You should be able to do something with a compiler switch. I did a quick search for IAR compiler switches and found this : ''The compiler can now optimize some floating-point expressions more agressively by using the option

--relaxed_fp

. If enabled, the compiler tries to reduce the floating-point type used in floating-point expressions. This can cause a small loss of accuracy. '' When it says '' This can cause a small loss of accuracy.'' - there really are rounding errors!! I worked on system where we passed data iteratively through the same algorithm (it was a curve fitting algorithm) and it went horribly wrong. IAR's support solution - use double instead of float - the library for float is less accurate! I think at the time MQX (it was a freescale part - hence the OS was MQX) did not support the FPU. (I am not sure what difference using the FPU and math libraries would have.)
chen
Associate II
Posted on January 23, 2014 at 11:14

Hi

I have thought about ''(I am not sure what difference using the FPU and math libraries would have.)'' and I do know.

The math library will be either built ONLY for integer based units or for units with FPUs.

The correct library/compiler switch/processor setting must be set!

''this code compile in IAR for STM32F4 as a library.''

If you are using IAR - you should ask their support people. They are very good - I know from experience!

igal
Associate II
Posted on January 23, 2014 at 14:13

thank you sung.chen_chung

the FPU can do float multiplications, sqrt etc. in one clock cycle, if I use double the execution time is more than doubled (for floating point operations).

what does it mean ''reduce the floating-point type used in floating-point expressions'', does it mean that instead of float it will use fixed point? that is not good

chen
Associate II
Posted on January 23, 2014 at 14:41

Hi

''what does it mean ''reduce the floating-point type used in floating-point expressions''''

Your guess is as good as mine.

''I use matlab to generate c code and have this code compile in IAR for STM32F4 as a library.''

So if the Matlab generated code relies on another imported library (math.h) for sqrt, sin functions then it will very much depend on the imported library. If the imported library is coded for integer maths - the FPU is redundant.

Your best bet is to ask IAR.

frankmeyer9
Associate II
Posted on January 23, 2014 at 14:48

thanks. but as far as I know Matlab doesn't work with float only double

 

Have you tried a Matlab user forum ?

igal
Associate II
Posted on January 23, 2014 at 15:37

math.h has both double and float versions for every function (sqrt vs sqrtf, sin vs sinf etc.) can't rename the entire code though 🙂

Matlab forum was a pain. hoped someone here delf with this issue before.

I will post a question in the Matlab site as well.

Thank you

frankmeyer9
Associate II
Posted on January 23, 2014 at 15:53

math.h has both double and float versions for every function (sqrt vs sqrtf, sin vs sinf etc.) can't rename the entire code though 🙂

That's probably because the math library (libm.a) uses the same separation between double and (single) float functions. Sure you can keep double, with or without using of the FPU in your code. But expect a performance penalty in order of a magnitude for using double, as the FPU only supports single precision natively.

I agree, renaming is not really a good option, especially if you want to develop your application on model-level. How about asking mathworks, or find another matlab user forum ?

Detailled matlab knowledge and experience is rarely found here.