EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Re: mul 24by16

Started by Eirik Karlsen December 28, 2003
Hi,
anyone have a good PIC17/PIC18 (mulwf) signed or unsigned 24by16 multiply routine?
I've tried some of those in AN617 but they are flawed.
--
*******************************************
VISIT MY HOME PAGE:
<http://home.online.no/~eikarlse/index.htm>
LAST UPDATED: 23/08/2003
*******************************************
Best Regards
Eirik Karlsen



I've used a few of the routines from AN617 with great success though
I must admit that I've not needed to do 24x16. Just curious what
flaws you ran into.

--Scott

--- In , Eirik Karlsen <eikarlse@o...> wrote:
> Hi,
> anyone have a good PIC17/PIC18 (mulwf) signed or unsigned 24by16
multiply routine?
> I've tried some of those in AN617 but they are flawed.
> --
> *******************************************
> VISIT MY HOME PAGE:
> <http://home.online.no/~eikarlse/index.htm>
> LAST UPDATED: 23/08/2003
> *******************************************
> Best Regards
> Eirik Karlsen




Scott,
The routines I tried was  the FXM2416S and FXM2416U  PIC17CXXX MULTIPLY ROUTINES  Revision: 2.2  from AN617.
I believe I tried some of the 1616 ones too....no joy. I used these with some minor alterations on a PIC18.

FXM2416S: essentially produces just garbage. The FXM2416U works better, but the upper byte in the product is garbage....
even if the product is say two bytes it is preceded by two clear bytes, and the upper is not clear...contains some garbage.

Apart from all this I don't like these routines because input/output is not 'clean'....
I like to have I/O Right Justified to ARGB0, and ARGB0 being LSB...this is not true at all for the AN617 routines where AARGB
 input is Left Justified to B0, and output is Right Justified to Bn.....gives me a headache.

Last night I started rolling my own configurable Signed/Unsigned mul2416 and got it half finished, should be ready for use later tonight.
This one is easily shrinked or stretched in all directions.

Good math routines are not easy to find...many of those in the PICLIST math archive is also flawed.
Scott Lee wrote:

 I've used a few of the routines from AN617 with great success though
I must admit that I've not needed to do 24x16.  Just curious what
flaws you ran into.

--Scott

--- In p...@yahoogroups.com, Eirik Karlsen <eikarlse@o...> wrote:
> Hi,
> anyone have a good PIC17/PIC18 (mulwf) signed or unsigned 24by16
multiply routine?
> I've tried some of those in AN617 but they are flawed.
> --
> *******************************************
> VISIT MY HOME PAGE:
> <http://home.online.no/~eikarlse/index.htm>
> LAST UPDATED: 23/08/2003
> *******************************************
> Best Regards
> Eirik Karlsen


--
*******************************************
VISIT MY HOME PAGE:
<http://home.online.no/~eikarlse/index.htm>
LAST UPDATED: 23/08/2003
*******************************************
Best Regards
Eirik Karlsen
 

The routine is now finished and tested, it is attached and FOC to anyone who wants it.
It does a signed 24by16 multiplication in about 99 clocks and an unsigned one in about 79 clocks.
About 20 clocks could have been saved by changing some of the MOVFF to MOVF, but then it will be banked.
As it is now it runs unbanked.

;*************************************************************************************************
; PIC18
; 24x16 Bit Signed or Unsigned Fixed Point Multiply 24 x 16 -> 40 ,by Eirik Karlsen 29.12.03
;
; Input:24 bit signed or unsigned fixed point multiplicand in AARGB2, AARGB1, AARGB0
; 16 bit signed or unsigned fixed point multiplier in BARGB1, BARGB0
;
; Use: CALL MUL2416SU
;
; Output: 40 bit signed or unsigned fixed point product in AARGB4, AARGB3, AARGB2, AARGB1, AARGB0
;
; Input and output is right justified and ARGB0 is LSB
;
; AARGB4,AARGB3,AARGB2,AARGB1,AARGB0,BARGB1,BARGB0,TEMP3,TEMP2,TEMP1,TEMP0,SIGNMODE
;
; Result: AARG <-- AARG * BARG
;
; Timing: 99 clks
;
;
MUL2416SU

