xref: /csrg-svn/lib/libc/vax/gen/urem.s (revision 17329)
1*17329Ssam/*	urem.s	4.3	84/11/01	*/
213421Sroot
313421Sroot/*
413421Sroot * urem - unsigned remainder for vax-11
513421Sroot *
613421Sroot * arguments: dividend, divisor
713421Sroot * result: remainder
813421Sroot * uses r0-r2
913421Sroot *
1013421Sroot * if 1 < divisor <= 2147483647, zero-extend the dividend
1113421Sroot * to 64 bits and let ediv do the work.  If the divisor is 1,
1213421Sroot * ediv will overflow if bit 31 of the dividend is on, so
1313421Sroot * just return 0.  If the divisor is 0, do the ediv also,
1413421Sroot * so it will generate the proper exception.  All other values
1513421Sroot * of the divisor have bit 31 on: in this case the remainder
1613421Sroot * must be the dividend if divisor > dividend, and the dividend
1713421Sroot * minus the divisor otherwise.  The comparison must be unsigned.
1813421Sroot */
1913421Sroot#include "DEFS.h"
2013421Sroot
21*17329SsamASENTRY(urem, 0)
2213421Sroot	movl	4(ap),r0	/* dividend */
2313421Sroot	movl	8(ap),r2	/* divisor */
2413421Sroot	jeql	1f		/* if divisor=0, force exception */
2513421Sroot	cmpl	r2,$1		/* if divisor <= 1 (signed), */
2613421Sroot	jleq	2f		/*  no division is necessary */
2713421Sroot1:
2813421Sroot	clrl	r1		/* zero-extend the dividend */
2913421Sroot	ediv	r2,r0,r2,r0	/* divide.  q->r2 (discarded), r->r0 */
3013421Sroot	ret
3113421Sroot2:
3213421Sroot	jneq	1f		/* if divisor=1, return 0 */
3313421Sroot	clrl	r0		/*  (because doing the divide will overflow */
3413421Sroot	ret			/*  if the dividend has its high bit on) */
3513421Sroot1:
3613421Sroot	cmpl	r0,r2		/* if dividend < divisor (unsigned) */
3713421Sroot	jlssu	1f		/*  remainder is dividend */
3813421Sroot	subl2	r2,r0		/*  else remainder is dividend - divisor */
3913421Sroot1:
4013421Sroot	ret
41