home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / lang / asm / 524 < prev    next >
Encoding:
Text File  |  1993-01-02  |  1.0 KB  |  31 lines

  1. Newsgroups: alt.lang.asm
  2. Path: sparky!uunet!microsoft!hexnut!chuckst
  3. From: chuckst@microsoft.com (Chuck Strouss)
  4. Subject: Re: looking for fast min()/max()
  5. Message-ID: <1993Jan02.141404.19648@microsoft.com>
  6. Date: 02 Jan 93 14:14:04 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec30.152808.25311@netcom.com>
  9. Keywords: speed min max
  10. Lines: 19
  11.  
  12. In article <1992Dec30.152808.25311@netcom.com> tm@netcom.com (Toshiyasu Morita) writes:
  13. >I'm looking for a fast min() and max() algorithm, preferably without branching.
  14. >
  15. >Anyone know of any?
  16. >
  17.  
  18. I think this is the standard r1=min(r1,r2) trick, assuming two registers
  19. holding unsigned quantities on an 80x86:
  20.  
  21.     sub   r2,r1   ; set carry if r1>r2, leave r2-r1 in r2 
  22.     sbb   rs,rs   ; scratch register = 0ffffh if we want r2 
  23.     and   rs,r2   ; zero difference if we want r1
  24.     add   r1,rs   ; replace r1 value with r2 value if r1>r2
  25.  
  26. This trashes r2, and requires a scratch register, but it screams
  27. if you have a >386 which bites when you branch.  There is an
  28. analog of this for max.
  29.  
  30.  
  31.