Andy David says:
I've added a 16-bit square root routine (again for the 17c43) that uses successive approximation to find the square root, the binary restoring method I'm fairly sure would be quicker. This is the only 16-bit sqrt routine I have specifically for the 17cxx PICs, I've only included it as the original poster requested a 16-bit routine. If you really want speed speed speed, I'd rewrite the 32-bit routine to be 16-bit.Standard disclaimer applies
;========================================================================== ; SQRT16 ; ; Calculates the square root of a sixteen bit number by successive ; approximation. ; ; ; Input in ACCaHI:ACCaLO ; ; Output in ACCbHI ; ; Also uses ACCbLO and ACCcHI. ; ;-------------------------------------------------------------------------- SQRT16: mov W, #$80 ; movpf WREG,ACCbLO ; set up walking tester bit. clr ACCbHI ; mov W, #$08 ; movpf WREG,ACCcHI ; tstest: movfp ACCbLO,WREG ; set bit for testing. or ACCbHI, W ; movpf WREG,ACCbHI ; mulwf ACCbHI ; square the current estimate. movfp ACCaLO,WREG ; compare PRODH:PRODL with ACCaHI:ACCaLO sub PRODL, W ; movfp ACCaHI,WREG ; subwfb PRODH,WREG ; sb ALUSTA.C ; If the result is <= input, jmp nxtbit ; ... keep this bit set. movfp ACCbLO,WREG ; Result was > input, so reset xor ACCbHI, W ; ... bit to zero. nxtbit: rrncf ACCbLO,f ; rotate tester bit right. decsz ACCcHI ; jmp tstest ; ret