1/*	$NetBSD: divsfm.c,v 1.4 2007/02/22 05:46:29 thorpej Exp $	*/
2
3/*	$OpenBSD: divsfm.c,v 1.4 2001/03/29 03:58:17 mickey Exp $	*/
4
5/*
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
7 *              All Rights Reserved
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appears in all copies and
12 * that both the copyright notice and this permission notice appear in
13 * supporting documentation.
14 *
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 */
26/*
27 * pmk1.1
28 */
29/*
30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
31 *
32 * To anyone who acknowledges that this file is provided "AS IS"
33 * without any express or implied warranty:
34 *     permission to use, copy, modify, and distribute this file
35 * for any purpose is hereby granted without fee, provided that
36 * the above copyright notice and this notice appears in all
37 * copies, and that the name of Hewlett-Packard Company not be
38 * used in advertising or publicity pertaining to distribution
39 * of the software without specific, written prior permission.
40 * Hewlett-Packard Company makes no representations about the
41 * suitability of this software for any purpose.
42 */
43
44#include <sys/cdefs.h>
45__KERNEL_RCSID(0, "$NetBSD: divsfm.c,v 1.4 2007/02/22 05:46:29 thorpej Exp $");
46
47#include "md.h"
48
49void
50divsfm(int opnd1, int opnd2, struct mdsfu_register *result)
51{
52	register int sign, op1_sign;
53
54	/* check divisor for zero */
55	if (opnd2 == 0) {
56		overflow = true;
57		return;
58	}
59
60	/* get sign of result */
61	sign = opnd1 ^ opnd2;
62
63	/* get absolute value of operands */
64	if (opnd1 < 0) {
65		opnd1 = -opnd1;
66		op1_sign = true;
67	}
68	else op1_sign = false;
69	if (opnd2 < 0) opnd2 = -opnd2;
70
71	/*
72	 * check for overflow
73	 *
74	 * if abs(opnd1) < 0, then opnd1 = -2**31
75	 * and abs(opnd1) >= abs(opnd2) always
76	 */
77	if (opnd1 >= opnd2 || opnd1 < 0) {
78		/* check for opnd2 = -2**31 */
79		if (opnd2 < 0 && opnd1 != opnd2) {
80			result_hi = 0;		/* remainder = 0 */
81			result_lo = opnd1 << 1;
82		}
83		else {
84			overflow = true;
85			return;
86		}
87	}
88	else {
89		/* do the divide */
90		divu(opnd1,0,opnd2,result);
91
92		/* return positive residue */
93		if (op1_sign && result_hi) {
94			result_hi = opnd2 - result_hi;
95			result_lo++;
96		}
97	}
98
99	/* check for overflow */
100	if (result_lo < 0) {
101		overflow = true;
102		return;
103	}
104	overflow = false;
105
106	/* return appropriately signed result */
107	if (sign<0) result_lo = -result_lo;
108	return;
109}
110