Author Topic: eZ80 Optimized Routines  (Read 7309 times)

0 Members and 1 Guest are viewing this topic.

Offline jcochran

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +0/-0
    • View Profile
Re: eZ80 Optimized Routines
« Reply #15 on: October 11, 2021, 12:28:05 pm »
The mult16 routine from Xeda112358 has a bug, and can be made one byte shorter.

To demonstrate bug, multiply 0ffffh by 0ffffh.
Correct result = 0fffe0001
Incorrect result = 0feff0001

New routine.
Code: [Select]
mul16:
;;Expects Z80 mode
;;Inputs: hl,bc
;;Outputs: Upper 16 bits in DE, lower 16 bits in BC
;;53 or 55 t-states
;;29 bytes
    ld d,c
    ld e,l
    mlt de
    push de     ; Push low product onto stack.
    ld d,h      ; Prepare for high product
    ld e,b
    ld h,e      ; Swap bytes for 2 middle products. Do here when I have copies of high values
    ld b,d      ; instead of later, needing A as temp.
    mlt de      ; Calculate high product
    mlt hl      ; Calculate first middle product
    mlt bc      ; Calculate second middle product
    add hl,bc   ; Sum middle products
    jr nc,$+3
    inc d       ; Inc D if carry.  (The INC DE was a bug).
    pop bc      ; Retrieve low product.
    ld a,b      ; Add in middle products to create final answer.
    add a,l
    ld b,a
    ld a,e
    adc a,h
    ld e,a
    ret nc
    inc d
    ret


Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4679
  • Rating: +718/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: eZ80 Optimized Routines
« Reply #16 on: October 11, 2021, 12:32:17 pm »
Oh, fantastic! Good catch!

Offline jcochran

  • LV0 Newcomer (Next: 5)
  • Posts: 2
  • Rating: +0/-0
    • View Profile
Re: eZ80 Optimized Routines
« Reply #17 on: October 12, 2021, 04:18:27 pm »
If you don't insist on the current register usage, you can get a bit shorter and faster.
Code: [Select]
; HLDE = HL * DE
; 52 cc, 28 bytes
Mult16:
ld b,h
ld c,d
mlt bc
push bc
ld b,l
ld c,e
ld l,c
ld e,b
mlt bc
mlt de
mlt hl
xor a
add hl,de
ld e,h
ld h,l
ld l,a
adc a,a
ld d,a
add hl,bc
ex de,hl
pop bc
adc hl,bc
ret

Unfortunately, I don't see any way to make it shorter or faster.