home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / msdos / programm / 10879 < prev    next >
Encoding:
Internet Message Format  |  1992-11-24  |  2.6 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!ames!sun-barr!cs.utexas.edu!ut-emx!slcs.slb.com!leo.asc.slb.com!nobelium!hargrove
  2. From: hargrove@nobelium.asc.slb.com (Jim Hargrove)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: Fast Square Root (All integer?)
  5. Message-ID: <1erog7INNmn@leo.asc.slb.com>
  6. Date: 23 Nov 92 23:12:07 GMT
  7. References: <wagner.902.722036355@main.mndly.umn.edu> <Bxw6oM.IKC@news.cso.uiuc.edu>
  8. Sender: hargrove@nobelium (Jim Hargrove)
  9. Organization: Schlumberger Austin Systems Center
  10. Lines: 67
  11. NNTP-Posting-Host: nobelium.asc.slb.com
  12.  
  13. The following seems to be a very reasonable solution for the GENERAL case. If, on the other hand, you have a smaller range of integers, you may want to use a table lookup instead. This trick has been used in computer graphics for years. It works best when you are limited to 12 bits or less. Then the table is a reasonable size.
  14. In article <Bxw6oM.IKC@news.cso.uiuc.edu>, jas37876@uxa.cso.uiuc.edu (John A. Slagel) writes:
  15. |> wagner@main.mndly.umn.edu (Rick Wagner) writes:
  16. |> 
  17. |> >Does anyone have any ideas (or code) on how to implement a FAST square root 
  18. |> >algorythm in C that only uses integers?  Obviously this only needs to be 
  19. |> >an approximation of the square root, the key factor here is speed.
  20. |> 
  21. |> >Any Ideas?
  22. |> 
  23. |>    Well, from a Numerical Computing class, we have this algorithm:
  24. |> 
  25. |>    procedure square root of c with error <= E
  26. |>        x := 1;  y := x;
  27. |>        while abs(x-y) > 2*E do
  28. |>        x:=(x+y)/2; y:= c/x
  29. |>        end while
  30. |>        return (x+y)/2
  31. |> 
  32. |>    I wrote it in Microsoft inline assembly for int's, with an error
  33. |>    of 1.  Enjoy.
  34. |> 
  35. |> 
  36. |> int isqrt( int c )
  37. |> {
  38. |>     _asm {
  39. |>     mov     cx, 1
  40. |>     mov     bx, c
  41. |>     jmp     skip_iterate
  42. |> 
  43. |> iterate:
  44. |>     mov     ax, cx
  45. |>     sub     ax, bx   ; AX = x - y
  46. |>     cwd
  47. |>     xor     ax, dx
  48. |>     sub     ax, dx  ; AX = abs( AX )
  49. |>     cmp     ax, 2   ; 2 is the allowed error * 2, so we're getting within 1.
  50. |>     jl      done    ; if abs(x-y) > 2 then goto done
  51. |> skip_iterate:
  52. |>     mov     ax, cx
  53. |>     add     ax, bx
  54. |>     shr     ax, 1
  55. |>     mov     cx, ax   ; x = (x+y)/2
  56. |>     cwd
  57. |>     mov     ax, c
  58. |>     idiv    cx
  59. |>     mov     bx, ax   ; y = c / x
  60. |>     jmp     iterate
  61. |> 
  62. |> done:
  63. |>     mov     ax, cx
  64. |>     add     ax, bx
  65. |>     shr     ax, 1
  66. |>     mov     c, ax
  67. |>     }
  68. |>     return c;
  69. |> }
  70. |> 
  71. |> -- 
  72. |> -----------------------------------------------------------------------------
  73. |>  John A. Slagel              "My old man used to tell me, before he left this
  74. |>  j-slagel1@uiuc.edu           shitty world, never chase buses or women- you
  75. |>  (217) 337-7930               always get left behind." -The Marlboro Man
  76.  
  77. -- 
  78.  
  79.         -- jwh
  80.