cancel
Showing results for 
Search instead for 
Did you mean: 

Floating point vs fixed point calculations

STuser2
Senior III

I need to perform lot of mathematical calculations and within a fixed time interval, the micro is STM32G474 my question is even if floating point calculator is available, do you recommend to use fixed point calcuations Q15 format etc. or I can go ahead with floating point calculations, will i get same performance? Please clarify.

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Super User

If you get the needed precision by integer/fixed point calculation, it will always be faster than floating point, just because loading the floating point registers needs more time (32 or 64b) than having the value in 16b in the CPU registers and do one clock multiply.

Just do some simple test loops with the calculations you need and compare the time needed. I always do it, because this is 100% what you will get on this machine.

And always have optimizer on , -O2 is a fine setting, otherwise you see not the real performance of an arm CPU.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
AScha.3
Super User

If you get the needed precision by integer/fixed point calculation, it will always be faster than floating point, just because loading the floating point registers needs more time (32 or 64b) than having the value in 16b in the CPU registers and do one clock multiply.

Just do some simple test loops with the calculations you need and compare the time needed. I always do it, because this is 100% what you will get on this machine.

And always have optimizer on , -O2 is a fine setting, otherwise you see not the real performance of an arm CPU.

If you feel a post has answered your question, please click "Accept as Solution".
mfgkw
Senior III

I agree.

But another issue should be considered: with integral or fixed point arithmetic you need to be very clear about the range for your initial, intermediate and final values.

Floating point arithmetic is much more indulgent.

Just one question related to above what is the benefit of CORDIC and FMAC modules? can i assume it is only in case i require more precision as per above explanation?  

Yes i really get confused with fixed point calculations, that is the concern i have. 

CORDIC gives you not more precision. It is either q1.15 or q1.31.

It is just a bunch of algorithms, which can be implemented efficiently in hardware for trigionometric functions, logarithms and square roots (using add and shift to avoid multiplication).

It is a bit strange regarding the representaion, e.g. angles range from  -1 to +1 instead of -π to +π. 

Andrew Neil
Super User

Beware that the hardware FP unit in Cortex-M4 is only single precision:

https://mcuoneclipse.com/2019/03/29/be-aware-floating-point-operations-on-arm-cortex-m4f/

 

And floating point inherently has a number of issues:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

 

There's an old computer science saying:

"If you think you need floating point to solve the problem, you don't understand the problem;

If you really do need floating point, then you have a problem you do not understand"

 

PS:

Previously, on this forum:

https://community.st.com/t5/stm32-mcus-products/stm32f446vet7-two-float-value-difference-problem/m-p/712815/highlight/true#M258906

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.