Article View: comp.arch
Article #11364Re: integer distance or sqrt
From: Ulrich Graef SUN Frankfurt TSE
Date: Fri, 20 Nov 1992 17:53
Date: Fri, 20 Nov 1992 17:53
34 lines
1859 bytes
1859 bytes
If you are able to spend some memory, you can improve the processing very much. First Method: There are only 2^16 square roots of a number, which is 32 Bits long. So generate a table of the squares of the numbers from 2 2 0 ... ( 2^16 - 1 ) and find the square root with a binary search. You need only 1 comparison, 1 addition, 1 shift and 1 assignment per search step. If you unroll the loop, you do not need any loop overhead. Disadvantage: the table consumes 256 KB. You can also use the same table to square your dx and dy, if your multiplication is slower than a table lookup. Second Method: Use Newton, but find the first approximation with a table lookup. Variant a: Use a table of the square roots of 0 .. 255 (* 2^24) Perform the lookup with the first byte. You need 2 additional iterations with Newton. The table consumes 1 KB. Variant b: Use a table of the square roots of 0 .. (2^16-1) (* 2^16) Perform the lookup with teh first two bytes. You need 1 Newton iteration (onle`y 1 division!). Disadvantage: the table consumes 256 KB. I think, if you spend enwough memory (you have no PC?) you can calculate the distance very fast with the second method, variant b! Uli
Technical Details
Message-ID:
<6839@sungy.Germany.Sun.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!wupost!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!seven-up.East.Sun.COM!sungy!weissnics!ulrichReferences:
<55F0TB1w165w@tsoft.sf-bay.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!wupost!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!seven-up.East.Sun.COM!sungy!weissnics!ulrich From: ulrich@weissnics.Germany.Sun.COM (Ulrich Graef SUN Frankfurt TSE) Newsgroups: comp.arch Subject: Re: integer distance or sqrt Message-ID: <6839@sungy.Germany.Sun.COM> Date: 20 Nov 92 17:53:43 GMT References: <55F0TB1w165w@tsoft.sf-bay.org> Sender: news@sungy.Germany.Sun.COM Reply-To: ulrich@weissnics.Germany.Sun.COM Organization: Sun Microsystems Lines: 34 Nntp-Posting-Host: weissnics