How it works...

This program finds the sum of sequence numbers from 1 up to the limit entered by the user. The user is asked to enter the limit, and the value entered by the user is assigned to the limit variable. For adding the sequence numbers, we will make use of a for loop. To do loop unrolling or to reduce the number of iterations of the loop, we find the integer by which the limit can be divided. That is, we divide the value in the limit variable by integers from 9 to 1. Once we get the integer by which the limit is divisible, we reduce the number of for loops by that integer.

Let's assume that the user enters a limit of 40, which is assigned to the limit variable. A for loop is set to run from the values 9 to 1, and every value from 9 to 1 will be used to try to divide the value in the limit variable. On any division, if the remainder appears as 0, the for loop will break; else, the next iteration will execute with the decreased value. Currently, the value in the limit variable is 40 and the value of i in the first iteration is 9. The remainder of dividing 40 by 9 is a non zero value, so the next iteration of the for loop will begin with the next decreased value, 8.

Because, on dividing 40 by 8, you get a remainder of 0, the for loop will break and the control will jump to the statement immediately after the for loop. The value of i at that time is 8, so the value of 8 is assigned to the incr variable. That is, the for loop will increment by a value of 8. It also means that we are applying loop unrolling by reducing the iterations of the for loop by 8 times. In other words, the for loop will be set to run from 1 until the limit, which is 40, with an increment of 8 after every iteration.

In the first iteration, the value of i is 1. The sum variable, in which the addition of the sequence of numbers will be computed, is initialized to 0. The value of i is added to the sum variable. As said earlier, the next iteration of the for loop will increment the value of i by 8. So, within the for loop, a while loop is used. Within the while loop, a variable, x, is used that executes from 0 to the value of the incr variable (that is, until the value of 8). In other words, the while loop will add the sequence of numbers from 1 to 8 into the sum variable.

Once the sum of the first eight values of the sequence of numbers is computed and assigned to the sum variable, the next iteration of the for loop will begin with the value of i incremented to 9. Again within the for loop, the while loop will execute to compute the sum of the sequence of numbers from 9 to 16. Again, the next iteration of the for loop will increase the value of i to 17. The process continues until the for loop completes. In short, the for loop is unrolled to the value assigned to the incr variable. Finally, the sum of the sequence of numbers is displayed on the screen.

The program is compiled using GCC, as shown in the following screenshot. Because no error appears on compilation, that means the loopunrolling.c program has successfully been compiled into an EXE file, loopunrolling.exe. On executing the file, the user is prompted to enter the limit up to which the sum of the sequence of numbers is desired. The program will not only print the sum of the sequence of numbers but will also print how many loop iterations it took for the sum to be computed, as shown in the following screenshot:

Figure 15.3

Voilà! We have successfully executed loop unrolling to generate a faster result.