Truncated match.
PICList
Thread
'16 bit math routines'
1996\08\29@105620
by
jimh
I know I have seen this subject talked about before, and code fragments
with it. I wanted to find some 16bit multilecation and subtraction
routines so I figured, there must be some on the WEB or someones FTP site.
Well I looked everywhere I could find, but found none. Perhaps I am
looking in the wrong places. If anyone has some or knows where I can find
some would you mind helping me with this.
Thanks for your help.
Take care spam_OUTjimhTakeThisOuTcalweb.com
1996\08\29@112633
by
Andy Errington
Hi,
you can find some examples in Application Notes in the Embedded
Controller Handbook (which you should have a copy of). These App. Notes
are also available on Microchip's Web Page, so you probably were looking
in the wrong place.
Andy (the other one)
{Quote hidden}>
>From: jimh[SMTP:
.....jimhKILLspam@spam@CALWEB.COM]
>Sent: 29 August 1996 15:53
>To: Multiple recipients of list PICLIST
>Subject: 16 bit math routines
>
>I know I have seen this subject talked about before, and code fragments
>with it. I wanted to find some 16bit multilecation and subtraction
>routines so I figured, there must be some on the WEB or someones FTP
>site.
>Well I looked everywhere I could find, but found none. Perhaps I am
>looking in the wrong places. If anyone has some or knows where I can
>find
>some would you mind helping me with this.
>
>Thanks for your help.
>
>
>Take care
jimhKILLspamcalweb.com
>
1996\08\29@124648
by
Bob Fehrenbach

jimh <.....jimhKILLspam.....CALWEB.COM> wrote:
>I know I have seen this subject talked about before, and code fragments
>with it. I wanted to find some 16bit multilecation and subtraction
>routines
Try these:
;*******************************************************************
; Double Precision Multiplication
; ACCd(16 bits) * ACCa(16 bits) > ACCb,ACCc ( 32 bits )
Standard shift and add.
Uses D_add
Execution time: ~200 to 409 clock cycles.
Code space: 27 locations (including D_add)
Author: Cleaned up and corrected version from Microchip Ap note
by BF. Note: Ap note has errors!
;*******************************************************************;
;
cblock h'20'
ACCbHI
ACCbLO
ACCcHI
ACCcLO
ACCaHI
ACCaLO
ACCdHI
ACCdLO
count
flags
endc
#define temp_carry flags, 0
org 0
;*******************************************************************
D_mpyS:
clrf ACCbHI
clrf ACCbLO
movlw 16 ; for 16 shifts
movwf count
mloop:
rrf ACCdHI, F ;rotate d right
rrf ACCdLO, F
btfsc STATUS,C ;need to add?
call D_add
rrf ACCbHI, F
rrf ACCbLO, F
rrf ACCcHI, F
rrf ACCcLO, F
decfsz count, F ;loop until all bits checked
goto mloop
retlw 0
;*******************************************************************
; Double Precision Addition ( ACCb + ACCa > ACCb )
; (Used in D_mpyS)
D_add:
bcf flags, temp_carry
movf ACCaLO,w ;( ACCb + ACCa > ACCb )
addwf ACCbLO, F ;add lsb
btfsc STATUS,C ;add in carry
incf ACCbHI, F
btfsc STATUS, Z ;Check for roll over
bsf flags, temp_carry
movf ACCaHI,w
addwf ACCbHI, F ;add msb
btfsc flags, temp_carry ;Shift in a carry if roll over
bsf STATUS, C
retlw 0
;****************************************************************
;*****************************************************************
;Two byte subtract. Standard PIC instruction set does not have
;a subtract with borrow instruction.
;Note, requires a temporary flag such as:
; #define temp_carry flags, 0
;Note also that if this is used in both the background and in
;the isr, a potential conflict exists with this flag.
;At exit, if C = 1, result is positive.
;Lines marked with an asterisk can be deleted if the carry bit
;at exit is not important.
;Usage example: sub_word new_count, old_count
sub_word: macro bbb, aaa ;bbb  aaa > bbb
bcf temp_carry ;*
movf aaa + 1, w
subwf bbb + 1, f
btfss STATUS, C
decf bbb, f
incf bbb, w ;* ;Check for roll over (under?)
btfsc STATUS, Z ;*
bsf temp_carry ;*
movf aaa, w
subwf bbb, f
btfsc temp_carry ;*
bcf STATUS, C ;*
endm
;**************************************************************************

