xref: /original-bsd/sys/vax/vax/urem.s (revision acfb0788)
1/*	urem.s	6.2	84/08/01
2 *
3 *	urem - unsigned remainder for vax-11
4 *
5 *	arguments: dividend, divisor
6 *	result: remainder
7 *	uses r0-r2
8 *
9 *	if 1 < divisor <= 2147483647, zero-extend the dividend
10 *	to 64 bits and let ediv do the work.  If the divisor is 1,
11 *	ediv will overflow if bit 31 of the dividend is on, so
12 *	just return 0.  If the divisor is 0, do the ediv also,
13 *	so it will generate the proper exception.  All other values
14 *	of the divisor have bit 31 on: in this case the remainder
15 *	must be the dividend if divisor > dividend, and the dividend
16 *	minus the divisor otherwise.  The comparison must be unsigned.
17 */
18	.text
19	.align	1
20	.globl	urem
21urem:	.word	0x0000
22#ifdef GPROF
23	jsb	mcount
24#endif GPROF
25	movl	4(ap),r0	# Dividend
26	movl	8(ap),r2	# Divisor
27	jeql	div		# If divisor=0, force exception
28	cmpl	r2,$1		# If divisor <= 1 (signed),
29	jleq	nodiv		#  no division is necessary
30div:	clrl	r1		# Zero-extend the dividend
31	ediv	r2,r0,r2,r0	# Divide.  q->r2 (discarded), r->r0
32	ret
33nodiv:	jneq	nzero		# If divisor=1, return 0
34	clrl	r0		#  (because doing the divide will overflow
35	ret			#  if the dividend has its high bit on)
36nzero:	cmpl	r0,r2		# If dividend < divisor (unsigned)
37	jlssu	retn		#  remainder is dividend
38	subl2	r2,r0		#  else remainder is dividend - divisor
39retn:	ret
40