xref: /linux/arch/mips/math-emu/dp_div.c (revision 3f7cac416b5e62d37aa693538729c6c23e9b938b)
11da177e4SLinus Torvalds /* IEEE754 floating point arithmetic
21da177e4SLinus Torvalds  * double precision: common utilities
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  * MIPS floating point support
61da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
91da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
101da177e4SLinus Torvalds  *  published by the Free Software Foundation.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
131da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
141da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
151da177e4SLinus Torvalds  *  for more details.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
181da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
19*3f7cac41SRalf Baechle  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "ieee754dp.h"
231da177e4SLinus Torvalds 
242209bcb1SRalf Baechle union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y)
251da177e4SLinus Torvalds {
26*3f7cac41SRalf Baechle 	u64 rm;
27*3f7cac41SRalf Baechle 	int re;
28*3f7cac41SRalf Baechle 	u64 bm;
29*3f7cac41SRalf Baechle 
301da177e4SLinus Torvalds 	COMPXDP;
311da177e4SLinus Torvalds 	COMPYDP;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	EXPLODEXDP;
341da177e4SLinus Torvalds 	EXPLODEYDP;
351da177e4SLinus Torvalds 
369e8bad1fSRalf Baechle 	ieee754_clearcx();
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	FLUSHXDP;
391da177e4SLinus Torvalds 	FLUSHYDP;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	switch (CLPAIR(xc, yc)) {
421da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
431da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
441da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
451da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
461da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
471da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
481da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
491da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
501da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
511da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
521da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
539e8bad1fSRalf Baechle 		ieee754_setcx(IEEE754_INVALID_OPERATION);
5490efba36SRalf Baechle 		return ieee754dp_nanxcpt(ieee754dp_indef());
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
571da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
581da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
591da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
601da177e4SLinus Torvalds 		return y;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
631da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
641da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
651da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
661da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
671da177e4SLinus Torvalds 		return x;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 
70*3f7cac41SRalf Baechle 	/*
71*3f7cac41SRalf Baechle 	 * Infinity handling
721da177e4SLinus Torvalds 	 */
731da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
749e8bad1fSRalf Baechle 		ieee754_setcx(IEEE754_INVALID_OPERATION);
7590efba36SRalf Baechle 		return ieee754dp_indef();
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
781da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
791da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
801da177e4SLinus Torvalds 		return ieee754dp_zero(xs ^ ys);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
831da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
841da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
851da177e4SLinus Torvalds 		return ieee754dp_inf(xs ^ ys);
861da177e4SLinus Torvalds 
87*3f7cac41SRalf Baechle 	/*
88*3f7cac41SRalf Baechle 	 * Zero handling
891da177e4SLinus Torvalds 	 */
901da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
919e8bad1fSRalf Baechle 		ieee754_setcx(IEEE754_INVALID_OPERATION);
9290efba36SRalf Baechle 		return ieee754dp_indef();
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
951da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
969e8bad1fSRalf Baechle 		ieee754_setcx(IEEE754_ZERO_DIVIDE);
9790efba36SRalf Baechle 		return ieee754dp_inf(xs ^ ys);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
1001da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
1011da177e4SLinus Torvalds 		return ieee754dp_zero(xs == ys ? 0 : 1);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
1041da177e4SLinus Torvalds 		DPDNORMX;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
1071da177e4SLinus Torvalds 		DPDNORMY;
1081da177e4SLinus Torvalds 		break;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
1111da177e4SLinus Torvalds 		DPDNORMX;
1121da177e4SLinus Torvalds 		break;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
1151da177e4SLinus Torvalds 		break;
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 	assert(xm & DP_HIDDEN_BIT);
1181da177e4SLinus Torvalds 	assert(ym & DP_HIDDEN_BIT);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/* provide rounding space */
1211da177e4SLinus Torvalds 	xm <<= 3;
1221da177e4SLinus Torvalds 	ym <<= 3;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/* now the dirty work */
1251da177e4SLinus Torvalds 
126*3f7cac41SRalf Baechle 	rm = 0;
127*3f7cac41SRalf Baechle 	re = xe - ye;
1281da177e4SLinus Torvalds 
129ad8fb553SRalf Baechle 	for (bm = DP_MBIT(DP_FBITS + 2); bm; bm >>= 1) {
1301da177e4SLinus Torvalds 		if (xm >= ym) {
1311da177e4SLinus Torvalds 			xm -= ym;
1321da177e4SLinus Torvalds 			rm |= bm;
1331da177e4SLinus Torvalds 			if (xm == 0)
1341da177e4SLinus Torvalds 				break;
1351da177e4SLinus Torvalds 		}
1361da177e4SLinus Torvalds 		xm <<= 1;
1371da177e4SLinus Torvalds 	}
138*3f7cac41SRalf Baechle 
1391da177e4SLinus Torvalds 	rm <<= 1;
1401da177e4SLinus Torvalds 	if (xm)
1411da177e4SLinus Torvalds 		rm |= 1;	/* have remainder, set sticky */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	assert(rm);
1441da177e4SLinus Torvalds 
145*3f7cac41SRalf Baechle 	/*
146*3f7cac41SRalf Baechle 	 * Normalise rm to rounding precision ?
1471da177e4SLinus Torvalds 	 */
148ad8fb553SRalf Baechle 	while ((rm >> (DP_FBITS + 3)) == 0) {
1491da177e4SLinus Torvalds 		rm <<= 1;
1501da177e4SLinus Torvalds 		re--;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
15390efba36SRalf Baechle 	return ieee754dp_format(xs == ys ? 0 : 1, re, rm);
1541da177e4SLinus Torvalds }
155