Re: A86: simple math optimization


[Prev][Next][Index][Thread]

Re: A86: simple math optimization






On Mon, 11 May 1998, BIOSERRORS wrote:
> 
> What's the fastest way to do
> 
> hl = (16 * d) + e
> 

(16 * d) + e 
is the same as 
(d << 4) + e

Try this:

ld   (D_temp), d
ld   hl, D_temp
xor  a
rld               ; now a = d >> 4   (High nibble of D) and
                  ;             D_temp = (d & %00001111) << 4 
                  ;             (Low nibble of D times 16)
ld   d, a         ; save high nibble in d for now
ld   a, (D_temp)
add  a, e
ld   l, a         ;l = e + (D's low nibble * 16)
ld   a, d
adc  a, 0
ld   h, a         ;h = D's high nibble plus any carry

or you could try this:

ld  h, 0
ld  l, d
add hl, hl
add hl, hl
add hl, hl
add hl, hl        ; now hl = 16 * d
ld  d, 0
add hl, de        ; and then we add the e


I'm not sure which is faster. The second one has fewer instructions, but
there are lots of 16-bit instructions (which generally take longer than
8-bit instructions).

I hope this helps,
Nathan Linger



References: