home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8603.arc
/
CAMPLST.MAR
< prev
next >
Wrap
Text File
|
1986-03-31
|
5KB
|
178 lines
Listing One
/* a square root algorithm
04/21/85, R. A. Campbell
*/
#define NUMBER 60000
long number, ndivs, nshfts;
long sqrrt, sqrrto;
main() /* a test loop for square root calculation */
{ nshfts = ndivs = 0;
sqrrto = 100;
for ( number = 0; number <= NUMBER; ++number )
{ sqrrt = sqrt ( number );
/* for timing, comment out next 2 lines */
if ( sqrrt != sqrrto )
printf ("sqr %6d=%4d ",number, sqrrt );
sqrrto = sqrrt;
}
printf ("\nTot Divs= %u Ave Divs.(x10)= %u",
ndivs, (10 * ndivs)/NUMBER);
printf ("\nTot Shfts= %u Ave Shfts.(x10)= %u",
nshfts, (10 * nshfts)/NUMBER);
}
sqrt ( numb ) /* a `rational' square root */
register long numb;
{ register long guess1, guess2, error;
/* return; for timing checks */
if ( numb != 0 )
{ guess1 =1;
guess2 = numb;
while ( (guess1 << 1) < guess2 )
{ guess1 <<= 1; /* multiply guess1 by 2 */
guess2 >>= 1; /* divide guess2 by 2 */
++nshfts; /* for statistics */
}
do /* now, check */
{ guess1 += guess2; /* figure sum */
guess1 >>= 1; /* fig mean, divide by two */
guess2 = numb / guess1; /* figure quotient */
error = guess1 - guess2; /* figure error */
++ndivs; /* for statistics */
} while ( error > 0 );
return guess1;
}
else
return 0;
}
Listing Two
;SCN:v5 (C) 1985
PROGRAM
STATIC 1
Qnumber: DOUBLE
Qndivs: DOUBLE
Qnshfts: DOUBLE
Qsqrrt: DOUBLE
Qsqrrto: DOUBLE
PROGRAM
JUMP Q_shell
Qmain: PROC
VAR
BEGIN
MOVQ.D 0,R6
MOV.D R6,Qndivs
MOV.D R6,Qnshfts
MOV.D 100,Qsqrrto
MOVQ.D 0,Qnumber
Q1: CMP.D 60000,Qnumber
BLT.S Q3
Q2: MOV.D Qnumber,TOS
BSR Qsqrt
MOV.D R6,Qsqrrt
CMP.D Qsqrrto,Qsqrrt
BEQ.S Q5
Q4: MOV.D Qsqrrt,TOS
MOV.D Qnumber,TOS
ADDR Z6,TOS
JSR Qprintf
ADJSP -8
Q5: MOV.D Qsqrrt,Qsqrrto
ADDQ.D 1,Qnumber
BR.S Q1
Q3: MOV.D 10,R6
MUL.D Qndivs,R6
QUO.D 60000,R6
MOV.D R6,TOS
MOV.D Qndivs,TOS
ADDR Z6+13,TOS
JSR Qprintf
ADJSP -8
MOV.D Qnshfts,R6
QUO.D 60000,R6
MOV.D R6,TOS
MOV.D Qnshfts,TOS
ADDR Z6+46,TOS
JSR Qprintf
ADJSP -8
ENDPROC
Z6: BYTE 115,113,114,32,37,53,100,61,37,51,100,32
BYTE 0,10,84,111,116,32,68,105,118,115,61,37
BYTE 51,117,32,65,118,101,32,68,105,118,115,46
BYTE 40,120,49,48,41,61,37,50,117,0,10,84
BYTE 111,116,32,83,104,102,116,115,61,37,51,117
BYTE 32,65,118,101,32,83,104,102,116,115,46,61
BYTE 37,50,117,0
Listing Three
Qsqrt: PROC ;square root, dividing version
Znumb: DOUBLE ;registers R0, R6 & R7 used by compiler
VAR ;registers R1 - R5 for register variables
BEGIN <R4,R3,R2,R1,> ;push used registers
; BR Q15 ;for timing
MOV.D Znumb,R1 ;get number into R1
CMPQ.D 0,R1 ;is number zero?
BEQ Q8 ;mustn't divide by zero
Q7: MOVQ.D 1,R2 ;guess1 = 1
MOV.D R1,R3 ;guess2 = number
Q9: MOV.D R2,R6 ;load guess1
ASH.D 1,R6 ;guess1 << 1
CMP.D R3,R6 ;compare with guess2
BLE.S Q11 ;while ( (guess1 << 1) < guess2 )
Q10: ASH.D 1,R2 ;guess1 <<= 1
ASH.D -1,R3 ;guess2 >>= 1
BR.S Q9 ;see if done
Q11:
Q13: ADD.D R3,R2 ;guess1 += guess2
ASH.D -1,R2 ;guess1 /= 2
MOV.D R1,R6 ;numb
QUO.D R2,R6 ;numb / guess1
MOV.D R6,R3 ;guess2 = numb / guess1
MOV.D R2,R6
SUB.D R3,R6
MOV.D R6,R4 ;error = guess1 - guess2
CMPQ.D 0,R4
BGE.S Q14 ;while ( error > 0 )
BR.S Q13
Q14: MOV.D R2,R6 ;return guess1
BR.S Q15
Q8: MOVQ.D 0,R6 ;return 0
Q16:
Q15: ENDPROC ;exit
IMPORT Q_shell 00
IMPORT Qprintf 01
END ;SCN, 03/22//85
Listing Four
Qsqrt: PROC ;square root, bit-shifting version
Znumb: DOUBLE
VAR
BEGIN <R4,R3,R1,> ;push registers used
; BR.S SQRTZ ;for timing
MOV.D Znumb,R0 ;number to R0
MOVQ.D 0,R1 ;estimate = 0
MOVQ.D 0,R6 ;trial root = 0
MOV.B 16,R4 ;loop count
SQRTL: ROT.D 2,R0 ;2 msb's to 2 lsb's of number
MOV.B R0,R3 ;get shifted number in R3
AND.B 3,R3 ;isolate 2 lsb's of R3
ASH.D 2,R1 ;shift estimate
OR.B R3,R1 ;OR 2 lsb's into estimate
ASH.D 1,R6 ;shift trial root
MOV.D R6,R3 ;get trial root
ASH.D 1,R3 ;trial root * 2
CMP.D R1,R3 ;compare to estimate
BLS.S SQRT2 ;need a bit?
ADDQ.D 1,R6 ;add to trial root
ADDQ.D 1,R3 ;add to estimate
SUB.D R3,R1 ;adjust estimate
SQRT2: ACB.S -1,R4,SQRTL ;count down, loop
SQRTZ: ENDPROC ;return root, in R6