24-bit square root routine

Started by ntricoup July 12, 2004
Searching the web for a squrae root routine i came up with many
usefull math routines in the next address
http://techref.massmind.org/techref/microchip
For square root i found many routines. I used the 32-bit which for
many inputs give irrelevant outputs. Next i tried the 24- bit integer
sqrt which follows next. I altered some variables name? I can't
figure out what test variable is. I can;t make it work. Any idea?
Thanks

; brSQRT24
;
; Calculates the square root of a twentyfour bit number using the
; binary restoring method.
;
; Result in ResultH,ResultL
; Input in InputH,InputM,InputL
; Test TestH,TestL
; Counter in Counter
;
; 40 words long, uses 8 bytes RAM (inc. 3 holding 24-bit input).
;
;--------------------------------
SQRT24 macro InputH,InputM,InputL,ResultH,ResultL,TestH,TestL
local ShftUp
local SubTest
local BitLoop
local Set1
local Set0

clrf ResultH ;
clrf ResultL ;

clrf TestL ;
clrf TestH ;

movlw .12 ;
movwf Counter ; (6 cycle intro, 8 incl. call)

ShftUp
rlf InputL,f ; Shift input up 2 places.
rlf InputM,f ; (33 cycles per loop if bit is 0)
rlf InputH,f ; (29 cycles per loop if bit is 1)
rlf TestL,f ;
rlf TestH,f ;
rlf InputL,f ;
rlf InputM,f ;
rlf InputH,f ;
rlf TestL,f ;
rlf TestH,f ;

rlf ResultL,f ; Shift root-so-far up by two and append
rlf ResultH,f ; ... '01'.
rlf ResultL,f ;
rlf ResultH,f ;
bcf ResultL,1 ;
bsf ResultL,0 ;

SubTest
movf ResultL,W ;
subwf TestL,f ;
movf ResultH,W ;
subwf TestH,f ;

btfsc STATUS,C ;
goto Set1

movf ResultL,W ; Restore the remainder.
addwf TestL,f ; ... (the current bit is 0).
movf ResultH,W ;
addwf TestH,f ;
goto Set0 ;

Set1
bsf ResultL,1 ;
Set0
rrf ResultH,f ;
rrf ResultL,f ;
bcf ResultH,7 ;

BitLoop
decfsz Counter,f ;
goto ShftUp
endm



Try this: ;==========================================================================
; brSQRT24
;
; Calculates the square root of a twentyfour bit number using the
; binary restoring method.
;
; Result in ACCaHI:ACCaLO
; Input in ACCcLO:ACCdHI:ACCdLO
; Test ACCbLO:ACCcHI
; Counter in ACCbHI
;
; 40 words long, uses 8 bytes RAM (inc. 3 holding 24-bit input).
;
;--
;Estas s instrues de PIC18.

;RLCF = RLF do PIC16
;RRCF = RRF do PIC16
;SUBWFB = subtrao com emprtimo, equivale em PIC16 a:
; BTFSS STATUS,C
; ADDLW 1
; SUBWF registrador,destino
;ADDWFC = adio com transporte, equivale em PIC16 a:
; BTFSC STATUS,C
; ADDLW 1
; ADDWF registrador,destino

RAIZ24:

clrf ACCAHI ;
clrf ACCALO ;

clrf ACCCHI ;
clrf ACCBLO ;
CLRW

movlw .12 ;
movwf ACCBHI ; (6 cycle intro, 8 incl. call)

ShftUp:
rlf ACCDLO,f ; Shift input up 2 places.
rlf ACCDHI,f ; (33 cycles per loop if bit is 0)
rlf ACCCLO,f ; (29 cycles per loop if bit is 1)
rlf ACCCHI,f ;
rlf ACCBLO,f ;
rlf ACCDLO,f ;
rlf ACCDHI,f ;
rlf ACCCLO,f ;
rlf ACCCHI,f ;
rlf ACCBLO,f ;

rlf ACCALO,f ; Shift root-so-far up by two and append
rlf ACCAHI,f ; ... '01'.
rlf ACCALO,f ;
rlf ACCAHI,f ;
bcf ACCALO,1 ;
bsf ACCALO,0 ;

