abrash-black-book/09-01.html
2013-12-30 12:21:49 +11:00

164 lines
9.6 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: Hints My Readers Gave Me</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=09//-->
<!--PAGES=167-172//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="08-05.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="09-02.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 9<BR>Hints My Readers Gave Me
</FONT></H2>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Optimization Odds and Ends from the Field</FONT></H3>
<P>Back in high school, I took a pre-calculus class from Mr. Bourgeis, whose most notable characteristics were incessant pacing and truly enormous feet. My friend Barry, who sat in the back row, right behind me, claimed that it was because of his large feet that Mr. Bourgeis was so restless. Those feet were <I>so</I> heavy, Barry hypothesized, that if Mr. Bourgeis remained in any one place for too long, the floor would give way under the strain, plunging the unfortunate teacher deep into the mantle of the Earth and possibly all the way through to China. Many amusing cartoons were drawn to this effect.</P>
<P>Unfortunately, Barry was too busy drawing cartoons, or, alternatively, sleeping, to actually learn any math. In the long run, that didn&#146;t turn out to be a handicap for Barry, who went on to become vice-president of sales for a ham-packing company, where presumably he was rarely called upon to derive the quadratic equation. Barry&#146;s lack of scholarship caused some problems back then, though. On one memorable occasion, Barry was half-asleep, with his eyes open but unfocused and his chin balanced on his hand in the classic &#147;if I fall asleep my head will fall off my hand and I&#146;ll wake up&#148; posture, when Mr. Bourgeis popped a killer problem:</P>
<P>&#147;Barry, solve this for X, please.&#148; On the blackboard lay the equation:</P>
<!-- CODE SNIP //-->
<PRE>
X - 1 = 0
</PRE>
<!-- END CODE SNIP //-->
<P>&#147;Minus 1,&#148; Barry said promptly.
</P>
<P>Mr. Bourgeis shook his head mournfully. &#147;Try again.&#148; Barry thought hard. He knew the fundamental rule that the answer to most mathematical questions is either 0, 1, infinity, -1, or minus infinity (do not apply this rule to balancing your checkbook, however); unfortunately, that gave him only a 25 percent chance of guessing right.</P>
<P>&#147;One,&#148; I whispered surreptitiously.</P>
<P>&#147;Zero,&#148; Barry announced. Mr. Bourgeis shook his head even more sadly.</P>
<P>&#147;One,&#148; I whispered louder. Barry looked still more thoughtful&#151;a bad sign&#151;so I whispered &#147;one&#148; again, even louder. Barry looked so thoughtful that his eyes nearly rolled up into his head, and I realized that he was just doing his best to convince Mr. Bourgeis that Barry had solved this one by himself.</P>
<P>As Barry neared the climax of his stirring performance and opened his mouth to speak, Mr. Bourgeis looked at him with great concern. &#147;Barry, can you hear me all right?&#148;</P>
<P>&#147;Yes, sir,&#148; Barry replied. &#147;Why?&#148;</P>
<P>&#147;Well, I could hear the answer all the way up here. Surely you could hear it just one row away?&#148;</P>
<P>The class went wild. They might as well have sent us home early for all we accomplished the rest of the day.</P>
<P>I like to think I know more about performance programming than Barry knew about math. Nonetheless, I always welcome good ideas and comments, and many readers have sent me a slew of those over the years. So in this chapter, I think I&#146;ll return the favor by devoting a chapter to reader feedback.</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Another Look at LEA</FONT></H4>
<P>Several people have pointed out that while <B>LEA</B> is great for performing certain additions (see Chapter 6), it isn&#146;t a perfect replacement for <B>ADD</B>. What&#146;s the difference? <B>LEA</B>, an addressing instruction by trade, doesn&#146;t affect the flags, while the arithmetic <B>ADD</B> instruction most certainly does. This is no problem when performing additions that involve only quantities that fit in one machine word (32 bits in 386 protected mode, 16 bits otherwise), but it renders <B>LEA</B> useless for multiword operations, which use the Carry flag to tie together partial results. For example, these instructions</P>
<!-- CODE SNIP //-->
<PRE>
ADD EAX,EBX
ADC EDX,ECX
</PRE>
<!-- END CODE SNIP //-->
<P>could <I>not</I> be replaced</P>
<!-- CODE SNIP //-->
<PRE>
LEA EAX,[EAX&#43;EBX]
ADC EDX,ECX
</PRE>
<!-- END CODE SNIP //-->
<P>because <B>LEA</B> doesn&#146;t affect the Carry flag.</P>
<P>The no-carry characteristic of <B>LEA</B> becomes a distinct advantage when performing pointer arithmetic, however. For instance, the following code uses <B>LEA</B> to advance the pointers while adding one 128-bit memory variable to another such variable:</P>
<!-- CODE //-->
<PRE>
MOV ECX,4 ;# of 32-bit words to add
CLC
;no carry into the initial ADC
ADDLOOP:
MOV EAX,[ESI] ;get the next element of one array
ADC [EDI],EAX ;add it to the other array, with carry
LEA ESI,[ESI&#43;4] ;advance one array&#146;s pointer
LEA EDI,[EDI&#43;4] ;advance the other array&#146;s pointer
LOOP ADDLOOP
</PRE>
<!-- END CODE //-->
<P>(Yes, I could use <B>LODSD</B> instead of <B>MOV/LEA</B>; I&#146;m just illustrating a point here. Besides, <B>LODS</B> is only 1 cycle faster than <B>MOV/LEA</B> on the 386, and is actually more than twice as slow on the 486.) If we used <B>ADD</B> rather than <B>LEA</B> to advance the pointers, the carry from one <B>ADC</B> to the next would have to be preserved with either <B>PUSHF/POPF</B> or <B>LAHF/SAHF</B>. (Alternatively, we could use multiple <B>INC</B>s, since <B>INC</B> doesn&#146;t affect the Carry flag.)</P>
<P>In short, <B>LEA</B> is indeed different from <B>ADD</B>. Sometimes it&#146;s better. Sometimes not; that&#146;s the nature of the various instruction substitutions and optimizations that will occur to you over time. There&#146;s no such thing as &#147;best&#148; instructions on the x86; it all depends on what you&#146;re trying to do.</P>
<P>But there sure are a lot of interesting options, aren&#146;t there?</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">The Kennedy Portfolio</FONT></H4>
<P>Reader John Kennedy regularly passes along intriguing assembly programming tricks, many of which I&#146;ve never seen mentioned anywhere else. John likes to optimize for size, whereas I lean more toward speed, but many of his optimizations are good for both purposes. Here are a few of my favorites:
</P>
<P>John&#146;s code for setting AX to its absolute value is:</P>
<!-- CODE SNIP //-->
<PRE>
CWD
XOR AX,DX
SUB AX,DX
</PRE>
<!-- END CODE SNIP //-->
<P>This does nothing when bit 15 of AX is 0 (that is, if AX is positive). When AX is negative, the code &#147;nots&#148; it and adds 1, which is exactly how you perform a two&#146;s complement negate. For the case where AX is not negative, this trick usually beats the stuffing out of the standard absolute value code:
</P>
<!-- CODE SNIP //-->
<PRE>
AND AX,AX ;negative?
JNS IsPositive ;no
NEG AX ;yes,negate it
IsPositive:
</PRE>
<!-- END CODE SNIP //-->
<P>However, John&#146;s code is slower on a 486; as you&#146;re no doubt coming to realize (and as I&#146;ll explain in Chapters 12 and 13), the 486 is an optimization world unto itself.
</P>
<P>Here&#146;s how John copies a block of bytes from DS:SI to ES:DI, moving as much data as possible a word at a time:</P>
<!-- CODE SNIP //-->
<PRE>
SHR CX,1 ;word count
REP MOVSW ;copy as many words as possible
ADC CX,CX ;CX=1 if copy length was odd,
;0 else
REP MOVSB ;copy any odd byte
</PRE>
<!-- END CODE SNIP //-->
<P>(<B>ADC CX,CX</B> can be replaced with <B>RCL CX,1</B>; which is faster depends on the processor type.) It might be hard to believe that the above is faster than this:</P>
<!-- CODE SNIP //-->
<PRE>
SHR CX,1 ;word count
REP MOVSW ;copy as many words as
;possible
JNC CopyDone ;done if even copy length
MOVSB ;copy the odd byte
CopyDone:
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="08-05.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="09-02.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 &copy; 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 -->