Blame sysdeps/sparc/sparc32/umul.S

Packit 6c4009
/*
Packit 6c4009
 * Unsigned multiply.  Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the
Packit 6c4009
 * upper 32 bits of the 64-bit product).
Packit 6c4009
 *
Packit 6c4009
 * This code optimizes short (less than 13-bit) multiplies.  Short
Packit 6c4009
 * multiplies require 25 instruction cycles, and long ones require
Packit 6c4009
 * 45 instruction cycles.
Packit 6c4009
 *
Packit 6c4009
 * On return, overflow has occurred (%o1 is not zero) if and only if
Packit 6c4009
 * the Z condition code is clear, allowing, e.g., the following:
Packit 6c4009
 *
Packit 6c4009
 *	call	.umul
Packit 6c4009
 *	nop
Packit 6c4009
 *	bnz	overflow	(or tnz)
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
ENTRY(.umul)
Packit 6c4009
	or	%o0, %o1, %o4
Packit 6c4009
	mov	%o0, %y			! multiplier -> Y
Packit 6c4009
	andncc	%o4, 0xfff, %g0		! test bits 12..31 of *both* args
Packit 6c4009
	be	LOC(mul_shortway)	! if zero, can do it the short way
Packit 6c4009
	 andcc	%g0, %g0, %o4		! zero the partial product; clear N & V
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Long multiply.  32 steps, followed by a final shift step.
Packit 6c4009
	 */
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 1
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 2
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 3
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 4
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 5
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 6
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 7
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 8
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 9
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 10
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 11
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 12
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 13
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 14
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 15
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 16
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 17
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 18
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 19
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 20
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 21
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 22
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 23
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 24
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 25
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 26
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 27
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 28
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 29
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 30
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 31
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 32
Packit 6c4009
	mulscc	%o4, %g0, %o4	! final shift
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * Normally, with the shift-and-add approach, if both numbers are
Packit 6c4009
	 * positive you get the correct result.  With 32-bit two's-complement
Packit 6c4009
	 * numbers, -x is represented as
Packit 6c4009
	 *
Packit 6c4009
	 *		  x		    32
Packit 6c4009
	 *	( 2  -  ------ ) mod 2  *  2
Packit 6c4009
	 *		   32
Packit 6c4009
	 *		  2
Packit 6c4009
	 *
Packit 6c4009
	 * (the `mod 2' subtracts 1 from 1.bbbb).  To avoid lots of 2^32s,
Packit 6c4009
	 * we can treat this as if the radix point were just to the left
Packit 6c4009
	 * of the sign bit (multiply by 2^32), and get
Packit 6c4009
	 *
Packit 6c4009
	 *	-x  =  (2 - x) mod 2
Packit 6c4009
	 *
Packit 6c4009
	 * Then, ignoring the `mod 2's for convenience:
Packit 6c4009
	 *
Packit 6c4009
	 *   x *  y	= xy
Packit 6c4009
	 *  -x *  y	= 2y - xy
Packit 6c4009
	 *   x * -y	= 2x - xy
Packit 6c4009
	 *  -x * -y	= 4 - 2x - 2y + xy
Packit 6c4009
	 *
Packit 6c4009
	 * For signed multiplies, we subtract (x << 32) from the partial
Packit 6c4009
	 * product to fix this problem for negative multipliers (see mul.s).
Packit 6c4009
	 * Because of the way the shift into the partial product is calculated
Packit 6c4009
	 * (N xor V), this term is automatically removed for the multiplicand,
Packit 6c4009
	 * so we don't have to adjust.
Packit 6c4009
	 *
Packit 6c4009
	 * But for unsigned multiplies, the high order bit wasn't a sign bit,
Packit 6c4009
	 * and the correction is wrong.  So for unsigned multiplies where the
Packit 6c4009
	 * high order bit is one, we end up with xy - (y << 32).  To fix it
Packit 6c4009
	 * we add y << 32.
Packit 6c4009
	 */
Packit 6c4009
#if 0
Packit 6c4009
	tst	%o1
Packit 6c4009
	bl,a	1f		! if %o1 < 0 (high order bit = 1),
Packit 6c4009
	 add	%o4, %o0, %o4	! %o4 += %o0 (add y to upper half)
Packit 6c4009
1:	rd	%y, %o0		! get lower half of product
Packit 6c4009
	retl
Packit 6c4009
	 addcc	%o4, %g0, %o1	! put upper half in place and set Z for %o1==0
Packit 6c4009
#else
Packit 6c4009
	/* Faster code from tege@sics.se.  */
Packit 6c4009
	sra	%o1, 31, %o2	! make mask from sign bit
Packit 6c4009
	and	%o0, %o2, %o2	! %o2 = 0 or %o0, depending on sign of %o1
Packit 6c4009
	rd	%y, %o0		! get lower half of product
Packit 6c4009
	retl
Packit 6c4009
	 addcc	%o4, %o2, %o1	! add compensation and put upper half in place
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
LOC(mul_shortway):
Packit 6c4009
	/*
Packit 6c4009
	 * Short multiply.  12 steps, followed by a final shift step.
Packit 6c4009
	 * The resulting bits are off by 12 and (32-12) = 20 bit positions,
Packit 6c4009
	 * but there is no problem with %o0 being negative (unlike above),
Packit 6c4009
	 * and overflow is impossible (the answer is at most 24 bits long).
Packit 6c4009
	 */
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 1
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 2
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 3
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 4
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 5
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 6
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 7
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 8
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 9
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 10
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 11
Packit 6c4009
	mulscc	%o4, %o1, %o4	! 12
Packit 6c4009
	mulscc	%o4, %g0, %o4	! final shift
Packit 6c4009
Packit 6c4009
	/*
Packit 6c4009
	 * %o4 has 20 of the bits that should be in the result; %y has
Packit 6c4009
	 * the bottom 12 (as %y's top 12).  That is:
Packit 6c4009
	 *
Packit 6c4009
	 *	  %o4		    %y
Packit 6c4009
	 * +----------------+----------------+
Packit 6c4009
	 * | -12- |   -20-  | -12- |   -20-  |
Packit 6c4009
	 * +------(---------+------)---------+
Packit 6c4009
	 *	   -----result-----
Packit 6c4009
	 *
Packit 6c4009
	 * The 12 bits of %o4 left of the `result' area are all zero;
Packit 6c4009
	 * in fact, all top 20 bits of %o4 are zero.
Packit 6c4009
	 */
Packit 6c4009
Packit 6c4009
	rd	%y, %o5
Packit 6c4009
	sll	%o4, 12, %o0	! shift middle bits left 12
Packit 6c4009
	srl	%o5, 20, %o5	! shift low bits right 20
Packit 6c4009
	or	%o5, %o0, %o0
Packit 6c4009
	retl
Packit 6c4009
	 addcc	%g0, %g0, %o1	! %o1 = zero, and set Z
Packit 6c4009
Packit 6c4009
END(.umul)