187 lines
10 KiB
HTML
187 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<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: Local Optimization</title>
|
|
<meta name="chapter" content="07" />
|
|
<meta name="pages" content="145-148" />
|
|
</head>
|
|
|
|
<body>
|
|
<center>
|
|
<table border="1">
|
|
<tr>
|
|
<td>
|
|
<a href="07-04.html">Previous</a>
|
|
</td>
|
|
|
|
<td>
|
|
<a href="index.html">Table of Contents</a>
|
|
</td>
|
|
|
|
<td>
|
|
<a href="08-01.html">Next</a>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
<h4 id="Heading8">Rotating and Shifting with Tables</h4>
|
|
|
|
<p>As another example of local optimization, consider the matter of rotating or shifting a mask into position. First, let’s look at the simple task of setting bit N of AX to 1.</p>
|
|
|
|
<p>The obvious way to do this is to place N in CL, rotate the bit into position, and OR it with AX, as follows:</p>
|
|
<pre>
|
|
MOV BX,1
|
|
SHL BX,CL
|
|
OR AX,BX
|
|
</pre>
|
|
|
|
<p>This solution is obvious because it takes good advantage of the special ability of the x86 family to shift or rotate by the variable number of bits specified by CL. However, it takes an average of about 45 cycles on an 8088. It’s actually far faster to precalculate the results, pass the bit number in BX, and look the shifted bit up, as shown in Listing 7.3.</p>
|
|
|
|
<p><b>LISTING 7.3 L7-3.ASM</b></p>
|
|
<pre>
|
|
SHL BX,1 ;prepare for word sized look up
|
|
OR AX,ShiftTable[BX] ;look up the bit and OR it in
|
|
:
|
|
ShiftTable LABEL WORD
|
|
BIT_PATTERN=0001H
|
|
REPT 16
|
|
DW BIT_PATTERN
|
|
BIT_PATTERN=BIT_PATTERN SHL 1
|
|
ENDM
|
|
</pre>
|
|
|
|
<p>Even though it accesses memory, this approach takes only 20 cycles—more than twice as fast as the variable shift. Once again, we were able to improve performance considerably—not by knowing the fastest instructions, but by selecting the fastest <i>sequence</i> of instructions.</p>
|
|
|
|
<p>In the particular example above, we once again run into the difficulty of optimizing across the x86 family. The table lookup is faster on the 8088 and 286, but it’s slightly slower on the 386 and no faster on the 486. However, 386/486-specific code could use enhanced addressing to accomplish the whole job in just one instruction, along the lines of the code snippet in Listing 7.4.</p>
|
|
|
|
<p><b>LISTING 7.4 L7-4.ASM</b></p>
|
|
<pre>
|
|
OR EAX,ShiftTable[EBX*4] ;look up the bit and OR it in
|
|
:
|
|
ShiftTable LABEL DWORD
|
|
BIT_PATTERN=0001H
|
|
REPT 32
|
|
DD BIT_PATTERN
|
|
BIT_PATTERN=BIT_PATTERN SHL 1
|
|
ENDM
|
|
</pre>
|
|
|
|
<table width="100%">
|
|
<tr>
|
|
<td align="left" valign="top" width="5%"><img src="images/i.jpg" /></td>
|
|
|
|
<td align="left" valign="top" width="95%"><small><i>Besides illustrating the advantages of local optimization, this example also shows that it generally pays to precalculate results; this is often done at or before assembly time, but precalculated tables can also be built at run time. This is merely one aspect of a fundamental optimization rule: Move as much work as possible out of your critical code by whatever means necessary.</i></small></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<h4 id="Heading9">NOT Flips Bits—Not Flags</h4>
|
|
|
|
<p>The <b>NOT</b> instruction flips all the bits in the operand, from 0 to 1 or from 1 to 0. That’s as simple as could be, but <b>NOT</b> nonetheless has a minor but interesting talent: It doesn’t affect the flags. That can be irritating; I once spent a good hour tracking down a bug caused by my unconscious assumption that <b>NOT</b> does set the flags. After all, every other arithmetic and logical instruction sets the flags; why not <b>NOT</b>? Probably because <b>NOT</b> isn’t considered to be an arithmetic or logical instruction at all; rather, it’s a data manipulation instruction, like <b>MOV</b> and the various rotates. (These are <b>RCR, RCL, ROR,</b> and <b>ROL,</b> which affect only the Carry and Overflow flags.) NOT is often used for tasks, such as flipping masks, where there’s no reason to test the state of the result, and in that context it can be handy to keep the flags unmodified for later testing.</p>
|
|
|
|
<table width="100%">
|
|
<tr>
|
|
<td align="left" valign="top" width="5%"><img src="images/i.jpg" /></td>
|
|
|
|
<td align="left" valign="top" width="95%"><small><i>Besides, if you want to <b>NOT</b> an operand and set the flags in the process, you can just <b>XOR</b> it with -1. Put another way, the only functional difference between <b>NOT AX</b> and <b>XOR AX,0FFFFH</b> is that <b>XOR</b> modifies the flags and <b>NOT</b> doesn’t.</i></small></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>The x86 instruction set offers many ways to accomplish almost any task. Understanding the subtle distinctions between the instructions—whether and which flags are set, for example—can be critical when you’re trying to optimize a code sequence and you’re running out of registers, or when you’re trying to minimize branching.</p>
|
|
|
|
<h4 id="Heading10">Incrementing with and without Carry</h4>
|
|
|
|
<p>Another case in which there are two slightly different ways to perform a task involves adding 1 to an operand. You can do this with <b>INC,</b> as in <b>INC AX,</b> or you can do it with <b>ADD,</b> as in <b>ADD AX,1.</b> What’s the difference? The obvious difference is that <b>INC</b> is usually a byte or two shorter (the exception being <b>ADD AL,1,</b> which at two bytes is the same length as <b>INC AL</b>), and is faster on some processors. Less obvious, but no less important, is that <b>ADD</b> sets the Carry flag while <b>INC</b> leaves the Carry flag untouched.</p>
|
|
|
|
<p>Why is that important? Because it allows <b>INC</b> to function as a data pointer manipulation instruction for multi-word arithmetic. You can use <b>INC</b> to advance the pointers in code like that shown in Listing 7.5 without having to do any work to preserve the Carry status from one addition to the next.</p>
|
|
|
|
<p><b>LISTING 7.5 L7-5.ASM</b></p>
|
|
<pre>
|
|
CLC ;clear the Carry for the initial addition
|
|
LOOP_TOP:
|
|
MOV AX,[SI];get next source operand word
|
|
ADC [DI],AX;add with Carry to dest operand word
|
|
INC SI ;point to next source operand word
|
|
INC SI
|
|
INC DI ;point to next dest operand word
|
|
INC DI
|
|
LOOP LOOP_TOP
|
|
</pre>
|
|
|
|
<p>If <b>ADD</b> were used, the Carry flag would have to be saved between additions, with code along the lines shown in Listing 7.6.</p>
|
|
|
|
<p><b>LISTING 7.6 L7-6.ASM</b></p>
|
|
<pre>
|
|
CLC ;clear the carry for the initial addition
|
|
LOOP_TOP:
|
|
MOV AX,[SI] ;get next source operand word
|
|
ADC [DI],AX ;add with carry to dest operand word
|
|
LAHF ;set aside the carry flag
|
|
ADD SI,2 ;point to next source operand word
|
|
ADD DI,2 ;point to next dest operand word
|
|
SAHF ;restore the carry flag
|
|
LOOP LOOP_TOP
|
|
</pre>
|
|
|
|
<p>It’s not that the Listing 7.6 approach is necessarily better or worse; that depends on the processor and the situation. The Listing 7.6 approach is <i>different,</i> and if you understand the differences, you’ll be able to choose the best approach for whatever code you happen to write. (<b>DEC</b> has the same property of preserving the Carry flag, by the way.)</p>
|
|
|
|
<p>There are a couple of interesting aspects to the last example. First, note that <b>LOOP</b> doesn’t affect any flags at all; this allows the Carry flag to remain unchanged from one addition to the next. Not altering the arithmetic flags is a common characteristic of program control instructions (as opposed to arithmetic and logical instructions like <b>SUB</b> and <b>AND,</b> which do alter the flags).</p>
|
|
|
|
<table width="100%">
|
|
<tr>
|
|
<td align="left" valign="top" width="5%"><img src="images/i.jpg" /></td>
|
|
|
|
<td align="left" valign="top" width="95%"><small><i>The rule is not that the arithmetic flags change whenever the CPU performs a calculation; rather, the flags change whenever you execute an arithmetic, logical, or flag control (such as <b>CLC</b> to clear the Carry flag) instruction.</i></small></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>Not only do <b>LOOP</b> and <b>JCXZ</b> not alter the flags, but <b>REP MOVS</b>, which counts down CX to 0, doesn’t affect the flags either.</p>
|
|
|
|
<p>The other interesting point about the last example is the use of <b>LAHF</b> and <b>SAHF,</b> which transfer the low byte of the FLAGS register to and from AH, respectively. These instructions were created to help provide compatibility with the 8080’s (that’s <i>8080</i>, not <i>8088</i>) <b>PUSH</b> <b>PSW</b> and <b>POP PSW</b> instructions, but turn out to be compact (one byte) instructions for saving and restoring the arithmetic flags. A word of caution, however: <b>SAHF</b> restores the Carry, Zero, Sign, Auxiliary Carry, and Parity flags—but <i>not</i> the Overflow flag, which resides in the high byte of the FLAGS register. Also, be aware that <b>LAHF</b> and <b>SAHF</b> provide a fast way to preserve the flags on an 8088 but are relatively slow instructions on the 486 and Pentium.</p>
|
|
|
|
<p>There are times when it’s a clear liability that <b>INC</b> doesn’t set the Carry flag. For instance</p>
|
|
<pre>
|
|
INC AX
|
|
ADC DX,0
|
|
</pre>
|
|
|
|
<p>does <i>not</i> increment the 32-bit value in DX:AX. To do that, you’d need the following:</p>
|
|
<pre>
|
|
ADD AX,1
|
|
ADC DX,0
|
|
</pre>
|
|
|
|
<p>As always, pay attention!</p>
|
|
|
|
<center>
|
|
<table border="1">
|
|
<tr>
|
|
<td>
|
|
<a href="07-04.html">Previous</a>
|
|
</td>
|
|
|
|
<td>
|
|
<a href="index.html">Table of Contents</a>
|
|
</td>
|
|
|
|
<td>
|
|
<a href="08-01.html">Next</a>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<hr width="90%" size="1" noshade="noshade" />
|
|
|
|
<div align="center">
|
|
Graphics Programming Black Book © 2001 Michael Abrash
|
|
</div>
|
|
</body>
|
|
</html>
|