6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 2:31 am

All times are UTC




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Tue Mar 26, 2013 10:31 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Interesting. I have a vague feeling that shift and add is easier for binary, and log/anti log easier in BCD. Might that be true?


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 27, 2013 5:34 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8507
Location: Midwestern USA
BigEd wrote:
Interesting. I have a vague feeling that shift and add is easier for binary, and log/anti log easier in BCD. Might that be true?

It could be. I haven't really considered it, but since BCD doesn't lend itself well to the traditional shift-add or shift-subtract algorithms, log approaches would seemingly be more efficient in software, especially if well-designed log tables are available to reduce the computation overhead. Naperian log tables typically are published to five significant digits, so a fairly complete table could be a practical size.

I note in the Leventhal and Saville 6502 Assembly Language Subroutines tome that their BCD multiplication and division algorithms extracts nybbles from each byte of the multiplicand or dividend and use them as counters to perform repetitive addition or subtraction on the multiplier or divisor. As written, these routines are relatively slow, especially since the most significant nybble of each byte of the multiplicand or dividend has to be right-shifted to become a counter, which is done inside a subroutine. The documentation says that the multiplication routine uses a bit more than 14,000 cycles for an "average" number size of six BCD bytes, with all digits in the multiplicand being $55. Division is even slower at about 20,600 cycles. Not helping any with execution speed is the fact that the routines access the operands in arbitrary memory locations through postindexed indirect addressing, rather than having the numbers loaded into accumulators on page zero, where read/write access would be much faster (plus would free up .Y for other purposes).

Although I haven't investigated it, I suspect a more efficient algorithm could be written, especially if for the 65C816 using 16 bit registers during the addition or subtraction steps. I've already done some work on a BCD model that uses an eight byte number in a classic sign-exponent-mantissa floating point structure, and have noted that writing it to use the 65C816 in 16 bit mode greatly speeds up things and reduces code size. In this preliminary code, I used two direct page accumulators, as well as some other DP locations to store the sign bits and exponents. The basic processes of transferring numbers between memory and accumulators (including normalization steps) are real fast, since it's 16 bit loads and stores, using the stack as the working pointers. Once the accumulators have been properly aligned, addition and subtraction goes very fast as well, as two bytes are added or subtracted at a time, reducing the loop to only four iterations. Therefore I expect a multiplication algorithm could be made correspondingly faster.

That said, it becomes a question of how much processing speed is one willing to sacrifice to get the exactness of decimal arithmetic.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 27, 2013 7:58 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Thanks for those cycle counts.

Hmm, it strikes me that a 10-times-table wouldn't be very large and might help collapse some of those repeated additions.

Quote:
Naperian log tables typically are published to five significant digits, so a fairly complete table could be a practical size.

Ah, I think you didn't read the patent! By computing the log/antilog you can operate at full precision. I can't quite see a table being a good tradeoff.

Quote:
...the exactness of decimal arithmetic

Careful! It's inexact as soon as you have general division (or even multiplication if you were working on say 2 decimal places) - I recently found an article in an HP newsletter which put it well:
Quote:
Another effect of doing BCD arithmetic is the "perceived accuracy" of the results. The perceived accuracy comes from the avoidance of binary arithmetic anomalies, which most people are not used to. The anomalies in the base 10 system are commonplace; most people understand them and know how to deal with them. (For instance, 3 times 1/3 not equaling one because 1/3 is not exactly representable with decimal numbers.) In the binary system different anomalies occur. For example, 0.01 (base 10) cannot be represented exactly in the binary number system, so if 0.01 is used on a binary machine for a loop counter, an incorrect number of loops results to the frustration of a user who is unfamiliar with binary numbers.

(from http://www.ebbsoft.com/hp/hp-cpu.htm which is garish, so try http://www.readability.com/articles/f2p9aaxc)

The advantages of BCD lie mostly in the ease of display and the ease of addition: ideal for point of sale or calculator. Don't make the mistake of thinking that decimal is in any way more true to the nature of numbers than binary is.

Cheers
Ed


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 27, 2013 9:10 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8544
Location: Southern California
Quote:
In the binary system different anomalies occur. For example, 0.01 (base 10) cannot be represented exactly in the binary number system

What we do in non-calculator (embedded systems) scaled-integer hex though is to represent a cent or a mill by 1, then a dollar is 100 (64H) or 1000 (3E8H), and they are absolutely exact.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 27, 2013 9:19 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Exact until you have to multiply by some fraction, such a tax?


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 27, 2013 10:44 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8544
Location: Southern California
Quote:
Exact until you have to multiply by some fraction, such a tax?

You can do the same thing, scaling it as necessary. Tax multiplications require rounding to the nearest cent anyway. For example, if we have 8.25% sales tax on a $14.99 purchase, 1499*825=1236675 ( or 12DEC3 in a hex integer), so the tax is $1.236675 (converted to non-integer decimal at output time), rounded to $1.24, for a total of $16.23. For the rounding, we add half a cent's value, 5000 in decimal or 1388 in hex, before forming the output and truncating at two digits past the decimal point.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 28, 2013 8:38 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
I suppose what I'm thinking is that you can happily make cent-accurate calculations on individual items, but totals won't match with a cent-accurate calculation on a total. Fixed-point is certainly a useful tactic, but (again) it's a mistake to think of it as being a flawless representation of number. You end up calculating extra digits, rounding, and still you will be left with a few anomalies. The most common example might be a column of percentages not adding up to 100.

On the subject of BCD multiplication and logarithms: it looks like the calculation of logs by Wang's method is approximately as expensive as multiplying by repeated addition for each digit. If so, the total cost of a multiply by logs would be a little more than the cost of multiply by repeated addition by digit, but the total cost of division or square root might well come out lower. If we had a 10x10 multiplication table to hand, then operating by digits might well come out faster.

Cheers
Ed


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 28, 2013 3:40 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8507
Location: Midwestern USA
BigEd wrote:
I suppose what I'm thinking is that you can happily make cent-accurate calculations on individual items, but totals won't match with a cent-accurate calculation on a total. Fixed-point is certainly a useful tactic, but (again) it's a mistake to think of it as being a flawless representation of number. You end up calculating extra digits, rounding, and still you will be left with a few anomalies. The most common example might be a column of percentages not adding up to 100.

In my earlier comment about BCD's "exactness," I was referring to the conversion between ASCII and BCD representation, not the actual arithmetic steps. As long as the ASCII representation of a number falls within the precision limits of the BCD representation, conversion to BCD will be exact. Likewise, conversion from BCD back to ASCII will be exact. Of course, as you note, precision limits may bring about cumulative computation errors, and anything involving division is potentially subject to round-off errors. However, the classic case of 1.80 - 1.79 = .009999997 seen in some excess-128 or IEEE-754 representations wouldn't occur in BCD arithmetic, because 1.80 and 1.79 can both be exactly represented in BCD.

As always, there's never a free lunch. 8)

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 18 guests


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 post attachments in this forum

Search for:
Jump to: