z80 library cleanup
[fw/sdcc] / device / lib / z80 / mul.s
index 92e0ceba34f13775946193b327f8f1ef5d7a4a5d..6dd917a1ec2c5961c24c9d838c5d27695915fa51 100644 (file)
@@ -1,38 +1,80 @@
-       ;; Originally from GBDK by Pascal Felber.
-       
-       .area   _CODE
+        .area   _CODE
 
-__mulschar_rr_s::
-        ld      hl,#2
+; This multiplication routine is similar to the one
+; from Rodnay Zaks, "Programming the Z80".
+
+; Now replaced by a builtin for code generation, but
+; still called from some asm files in this directory.
+__muluchar_rrx_s::
+        ld      hl, #2+1
+        add     hl, sp
+        ld      e, (hl)
+        dec     hl
+        ld      h, (hl)
+        ld      l, #0
+        ld      d, l
+        ld      b, #8
+muluchar_rrx_s_loop:
+        add     hl, hl
+        jr      nc, muluchar_rrx_s_noadd
+        add     hl, de
+muluchar_rrx_s_noadd:
+        djnz    muluchar_rrx_s_loop
+        ret
+
+; operands have different sign
+
+__mulsuchar_rrx_s::
+        ld      hl,#2+1
         add     hl,sp
 
         ld      e,(hl)
-        inc     hl
-        ld      l,(hl)                
+        dec     hl
+        ld      c,(hl)
+        ld      b, #0
+        jr      signexte
+
+__muluschar_rrx_s::
+        ld      hl,#2+1
+        add     hl,sp
+
+        ld      c,(hl)
+        ld      b, #0
+        dec     hl
+        ld      e,(hl)
+        jr      signexte
+
+;; Originally from GBDK by Pascal Felber.
+
+__mulschar_rrx_s::
+        ld      hl,#2+1
+        add     hl,sp
+
+        ld      e,(hl)
+        dec     hl
+        ld      l,(hl)
 
         ;; Fall through
-__mulschar_rr_hds::
+__mulschar_rrx_hds::
         ;; Need to sign extend before going in.
         ld      c,l
-        
+
         ld      a,l
         rla
         sbc     a,a
         ld      b,a
-
+signexte:
         ld      a,e
         rla
         sbc     a,a
         ld      d,a
 
-        jp      .mul16
+        jr      .mul16
 
-__muluchar_rr_s::
-__mulsint_rr_s::
-__muluint_rr_s::
+__mulint_rrx_s::
         ld      hl,#2
         add     hl,sp
-        
+
         ld      e,(hl)
         inc     hl
         ld      d,(hl)
@@ -43,47 +85,45 @@ __muluint_rr_s::
         ld      l,a
 
         ;; Fall through
-        
-__muluchar_rr_hds::
-__mulsint_rr_hds::
-__muluint_rr_hds::
+
+__muluchar_rrx_hds::
+__mulint_rrx_hds::
        ;; Parameters:
-       ;;      HL, DE (left, right irrelivent)
-       ;; Must preserve BC
+       ;;      HL, DE (left, right irrelevant)
        ld      b,h
        ld      c,l
-       
+
        ;; 16-bit multiplication
-       ;; 
+       ;;
        ;; Entry conditions
        ;;   BC = multiplicand
        ;;   DE = multiplier
-       ;; 
+       ;;
        ;; Exit conditions
        ;;   DE = less significant word of product
        ;;
        ;; Register used: AF,BC,DE,HL
 .mul16:
-.mulu16:
-       LD      HL,#0x00        ; Product = 0
-       LD      A,#15           ; Count = bit length - 1
-       ;; Shift-and-add algorithm
-       ;; If MSB of multiplier is 1, add multiplicand to partial product
-       ;; Shift partial product, multiplier left 1 bit
-.mlp:
-       SLA     E               ; Shift multiplier left 1 bit
-       RL      D
-       jp      NC,.mlp1        ; Jump if MSB of multiplier = 0
-       ADD     HL,BC           ; Add multiplicand to partial product
-.mlp1:
-       ADD     HL,HL           ; Shift partial product left
-       DEC     A
-       jp      NZ,.mlp         ; Continue until count = 0
-       ;; Add multiplicand one last time if MSB of multiplier is 1
-       BIT     7,D             ; Get MSB of multiplier
-       JR      Z,.mend         ; Exit if MSB of multiplier is 0
-       ADD     HL,BC           ; Add multiplicand to product
-.mend:
-                               ; HL = result
-       ret
+        ld      hl,#0
+        ld      a,b
+        ; ld c,c
+        ld      b,#16
+
+        ;; Optimise for the case when this side has 8 bits of data or
+        ;; less.  This is often the case with support address calls.
+        or      a
+        jr      NZ,1$
 
+        ld      b,#8
+        ld      a,c
+1$:
+        ;; Taken from z88dk, which originally borrowed from the
+        ;; Spectrum rom.
+        add     hl,hl
+        rl      c
+        rla                     ;DLE 27/11/98
+        jr      NC,2$
+        add     hl,de
+2$:
+        djnz    1$
+        ret