Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compiler Speed Trials
#24
Ha!

I've been looking at Z80 optimization pages lately.

Under math tricks at <!-- m --><a class="postlink" href="http://wikiti.brandonw.net/index.php?title=Z80_Optimization">http://wikiti.brandonw.net/index.php?ti ... timization</a><!-- m --> there are some good ones, including that one. I like the alternatives to using NEG - which is awkward, being an extended instruction:
Code:
;Instead of
    neg
    add a,N   ;you want to calculate N-A

;Do it this way:
    cpl
    add a,N+1    ;neg is practically equivalent to cpl \ inc a
; -> save 1 byte and 4 T-states

And this one, when you learn it, is solid gold:

Looping with 16 bit counter

There are two ways to make loops with a 16bit counter :

* the naive one, which results in smaller code but increased loop overhead (24 * n T-states) and destroys a
Code:
ld bc, ...
loop:
  ; loop body here

  dec bc
  ld  a, b
  or  c
  jp  nz,loop

* the slightly trickier one, which takes a couple more bytes but has a much lower overhead (12 * n + 14 * (n / 16) T-states)
(This is harder to understand why it works, but is MUCH faster - almost as fast as djnz with an 8 bit counter (and oddly written, such that it uses B and D as loop counters; personally I'd use B and C, I think)
Code:
dec  de
  ld  b, e
  inc  b
  inc  d
loop2:
  ; loop body here
  
  djnz loop2
  dec  d
  jp  nz,loop2


How it works is because if b=0 DJNZ will loop 256 times. So we start with B set to the number of loops past a multiple of 256, and then loop D lots of 256 times. It saves a lot of time over a reasonably large loop - for example, it's about half the overhead, just for something as small as a 1000 loop.

There are quite a few other similar tricks listed. You might find some are handy.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 5 Guest(s)