Searching \ for '[PIC]: Looking for improved integer's math routine' 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/microchip/math/index.htm?key=math
Search entire site for: 'Looking for improved integer's math routine'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Looking for improved integer's math routine'
2001\05\31@235254 by Alejandro Lavarello

picon face
Hi all!

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?

(Improved for code size or for speed, or better, for two !)

Years ago, I have used the Microchip's binary-toBCD routine.

Recently, I was discovered the Jhon Payson's binary-to-BCD,
and it is four times more fast.

Maybe for division and multiplcation another Pic-Lister
have developed improved versions...

Thanks to all!
               Alejandro.


--
http://www.piclist.com hint: To leave the PICList
spam_OUTpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu



'[PIC]: Looking for improved integer's math routine'
2001\06\01@024505 by Vasile Surducan
flavicon
face
On Fri, 1 Jun 2001, Alejandro Lavarello wrote:

> Hi all!
>
> 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?
>
 I'm impressed ! Does really the 16bit division from AN617 works ?
Because I can't use it.
I've tested two shortest routines, one is Mike Predko's one and the other
one you found in piclist archive.
Andrew Warren has the shortest one but unfortunately is not good only if
you don't care about remainder.
Nikolai Golovnicenko have also some short division routines which I don't
understand. Maybe you will, so check piclist archive first.
Vasile

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\06\01@040522 by Kashif Ali

flavicon
face
I also searching for better math routines, Can u send me some of speedy
math routines.

Kashif ali

Alejandro Lavarello wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\06\01@042952 by Vasile Surducan

flavicon
face
www.piclist.com/techref/piclist/index.htm

in Archive, click on Apply here,


On Fri, 1 Jun 2001, Kashif Ali wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\06\01@193716 by Mike Mansheim

flavicon
face
> 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.

Which reminds me, thanks to the writers for posting their
code, and thanks to James for making it available.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\06\01@210650 by Dwayne Reid

flavicon
face
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   <.....dwaynerKILLspamspam.....planet.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


2001\06\02@030238 by Kashif Ali

flavicon
face
Did you have 64 bit Bin2dec routine.

I have but it taks much time to execute.


Kashif ali


Dwayne Reid wrote:

