Searching \ for 'More on square root' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=more+square+root
Search entire site for: 'More on square root'.

Truncated match.
PICList Thread
'More on square root'
1996\03\17@155317 by Bob Fehrenbach

picon face
I think that this version fixes the least significant bit problem
pointed out by Andy Warren.  Anybody have any suggestions that make
it smaller or faster?

                ;Call with four byte number in n_a (ms).. n_d (ls)
                ;Two byte result in r_a (ms) r_b (ls)
                ;Also uses n_x, p_a, p_b, t_a, t_b, t_x
                ;56 program memory locations.
                ;Worst case execution time: about 759 clock cycles.
sq_root:
  clrf    n_x
  clrf    r_a
  clrf    r_b
  movlw   b'10000000'
  movwf   p_a
  clrf    p_b

root_loop:
  clrf    t_x                     ;Set up next trial root
  bcf     STATUS, C
  rrf     p_a, w
  iorwf   r_a, w
  movwf   t_a
  rrf     p_b, w
  iorwf   r_b, w
  movwf   t_b
                                  ;LSB fix
  btfss   p_b, 0                  ;See note at bottom
  goto    ww
  incfsz  t_b, f
  goto    ww
  incf    t_a, f

ww:                                ;Test trial root
  subwf   n_b, w
  movwf   t_b
  btfss   STATUS, C
  incfsz  t_a, f
  goto    xx
  incf    t_x, f

xx:
  movf    t_a, w
  subwf   n_a, w
  movwf   t_a
  btfss   STATUS, C
  incf    t_x, f

yy:
  movf    t_x, w
  subwf   n_x, w
  bnc     shift                   ;Branch if too big

  movwf   n_x                     ;Else save result
  movf    t_a, w
  movwf   n_a
  movf    t_b, w
  movwf   n_b

  movf    p_a, w                  ;'Or' root with new position bit
  iorwf   r_a, f
  movf    p_b, w
  iorwf   r_b, f

  bcf     STATUS, C

shift:                             ;Shift position bit right
  rrf     p_a, f
  rrf     p_b, f
  bc      done

  rlf     n_d, f                  ;Shift number left
  rlf     n_c, f
  rlf     n_b, f
  rlf     n_a, f
  rlf     n_x, f
  goto    root_loop

done:
  return

  ;Note this:                     executes faster than:

  btfss   p_b, 0                  btfsc   p_b, 0
  goto    ww                      incfsz  t_b, f
  incfsz  t_b, f                  goto    ww
  goto    ww                      incf    t_a, f
  incf    t_a, f
                                  because p_b, 0 is clear for 15
                                  out of 16 passes.

--
Bob Fehrenbach    Wauwatosa, WI     spam_OUTbfehrenbTakeThisOuTspamexecpc.com

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...