xref: /openbsd/sys/arch/hppa/spmath/divsfm.c (revision c2feb252)
1 /*	$OpenBSD: divsfm.c,v 1.5 2002/05/07 22:19:30 mickey Exp $	*/
2 /*
3   (c) Copyright 1986 HEWLETT-PACKARD COMPANY
4   To anyone who acknowledges that this file is provided "AS IS"
5   without any express or implied warranty:
6       permission to use, copy, modify, and distribute this file
7   for any purpose is hereby granted without fee, provided that
8   the above copyright notice and this notice appears in all
9   copies, and that the name of Hewlett-Packard Company not be
10   used in advertising or publicity pertaining to distribution
11   of the software without specific, written prior permission.
12   Hewlett-Packard Company makes no representations about the
13   suitability of this software for any purpose.
14 */
15 /* @(#)divsfm.c: Revision: 1.6.88.1 Date: 93/12/07 15:05:51 */
16 
17 #include "md.h"
18 
19 void
divsfm(opnd1,opnd2,result)20 divsfm(opnd1,opnd2,result)
21 	int opnd1, opnd2;
22 	struct mdsfu_register *result;
23 {
24 	register int sign, op1_sign;
25 
26 	/* check divisor for zero */
27 	if (opnd2 == 0) {
28 		overflow = TRUE;
29 		return;
30 	}
31 
32 	/* get sign of result */
33 	sign = opnd1 ^ opnd2;
34 
35 	/* get absolute value of operands */
36 	if (opnd1 < 0) {
37 		opnd1 = -opnd1;
38 		op1_sign = TRUE;
39 	}
40 	else op1_sign = FALSE;
41 	if (opnd2 < 0) opnd2 = -opnd2;
42 
43 	/*
44 	 * check for overflow
45 	 *
46 	 * if abs(opnd1) < 0, then opnd1 = -2**31
47 	 * and abs(opnd1) >= abs(opnd2) always
48 	 */
49 	if (opnd1 >= opnd2 || opnd1 < 0) {
50 		/* check for opnd2 = -2**31 */
51 		if (opnd2 < 0 && opnd1 != opnd2) {
52 			result_hi = 0;		/* remainder = 0 */
53 			result_lo = opnd1 << 1;
54 		}
55 		else {
56 			overflow = TRUE;
57 			return;
58 		}
59 	}
60 	else {
61 		/* do the divide */
62 		divu(opnd1,0,opnd2,result);
63 
64 		/* return positive residue */
65 		if (op1_sign && result_hi) {
66 			result_hi = opnd2 - result_hi;
67 			result_lo++;
68 		}
69 	}
70 
71 	/* check for overflow */
72 	if (result_lo < 0) {
73 		overflow = TRUE;
74 		return;
75 	}
76 	overflow = FALSE;
77 
78 	/* return appropriately signed result */
79 	if (sign<0) result_lo = -result_lo;
80 	return;
81 }
82