xref: /linux/arch/mips/math-emu/sp_div.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /* IEEE754 floating point arithmetic
2  * single precision
3  */
4 /*
5  * MIPS floating point support
6  * Copyright (C) 1994-2000 Algorithmics Ltd.
7  *
8  *  This program is free software; you can distribute it and/or modify it
9  *  under the terms of the GNU General Public License (Version 2) as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope it will be useful, but WITHOUT
13  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  *  for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
20  */
21 
22 #include "ieee754sp.h"
23 
24 union ieee754sp ieee754sp_div(union ieee754sp x, union ieee754sp y)
25 {
26 	unsigned rm;
27 	int re;
28 	unsigned bm;
29 
30 	COMPXSP;
31 	COMPYSP;
32 
33 	EXPLODEXSP;
34 	EXPLODEYSP;
35 
36 	ieee754_clearcx();
37 
38 	FLUSHXSP;
39 	FLUSHYSP;
40 
41 	switch (CLPAIR(xc, yc)) {
42 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
43 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
44 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
45 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
46 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
47 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
48 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
49 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
50 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
51 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
52 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
53 		ieee754_setcx(IEEE754_INVALID_OPERATION);
54 		return ieee754sp_nanxcpt(ieee754sp_indef());
55 
56 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
57 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
58 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
59 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
60 		return y;
61 
62 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
63 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
64 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
65 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
66 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
67 		return x;
68 
69 
70 	/*
71 	 * Infinity handling
72 	 */
73 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
74 		ieee754_setcx(IEEE754_INVALID_OPERATION);
75 		return ieee754sp_indef();
76 
77 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
78 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
79 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
80 		return ieee754sp_zero(xs ^ ys);
81 
82 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
83 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
84 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
85 		return ieee754sp_inf(xs ^ ys);
86 
87 	/*
88 	 * Zero handling
89 	 */
90 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
91 		ieee754_setcx(IEEE754_INVALID_OPERATION);
92 		return ieee754sp_indef();
93 
94 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
95 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
96 		ieee754_setcx(IEEE754_ZERO_DIVIDE);
97 		return ieee754sp_inf(xs ^ ys);
98 
99 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
100 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
101 		return ieee754sp_zero(xs == ys ? 0 : 1);
102 
103 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
104 		SPDNORMX;
105 
106 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
107 		SPDNORMY;
108 		break;
109 
110 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
111 		SPDNORMX;
112 		break;
113 
114 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
115 		break;
116 	}
117 	assert(xm & SP_HIDDEN_BIT);
118 	assert(ym & SP_HIDDEN_BIT);
119 
120 	/* provide rounding space */
121 	xm <<= 3;
122 	ym <<= 3;
123 
124 	/* now the dirty work */
125 
126 	rm = 0;
127 	re = xe - ye;
128 
129 	for (bm = SP_MBIT(SP_FBITS + 2); bm; bm >>= 1) {
130 		if (xm >= ym) {
131 			xm -= ym;
132 			rm |= bm;
133 			if (xm == 0)
134 				break;
135 		}
136 		xm <<= 1;
137 	}
138 
139 	rm <<= 1;
140 	if (xm)
141 		rm |= 1;	/* have remainder, set sticky */
142 
143 	assert(rm);
144 
145 	/* normalise rm to rounding precision ?
146 	 */
147 	while ((rm >> (SP_FBITS + 3)) == 0) {
148 		rm <<= 1;
149 		re--;
150 	}
151 
152 	return ieee754sp_format(xs == ys ? 0 : 1, re, rm);
153 }
154