1
\$\begingroup\$

I am using a PLC that uses a STM32F4 as its processor. The PLC software has Integer and Float calculation commands. When I work with floating point numbers I usually try to use integer calculations and the result is divided by 1000 to get the final result. I think this method saves program execution time and will be able to get a faster cycle time in the PLC program.

Ex: If I want to multiply a value (x) by constant (1.234567),

  1. Multiply x by 1234567
  2. divide the result by 1000000
  3. get final result

But, STM32F4 has a hardware floating point unit (FPU). So, I wonder if the above method actually makes the program faster.

will my integer calculation method actually save program execution time, compared to the floating point calculation method?

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Can't you test this and find out? I mean... how difficult can it be? And if you can't test it, then no answer here will be meaningful because you won't be able to verify what you are told. Is there a problem, either way?? \$\endgroup\$
    – jonk
    Sep 27, 2021 at 5:00
  • 2
    \$\begingroup\$ Your integer scaling method should be using a power of 2 for the scaling value to save on an expensive divide. The first rule of optimisation is ‘don’t’. The second rule is ‘measure’. From your measurements you should be able to determine if there is any real gain to be had. Then you can make an engineering decision based on numbers. \$\endgroup\$
    – Kartman
    Sep 27, 2021 at 5:11
  • \$\begingroup\$ @jonk I got the separate cycle time and there is no difference between them. \$\endgroup\$
    – user_fs10
    Sep 27, 2021 at 5:26
  • 1
    \$\begingroup\$ @user_fs10 Then it doesn't matter, does it? You can go either way. My own preference is to use integers because they obey some mathematical principles that floating point does not. But here, it is really your call I suppose. \$\endgroup\$
    – jonk
    Sep 27, 2021 at 5:27
  • 2
    \$\begingroup\$ @user_fs10 In floating point, \$A\left(B+C\right)\ne A B + A C\$. But in the integer domain, this is always true. When detailing with integer ADC values, I avoid floating point like I would the plague. That said, there are times. Particularly when there is a very wide dynamic range that requires the use of floating point. \$\endgroup\$
    – jonk
    Sep 27, 2021 at 5:41

1 Answer 1

2
\$\begingroup\$

You are worrying about the wrong things.

Float divides are the most expensive taking 14 cycles.
Integer divide can take 2 to 12, depending on the values.
Float multiply only 3.
Integer multiply also takes 2 cycles.

Now, an F4 typically is running 100 MHz or better. So you're not going to fill the slow PLC cycle with maths.
Typically you want to look at these number when your doing digital signal processing on fast samplerates.

Fixed point math can be better, for example when you cannot accept the uncertainty of IEEE 745 rounding or you don't have floating point hardware. If you can accept the rounding, float is superior in speed and range. Since fixed point requires additional operations after arithmetic that make it slower that hardware floating point.

https://developer.arm.com/documentation/ddi0439/b/BEHJADED https://developer.arm.com/documentation/ddi0439/b/CHDDIGAC

\$\endgroup\$
3
  • \$\begingroup\$ So in best possible situation it would take 2cyc int. mul, + 2cyc int. div = 4cyc VS. 3cyc float mul. In worst situation is 14cyc int VS 3cyc float. \$\endgroup\$ Sep 27, 2021 at 8:01
  • 2
    \$\begingroup\$ All of this depends on how the PLC program translates the PLC language to machine code. If there are optimizations, if it is using C as a middle-step, how well it optimizes the code etc. \$\endgroup\$
    – Lundin
    Sep 27, 2021 at 12:52
  • \$\begingroup\$ @Lundin Yes, the PLC runtime may also do some checks to see if the math will be possible. Eg: isnormal() \$\endgroup\$
    – Jeroen3
    Sep 28, 2021 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.