The Pentium III-M processor is not sensitive to branch target instruction alignment. In this experiment, I created a tight loop that executes 100,000,000 times, and aligned the start of the loop at +1 and +2 bytes beyond the beginning of the 16-byte boundary, as in the following two code fragments:
00433A51 47 inc edi
00433A52 81 FF 00 E1 F5 05 cmp edi,5F5E100h
00433A58 7C F7 jl loopx (433A51h)
and
00433A52 47 inc edi
00433A53 81 FF 00 E1 F5 05 cmp edi,5F5E100h
00433A59 7C F7 jl loopx (433A52h)
The results showed that there was no difference in performance between these two loops. On a 1.13GHz PIII-M processor, these loops took 0.180 +/- 0.001 and 0.182 +/- 0.002 seconds to complete respectively.
However, Pentium III-M processors pre-fetch instructions 16 bytes at a time. If you force loops to straddle a 16-byte boundary unnecessarily, you will pay a 1 cycle penalty for your transgression. Consider the following two loops:
00433370 47 inc edi
00433371 81 FF 00 E1 F5 05 cmp edi,5F5E100h
00433377 7C F7 jl loopx (433370h)
and
0043351F 47 inc edi
00433520 81 FF 00 E1 F5 05 cmp edi,5F5E100h
00433526 7C F7 jl loopx (43351Fh)
The first loop is set up so that all of its instructions fit within a single 16-byte paragraph. The second loop is set up so that it straddles two 16-byte paragraphs. The performance penalty in this case is significant; these loops execute in 0.182 +/- 0.001 and 0.274 +/- 0.001 seconds respectively.
The penalty paid in this case is 0.092 seconds. These experiments were conducted on a 1.13GHz Pentium III-M processor. In 0.092 seconds, the processor clock ticks 0.092 * 1.13 billion = 0.104 billion times. Our loop executed 0.1 billion times. Therefore, we we are paying a penalty of exactly 1 clock cycle for forcing an additional 16-byte prefetch operation. In a tight loop such as this, we are paying a significant performance penalty of 49.7%!
To see whether this 1 cycle penalty is consistent with larger loops, I constructed a larger loop whose body was padded using 9 nop instructions. This was sufficient to make the loop 18 bytes in length. This lets me construct a case where the same loop could straddle either two or three 16-byte paragraphs. The loop that straddled two 16-byte paragraphs took 0.540 +/- 0.001 seconds to complete. The loop that straddled three 16-byte paragraphs took 0.630 +/- 0.001 seconds to complete. The difference in time is 0.90 seconds, which implies that we pay yet another 1 cycle penalty for fetching three 16-byte paragraphs vs. two 16-byte paragraphs, consistent with our previous set of observations.
Therefore, for tight loops, a very useful performance optimization on Pentium III-M processors is to ensure that the loops fit entirely within the smallest number of 16-byte paragraphs possible.
I haven’t investigated these effects on other processor types yet, so I’ll leave that for a future installment.
If you are interested in the source code for this simple experiment, along with an Excel spreadsheet that contains some of the raw numbers used to generate these results, click here. Note that you must compile as a debug build so that the padding that I added in inline assembler will cause the expected results.