please dont rip this site

PIC Microcontoller Math Method

Multiply 8 x 8 bits

From Scott Dattalo

Scott says:

The other day I stated that there is a faster Multiplication algorithm than the one posted by Martin, which is a variation of the one on James' Web page, which in turn is variation of the one found in three different places of the ECH. (Martin's routine is one cycle shorter, however if you allow the other routines to pass one of the multiplicands in W then they'd be one cycle shorter too.) The algorithm to which I referred is also found in the ECH - at least I thought it was. I was unable to find it. (Perhaps I was dreaming...) But it goes something like this:

If the first bit tested in the shift-and-add multiplication algorithm is zero, then there's no need to perform the shift-and-add operation for the first iteration. If the next bit is zero too, you can skip that one as well. The first non-zero bit encountered doesn't need to be added, but it does need to be shifted.

Here's an example of the algorithm. However, I'm not sure if this is the optimum. It has a worst case execution time of 36 cycles excluding the return and call and has a best case excution time of 21 cycles and an average right around 34 cycles. So on average it saves one cycle over the other inline multiplication functions, but it takes 50% more code to do so. Unless I was desparate to save one cycle, I'd probably stick with the other versions.

;
; Multiply x*y and produce a 16bit result. The high byte of the
;result is aliased with x.
;

multiply
     movf   x,w     ;; or save a cycle by letting the caller init. ;)

     clrc
     clrf   res_lo

     btfsc  y,0
      goto  l0

     btfsc  y,1
      goto  l1

     btfsc  y,2
      goto  l2

     btfsc  y,3
      goto  l3

     btfsc  y,4
      goto  l4

     btfsc  y,5
      goto  l5

     btfsc  y,6
      goto  l6

     btfsc  y,7
      goto  l7

     clrf    x       ;Dmitry Kiryashov says: otherwise y==0 but x isn't

     goto   l8      ;; or return


l0   rrf    x,f
     rrf    res_lo,f

     btfsc  y,1
      addwf x,w

l1   rrf    x,f
     rrf    res_lo,f

     btfsc  y,2
      addwf x,w

l2   rrf    x,f
     rrf    res_lo,f

     btfsc  y,3
      addwf x,w

l3   rrf    x,f
     rrf    res_lo,f

     btfsc  y,4
      addwf x,w

l4   rrf    x,f
     rrf    res_lo,f

     btfsc  y,5
      addwf x,w

l5   rrf    x,f
     rrf    res_lo,f

     btfsc  y,6
      addwf x,w

l6   rrf    x,f
     rrf    res_lo,f

     btfsc  y,7
      addwf x,w

l7   rrf    x,f
     rrf    res_lo,f


l8   return


Questions:

Luc Laplante Says: " Code not working...
Line 10 to 17 "addwf x,w" have error, "addwf x,f" is better!
" +


file: /Techref/microchip/math/mul/8x8-sd.htm, 3KB, , updated: 2010/12/6 06:53, local time: 2024/11/25 09:01,
TOP NEW HELP FIND: 
18.227.0.255:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/microchip/math/mul/8x8-sd.htm"> PIC Microcontoller Math Method </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .