cancel
Showing results for 
Search instead for 
Did you mean: 

Speed of dividing operation

luter
Associate II
Posted on September 16, 2005 at 12:19

Speed of dividing operation

8 REPLIES 8
luter
Associate II
Posted on September 15, 2005 at 10:50

Hi All.

I would like to know speed of dividing. Is there any tables that show speeds of different operations.

Now i'm optimizing ISRs, where dividing exists. Whether it helps, if i decrease denominator (not size, but value)?

pete3
Associate II
Posted on September 15, 2005 at 11:34

Many micros use a series of subtraction operations to divide. If this is the case on the ST7, decreasing the size of the denominator would lead to more subtractions and hence longer execution times.

Another common implementation is to right-shift (divide by two) repeatedly and then use the method above on a common factor of the denominator. Here a smaller denominator is not as important as one which easily multiplies with a power of two (eg 2, 4, 8, 16, 32 etc.).

I would try both to see which leads to shorter execution times.

luca239955_st
Associate III
Posted on September 15, 2005 at 13:29

are you using C or ASM? What kind of numbers are you trying to divide (char, int, float)?

Generally speaking:

- division is not hardware assisted on st7, therefore it will always be slow

- division should be avoided in ISR; take special care if you are using the Cosmic compiler (need to save extra registers in some cases)

- you can easily simulate whatever division routine you are using and get the numbers you are looking for.

Hope it hels.

Regards,

Luca (Cosmic)

luter
Associate II
Posted on September 16, 2005 at 05:53

I am using Cosmis C compiler. i divide 16 and 32 bit int numbers. i know--it is terrible, but now i can not see another solution. now i am about testing speed of division by myself. i will post results.

luter
Associate II
Posted on September 16, 2005 at 05:55

To _luca:

What do u mean telling ''take special care if you are using the Cosmic compiler (need to save extra registers in some cases)'' ?

luter
Associate II
Posted on September 16, 2005 at 07:27

See results in the attachment.

Сonclusion: as smaller numerator and as bigger denominator as faster division for same size of operands.

________________

Attachments :

div_speed.xls : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I09y&d=%2Fa%2F0X0000000bUY%2FuzKp3Jt0ukUTmUypkExJeLkSrWbOwTcwuONgl1f4hR4&asPdf=false
luca239955_st
Associate III
Posted on September 16, 2005 at 11:52

Quote:

What do u mean telling ''take special care if you are using the Cosmic compiler (need to save extra registers in some cases)'' ?

When you use 32 bit math (long, float) in interrupt routines you need to specify @svlreg, see a quote from the manual below

Quote:

The c_lreg area is not saved implicitely in such a case, in order to keep the interrupt function as efficient as possible. If any function called by the interrupt function uses longs or floats, the c_lreg area can be saved by using the type qualifier @svlreg on the interrupt function definition.

About your table: the numbers give a pretty good idea about the average time you can expect, but, if you need to calculate the worst possible case (which is often the most interesting info for an interrupt routine), you need either to test all possible cases or to understand the division algorithm (the source is in your compiler installation, but it's not trivial) and deduce what values for a32, b16, and c32 (reference to you table) would cause the slowest calculation. I'll try to ask the person who wrote the algorithm and then let you know.

Regards,

Luca

luter
Associate II
Posted on September 16, 2005 at 12:19

Tnx for information.

As for my case i had 2 ways :

- use big value of denominator

- split denominator on 2 parts: smaller denominator( for division) and some power of 2 (for right shift).

After my investigation i decided to take one big denominator.

Am i right?

Waiting for a news...