Integer Math Error
pjv
Posts: 1,903
Hi All;
For those who work with integer math, I just ran accross what I think is an error in one of the integer multiply routines on the SX List.
Typically mutiplication is performed by successive shifting of the multiplier, then if the shifted-out bit is a one,·adding the multiplicand to an accumulator. This works for precisions using any number of bytes, I believe the errant code was for a 24 bit example.
As the multiplicand is added to the muli-byte accumulator, the carry represents overflows from the current accumulator byte being summed, and must cascade as a "plus one" into the next higher accumulator byte. (This can be handled directly·by the processor·if the carryx extension is enabled,·then the programmer need not bother).
In the errant example, this adding of "one" is handled by loading·an incremented value of the multiplicand if the carry was set (MOV w,++MultiplicandByte), or the regular value of the mutiplicand if carry was not set (MOV w,MultiplicandByte), and adding this carry-adjusted value to the accumulator. All good so far.
The problem comes in when the value in the loaded multiplicand byte is $FF, then loading the incremented multiplicand byte would be $00, and the ripple-through of the carry is lost, causing an error in the result.
To prevent this, the ripple-though carry must instead be handled·by·successively ADDING a·one to each of the higher accumulator bytes when a carry-out occurs, so that the carries can properly cascade through each byte of the accumulator.
Cheers,
Peter (pjv)
Post Edited (pjv) : 4/16/2005 10:57:28 PM GMT
For those who work with integer math, I just ran accross what I think is an error in one of the integer multiply routines on the SX List.
Typically mutiplication is performed by successive shifting of the multiplier, then if the shifted-out bit is a one,·adding the multiplicand to an accumulator. This works for precisions using any number of bytes, I believe the errant code was for a 24 bit example.
As the multiplicand is added to the muli-byte accumulator, the carry represents overflows from the current accumulator byte being summed, and must cascade as a "plus one" into the next higher accumulator byte. (This can be handled directly·by the processor·if the carryx extension is enabled,·then the programmer need not bother).
In the errant example, this adding of "one" is handled by loading·an incremented value of the multiplicand if the carry was set (MOV w,++MultiplicandByte), or the regular value of the mutiplicand if carry was not set (MOV w,MultiplicandByte), and adding this carry-adjusted value to the accumulator. All good so far.
The problem comes in when the value in the loaded multiplicand byte is $FF, then loading the incremented multiplicand byte would be $00, and the ripple-through of the carry is lost, causing an error in the result.
To prevent this, the ripple-though carry must instead be handled·by·successively ADDING a·one to each of the higher accumulator bytes when a carry-out occurs, so that the carries can properly cascade through each byte of the accumulator.
Cheers,
Peter (pjv)
Post Edited (pjv) : 4/16/2005 10:57:28 PM GMT
Comments
I have run into errors on the SX list in some of the mutliply divide routines.
I will have to check my notes when I get home. I had to do a 'repair' of one
of the samples of code to get my multiply/divide correct.
I was going to check it out on the SX list page to see if any of the code
would 'jog' my memory, but...··· ...apparently I was accessing pages too
quickly...·· ...so no go.
-Dan
·
If ever you have any problem with the code you find on the site, do please use the little form at the bottom to post a comment, even if it is just "this didn't work for me" but hopefully people will post the corrections.
If you guys remember what page you where looking at, please let me know.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!
I hope someone who tags onto your thread can use them.
Leinster