MOVFF AARGB0,TEMP0 ;Store AARGB, each byte is moved to TEMP3 for processing.
MOVFF AARGB1,TEMP1
MOVFF AARGB2,TEMP2
CLRF AARGB3 ;Clear high order bytes
CLRF AARGB4 ;
SETF SIGNMODE ;Set Signed operation mode
;CLRF SIGNMODE ;Clear SIGNMODE here and the routine will operate unsigned.
MOVFF BARGB0,WREG ;Multiply B0*A
MULWF AARGB0 ;B0*A0
MOVFF PRODH,AARGB1 ;
MOVFF PRODL,AARGB0 ;->A1,A0
MOVFF TEMP1,TEMP3 ;Get A1
MULWF TEMP3 ;B0*A1
MOVFF PRODL,WREG ;
ADDWF AARGB1 ;Add to partial sum
MOVFF PRODH,AARGB2 ;
MOVFF BARGB0,WREG ;
MOVFF TEMP2,TEMP3 ;A2
MULWF TEMP3 ;B0*A2
MOVFF PRODL,WREG ;
ADDWFC AARGB2 ;Propagate carry from previous addition
MOVFF PRODH,WREG ;
ADDWFC AARGB3 ;Done B0*A ->A3,A0
MOVFF BARGB1,WREG ;Multiply B1*A
MOVFF TEMP0,TEMP3 ;Get A0
MULWF TEMP3 ;B1*A0
MOVFF PRODL,WREG ;
ADDWF AARGB1 ;B1 additions are shifted 1 byte to MSB
MOVFF PRODH,WREG ;
ADDWFC AARGB2 ;
CLRF WREG ;
ADDWFC AARGB3 ;Propagate carry from previous additions
ADDWFC AARGB4 ;Done A+256*(B1*A0)->A4,A0
MOVFF BARGB1,WREG ;
MOVFF TEMP1,TEMP3 ;Get A1
MULWF TEMP3 ;B1*A1
MOVFF PRODL,WREG ;
ADDWF AARGB2 ;
MOVFF PRODH,WREG ;
ADDWFC AARGB3 ;
CLRF WREG ;
ADDWFC AARGB4 ;Done B1*A1
MOVFF BARGB1,WREG ;
MOVFF TEMP2,TEMP3 ;Get A2
MULWF TEMP3 ;B1*A2
MOVFF PRODL,WREG ;
ADDWFC AARGB3 ;
MOVFF PRODH,WREG ;
ADDWFC AARGB4 ;Done B1*A2
BTFSS SIGNMODE,0 ;Operatin in signed mode?
RETURN ;No!
BTFSS BARGB1,7 ;Yes! B negative?
GOTO TSIGN2416 ;No!
MOVFF TEMP0,WREG ;
SUBWF AARGB2 ;
MOVFF TEMP1,WREG ;
SUBWFB AARGB3 ;
MOVFF TEMP2,WREG ;
SUBWFB AARGB4 ;
TSIGN2416 BTFSS TEMP2,7 ;A negative?
RETURN ;No!
MOVFF BARGB0,WREG ;
SUBWF AARGB3 ;
MOVFF BARGB1,WREG ;
SUBWFB AARGB4 ;
RETURN ;
;*************************************************************************************************

--
*******************************************
VISIT MY HOME PAGE:
<http://home.online.no/~eikarlse/index.htm>
LAST UPDATED: 23/08/2003
*******************************************
Best Regards
Eirik Karlsen



Attachment (not stored)
MUL2416SU.asm
Type: application/x-unknown-content-type-asm_auto_file


The 2024 Embedded Online Conference