Bob Fehrenbach Wauwatosa, WI EraseMEbfehrenbspam_OUTTakeThisOuTexecpc.com
1996\08\29@171309
by
Scott Dattalo

jimh wrote:
>
> I know I have seen this subject talked about before, and code fragments
> with it. I wanted to find some 16bit multilecation and subtraction
> routines so I figured, there must be some on the WEB or someones FTP site.
> Well I looked everywhere I could find, but found none. Perhaps I am
> looking in the wrong places. If anyone has some or knows where I can find
> some would you mind helping me with this.
>
Jim,
As the other Andy said, the Embedded Controller Handbook and the Micro Chip
WEB page have the info. But ... I also needed a 16 bit multilecation routine
and did not have the ECH handy (and I hate to turn my computer on before 7:00
AM). So here's something I threw together. And as usual, it has been tested
for many but not all of the possible combinations of inputs (Tom Van Baak seems
to always check them for me though :) ). It's a little faster than the ECH/Bob
Fehrenbach solution for all cases except when yh:yl < 4. It's based on the
same "shift and add" principle. In fact, some code segments are identical.
;
;mul16_16  unsigned 16bit by 16bit multipication
;
; This routine will multiply the 16bit value xh:xl by the
;16bit value yh:yl and store the result in the 32bit
;variable th:tl:ph:pl.
;
;Inputs:
; xh:xl
; yh:yl
;Outputs
; th:tl:ph:pl
; yh:yl is destroyed (either 0 or 1)
;Other RAM
; none
;Program ROM: 22 words
;Execution time: 215 to 295 cycles
mul16_16
CLRF ph ;Clear 32bit result
CLRF pl
CLRF th
CLRF tl
BSF ph,7 ;Set MSB of lower 16 bits.
;(Used as the loop counter)
m1 RRF yh,F ;If the LSB of yh:yl is set
RRF yl,F
SKPC
goto m2
MOVF xl,W ;Then add xh:xl to the result
ADDWF tl,F
MOVF xh,W
SKPNC
INCFSZ xh,W
ADDWF th,F
m2 RRF th,F ;Shift the 32bit result right one bit
RRF tl,F ;(note that the carry bit is picked up too)
RRF ph,F
RRF pl,F
SKPNC ;If the "loop counter" bit squirts out, we're
RETURN ; done
GOTO m1
I also took a quick look at AN617 (the Microchip Application Note for fixed
point arithmetic for the PIC16/17 families). The unsigned 16 by 16 bit
multiplication routine there executes in 180 cycles worst case! That's great.
However, it's 210 words long. That's not great.
Scott
1996\08\29@195238
by
Steve Hardy
> From: Scott Dattalo <sdattalospam_OUTUNIX.SRI.COM>
> [cut]
> WEB page have the info. But ... I also needed a 16 bit multilecation routine
^^^^^^^^^^^^^
What is this new arithmetic op??? Or is it American for multiplication?
Regards,
SJH
1996\08\29@201741
by
Scott Dattalo
Steve Hardy wrote:
>
> > From: Scott Dattalo <@spam@sdattaloKILLspamUNIX.SRI.COM>
> > [cut]
> > WEB page have the info. But ... I also needed a 16 bit multilecation routine
> ^^^^^^^^^^^^^
>
> What is this new arithmetic op???
>
> Regards,
> SJH
It's the crosscorrelation between mutilation and articulation sort of like
disintegration is the reverse process of integration.
> Or is it American for multiplication?
No, it's generic nerd humor. Gee Steve, and all this time I thought you were
one of us!
Scott
More... (looser matching)
 Last day of these posts
 In 1996
, 1997 only
 Today
 New search...