home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / coding / dsp / c30ug.exe / $SQRT.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-02-17  |  3.7 KB  |  122 lines

  1. ;**************************************************************
  2. ;  
  3. ;                 $sqrt.asm
  4. ;  
  5. ;                 staff
  6. ;  
  7. ;                 02-17-89
  8. ;  
  9. ;           (C) Texas Instruments Inc., 1992 
  10. ;  
  11. ;           Refer to the file 'license.txt' included with this 
  12. ;           this package for usage and license information. 
  13. ;  
  14. ;**************************************************************
  15. *******************************************************************************
  16. *                            SUBROUTINE SQRT
  17. *******************************************************************************
  18. *
  19. * SQRT == SQUARE ROOT OF A POSITIVE FLOATING-POINT NUMBER
  20. *
  21. * Abstract:
  22. * Given a positive floating-point value, determine its square root.
  23. *
  24. * AUTHOR: PANOS PAPAMICHALIS
  25. *    TEXAS INSTRUMENTS, INC.        15 JAN 1988
  26. *
  27. *
  28. * Algorithm:
  29. *
  30. * Given v = a * 2**e
  31. * x[0] = 1.0 * 2**(-e/2)
  32. * for (i = 1; i <= 5; i++)
  33. *   x[i] = x[i-1] * (1.5 - (v/2) * x[i-1] * x[i-1])
  34. *
  35. * The final result is sqrt(v**(-1)), and it has to be multiplied by v
  36. * to obtain the desired square root.
  37. * The algorithm's error at an iteration i (e[i]) is defined as
  38. *   e[i] = 1 - sqrt(v**(-1)) * x[i]
  39. * It can also be shown that e[i+1] < e[i].
  40. *    
  41. * Typical calling sequence:
  42. *   LDF v, R0
  43. *   CALL SQRT
  44. *
  45. * Argument Assignments:
  46. *   argument | function
  47. *   ---------+------------------
  48. *   R0       | v = number to find the square root of (upon the call)
  49. *   R0       | sqrt(v) (upon the return)
  50. *
  51. * Register used as input: R0
  52. * Registers modified: R0, R1, R2, R3
  53. * Register containing result: R0
  54. *
  55. * CYCLES: 43        WORDS: 37
  56. *
  57. * Version 1.1: Correction to round e/2.
  58. * Version 1.2: Correction to round x[i].   2/15/89 AL
  59. *******************************************************************************
  60.         .global SQRT
  61. ;
  62. ;   Extract the exponent of v.
  63. ;
  64. SQRT:   LDF    R0,R3        ; save v
  65.     RETSLE            ; return if number non-positive
  66.         MPYF    2.0,R0          ; add a rounding bit in the exponent
  67.     PUSHF   R0
  68.         POP     R1
  69.         ASH     -25,R1      ; The 8 LSBs of R1 contain 1/2 the exponent of v.
  70. ;
  71. ;   x[0] formation given the exponent of v.
  72. ;
  73.         NEGI    R1
  74.         ASH     24,R1
  75.         PUSH    R1
  76.         POPF    R1              ; Now R1 = x[0] = 1.0 * 2**(-e/2).
  77. ;
  78. ; Generate v/2.
  79. ;
  80.         MPYF    0.25,R0         ; v/2 and take rounding bit out.
  81. ;
  82. ; Now the iterations begin.
  83. ;
  84.         MPYF    R1,R1,R2        ; R2 = x[0] * x[0]
  85.     MPYF    R0,R2        ; R2 = (v/2) * x[0] * x[0]
  86.         SUBRF   1.5,R2          ; R2 = 1.5 - (v/2) * x[0] * x[0]
  87.         MPYF    R2,R1           ; R1 = x[1] = x[0] * (1.5 - (v/2)*x[0]*x[0])
  88.     RND    R1
  89. ;
  90.         MPYF    R1,R1,R2        ; R2 = x[1] * x[1]
  91.     MPYF    R0,R2        ; R2 = (v/2) * x[1] * x[1]
  92.         SUBRF   1.5,R2          ; R2 = 1.5 - (v/2) * x[1] * x[1]
  93.         MPYF    R2,R1           ; R1 = x[2] = x[1] * (1.5 - (v/2)*x[1]*x[1])
  94.     RND    R1
  95. ;
  96.         MPYF    R1,R1,R2        ; R2 = x[2] * x[2]
  97.     MPYF    R0,R2        ; R2 = (v/2) * x[2] * x[2]
  98.         SUBRF   1.5,R2          ; R2 = 1.5 - (v/2) * x[2] * x[2]
  99.         MPYF    R2,R1           ; R1 = x[3] = x[2] * (1.5 - (v/2)*x[2]*x[2])
  100.     RND    R1
  101. ;
  102.         MPYF    R1,R1,R2        ; R2 = x[3] * x[3]
  103.     MPYF    R0,R2        ; R2 = (v/2) * x[3] * x[3]
  104.         SUBRF   1.5,R2          ; R2 = 1.5 - (v/2) * x[3] * x[3]
  105.         MPYF    R2,R1           ; R1 = x[4] = x[3] * (1.5 - (v/2)*x[3]*x[3])
  106.     RND    R1
  107. ;
  108.         MPYF    R1,R1,R2        ; R2 = x[4] * x[4]
  109.     MPYF    R0,R2        ; R2 = (v/2) * x[4] * x[4]
  110.         SUBRF   1.5,R2          ; R2 = 1.5 - (v/2) * x[4] * x[4]
  111.         MPYF    R2,R1           ; R1 = x[5] = x[4] * (1.5 - (v/2)*x[4]*x[4])
  112. ;
  113.     RND    R1,R0        ; Round
  114. ;
  115.     MPYF    R3,R0        ; sqrt(v) from sqrt(v**(-1))
  116. ;
  117.     RETS
  118. ;
  119. ; end
  120. ;
  121.         .end
  122.