🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.asm
Article #146

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

#146
From: Morten Welinder
Date: Mon, 04 Jan 1993 09:47
62 lines
2057 bytes
tm@netcom.com (Toshiyasu Morita) writes:

>I'm looking for a fast min() and max() algorithm, preferably without branching.

----------------------------------------------------------------------
; ax := unsigned-min(ax,dx)
;
	sub	ax,dx
	sbb	cx,cx
	and	ax,cx
	add	ax,dx
----------------------------------------------------------------------
; ax := unsigned-max(ax,dx)
;
	sub	dx,ax
	add	ax,dx
	sbb	cx,cx
	and	cx,dx
	sub	ax,cx
----------------------------------------------------------------------

The sequences are optimal for the 386 and 486 in 16-bit segments.
In 32-bit segments use 32 bit registers.

The best signed versions I can think of is by adding 8000h to the
operands, using the unsigned versions, and subtracting 8000h from the 
results:

----------------------------------------------------------------------
; ax := signed-min(ax,dx)
;
	xor	ah,80h
	xor	dh,80h
	sub	ax,dx
	sbb	cx,cx
	and	ax,cx
	add	ax,dx
	xor	ah,80h
----------------------------------------------------------------------
; ax := signed-max(ax,dx)
;
	xor	ah,80h
	xor	dh,80h
	sub	dx,ax
	add	ax,dx
	sbb	cx,cx
	and	cx,dx
	sub	ax,cx
	xor	ah,80h
----------------------------------------------------------------------



Morten Welinder
terra@diku.dk


-- 
------------------------------------------------------------------------
Visit the lyrics archive at ftp.cs.uwp (mirrored to nic.funet.fi, a site
in Finland). All kinds of lyrics available -- upload "yours" and join.
------------------------------------------Morten Welinder, terra@diku.dk
Technical Details
Message-ID: <1993Jan4.094755.9593@odin.diku.dk>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!spool.mu.edu!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!dkuug!diku!terra
References: <1992Dec30.152808.25311@netcom.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!spool.mu.edu!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!dkuug!diku!terra
From: terra@diku.dk (Morten Welinder)
Newsgroups: alt.lang.asm
Subject: Re: looking for fast min()/max()
Keywords: speed min max
Message-ID: <1993Jan4.094755.9593@odin.diku.dk>
Date: 4 Jan 93 09:47:55 GMT
References: <1992Dec30.152808.25311@netcom.com>
Sender: terra@embla.diku.dk
Organization: Department of Computer Science, U of Copenhagen
Lines: 62