> At 06:26 PM 6/1/01 -0500, Mike Mansheim wrote:
> > > Recently, I have used a pair of routines from Microchip's AN617
> > > and it works well. But, anybody knows better routines for
> > > 16-bit and 32-bit integer multiplication and division?
> >
> >piclist.com has a bunch of math routines. I've used 16x16
> >multiply and 32x16 divide and they work great.
>
> I have made much use of a couple of routines that were posted on the
> piclist many years back and keep using them because they are so easy to
> customize. I'll post both the multiply and divide routines from my a
> previous project. They aren't particularly fast or small but notice how
> easy it is to extend or shrink them!
>
> dwayne
>
> ;*************************************************************************
> ; Multiply 24 bits by 16 bits: 40 bit result, exits with MH_h,m,l destroyed
> ; MH_h,m,l * t_1,2 -> q_5,4,3,2,1
> ; 27 ROM, 358 cycles max
> ; Based on Scott Dattalo's 16x16 multiply. This version by Dwayne Reid.
> ;*************************************************************************
> mul24_16
> clrf q_1 ;Clear 40-bit result
> clrf q_2
> clrf q_3
> clrf q_4
> clrf q_5
>
> bsf q_2,7 ;16 loops
>
> mul24_loop
> rrf t_2,F
> rrf t_1,F ;If the LSB of t_2:1 is set
> skpc
> goto mul24_skip
>
> movfw MH_low ;Then add MH l:m:h to the result
> addwf q_3,F
>
> movfw MH_mid
> skpnc
> incfsz MH_mid,W
> addwf q_4,F
>
> movfw MH_high
> skpnc
> incfsz MH_high,W
> addwf q_5,F
>
> mul24_skip
> rrf q_5,F ;Shift result, pick up carry bit
> rrf q_4,F ;
> rrf q_3,F
> rrf q_2,F
> rrf q_1,F
>
> skpc ;loop counter bit rotate into carry?
> goto mul24_loop ;no - loop again
>
> ;done!
>
> ;*************************************************************************
> ; Divide 40 bits by 24 bits: 40 bit quotient, 24 bit remainder.
> ; q_5,4,3,2,1 / ML_h,m,l -> q_5,4,3,2,1 remainder in t_3,2,1
> ; Uses LOOPER
> ; 43 ROM, 1365 cycles max
> ; based on Bob Fehrenbach's 32_32 div routine. This version by Dwayne Reid
> ;*************************************************************************
>
> div40_24
>
> clrf t_1
> clrf t_2
> clrf t_3
>
> movlw .40
> movwf LOOPER
>
> loop
> rlf q_1, f
> rlf q_2, f
> rlf q_3, f
> rlf q_4, f
> rlf q_5, f
> rlf t_1, f
> rlf t_2, f
> rlf t_3, f
>
> movfw ML_high
> subwf t_3,W
> skpz
> goto check_sign
> movfw ML_mid
> subwf t_2,W
> skpz
> goto check_sign
> movfw ML_low
> subwf t_1,W
>
> check_sign
> skpc
> goto check_count
>
> movfw ML_low ;subtract least significant byte
> subwf t_1,F
>
> movfw ML_mid ;2nd byte
> skpc ;subtract with borrow
> incfsz ML_mid,W
> subwf t_2,F
>
> movfw ML_high ;3rd byte
> skpc
> incfsz ML_high,W
> subwf t_3,F
>
> setc
>
> check_count
> decfsz LOOPER,F
> goto loop
> rlf q_1,F
> rlf q_2,F
> rlf q_3,F
> rlf q_4,F
> rlf q_5,F
> ;done!
>
> Hope someone finds these useful!
>
> dwayne
>
> Dwayne Reid <
EraseMEdwaynerspam_OUT
TakeThisOuTplanet.eon.net>
> Trinity Electronics Systems Ltd Edmonton, AB, CANADA
> (780) 489-3199 voice (780) 487-6397 fax
>
> Celebrating 17 years of Engineering Innovation (1984 - 2001)
>
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> Do NOT send unsolicited commercial email to this email address.
> This message neither grants consent to receive unsolicited
> commercial email nor is intended to solicit commercial email.
>
> --
>
http://www.piclist.com hint: PICList Posts must start with ONE topic:
> [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads