🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.asm
Article #133

Re: looking for fast min()/max()

#133
From: Chuck Strouss
Date: Sat, 02 Jan 1993 14:14
17 lines
1073 bytes
In article <1992Dec30.152808.25311@netcom.com> tm@netcom.com (Toshiyasu Morita) writes:
>I'm looking for a fast min() and max() algorithm, preferably without branching.
>
>Anyone know of any?
>

I think this is the standard r1=min(r1,r2) trick, assuming two registers
holding unsigned quantities on an 80x86:

    sub   r2,r1   ; set carry if r1>r2, leave r2-r1 in r2 
    sbb   rs,rs   ; scratch register = 0ffffh if we want r2 
    and   rs,r2   ; zero difference if we want r1
    add   r1,rs   ; replace r1 value with r2 value if r1>r2

This trashes r2, and requires a scratch register, but it screams
if you have a >386 which bites when you branch.  There is an
analog of this for max.
Technical Details
Message-ID: <1993Jan02.141404.19648@microsoft.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!microsoft!hexnut!chuckst
References: <1992Dec30.152808.25311@netcom.com>

Show Full Headers
Complete Article Headers
Newsgroups: alt.lang.asm
Path: arc1-ca.pugleaf.net!sparky!uunet!microsoft!hexnut!chuckst
From: chuckst@microsoft.com (Chuck Strouss)
Subject: Re: looking for fast min()/max()
Message-ID: <1993Jan02.141404.19648@microsoft.com>
Date: 02 Jan 93 14:14:04 GMT
Organization: Microsoft Corporation
References: <1992Dec30.152808.25311@netcom.com>
Keywords: speed min max
Lines: 19