Shop OBEX P1 Docs P2 Docs Learn Events
Integer math library for sxb 2.0 — Parallax Forums

Integer math library for sxb 2.0

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2009-02-21 22:45 in General Discussion
Attached is a library for integer math.
This library contains wrapper functions that implement
the *, /, //, */ and ** operators for 16bits signed and unsigned variables,
plus 32bits addition and subtraction. As these operators generate quite
a bit of code, calling a subroutine might save some codespace.
I didn't bother including wrapper functions for 8bit variables as the calling
overhead would prabably cancel out any space saving.

regards peter

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-02-18 12:44
    I had forgotten to enclose all sx type conditional compile blocks
    inside a seperate $ifused block. Sorry about that.

    regards peter
  • ZootZoot Posts: 2,227
    edited 2009-02-19 13:56
    Peter -- this libs are fairly similar to my own math libs, although I handle banking for tmp variables a little differently. In any case, one thing I noticed is that you are not accounting for rounding, something I do in my own functions, e.g. (simplified example, my actual libs have more ASM)....

    
    FUNC MATH_DIV
       IF __PARAMCNT < 4 THEN
          __PARAM4 = 0
          IF __PARAMCNT < 3 THEN
             __PARAM3 = __PARAM2
             __PARAM2 = 0
          ENDIF
       ENDIF
       tmpW1 = __WPARAM12
       tmpW2 = __WPARAM34
       \CLC                              ' add 1/2 of divisor for cleaner rounding
       \RR __PARAM4
       \RR __PARAM3
       tmpW1 = tmpW1 + __WPARAM34
       tmpW1 = tmpW1 / tmpW2
       RETURN tmpW1
       ENDFUNC
    
    



    For my mid-multiply (*/) function I do something similar, but obviously #128 is added to the first word since the divisor is always 256.

    I've found that this gives me much closer integer results, esp. when numbers may be manipulated a bit (a good example would be encoder odometry and trig).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-02-21 01:02
    Library update.
    I added wrapper functions for roundoff versions (thanks Zoot for mentioning that)
    and some functions that deal with 16bit fractions, eg. converting between 65536 based fractions
    and 10000 based·fractions which are·great for displaying decimals of a fractional number.
    Also included a function (signed and unsigned) to calculate the 65536 based fraction for a given
    numerator and denominator.


    regards peter

    ·
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2009-02-21 22:45
    Peter,
    Thanks for that ..
    Regards,
    John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'

    Those who can, do.Those who can’t, teach.
Sign In or Register to comment.