===========================================================================
 BBS: Inland Empire Archive
Date: 02-12-94 (05:32)             Number: 195
From: BJRN FELTEN                 Refer#: NONE
  To: SOLEIL LAPIERRE               Recvd: NO  
Subj: Questions about derivati       Conf: (1) 80xxx
---------------------------------------------------------------------------
 > I'm looking for the fastest algorithm for finding
 > square roots. The one
 > I posted uses only integers, which is acceptable, and
 > it's very fast for
 > small numbers. It's for a game.

   If you want the fractional part as well, I've got a routine that gives you
that in the low word and the integer part in the high word.
It's an inline macro for use in Borland Pascal, but I don't
think it should be hard to convert it to real assembler.
Originally it's a routine I wrote for the 6800 processor in
the late 70's, and since then I've ported it several times
to other platforms, so don't ask me now how it works -- but
it does! :)

   Anyway, here we go:

                           - = * = -


{ fast sqrt -- returns int part in hi word and fract part in low}
{ donated to the Public Domain by Bjrn Felten @ 2:203/208 1994}
function iSqrt(x:longint):longint;
inline
 ($66/$33/$c0       { xor   eax,eax}
 /$66/$33/$d2       { xor   edx,edx}
 /$66/$5f           { pop   edi   ; get x from stack}
 /$b9/$20/$00       { mov   cx,32}
                    {@L:}
 /$66/$d1/$e7       { shl   edi,1}
 /$66/$d1/$d2       { rcl   edx,1}
 /$66/$d1/$e7       { shl   edi,1}
 /$66/$d1/$d2       { rcl   edx,1}
 /$66/$d1/$e0       { shl   eax,1}
 /$66/$8b/$d8       { mov   ebx,eax}
 /$66/$d1/$e3       { shl   ebx,1}
 /$66/$43           { inc   ebx}
 /$66/$3b/$d3       { cmp   edx,ebx}
 /$7c/$05           { jl    @S}
 /$66/$2b/$d3       { sub   edx,ebx}
 /$66/$40           { inc   eax}
                    {@S:}
 /$e2/$dd           { loop  @L}
 /$66/$8b/$d0       { mov   edx,eax ; lo word already in AX}
 /$66/$c1/$ea/$10); { shr   edx,16  ; hi word returned in DX}

---
 * Origin: -=P=I=X=- / Psion Info Xchange (+46-31-960447) (2:203/208)
