To: cctalk at
classiccmp.org
From: dwight elvey:
Well, you've got me confused.
HL = dividend (Dnd)
ld d,h
ld e,l ; DE=Dnd
add hl,hl ; HL=Dnd*2
add hl,hl ; HL=Dnd*4
add hl,de ; HL=Dnd*5
add hl,hl ; HL=Dnd*10
ld b,h
ld c,l ; BC=Dnd*10
add hl,hl ; HL=Dnd*20
add hl,hl ; HL=Dnd*40
add hl,bc ; HL=Dnd*50
add hl,de ; HL=Dnd*51
; At this point, HL=0-41000
ld a,h ; A=HIGH(Dnd*51)
rra ; A=HIGH(Dnd*51)2
ld l,a ; L=HIGH(Dnd*51)/2
; At this point, you've lost me. But a non TLU fall-through 10-bit
divide with only one conditional branch at the end would be worth a
paper in CALGO, surely.
Hi Chuck
Remeber when, I think it was you, said that we might
multiply by a constant that was the fraction of ten
base 2. When I looked at it, I found that it overflowed
16 bits for a number like 799.
I started wacking bits off the end until I could keep it from
overflowing the 16 bits.
51 was the number I found. It wasn't perfect and created
an error but it was close. I wrote some code on the side ( in Forth )
to see what the worst case error was. It turns out that the remainder
was worst case 13. This means that to correct the result, I only
needed to multiply the result of the first calculation by 10
and then subtract that from the original number to find the
error. If the error was 10 or above, I needed to increment the result
and subrtact 10 from the remainder.
If it was less than 10, the result was correct and I just needed
to restore the remainder.
I'm not sure if I did the math right at the end but it may need
a little patching. The idea is sound. I think I need to use 9 instead
of 10 for the subtract but I need to check it out.
Oh, I forgot. the rra is because the result is 2* to large in the
h register after multiplying times 51. One could shorten and only
multiply by 5 but you still need to mask off to truncate. The
result still needs the rra at the end but it might save a few clocks.
It won't work for much over 799 before overflowing. It overflows
at 1286.
I figure that one might do part of the multiplication and then
right shift the result some since were are going to truncate the
LSBs anyway. The first result is expected to error some. With
the correct value for the multiply, the error will always be on
the low side, keeping the error calculation simple. The largest
error seems to grow linearly so even with some truncation,
one should be able to hit 10K or so with only 1 or2 conditionals,
using a large fractional munber to multiply.
I don't think I'd ever use this but it was fun to think about.
Dwight
_________________________________________________________________
Helping your favorite cause is as easy as instant messaging.?You IM, we give.