// This may look like C code, but it is really -*- Asm -*-
/*
 *  smoke16/src16/_umul.s - Unsigned multiply function.
 *  $Revision: 1.2 $
 *  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.
 *
 */

	// _umul -- multiplies the unsigned shorts in %a0 and %a1, placing
	// the result in %a0 (high word) and %a1 (low word)
	.text
	.align 1
	// long _umul(short x, short y)
	.global __umul
__umul:	push %t0		// Save %t0, since we use it
	mov 16, %t0		// as a counter to step through the bits.
	mov %0, %a2		// %a2 is the high word
	mov %0, %a3		// and %a3 is the low word of our result

loop:	sll 1, %a2		// Double the result.
	sll 1, %a3
	bnc ncarry
	add 1, %a2
ncarry:	sll 1, %a0		// Examine the high bit of x (%a0) by
	bnc next		// shifting it through the carry flag.
	add %a1, %a3		// If it's set, add y to the result.
	bnc next
	add 1, %a2
next:	sub 1, %t0		// Decrement the bit-counter.
	cmpl %0, %t0		// Until it reaches zero, loop.
	bne loop
	mov %a2, %a0		// Move the result into %a0 and %a1
	mov %a3, %a1
	pop %t0			// Restore %t0.
	ret
