complete the set */ switch (left) { case 7 : printf("process(%d)\n", i + 6); /* process and rely on drop through */ case 6 : printf("process(%d)\n", i + 5); case 5 : printf("process(%d)\n", i + 4); case 4 : printf("process(%d)\n", i + 3); case 3 : printf("process(%d)\n", i + 2); case 2 : printf("process(%d)\n", i + 1); /* two left */ case 1 : printf("process(%d)\n", i); /* just one left to process */ case 0 : ; /* none left */ } } Code duplication could be avoided by writing the two parts together as in Duff's device. C to MIPS assembly language loop unrolling example The following example will compute a dot product of two 100-entry vectors A and B of type double. Here is the code in C:double dotProduct = 0; for (int i = 0; i < 100; i++) { dotProduct += A[i]*B[i]; } Converting to MIPS assembly language The following is MIPS assembly code that will compute the dot product of two 100-entry vectors, A and B, before implementing loop unrolling. The code below omits the loop initializations: Initialize loop count ($7) to 100. Initialize dot product ($f10) to 0. Initialize A[i] pointer ($5) to the base address of A. Initialize B[i] pointer ($6) to the base address of B. Note that the size of one element of the arrays (a double) is 8 bytes. loop3: l.d $f10, 0($5) ; $f10 ← A[i] l.d $f12, 0($6) ; $f12 ← B[i] mul.d $f10, $f10, $f12 ; $f10 ← A[i]*B[i] add.d $f8, $f8, $f10 ; $f8 ← $f8 + A[i]*B[i] addi $5, $5, 8 ; increment pointer for A[i] by the size ; of a double. addi $6, $6, 8 ; increment pointer for B[i] by the size ; of a double. addi $7, $7, -1 ; decrement loop count test: bgtz $7, loop3 ; Continue if loop count > 0 Unrolling the Loop in MIPS The following is the same as above, but with loop unrolling implemented at a factor of 4. Note again that the size of one element of the arrays (a double) is 8 bytes; thus the 0, 8, 16, 24 displacements and the 32 displacement on each loop. loop3: l.d $f10, 0($5) ; iteration with displacement 0 l.d $f12, 0($6) mul.d $f10, $f10, $f12 add.d $f8, $f8, $f10 l.d $f10, 8($5) ; iteration with displacement 8 l.d $f12, 8($6) mul.d $f10, $f10, $f12