// This may look like C code, but it is really -*- Asm -*-
/*
 *  smoke16/src16/_smul.s - Signed multiply function.
 *  $Revision: 1.1 $
 *  Copyright 1997, 2000, Benjamin Sittler
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

	.text
	.align 1

	// _smul -- multiplies the signed shorts in %a0 and %a1, placing
	// the result in %a0 (high word) and %a1 (low word)

	// implements Booth's multiplication algorithm

	// long _smul(short x, short y)
	// x:		muliplicand (%a0)
	// y:		multiplier (%a1)
	// returns:	product (%a0`%a1)
	.global __smul
__smul:	push %t0 // counter
	mov 16, %t0
	// clear MSW of product (%a2`%a1)
	mov %0, %a2

smul0:	// vbit == 0
	srl %a1, 1, %a3
	bc smul01
smul00:	// right-shift product
	sll %a2, 15, %a1
	or %a1, %a3, %a1
	sra %a2, 1, %a2
smul0N:	// check counter
	sub 1, %t0
	bz smulN
	b smul0
smul01:	// subtract multiplicand from MSW of product
	sub %a2, %a0, %a2
	b smul11

smul1:	// vbit == 1
	srl %a1, 1, %a3
	bnc smul10
smul11:	// right-shift product
	sll %a2, 15, %a1
	or %a1, %a3, %a1
	sra %a2, 1, %a2
smul1N:	// check counter
	sub 1, %t0
	bz smulN
	b smul1
smul10:	// add multiplicand to MSW of product
	add %a2, %a0, %a2
	b smul00

smulN:	mov %a2, %a0
	pop %t0
	ret
