Difference between revisions of "Z80 Routines:Math:Multiplication"

From WikiTI
Jump to: navigation, search
(added categorization)
Line 7: Line 7:
 
They can easily be unrolled to gain some speed.
 
They can easily be unrolled to gain some speed.
  
== 8*8 multiplication ==
+
== Unsigned versions ==
 +
 
 +
=== 8*8 multiplication ===
  
 
The following routine multiplies h by e and places the result in hl
 
The following routine multiplies h by e and places the result in hl
Line 31: Line 33:
 
  </nowiki>
 
  </nowiki>
  
== 16*8 multiplication ==
+
=== 16*8 multiplication ===
  
 
The following routine multiplies de by a and places the result in ahl (which means a is the most significant byte of the product, l the least significant and h the intermediate one...)
 
The following routine multiplies de by a and places the result in ahl (which means a is the most significant byte of the product, l the least significant and h the intermediate one...)
Line 59: Line 61:
 
  </nowiki>
 
  </nowiki>
  
== 16*16 multiplication ==
+
=== 16*16 multiplication ===
  
 
The following routine multiplies bc by de and places the result in dehl.
 
The following routine multiplies bc by de and places the result in dehl.
Line 89: Line 91:
 
   ret
 
   ret
 
  </nowiki>
 
  </nowiki>
 +
 +
== Signed versions ==
 +
 +
<!-- do the routines later, basically try to use cpu instruction that preserve bit 7 (sign) or if this is not possible, just take the sign out, do the multiplication and give the sign afterwards and accordingly. -->
 +
 +
=== 8*8 multiplication ===
 +
 +
=== 16*8 multiplication ===

Revision as of 01:47, 9 November 2009


Introduction

All these routines use the restoring multiplication algorithm, adapted to the z80 architecture to maximize speed. They can easily be unrolled to gain some speed.

Unsigned versions

8*8 multiplication

The following routine multiplies h by e and places the result in hl

mult_h_e
   ld	l, 0
   ld	d, l

   sla	h		; optimised 1st iteration
   jr	nc, $+3
   ld	l, e
   
   ld b, 7
_loop:
   add	hl, hl          
   jr	nc, $+3
   add	hl, de
   
   djnz	_loop
   
   ret
 

16*8 multiplication

The following routine multiplies de by a and places the result in ahl (which means a is the most significant byte of the product, l the least significant and h the intermediate one...)

mult_a_de
   ld	c, 0
   ld	h, c
   ld	l, h

   add	a, a		; optimised 1st iteration
   jr	nc, $+4
   ld	h,d
   ld	l,e

   ld b, 7
_loop:
   add	hl, hl
   rla
   jr	nc, $+4
   add	hl, de
   adc	a, c            ; yes this is actually adc a, 0 but since c is free we set it to zero and so we can save 1 byte and up to 3 T-states per iteration
   
   djnz	_loop
   
   ret
 

16*16 multiplication

The following routine multiplies bc by de and places the result in dehl.

mult_de_bc
   ld	h, 0
   ld	l, h

   sla	e		; optimised 1st iteration
   rl	d
   jr	nc, $+4
   ld	h, b
   ld	l, c

   ld	a, 15
_loop:
   add	hl, hl
   rl	e
   rl	d
   jr	nc, $+6
   add	hl, bc
   jr	nc, $+3
   inc	de
   
   dec	a
   jr	nz, _loop
   
   ret
 

Signed versions

8*8 multiplication

16*8 multiplication