Multiple precision arithmetic (32bit , 48bit, 64bit etc.), e.g for AD9850

matherp

Senior Member
Multiple precision arithmetic (32bit , 48bit, 64bit etc.), e.g for AD9850

Sometimes we need more "bits" than the 16 in the standard picaxe word. One of the chips that has been frequently discussed is the AD9850 which needs the calculation val = (n * 2^32)/125,000,000 solving, where n is the desired output frequency, to program the frequency on the chip.

In the code arithmetic is possible up to 64 bits. It can be any multiple of 8 below this settable with a single symbol 'precision' and with a minor modification to the storage registers (these currently use ram locations 56 to 79) can be more.

The code has been optimised to the extent I am able and tested against excel and the AD9850 design calculator and an online multi-precision calculator http://www.javascripter.net/math/calculators/100digitbigintcalculator.htm
Overall the routines now allow for addition, subtraction, multiplication and division of up to 20 digit numbers (18446744073709551615 is the maximum!!) including conversion to and from ascii text strings but also work well for (for example) 32 bit arithmetic. The example now uses 56 bit arithmetic which is what is needed for the AD9850. Speed scales with the number of bits. Run times for 64bit multiply and divide on a 20X2 at 64Meg are now something less than 0.5 seconds

The code is now too long to include in line so is attached. The code has now been tested on a 14M2 and 20X2 so should work on any of the more modern chips. As always any feedback would be useful. The use of the code modules should be self-explanatory but I'll try and answer any questions.

The code has been ported from the assembler given by http://avtanski.net/projects/math/

Hope this is helpful

Peter
 

Attachments

Last edited:

matherp

Senior Member
Hack version (comments out of date) which is slightly faster using word arithmetic rather than byte. The disadvantage is that it is bigger and can only manage 32, 48, or 64 bit numbers
 

Attachments

Top