Node:Assembler Loop Unrolling, Previous:Assembler Software Pipelining, Up:Assembler Coding



Loop Unrolling

Loop unrolling consists of replicating code so that several limbs are processed in each loop. At a minimum this reduces loop overheads by a corresponding factor, but it can also allow better register usage, for example alternately using one register combination and then another. Judicious use of m4 macros can help avoid lots of duplication in the source code.

Unrolling is commonly done to a power of 2 multiple so the number of unrolled loops and the number of remaining limbs can be calculated with a shift and mask. But other multiples can be used too, just by subtracting each n limbs processed from a counter and waiting for less than n remaining (or offsetting the counter by n so it goes negative when there's less than n remaining).

The limbs not a multiple of the unrolling can be handled in various ways, for example

One way to write the setups and finishups for a pipelined unrolled loop is simply to duplicate the loop at the start and the end, then delete instructions at the start which have no valid antecedents, and delete instructions at the end whose results are unwanted. Sizes not a multiple of the unrolling can then be handled as desired.