SubTest:
movfW ACCALO ;
subwf ACCCHI,f ;
movfW ACCAHI ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; subwfb ACCbLO,f ;
;SUBWFB = subtrao com emprtimo, equivale em PIC16 a:
; BTFSS STATUS,C
; ADDLW 1
; SUBWF registrador,destino
BTFSS STATUS,C
ADDLW 1
SUBWF ACCBLO,F
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
btfsc STATUS,C ;
goto Set1

movfW ACCALO ; Restore the remainder.
addwf ACCCHI,f ; ... (the current bit is 0).
movfW ACCAHI ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; addwfc ACCbLO,f ;
;ADDWFC = adio com transporte, equivale em PIC16 a:
; BTFSC STATUS,C
; ADDLW 1
; ADDWF registrador,destino
BTFSC STATUS,C
ADDLW 1
ADDWF ACCBLO,F goto Set0 ;

Set1:
bsf ACCALO,1 ;
Set0:
rrf ACCAHI,f ;
rrf ACCALO,f ;
bcf ACCAHI,7 ;

BitLoop:
decfsz ACCBHI,f ;
goto ShftUp return Really works!

[]'s

Marcelo.
>From: "ntricoup" <>
>Reply-To:
>To:
>Subject: [piclist] 24-bit square root routine
>Date: Mon, 12 Jul 2004 19:42:58 -0000
>
>Searching the web for a squrae root routine i came up with many
>usefull math routines in the next address
>http://techref.massmind.org/techref/microchip
>For square root i found many routines. I used the 32-bit which for
>many inputs give irrelevant outputs. Next i tried the 24- bit integer
>sqrt which follows next. I altered some variables name? I can't
>figure out what test variable is. I can;t make it work. Any idea?
>Thanks
>
>; brSQRT24
>;
>; Calculates the square root of a twentyfour bit number using the
>; binary restoring method.
>;
>; Result in ResultH,ResultL
>; Input in InputH,InputM,InputL
>; Test TestH,TestL
>; Counter in Counter
>;
>; 40 words long, uses 8 bytes RAM (inc. 3 holding 24-bit input).
>;
>;--------------------------------
>SQRT24 macro InputH,InputM,InputL,ResultH,ResultL,TestH,TestL
> local ShftUp
> local SubTest
> local BitLoop
> local Set1
> local Set0
>
> clrf ResultH ;
> clrf ResultL ;
>
> clrf TestL ;
> clrf TestH ;
>
> movlw .12 ;
> movwf Counter ; (6 cycle intro, 8 incl. call)
>
>ShftUp
> rlf InputL,f ; Shift input up 2 places.
> rlf InputM,f ; (33 cycles per loop if bit is 0)
> rlf InputH,f ; (29 cycles per loop if bit is 1)
> rlf TestL,f ;
> rlf TestH,f ;
> rlf InputL,f ;
> rlf InputM,f ;
> rlf InputH,f ;
> rlf TestL,f ;
> rlf TestH,f ;
>
> rlf ResultL,f ; Shift root-so-far up by two and append
> rlf ResultH,f ; ... '01'.
> rlf ResultL,f ;
> rlf ResultH,f ;
> bcf ResultL,1 ;
> bsf ResultL,0 ;
>
>SubTest
> movf ResultL,W ;
> subwf TestL,f ;
> movf ResultH,W ;
> subwf TestH,f ;
>
> btfsc STATUS,C ;
> goto Set1
>
> movf ResultL,W ; Restore the remainder.
> addwf TestL,f ; ... (the current bit is 0).
> movf ResultH,W ;
> addwf TestH,f ;
> goto Set0 ;
>
>Set1
> bsf ResultL,1 ;
>Set0
> rrf ResultH,f ;
> rrf ResultL,f ;
> bcf ResultH,7 ;
>
>BitLoop
> decfsz Counter,f ;
> goto ShftUp
>endm

_________________________________________________________________
MSN Messenger: instale gris e converse com seus amigos.
http://messenger.msn.com.br