I wanted to write a simple programmer's calculator program for quick use while programming/disassembling (look, disassembling is complicated and all five other monitors are already spoken for), but I didn't think there's an existing eZ80 bigint library, so I spent today writing one. It's got a full suite of already-optimized assembly routines:
  • set var to zero, one, or negative one (used for initializing temps in other routines);
  • addition and subtraction;
  • increment and decrement;
  • get sign and negate;
  • test for zero, non-zero, and compare;
  • left-shift, right-shift, with optional bit to shift in;
  • bit-wise and, or, not, xor, and nand;
  • and get/set bit.
Multiplication and division are also implemented, but in rather-less-optimized C. For my immediate needs, optimizing multiplication and division aren't worth the trouble, but I've created this topic to encourage our optimization experts to give it a try. For quick reference, the current implementation has BIG_INT_SIZE set to 16 (i.e. 128 bits), and numbers are stored little-endian.

Screen shot of test program:

n1 = 0x12345678901234567890123456789012
n2 = 0x11111111111111111111111111111111
n3 = 0x123456789ABCDEF0123456789ABCDEF0
n4 = 0x1DEADBEEF4C0FFEE
prod = 0xDEADBEEF times 0x41COFFEE

I'm also wondering about the API. The worst offender is that I've typed BigInt_t as just uint8_t instead of a proper struct or array. I did it that way so that I could do literals in C89, but I'd rather have a more type-safe stuct definition. The size of a BigInt is currently fixed at compile time, but I imagine that if people are interested in a proper library for use with the C SDK, it would be nice to have the size be a runtime option. The convention for this library is that every routine except multiply and divide overwrites one of its arguments with the output, which is slightly smaller and slightly faster; however, I'm wondering if people would prefer a three-argument system where the output is written somewhere else.

Feel free to send a PR with suggested code changes.
This is super cool, I'm impressed you were able to do this in the span of a day.

Is the test program on githhb?
Ohh, I wish I had more free time now Sad This looks quite fun, good luck DrDnar!
Update 8 PM EDT: I have converted the exact same algorithms I used for multiply and divide into assembly. The divide is terrible but the multiply is probably merely suboptimal.

Xeda112358 wrote:
Ohh, I wish I had more free time now Sad This looks quite fun, good luck DrDnar!
Bugger! You're the best mathtimization expert here.

Dapianokid wrote:
I'm impressed you were able to do this in the span of a day.
The Z80 instruction set (and its predecessor, the 8080, the beta version of x86) is actually specifically designed to make arbitrary-precision addition and subtraction easy to implement. In fact, the first target application of single-chip CPUs was desktop calculators.

Dapianokid wrote:
Is the test program on githhb?
Only if you actually look.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement