![]() |
Forum Index : Microcontroller and PC projects : [MMBasic]BCD Arithmetic Library
Author | Message | ||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi Here is Ver 1 of the Arbitrary Precision BCD Arithmetic Library for MMBasic. The BCD Library will operate on numbers from 12 Digits to 252 Digits in length. '*********************************************************** *************** 'BCD Library for MMBasic - V 1. 18 August 2014 ' '(c) Peter Carnegie 2014 with thanks to TassyJim from the BackShed 'who inspired this library with his BigInt Library, and JohnS who had 'the insight that integer operation on 6 digit numbers in MMBasic was 'viable without loss of precision. ' 'This library is NOT Re-entrant, eg should not be used within ISR's ' 'All Global/Externally visible symbols have the prefix BCD. , eg BCD.AddAB2C ' 'The BCD Library will operate on numbers from 12 Digits to 252 Digits ' 'BCD Library uses 6 arrays, BCD.A(), BCD.B(), BCD.C(), ' BCD._A(), BCD._B(), BCD._C() 'These arrays are treated as BCD registers capable 'of holding up to 252 decimal digit numbers. ' 'Numbers are stored in unpacked form, 6 decimal digits per 'array entry, in little endian form. 'eg 123456789 is stored in BCD.A() as 'BCD.A(0)=456789, BCD.A(1)=000123, BCD.A(2)=000000 ' 'Algorithms process Sextets of digits at a time reducing 'processing times, eg on a uMite at 48MHz, an 18 digit BCD.AddAB2C 'operation takes about 4mS. ' 'Generally, register BCD.C() will contain the results of 'two part operations, eg BCD.AddAB2C takes BCD.A() + BCD.B() 'and returns the result in BCD.C(). ' ' ' 'The list of functions/subs in the library include ' ' BCD.InitLib(NumDigits) MUST be called first. Creates Global BCD Lib Vars, BCD.A/B/C(), BCD.Size, BCD.Width, BCD.NumDigits ' NumDigits is Number of BCD digits, eg 24, ' BCD.Size will return number of elements used to represent NumDigits, ie NumDigits\BCD.Width ' BCD.Width will return number of digits per numeric var - always 6 ' BCD.NumDigits will return number of BCD Digits, ie precision ' ' BCD.AddAB2C BCD.C()=BCD.A()+BCD.B() ' BCD.SubAB2C BCD.C()=BCD.A()-BCD.B() ' BCD.SubAB2A BCD.A()=BCD.A()-BCD.B() ' BCD.IncA(Inc) BCD.A()=BCD.A()+Inc Note Inc is a SINGLE ' BCD.DecA(Dec) BCD.A()=BCD.A()-Dec Note Dec is a SINGLE ' BCD.MulAB2C BCD.C()=BCD.A()*BCD.B() ' BCD.DivAB2C BCD.C()=BCD.A()\BCD.B() ' 'Utility Routines****************** ' ' BCD.Clr(Dest,NumDigits) Dest(0..NumDigits-1)=0 ' ' BCD.Load(Dest,A$,NumDigits) Load Dest BCD with VAL(A$),NumDigits is optional, if 0 then Numdigits will default to 7 ' ' BCD.LPad$(A$,ML) Pad a string with leading zeroes to make a string ML chars long ' ' BCD.A2Bin BCD.C() = BCD to Binary conversion of BCD.A() in little endian format ' BCD.A2Asc$() Returns String representation of BCD.A() ' ' BCD.Print(Src,Mode,NumDigits) Print a BCD register to console,Src is first element of BCDregister, eg FREQ(0) ' ' BCD.Copy(Src,Dest,NumDigits) Copy a BCD number from source to dest, Src and Dest are first element opf BCD register, BCD.A(0) ' '*********************************************************** *************** The attached .ZIP includes a test suite used to verify correct library operation, as well as provide the prospective user with example code. When including the BCD library in your projects, you should cut and throw away the test suite code. The BCD Library uses the symbol naming idea introduced by @halldave which a) makes names space collisions less likely b) makes ownership of the symbols much clear, ie all symbols 'owned' by the BCD library begin with 'BCD.' c) makes for an OO style - although MMBasic is not OO at all Finally, you will probably need to use MMEdit 3.4.15 or newer to download the library code to a uMite in order to carry out Auto-Crunch to remove the comments/blank-lines/leading|trailing white space. I will be producing highly optimised/tuned versions of this library specifically for 12, 18 and 24 digit operations. Anyway, I hope someone finds the attached code/BCDLibrary interesting and useful. Any bugs/problems/suggestions, please get back to me. 73 Peter 2014-08-17_102634_BCDLib_ArbitraryPrecision_V1.zip The only Konstant is Change |
||||
halldave![]() Senior Member ![]() Joined: 04/05/2014 Location: AustraliaPosts: 121 |
Peter what a valuable library for everyone's use regards David |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Thank you David, and thank you very much for the dotted notation idea, really excellent. Peter The only Konstant is Change |
||||
halldave![]() Senior Member ![]() Joined: 04/05/2014 Location: AustraliaPosts: 121 |
Peter for me it makes an easy read... thanks David |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |