Topic : STM32F0 - long division

Forum : ARM

Original Post
Post Information Post
October 15, 2013 - 7:33pm
Guest

I am using Rkit-Arm version 1.50.13.0109 & Ride 7.44.13.0109

I have a variable
int32_t drive_pwm_cnt;

The below line of code requires 6400nS of execution time while running at a clock speed of 48Mhz. This is approximately 150 instruction cycles - very slow execution.
drive_pwm_cnt /= 20000;

The Assembly from the compiler is
LDR R0,...
LDR R1,...
BL 0x8000f58 (the branch to the division function)
STR R0,...

It appears that the compiler is not utilizing the processor long division LDIV function. I need this division to execute very fast.

My question is how do I work with the compiler in C to take advantage of the processor LDIV instruction? Does anyone have experience on how to reduce execution to less then 1uS.

Replies
Post Information Post
+1
0
-1
October 17, 2013 - 11:11pm
Guest

Software support team. I have not solved my use of long division so I worked around the concern. I scaled the numbers by (2^15/20,000) so I could >> 15 which executes very fast (same as / 32768). For some reason the multiply is fast but the division is slow. The solution was to remove the division and shift right.

If anyone has a compiler directive to force the use of the LDIV instruction in the arm I would still like to know how to do this. It is possible I will not always have an easy work around.

+1
0
-1
October 21, 2013 - 12:08pm
Raisonance Support Team

Hi,

There is no ldiv instruction in Cortex-M cores.

ldiv() is a library function, as you observed.

Some Cortex cores, like the Cortex-M3, include sdiv and udiv instructions, which are probably used inside ldiv (or in place of it). But the Cortex-M0 does not provide these integer division instructions, so on this core the integer division must be performed by software. About 150 cycles is around what we can expect for that.

So your workaround is probably the best solution ... unless you can switch to a Cortex-M3-based device. ;)
(STM32F1...)

Best Regards,

Vincent

+1
0
-1
October 23, 2013 - 9:20pm
Guest

Vincent
Thank you for the clarification. I will note this for future choices of cores. Knowing the math and having control to scale the numbers allowing a shift does work well. Your support is much appreciated - thanks.