{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\04@185930 by Mike Mansheim

flavicon
face
>  Did you have 64 bit Bin2dec routine.
> I have but it taks much time to execute.

No, but I was able to extend John Payson's 16 bit routine using
Scott Dattalo's excellent explanation of the algorithm, and I'm
no math whiz.  I only needed an extra bit, to fully utilize a
5 digit display.  The coefficient calculations are no longer
highly optimized, but it is still much quicker than the "normal"
(to me) way of doing this.  With a bit of work, it could be
extended to 64 bits.

By the way, 64 bits???  That's a lot of digits!

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2001\06\04@193956 by James Newton. Admin 3

face picon face
Please share?

James Newton, PICList Admin #3
@spam@jamesnewtonKILLspamspampiclist.com
1-619-652-0593 phone
http://www.piclist.com

{Original Message removed}

2001\06\05@102709 by James Newton

face picon face
www.piclist.com/techref/microchip/math

James Newton (PICList Admin #3) KILLspamjamesnewtonKILLspamspampiclist.com 1-619-652-0593

Alejandro Lavarello wrote on 01-5-31 20:58:

Hi all!

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?

(Improved for code size or for speed, or better, for two !)

Years ago, I have used the Microchip's binary-toBCD routine.

Recently, I was discovered the Jhon Payson's binary-to-BCD, and it is four times more fast.

Maybe for division and multiplcation another Pic-Lister have developed improved versions...

Thanks to all!
               Alejandro.


--
http://www.piclist.com hint: To leave the PICList RemoveMEpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestspamBeGonespammitvma.mit.edu


2001\06\05@102732 by James Newton

face picon face
All the math routines from the archive have been collected at
http://www.piclist.com/techref/microchip/math

If you find something missing, please add it.

James Newton (PICList Admin #3) TakeThisOuTjamesnewtonEraseMEspamspam_OUTpiclist.com 1-619-652-0593

Vasile Surducan wrote on 01-6-1 1:35:

http://www.piclist.com/techref/piclist/index.htm

in Archive, click on Apply here,


On Fri, 1 Jun 2001, Kashif Ali wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestEraseMEspam.....mitvma.mit.edu


2001\06\05@174441 by Mike Mansheim

flavicon
face
>>No, but I was able to extend John Payson's 16 bit routine using
>>Scott Dattalo's excellent explanation of the algorithm, ...

> Please share?

No problem, but many questions:
Should I just post it using the form on piclist.com?
Do those postings get reviewed to make sure they aren't total junk?
Or do I post it directly to the list?

My version is mostly c - that's why the coefficient calculations
are not optimized.  The routine I copied is from the "asm"
source code area.
Does that matter if I post it to piclist.com?

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspammitvma.mit.edu


2001\06\05@180657 by jamesnewton

face picon face
Post to the site and the list...

On the site, just be sure to select code listing and put the HTML tags
<PRE>
before the code and
</PRE>
after it. It will immediately be posted, but it will also be reviewed and
updating if it needs to be.

The C code is still useful just post it and I will make sure it gets where
it needs to go.

---
James Newton (PICList Admin #3)
RemoveMEjamesnewtonEraseMEspamEraseMEpiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2001\06\06@145948 by Mike Mansheim

flavicon
face
> Post to the site and the list...

> On the site, just be sure to select code listing and put the HTML tags
> <PRE>
> before the code and
> </PRE>
> after it.

OK, here 'tis.  I don't know anything about HTML tags - are the <> part
of what is typed in?  I will post to the site as soon as I hear back.
Thanks for the help.

/*
- based on the 16 bit to 5 digit conversion written by John Payson
 & explained by Scott Dattalo
- this version adds the extra bit so that the 5 digit display can
 read to 99,999
- want to express the b_i's in terms of the a_i's for the equations:
      N = a4*16^4 + a3*16^3 + a2*16^2 + a1*16 + a0
      N = b4*10^4 + b3*10^3 + b2*10^2 + b1*10 + b0
 the solution coded below:
      b0 = a0 - (4 * (a4 + a3 + a2 + a1)) - 20
      b1 = (4 * a4) + (6 * a2) + (2 * a1) - 198
      b2 = (5 * a4) + a3 + (2 * a2) - 140
      b3 = (5 * a4) + (4 * a3) - 144
      b4 = (6 * a4) + 16
- written in CCS C
- since ram was plentiful, I used a 3 byte number - only one bit
 of the extra byte is actually required.  I also used three bytes
 of scratchpad ram to increase the efficiency of the compiled code.
 The equations can be coded in C exactly as shown above, but that
 doesn't take advantage of common terms & progressive multiplies.
- even with just the one extra bit, the incoming number  can exceed
 99,999, so it needs to be checked.  This code doesn't show that.
- CCS C is limited to 16 bit as the largest integer data type.  To
 use a 24 bit number, I use an array to make sure the bytes are
 consecutive.  This function operates on the array as a global,
 because I think that is more efficient than passing arrays to
 the function.
- the code could be quite a bit smaller - in John's original, the
 portion of the code that isolated the nibbles and calculated the
 digits took 32 instructions.  If I code that solution in C, using
 two scratchpad bytes to improve things, it compiles to 64 instructions.
 This version compiles to 85 instructions, so I'm sure an asm whiz
 could show quite an improvement.  The C version is still much faster
 than the the original way I did this, and does what I need.
*/

unsigned int  TenK, Thou, Hund, Tens, Ones;
unsigned int  num[3];

void BIN2DEC(void)
{
    // operates on global variables:
    // set num[0]-[3] (lo-hi) before calling
    // returns with TenK...Ones set from 0-9

    unsigned int a0, a1, a2, a3, a4;
    unsigned int t1, t2, t3;           // scratchpad variables

    // isolate the 4 bit nibbles
    // for the purposes of this routine, a4 can be only 0 or 1
    a4 = num[2];
    a3 = num[1] / 16;
    a2 = num[1] & 0b00001111;
    a1 = num[0] / 16;
    a0 = num[0] & 0b00001111;

    // calculate the decimal digits (the b_i's are expressed here
    // as TenK...Ones, as in the original)
    // all are negative, except TenK
    t1 = 4 * a4;
    Ones = a0 - t1 - 20;
    t2 = 2 * a1;
    Tens = t1 + t2 - 198;
    t3 = 2 * a2;
    Hund = a3 + t3 - 140;
    t3 = t3 * 2;              // now = 4 * a2
    t2 = t2 * 2;              // now = 4 * a1
    Ones = Ones - t3 - t2;
    t3 = t3 + a2 + a2;        // now = 6 * a2
    Tens = Tens + t3;         // Tens done
    t1 = t1 + a4;             // now = 5 * a4
    Hund = Hund + t1;
    t3 = 4 * a3;
    Thou = t1 + t3 - 144;     // Thou done
    Ones = Ones - t3;         // Ones done
    TenK = t1 + a4 + 16;      // Tenk done

    // "normalize" the digits - this asm code was
    // copied directly from the original
#asm
      movlw   0x0A
    Lb1:
      addwf   Ones,f
      decf    Tens,f
      btfss   3,0
      goto   Lb1
    Lb2:
      addwf   Tens,f
      decf    Hund,f
      btfss   3,0
      goto   Lb2
    Lb3:
      addwf   Hund,f
      decf    Thou,f
      btfss   3,0
      goto   Lb3
    Lb4:
      addwf   Thou,f
      decf    TenK,f
      btfss   3,0
      goto   Lb4
#endasm
}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\06\06@162607 by jamesnewton

face picon face
Yep, the "<" and ">" are necessary. Also, the "&"'s in the code need to be
changed to "&amp;" and any "<"'s needs to be "&lt;" One of these days, I'll
make that automatic....

...never mind, some day is today...

I've made it automatic. If you just select "program listing" (rather than
"question") in the pull down above the text area and paste your code into
the text area and press the button, it will do it for you. No <PRE> or
</PRE> or & to &amp; or < to &lt; or nothing... just paste it as is and
select program listing.

Thanks for sharing.

I wonder what it would take to extend the code to use more of the bits in
the top byte?

---
James Newton (PICList Admin #3)
RemoveMEjamesnewtonspam_OUTspamKILLspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2001\06\10@125007 by James Newton

face picon face
Well I'll be damned...
(just ask my wife.)

I do believe that you are the first person who has ever said that. Or it has been a while. People have donated cash but it is also nice to just hear a "thank you" for all the authors who were willing to share.

James Newton (PICList Admin #3) RemoveMEjamesnewtonTakeThisOuTspamspampiclist.com 1-619-652-0593

Mike Mansheim wrote on 01-6-1 16:43:

> 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.

Which reminds me, thanks to the writers for posting their code, and thanks to James for making it available.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspamspamspamBeGonemitvma.mit.edu


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