xref: /netbsd-src/sys/lib/libkern/arch/m68k/udivsi3.S (revision d295cd72e947bdb963f574ec4395f408dc252fef)
1/*	$NetBSD: udivsi3.S,v 1.6 2020/04/23 03:12:49 rin 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.6 2020/04/23 03:12:49 rin Exp $")
43#endif
44#endif /* LIBC_SCCS and not lint */
45
46/* unsigned / unsigned */
47#ifndef __mc68010__
48/* XXX sun3 need this for bootloader */
49ENTRY(__udivsi3)
50	movel	4(%sp),%d0
51	divul	8(%sp),%d0
52	rts
53END(__udivsi3)
54#else
55ENTRY(__udivsi3)
56	movel	%d2, -(%sp)	| save %d2
57	movel	12(%sp), %d0	| load divisor
58	movel	8(%sp), %d1	| load dividend
59
60| first, we divide the divisor and dividend by two until
61| the divisor fits into 16 bits:
621:	cmpil	#0x10000, %d0
63	bcs	2f
64	lsrl	#1, %d0
65	lsrl	#1, %d1
66	bra	1b
672:
68
69| now we can do the divide.  to avoid overflow, we have to
70| do the divide in two parts, high and low, and add the
71| results together:
72	movew	%d1, %d2	| save low(dividend)
73	clrw	%d1
74	swap	%d1		| %d1 = dividend >> 16
75	divuw	%d0, %d1	| do the high divide
76	moveal	%d1, %a1	| save high divide result
77	movew	%d2, %d1	| concat(remainder, low(dividend))
78	divuw	%d0, %d1	| do the low divide
79	movel	%a1, %d0	| recover high divide result
80	swap	%d0
81	clrw	%d0		| %d0 = finished high divide result
82	andil	#0xffff, %d1	| %d1 = finished low divide result
83	addl	%d1, %d0	| %d0 = quotient guess
84
85| the quotient we have so far is only a guess.  the divide we
86| did above was really the divide of some dividendB by some
87| divisorB, where the following hold:
88|
89| (dividend - divisor) <= dividendB <= dividend
90| (divisor / 2) < divisorB <= divisor
91|
92| so our guess quotient cannot be less than our real desired
93| quotient.  however, it might be one too big.
94|
95| to adjust this quotient, we multiply it by the original
96| divisor and subtract the result from the original dividend.
97| if the result is nonnegative, our guessed quotient was
98| correct, and the subtraction result is our remainder.
99| if the result is negative, our guessed quotient was one
100| too big, and the subtraction result plus the original
101| divisor is our remainder.
102|
103| as in mulsi3, we have to do the multiply in stages to avoid
104| overflow:
105
106	movel	12(%sp), %d2	| load divisor
107	swap	%d2
108	movel	%d0, %d1
109	muluw	%d2, %d1	| high(divisor) * low(guess)
110	moveal	%d1, %a1	| save high(divisor) * low(guess)
111	swap	%d2
112	movel	%d0, %d1
113	swap	%d1
114	muluw	%d2, %d1	| low(divisor) * high(guess)
115	addl	%a1, %d1
116	swap	%d1
117	clrw	%d1		| %d1 = finished high multiply result
118	moveal	%d2, %a1	| save original divisor
119	muluw	%d0, %d2	| low(guess) * low(divisor)
120	addl	%d1, %d2	| %d2 = guess * divisor
121
122	movel	8(%sp), %d1	| load original dividend
123	subl	%d2, %d1	| subtract
124	bcc	3f
125	subql	#1, %d0		| adjust quotient
126	addl	%a1, %d1	| adjust remainder
1273:	movel	(%sp)+, %d2	| restore %d2
128	rts
129END(__udivsi3)
130#endif /* __mc68010__ */
131