162306a36Sopenharmony_ci/* libgcc1 routines for 68000 w/o floating-point hardware.
262306a36Sopenharmony_ci   Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc.
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciThis file is part of GNU CC.
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciGNU CC is free software; you can redistribute it and/or modify it
762306a36Sopenharmony_ciunder the terms of the GNU General Public License as published by the
862306a36Sopenharmony_ciFree Software Foundation; either version 2, or (at your option) any
962306a36Sopenharmony_cilater version.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciIn addition to the permissions in the GNU General Public License, the
1262306a36Sopenharmony_ciFree Software Foundation gives you unlimited permission to link the
1362306a36Sopenharmony_cicompiled version of this file with other programs, and to distribute
1462306a36Sopenharmony_cithose programs without any restriction coming from the use of this
1562306a36Sopenharmony_cifile.  (The General Public License restrictions do apply in other
1662306a36Sopenharmony_cirespects; for example, they cover modification of the file, and
1762306a36Sopenharmony_cidistribution when not linked into another program.)
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciThis file is distributed in the hope that it will be useful, but
2062306a36Sopenharmony_ciWITHOUT ANY WARRANTY; without even the implied warranty of
2162306a36Sopenharmony_ciMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2262306a36Sopenharmony_ciGeneral Public License for more details. */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/* As a special exception, if you link this library with files
2562306a36Sopenharmony_ci   compiled with GCC to produce an executable, this does not cause
2662306a36Sopenharmony_ci   the resulting executable to be covered by the GNU General Public License.
2762306a36Sopenharmony_ci   This exception does not however invalidate any other reasons why
2862306a36Sopenharmony_ci   the executable file might be covered by the GNU General Public License.  */
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* Use this one for any 680x0; assumes no floating point hardware.
3162306a36Sopenharmony_ci   The trailing " '" appearing on some lines is for ANSI preprocessors.  Yuk.
3262306a36Sopenharmony_ci   Some of this code comes from MINIX, via the folks at ericsson.
3362306a36Sopenharmony_ci   D. V. Henkel-Wallace (gumby@cygnus.com) Fete Bastille, 1992
3462306a36Sopenharmony_ci*/
3562306a36Sopenharmony_ci#include <linux/export.h>
3662306a36Sopenharmony_ci/* These are predefined by new versions of GNU cpp.  */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#ifndef __USER_LABEL_PREFIX__
3962306a36Sopenharmony_ci#define __USER_LABEL_PREFIX__ _
4062306a36Sopenharmony_ci#endif
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#ifndef __REGISTER_PREFIX__
4362306a36Sopenharmony_ci#define __REGISTER_PREFIX__
4462306a36Sopenharmony_ci#endif
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#ifndef __IMMEDIATE_PREFIX__
4762306a36Sopenharmony_ci#define __IMMEDIATE_PREFIX__ #
4862306a36Sopenharmony_ci#endif
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* ANSI concatenation macros.  */
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define CONCAT1(a, b) CONCAT2(a, b)
5362306a36Sopenharmony_ci#define CONCAT2(a, b) a ## b
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* Use the right prefix for global labels.  */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define SYM(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/* Use the right prefix for registers.  */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define REG(x) CONCAT1 (__REGISTER_PREFIX__, x)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* Use the right prefix for immediate values.  */
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define IMM(x) CONCAT1 (__IMMEDIATE_PREFIX__, x)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define d0 REG (d0)
6862306a36Sopenharmony_ci#define d1 REG (d1)
6962306a36Sopenharmony_ci#define d2 REG (d2)
7062306a36Sopenharmony_ci#define d3 REG (d3)
7162306a36Sopenharmony_ci#define d4 REG (d4)
7262306a36Sopenharmony_ci#define d5 REG (d5)
7362306a36Sopenharmony_ci#define d6 REG (d6)
7462306a36Sopenharmony_ci#define d7 REG (d7)
7562306a36Sopenharmony_ci#define a0 REG (a0)
7662306a36Sopenharmony_ci#define a1 REG (a1)
7762306a36Sopenharmony_ci#define a2 REG (a2)
7862306a36Sopenharmony_ci#define a3 REG (a3)
7962306a36Sopenharmony_ci#define a4 REG (a4)
8062306a36Sopenharmony_ci#define a5 REG (a5)
8162306a36Sopenharmony_ci#define a6 REG (a6)
8262306a36Sopenharmony_ci#define fp REG (fp)
8362306a36Sopenharmony_ci#define sp REG (sp)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	.text
8662306a36Sopenharmony_ci	.proc
8762306a36Sopenharmony_ci	.globl	SYM (__udivsi3)
8862306a36Sopenharmony_ciSYM (__udivsi3):
8962306a36Sopenharmony_ci#if !(defined(__mcf5200__) || defined(__mcoldfire__))
9062306a36Sopenharmony_ci	movel	d2, sp@-
9162306a36Sopenharmony_ci	movel	sp@(12), d1	/* d1 = divisor */
9262306a36Sopenharmony_ci	movel	sp@(8), d0	/* d0 = dividend */
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	cmpl	IMM (0x10000), d1 /* divisor >= 2 ^ 16 ?   */
9562306a36Sopenharmony_ci	jcc	L3		/* then try next algorithm */
9662306a36Sopenharmony_ci	movel	d0, d2
9762306a36Sopenharmony_ci	clrw	d2
9862306a36Sopenharmony_ci	swap	d2
9962306a36Sopenharmony_ci	divu	d1, d2          /* high quotient in lower word */
10062306a36Sopenharmony_ci	movew	d2, d0		/* save high quotient */
10162306a36Sopenharmony_ci	swap	d0
10262306a36Sopenharmony_ci	movew	sp@(10), d2	/* get low dividend + high rest */
10362306a36Sopenharmony_ci	divu	d1, d2		/* low quotient */
10462306a36Sopenharmony_ci	movew	d2, d0
10562306a36Sopenharmony_ci	jra	L6
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciL3:	movel	d1, d2		/* use d2 as divisor backup */
10862306a36Sopenharmony_ciL4:	lsrl	IMM (1), d1	/* shift divisor */
10962306a36Sopenharmony_ci	lsrl	IMM (1), d0	/* shift dividend */
11062306a36Sopenharmony_ci	cmpl	IMM (0x10000), d1 /* still divisor >= 2 ^ 16 ?  */
11162306a36Sopenharmony_ci	jcc	L4
11262306a36Sopenharmony_ci	divu	d1, d0		/* now we have 16 bit divisor */
11362306a36Sopenharmony_ci	andl	IMM (0xffff), d0 /* mask out divisor, ignore remainder */
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/* Multiply the 16 bit tentative quotient with the 32 bit divisor.  Because of
11662306a36Sopenharmony_ci   the operand ranges, this might give a 33 bit product.  If this product is
11762306a36Sopenharmony_ci   greater than the dividend, the tentative quotient was too large. */
11862306a36Sopenharmony_ci	movel	d2, d1
11962306a36Sopenharmony_ci	mulu	d0, d1		/* low part, 32 bits */
12062306a36Sopenharmony_ci	swap	d2
12162306a36Sopenharmony_ci	mulu	d0, d2		/* high part, at most 17 bits */
12262306a36Sopenharmony_ci	swap	d2		/* align high part with low part */
12362306a36Sopenharmony_ci	tstw	d2		/* high part 17 bits? */
12462306a36Sopenharmony_ci	jne	L5		/* if 17 bits, quotient was too large */
12562306a36Sopenharmony_ci	addl	d2, d1		/* add parts */
12662306a36Sopenharmony_ci	jcs	L5		/* if sum is 33 bits, quotient was too large */
12762306a36Sopenharmony_ci	cmpl	sp@(8), d1	/* compare the sum with the dividend */
12862306a36Sopenharmony_ci	jls	L6		/* if sum > dividend, quotient was too large */
12962306a36Sopenharmony_ciL5:	subql	IMM (1), d0	/* adjust quotient */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciL6:	movel	sp@+, d2
13262306a36Sopenharmony_ci	rts
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#else /* __mcf5200__ || __mcoldfire__ */
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci/* Coldfire implementation of non-restoring division algorithm from
13762306a36Sopenharmony_ci   Hennessy & Patterson, Appendix A. */
13862306a36Sopenharmony_ci	link	a6,IMM (-12)
13962306a36Sopenharmony_ci	moveml	d2-d4,sp@
14062306a36Sopenharmony_ci	movel	a6@(8),d0
14162306a36Sopenharmony_ci	movel	a6@(12),d1
14262306a36Sopenharmony_ci	clrl	d2		| clear p
14362306a36Sopenharmony_ci	moveq	IMM (31),d4
14462306a36Sopenharmony_ciL1:	addl	d0,d0		| shift reg pair (p,a) one bit left
14562306a36Sopenharmony_ci	addxl	d2,d2
14662306a36Sopenharmony_ci	movl	d2,d3		| subtract b from p, store in tmp.
14762306a36Sopenharmony_ci	subl	d1,d3
14862306a36Sopenharmony_ci	jcs	L2		| if no carry,
14962306a36Sopenharmony_ci	bset	IMM (0),d0	| set the low order bit of a to 1,
15062306a36Sopenharmony_ci	movl	d3,d2		| and store tmp in p.
15162306a36Sopenharmony_ciL2:	subql	IMM (1),d4
15262306a36Sopenharmony_ci	jcc	L1
15362306a36Sopenharmony_ci	moveml	sp@,d2-d4	| restore data registers
15462306a36Sopenharmony_ci	unlk	a6		| and return
15562306a36Sopenharmony_ci	rts
15662306a36Sopenharmony_ci#endif /* __mcf5200__ || __mcoldfire__ */
15762306a36Sopenharmony_ci	EXPORT_SYMBOL(__udivsi3)
158