home *** CD-ROM | disk | FTP | other *** search
- ;**************************************************************
- ;
- ; $sqrt.asm
- ;
- ; staff
- ;
- ; 02-17-89
- ;
- ; (C) Texas Instruments Inc., 1992
- ;
- ; Refer to the file 'license.txt' included with this
- ; this package for usage and license information.
- ;
- ;**************************************************************
- *******************************************************************************
- * SUBROUTINE SQRT
- *******************************************************************************
- *
- * SQRT == SQUARE ROOT OF A POSITIVE FLOATING-POINT NUMBER
- *
- * Abstract:
- * Given a positive floating-point value, determine its square root.
- *
- * AUTHOR: PANOS PAPAMICHALIS
- * TEXAS INSTRUMENTS, INC. 15 JAN 1988
- *
- *
- * Algorithm:
- *
- * Given v = a * 2**e
- * x[0] = 1.0 * 2**(-e/2)
- * for (i = 1; i <= 5; i++)
- * x[i] = x[i-1] * (1.5 - (v/2) * x[i-1] * x[i-1])
- *
- * The final result is sqrt(v**(-1)), and it has to be multiplied by v
- * to obtain the desired square root.
- * The algorithm's error at an iteration i (e[i]) is defined as
- * e[i] = 1 - sqrt(v**(-1)) * x[i]
- * It can also be shown that e[i+1] < e[i].
- *
- * Typical calling sequence:
- * LDF v, R0
- * CALL SQRT
- *
- * Argument Assignments:
- * argument | function
- * ---------+------------------
- * R0 | v = number to find the square root of (upon the call)
- * R0 | sqrt(v) (upon the return)
- *
- * Register used as input: R0
- * Registers modified: R0, R1, R2, R3
- * Register containing result: R0
- *
- * CYCLES: 43 WORDS: 37
- *
- * Version 1.1: Correction to round e/2.
- * Version 1.2: Correction to round x[i]. 2/15/89 AL
- *******************************************************************************
- .global SQRT
- ;
- ; Extract the exponent of v.
- ;
- SQRT: LDF R0,R3 ; save v
- RETSLE ; return if number non-positive
- MPYF 2.0,R0 ; add a rounding bit in the exponent
- PUSHF R0
- POP R1
- ASH -25,R1 ; The 8 LSBs of R1 contain 1/2 the exponent of v.
- ;
- ; x[0] formation given the exponent of v.
- ;
- NEGI R1
- ASH 24,R1
- PUSH R1
- POPF R1 ; Now R1 = x[0] = 1.0 * 2**(-e/2).
- ;
- ; Generate v/2.
- ;
- MPYF 0.25,R0 ; v/2 and take rounding bit out.
- ;
- ; Now the iterations begin.
- ;
- MPYF R1,R1,R2 ; R2 = x[0] * x[0]
- MPYF R0,R2 ; R2 = (v/2) * x[0] * x[0]
- SUBRF 1.5,R2 ; R2 = 1.5 - (v/2) * x[0] * x[0]
- MPYF R2,R1 ; R1 = x[1] = x[0] * (1.5 - (v/2)*x[0]*x[0])
- RND R1
- ;
- MPYF R1,R1,R2 ; R2 = x[1] * x[1]
- MPYF R0,R2 ; R2 = (v/2) * x[1] * x[1]
- SUBRF 1.5,R2 ; R2 = 1.5 - (v/2) * x[1] * x[1]
- MPYF R2,R1 ; R1 = x[2] = x[1] * (1.5 - (v/2)*x[1]*x[1])
- RND R1
- ;
- MPYF R1,R1,R2 ; R2 = x[2] * x[2]
- MPYF R0,R2 ; R2 = (v/2) * x[2] * x[2]
- SUBRF 1.5,R2 ; R2 = 1.5 - (v/2) * x[2] * x[2]
- MPYF R2,R1 ; R1 = x[3] = x[2] * (1.5 - (v/2)*x[2]*x[2])
- RND R1
- ;
- MPYF R1,R1,R2 ; R2 = x[3] * x[3]
- MPYF R0,R2 ; R2 = (v/2) * x[3] * x[3]
- SUBRF 1.5,R2 ; R2 = 1.5 - (v/2) * x[3] * x[3]
- MPYF R2,R1 ; R1 = x[4] = x[3] * (1.5 - (v/2)*x[3]*x[3])
- RND R1
- ;
- MPYF R1,R1,R2 ; R2 = x[4] * x[4]
- MPYF R0,R2 ; R2 = (v/2) * x[4] * x[4]
- SUBRF 1.5,R2 ; R2 = 1.5 - (v/2) * x[4] * x[4]
- MPYF R2,R1 ; R1 = x[5] = x[4] * (1.5 - (v/2)*x[4]*x[4])
- ;
- RND R1,R0 ; Round
- ;
- MPYF R3,R0 ; sqrt(v) from sqrt(v**(-1))
- ;
- RETS
- ;
- ; end
- ;
- .end