xref: /minix3/common/lib/libc/arch/m68k/gen/udivsi3.S (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1/*	$NetBSD: udivsi3.S,v 1.4 2013/07/16 23:24:18 matt Exp $	*/
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <machine/asm.h>
37
38#if defined(LIBC_SCCS) && !defined(lint)
39#if 0
40	RCSID("from: @(#)udivsi3.s	5.1 (Berkeley) 6/7/90")
41#else
42	RCSID("$NetBSD: udivsi3.S,v 1.4 2013/07/16 23:24:18 matt Exp $")
43#endif
44#endif /* LIBC_SCCS and not lint */
45
46/* unsigned / unsigned */
47#ifndef __mc68010__
48ENTRY(__udivsi3)
49	movel	4(%sp),%d0
50	divul	8(%sp),%d0
51	rts
52END(__udivsi3)
53#else
54ENTRY(__udivsi3)
55	movel	%d2, -(%sp)	| save %d2
56	movel	12(%sp), %d0	| load divisor
57	movel	8(%sp), %d1	| load dividend
58
59| first, we divide the divisor and dividend by two until
60| the divisor fits into 16 bits:
611:	cmpil	#0x10000, %d0
62	bcs	2f
63	lsrl	#1, %d0
64	lsrl	#1, %d1
65	bra	1b
662:
67
68| now we can do the divide.  to avoid overflow, we have to
69| do the divide in two parts, high and low, and add the
70| results together:
71	movew	%d1, %d2	| save low(dividend)
72	clrw	%d1
73	swap	%d1		| %d1 = dividend >> 16
74	divuw	%d0, %d1	| do the high divide
75	moveal	%d1, %a1	| save high divide result
76	movew	%d2, %d1	| concat(remainder, low(dividend))
77	divuw	%d0, %d1	| do the low divide
78	movel	%a1, %d0	| recover high divide result
79	swap	%d0
80	clrw	%d0		| %d0 = finished high divide result
81	andil	#0xffff, %d1	| %d1 = finished low divide result
82	addl	%d1, %d0	| %d0 = quotient guess
83
84| the quotient we have so far is only a guess.  the divide we
85| did above was really the divide of some dividendB by some
86| divisorB, where the following hold:
87|
88| (dividend - divisor) <= dividendB <= dividend
89| (divisor / 2) < divisorB <= divisor
90|
91| so our guess quotient cannot be less than our real desired
92| quotient.  however, it might be one too big.
93|
94| to adjust this quotient, we multiply it by the original
95| divisor and subtract the result from the original dividend.
96| if the result is nonnegative, our guessed quotient was
97| correct, and the subtraction result is our remainder.
98| if the result is negative, our guessed quotient was one
99| too big, and the subtraction result plus the original
100| divisor is our remainder.
101|
102| as in mulsi3, we have to do the multiply in stages to avoid
103| overflow:
104
105	movel	12(%sp), %d2	| load divisor
106	swap	%d2
107	movel	%d0, %d1
108	muluw	%d2, %d1	| high(divisor) * low(guess)
109	moveal	%d1, %a1	| save high(divisor) * low(guess)
110	swap	%d2
111	movel	%d0, %d1
112	swap	%d1
113	muluw	%d2, %d1	| low(divisor) * high(guess)
114	addl	%a1, %d1
115	swap	%d1
116	clrw	%d1		| %d1 = finished high multiply result
117	moveal	%d2, %a1	| save original divisor
118	muluw	%d0, %d2	| low(guess) * low(divisor)
119	addl	%d1, %d2	| %d2 = guess * divisor
120
121	movel	8(%sp), %d1	| load original dividend
122	subl	%d2, %d1	| subtract
123	bcc	3f
124	subql	#1, %d0		| adjust quotient
125	addl	%a1, %d1	| adjust remainder
1263:	movel	(%sp)+, %d2	| restore %d2
127	rts
128END(__udivsi3)
129#endif /* __mc68010__ */
130