193 lines
10 KiB
HTML
193 lines
10 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<META name=vsisbn content="1576101746">
|
|
<META name=vstitle content="Michael Abrash's Graphics Programming Black Book, Special Edition">
|
|
<META name=vsauthor content="Michael Abrash">
|
|
<META name=vspublisher content="The Coriolis Group">
|
|
<META name=vspubdate content="07/01/97">
|
|
<META name=vscategory content="Web and Software Development: Game Development,Web and Software Development: Graphics and Multimedia Development">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<TITLE>Michael Abrash's Graphics Programming Black Book Special Edition: Pentium Rules</TITLE>
|
|
|
|
<!-- HEADER -->
|
|
<!-- Empty Reference Subhead -->
|
|
|
|
<!--ISBN=1576101746//-->
|
|
<!--TITLE=Michael Abrash's Graphics Programming Black Book Special Edition//-->
|
|
<!--AUTHOR=Michael Abrash//-->
|
|
<!--PUBLISHER=The Coriolis Group, Inc.//-->
|
|
<!--CHAPTER=20//-->
|
|
<!--PAGES=386-390//-->
|
|
<!--UNASSIGNED1//-->
|
|
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
|
|
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="20-01.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="20-03.html">Next</A></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</CENTER>
|
|
<P><BR></P>
|
|
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">V-Pipe-Capable Instructions</FONT></H3>
|
|
<P>Any instruction can go through the U-pipe, and, for practical purposes, the U-pipe is always executing instructions. (The exceptions are when the U-pipe execution unit is waiting for instruction or data bytes after a cache miss, and when a U-pipe instruction finishes before a paired V-pipe instruction, as I’ll discuss below.) Only the instructions shown in Table 20.1 can go through the V-pipe. In addition, the V-pipe can execute a separate instruction only when one of the instructions listed in Table 20.2 is executing in the U-pipe; superscalar execution is not possible while any instruction not listed in Table 20.2 is executing in the U-pipe. So, for example, if you use <B>SHR EDX,CL</B>, which takes 4 cycles to execute, no other instructions can execute during those 4 cycles; if, on the other hand, you use <B>SHR EDX,10</B>, it will take 1 cycle to execute in the U-pipe, and another instruction can potentially execute concurrently in the V-pipe. (As you can see, similar instruction sequences can have vastly different performance characteristics on the Pentium.)</P>
|
|
<P>Basically, after the current instruction or pair of instructions is finished (that is, once neither the U- nor V-pipe is executing anything), the Pentium sends the next instruction through the U-pipe. If the instruction after the one in the U-pipe is an instruction the V-pipe can handle, if the instruction in the U-pipe is pairable, and if register contention doesn’t occur, then the V-pipe starts executing that instruction, as shown in Figure 20.2. Otherwise, the second instruction waits until the first instruction is done, then executes in the U-pipe, possibly pairing with the next instruction in line if all pairing conditions are met.</P>
|
|
<HR><!-- CODE //-->
|
|
<PRE>
|
|
MOV reg,reg (1 cycle)
|
|
mem,reg (1 cycle)
|
|
reg,mem (1 cycle)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (1 cycle)†
|
|
|
|
AND/OR/XOR/ADD/SUB reg,reg (1 cycle)
|
|
mem,reg (3 cycles)
|
|
reg,mem (2 cycles)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (3 cycles)<SUP>†</SUP>
|
|
|
|
INC/DEC reg (1 cycle)
|
|
mem (3 cycles)
|
|
|
|
CMP reg,reg (1 cycle)
|
|
mem,reg (2 cycles)
|
|
reg,mem (2 cycles)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (2 cycles)<SUP>†</SUP>
|
|
|
|
TEST reg,reg (1 cycle)
|
|
EAX,immediate (1 cycle)
|
|
|
|
PUSH/POP reg (1 cycle)
|
|
immediate (1 cycle)
|
|
|
|
LEA reg,mem (1 cycle)
|
|
|
|
JCC near (1 cycle if predicted correctly;
|
|
5 cycles otherwise in V-pipe,
|
|
4 cycles otherwise in U-pipe)
|
|
|
|
JMP/CALL near (1 cycle if predicted correctly;
|
|
3 cycles otherwise)
|
|
|
|
<SUP>†</SUP> Can’t execute in V-pipe if address contains a displacement
|
|
</PRE>
|
|
<!-- END CODE //-->
|
|
<P><B>Table 20.1 Instructions that can execute in the V-pipe.</B><HR></P>
|
|
<P>The list of instructions the V-pipe can handle is not very long, and the list of U-pipe pairable instructions is not much longer, but these actually constitute the bulk of the instructions used in PC software. As a result, a fair amount of pairing happens even in normal, non-Pentium-optimized code. This fact, plus the 64-bit 66 MHz bus, branch prediction, dual 8K internal caches, and other Pentium features, together mean that a Pentium is considerably faster than a 486 at the same clock speed, even without Pentium-specific optimization, contrary to some reports.</P>
|
|
<P>Besides, almost all operations can be performed by combinations of pairable instructions. For example, <B>PUSH [<I>mem</I>]</B> is not on either list, but both <B>MOV <I>reg</I>,[<I>mem</I>]</B> and <B>PUSH <I>reg</I></B> are, and those two instructions can be used to push a value stored in memory. In fact, given the proper instruction stream, the discrete instructions can perform this operation effectively in just 1 cycle (taking one-half of each of 2 cycles, for 2*0.5 = 1 cycle total execution time), as shown in Figure 20.3—a full cycle <I>faster</I> than <B>PUSH [<I>mem</I>]</B>, which takes 2 cycles.</P>
|
|
<HR><!-- CODE //-->
|
|
<PRE>
|
|
MOV reg,reg (1 cycle)
|
|
mem,reg (1 cycle)
|
|
reg,mem (1 cycle)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (1 cycle)†
|
|
|
|
AND/OR/XOR/ADD/SUB/ADC/SBB reg,reg (1 cycle)
|
|
mem,reg (3 cycles)
|
|
reg,mem (2 cycles)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (3 cycles)<SUP>†</SUP>
|
|
|
|
INC/DEC reg (1 cycle)
|
|
mem (3 cycles)
|
|
|
|
CMP reg,reg (1 cycle)
|
|
mem,reg (2 cycles)
|
|
reg,mem (2 cycles)
|
|
reg,immediate (1 cycle)
|
|
mem,immediate (2 cycles)†
|
|
|
|
TEST reg,reg (1 cycle)
|
|
EAX,immediate (1 cycle)
|
|
|
|
PUSH/POP reg (1 cycle)
|
|
immediate (1 cycle)
|
|
|
|
LEA reg,mem (1 cycle)
|
|
|
|
SHL/SHR/SAL/SAR reg,immediate (1 cycle)<SUP>††</SUP>
|
|
|
|
ROL/ROR/RCL/RCR reg,1 (1 cycle)
|
|
|
|
<SUP>†</SUP> Can’t pair if address contains a displacement
|
|
<SUP>††</SUP> Includes shift-by-1 forms of instructions
|
|
</PRE>
|
|
<!-- END CODE //-->
|
|
<P><B>Table 20.2 Instructions that, when executed in the U-pipe, allow V-pipe-executable instructions to execute simultaneously (pair) in the V-pipe.</B><HR></P>
|
|
<TABLE WIDTH="100%"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="5%"><IMG SRC="images/20-02i.jpg"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="95%"><SMALL><I>A fundamental rule of Pentium optimization is that it pays to break complex instructions into equivalent simple instructions, then shuffle the simple instructions for maximum use of the V-pipe. This is true partly because most of the pairable instructions are simple instructions, and partly because breaking instructions into pieces allows more freedom to rearrange code to avoid the AGIs and register contention I’ll discuss in the next chapter.</I></SMALL>
|
|
</TABLE>
|
|
<P><A NAME="Fig2"><!-- </A><A HREF="javascript:displayWindow('images/20-02.jpg',405,144 )"> --><IMG SRC="images/20-02.jpg"><BR><!-- </A>
|
|
<BR><A HREF="javascript:displayWindow('images/20-02.jpg',405,144)"> --><FONT COLOR="#000077"><B>Figure 20.2</B></FONT></A> <I>Instruction flow through the two pipes.</I>
|
|
</P>
|
|
<P>One downside of this “RISCification” (turning complex instructions into simple, RISC-like ones) of Pentium-optimized code is that it makes for substantially larger code. For example,
|
|
</P>
|
|
<!-- CODE SNIP //-->
|
|
<PRE>
|
|
push dword ptr [esi]
|
|
</PRE>
|
|
<!-- END CODE SNIP //-->
|
|
<P>is one byte smaller than this sequence:
|
|
</P>
|
|
<!-- CODE SNIP //-->
|
|
<PRE>
|
|
mov eax,[esi]
|
|
push eax
|
|
</PRE>
|
|
<!-- END CODE SNIP //-->
|
|
<P><A NAME="Fig3"><!-- </A><A HREF="javascript:displayWindow('images/20-03.jpg',409,210 )"> --><IMG SRC="images/20-03.jpg"><BR><!-- </A>
|
|
<BR><A HREF="javascript:displayWindow('images/20-03.jpg',409,210)"> --><FONT COLOR="#000077"><B>Figure 20.3</B></FONT></A> <I>Pushing a value from memory effectively in one cycle.</I>
|
|
</P>
|
|
<P>A more telling example is the following
|
|
</P>
|
|
<!-- CODE SNIP //-->
|
|
<PRE>
|
|
add [MemVar],eax
|
|
</PRE>
|
|
<!-- END CODE SNIP //-->
|
|
<P>versus the equivalent:
|
|
</P>
|
|
<!-- CODE SNIP //-->
|
|
<PRE>
|
|
mov edx,[MemVar]
|
|
add edx,eax
|
|
mov [MemVar],edx
|
|
</PRE>
|
|
<!-- END CODE SNIP //-->
|
|
<P>The single complex instruction takes 3 cycles and is 6 bytes long; with proper sequencing, interleaving the simple instructions with other instructions that don’t use EDX or <B>Mem Var</B>, the three-instruction sequence can be reduced to 1.5 cycles, but it is <I>14</I> bytes long.</P>
|
|
<TABLE WIDTH="100%"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="5%"><IMG SRC="images/20-03i.jpg"><TD VALIGN="TOP" ALIGN="LEFT" WIDTH="95%"><SMALL><I>It’s not unusual for Pentium optimization to approximately double both performance and code size at the same time. In an important loop, go for performance and ignore the size, but on a program-wide basis, the size bears watching.</I></SMALL>
|
|
</TABLE>
|
|
<P><BR></P>
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="20-01.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="20-03.html">Next</A></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</CENTER>
|
|
|
|
<hr width="90%" size="1" noshade>
|
|
<div align="center">
|
|
<font face="Verdana,sans-serif" size="1">Graphics Programming Black Book © 2001 Michael Abrash</font>
|
|
</div>
|
|
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
|
|
<!-- reference_subfoot = footer -->
|
|
<!-- reference_footer = subfoot -->
|
|
|
|
<!-- BEGIN SUB FOOTER -->
|
|
</BODY>
|
|
</HTML>
|
|
|
|
<!-- END FOOTER -->
|
|
|
|
|