home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 364b.lha / PCQ_v1.1 / Runtime / PCQ / sqrt.asm < prev    next >
Encoding:
Assembly Source File  |  1990-04-08  |  1.6 KB  |  62 lines

  1. *
  2. *    Sqrt.asm (of PCQ Pascal)
  3. *    Copyright 1990 Patrick Quaid
  4. *
  5. *    This routine just implements the sqrt function using
  6. *    Newton's method.  As I recall, this routine will in
  7. *    some cases diverge, but it seems to work reasonably
  8. *    well.  It figures the square root to 5 or 6 digits,
  9. *    which in this implementation requires between, say,
  10. *    6 and 12 iterations (for 100 and 200000, respectively).
  11. *
  12.  
  13.     XREF    _p%MathBase
  14.  
  15.     XREF    _LVOSPDiv
  16.     XREF    _LVOSPMul
  17.     XREF    _LVOSPCmp
  18.     XREF    _LVOSPAdd
  19.     XREF    _LVOSPSub
  20.     XREF    _LVOSPAbs
  21.  
  22.     XDEF    _p%sqrt
  23. _p%sqrt
  24.     move.l    4(sp),d0        ; d0 := x
  25.     move.l    #$80000042,d1        ; d1 := 2.0
  26.     move.l    _p%MathBase,a6
  27.     jsr    _LVOSPDiv(a6)        ; d0 := x / 2.0
  28.     move.l    d0,-(sp)        ; save approx on the stack
  29.  
  30.     move.l    8(sp),d0        ; d0 := x
  31.     move.l    #$C3500051,d1        ; d1 := 100000.0
  32.     jsr    _LVOSPDiv(a6)        ; d0 := x / 100000.0
  33.     move.l    d0,-(sp)        ; save epsilon on the stack
  34.  
  35. ComputeError:
  36.     move.l    4(sp),d0        ; get approx
  37.     move.l    d0,d1            ; move to d1
  38.     jsr    _LVOSPMul(a6)        ; d0 := Sqr(approx)
  39.     move.l    d0,d1            ; d1 := Sqr(approx)
  40.     move.l    12(sp),d1        ; d0 := x
  41.     jsr    _LVOSPSub(a6)        ; d0 := x - Sqr...
  42.     jsr    _LVOSPAbs(a6)        ; d0 := abs(x - sqr())
  43.     move.l    (sp),d1            ; d1 := epsilon
  44.     jsr    _LVOSPCmp(a6)
  45.     ble.s    ReturnApprox        ; if error <= epsilon, leave
  46.  
  47.     move.l    12(sp),d0        ; d0 := x
  48.     move.l    4(sp),d1        ; d1 := approx
  49.     jsr    _LVOSPDiv(a6)        ; d0 := x / approx
  50.     move.l    4(sp),d1        ; d1 := approx
  51.     jsr    _LVOSPAdd(a6)        ; d0 := approx + x / approx
  52.     move.l    #$80000042,d1        ; d1 := 2.0
  53.     jsr    _LVOSPDiv(a6)        ; d0 := (approx + x/approx)/2
  54.     move.l    d0,4(sp)        ; approx := d0
  55.     bra    ComputeError        ; iterate
  56. ReturnApprox
  57.     move.l    4(sp),d0        ; d0 := approx
  58.     addq.l    #8,sp            ; pop stack
  59.     rts
  60.  
  61.     END
  62.