ARM Technical Support Knowledge Articles

# FASTER INTEGER DIVISION

Applies to: C51 C Compiler

Information in this knowledgebase article applies to:

• C51 Version 8 or higher

### QUESTION

I have an algorithm that depends on the execution speed of integer division. I am using a device without an arithmetic unit. Therefore, I cannot use an algorithm that relies on an arithmetic unit.

The algorithm that I need to speed up has the following requirements:

• The dividend is an unsigned integer value in the range 0-4095 (only 12-bit values are used).
• The divisor is an unsigned character value in the range 0-255 (only 8-bit values are used).

I have investigated the algorithm provided in the C51 Compiler Run-Time Library. It appears to be quite clever. But, is there a way to speed it up even further?

Yes. When it is possible to constrain the values used in math operations (like division) so that they fall within a specific range, you can create an algorithm that is faster than the standard division algorithm. In addition, loops in the division algorithm can be unrolled to gain a little more speed (at the cost of wasting memory).

The attachment to this knowledgebase article presents two solutions to your problem:

1. An optimized library function that improves execution speed of division by about 23%. This function works for all value ranges and may be used instead of the C51 Run-Time Library function. To use this function, just add the module UIDIV_F.A51 to your project.
2. A dedicated function that improves execution speed of division by about 37%. This function is defined as follows:
```unsigned int divu12u8 (unsigned int v, unsigned char d);
```
To use this function you must modify your application code so that it calls the divu12u8 function rather than the standard division operation (using the / operator). Note that this routine does not yield correct results when v exceeds the value 4095. Therefore, you must be certain that your values are within a specific range.

To use this division routine add the module DIVU12U8.A51 to your application. Enable Register Coloring under Projects - Options for Target - C51 to further optimize the register usage of your application.

### ATTACHED FILES

Request the files attached to this knowledgebase article.

Article last edited on: 2010-07-14 08:12:07