xref: /openbmc/linux/arch/m68k/math-emu/fp_arith.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
16253c22eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds    fp_arith.c: floating-point math routines for the Linux-m68k
51da177e4SLinus Torvalds    floating point emulator.
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds    Copyright (c) 1998-1999 David Huggins-Daines.
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds    Somewhat based on the AlphaLinux floating point emulator, by David
101da177e4SLinus Torvalds    Mosberger-Tang.
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include "fp_emu.h"
151da177e4SLinus Torvalds #include "multi_arith.h"
161da177e4SLinus Torvalds #include "fp_arith.h"
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds const struct fp_ext fp_QNaN =
191da177e4SLinus Torvalds {
201da177e4SLinus Torvalds 	.exp = 0x7fff,
211da177e4SLinus Torvalds 	.mant = { .m64 = ~0 }
221da177e4SLinus Torvalds };
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds const struct fp_ext fp_Inf =
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds 	.exp = 0x7fff,
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /* let's start with the easy ones */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds struct fp_ext *
fp_fabs(struct fp_ext * dest,struct fp_ext * src)321da177e4SLinus Torvalds fp_fabs(struct fp_ext *dest, struct fp_ext *src)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	dprint(PINSTR, "fabs\n");
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	fp_monadic_check(dest, src);
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	dest->sign = 0;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	return dest;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds struct fp_ext *
fp_fneg(struct fp_ext * dest,struct fp_ext * src)441da177e4SLinus Torvalds fp_fneg(struct fp_ext *dest, struct fp_ext *src)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	dprint(PINSTR, "fneg\n");
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	fp_monadic_check(dest, src);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	dest->sign = !dest->sign;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	return dest;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* Now, the slightly harder ones */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* fp_fadd: Implements the kernel of the FADD, FSADD, FDADD, FSUB,
581da177e4SLinus Torvalds    FDSUB, and FCMP instructions. */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds struct fp_ext *
fp_fadd(struct fp_ext * dest,struct fp_ext * src)611da177e4SLinus Torvalds fp_fadd(struct fp_ext *dest, struct fp_ext *src)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	int diff;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	dprint(PINSTR, "fadd\n");
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if (IS_INF(dest)) {
701da177e4SLinus Torvalds 		/* infinity - infinity == NaN */
711da177e4SLinus Torvalds 		if (IS_INF(src) && (src->sign != dest->sign))
721da177e4SLinus Torvalds 			fp_set_nan(dest);
731da177e4SLinus Torvalds 		return dest;
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 	if (IS_INF(src)) {
761da177e4SLinus Torvalds 		fp_copy_ext(dest, src);
771da177e4SLinus Torvalds 		return dest;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	if (IS_ZERO(dest)) {
811da177e4SLinus Torvalds 		if (IS_ZERO(src)) {
821da177e4SLinus Torvalds 			if (src->sign != dest->sign) {
831da177e4SLinus Torvalds 				if (FPDATA->rnd == FPCR_ROUND_RM)
841da177e4SLinus Torvalds 					dest->sign = 1;
851da177e4SLinus Torvalds 				else
861da177e4SLinus Torvalds 					dest->sign = 0;
871da177e4SLinus Torvalds 			}
881da177e4SLinus Torvalds 		} else
891da177e4SLinus Torvalds 			fp_copy_ext(dest, src);
901da177e4SLinus Torvalds 		return dest;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	dest->lowmant = src->lowmant = 0;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if ((diff = dest->exp - src->exp) > 0)
961da177e4SLinus Torvalds 		fp_denormalize(src, diff);
971da177e4SLinus Torvalds 	else if ((diff = -diff) > 0)
981da177e4SLinus Torvalds 		fp_denormalize(dest, diff);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if (dest->sign == src->sign) {
1011da177e4SLinus Torvalds 		if (fp_addmant(dest, src))
1021da177e4SLinus Torvalds 			if (!fp_addcarry(dest))
1031da177e4SLinus Torvalds 				return dest;
1041da177e4SLinus Torvalds 	} else {
1051da177e4SLinus Torvalds 		if (dest->mant.m64 < src->mant.m64) {
1061da177e4SLinus Torvalds 			fp_submant(dest, src, dest);
1071da177e4SLinus Torvalds 			dest->sign = !dest->sign;
1081da177e4SLinus Torvalds 		} else
1091da177e4SLinus Torvalds 			fp_submant(dest, dest, src);
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return dest;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* fp_fsub: Implements the kernel of the FSUB, FSSUB, and FDSUB
1161da177e4SLinus Torvalds    instructions.
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds    Remember that the arguments are in assembler-syntax order! */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds struct fp_ext *
fp_fsub(struct fp_ext * dest,struct fp_ext * src)1211da177e4SLinus Torvalds fp_fsub(struct fp_ext *dest, struct fp_ext *src)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	dprint(PINSTR, "fsub ");
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	src->sign = !src->sign;
1261da177e4SLinus Torvalds 	return fp_fadd(dest, src);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds struct fp_ext *
fp_fcmp(struct fp_ext * dest,struct fp_ext * src)1311da177e4SLinus Torvalds fp_fcmp(struct fp_ext *dest, struct fp_ext *src)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	dprint(PINSTR, "fcmp ");
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	FPDATA->temp[1] = *dest;
1361da177e4SLinus Torvalds 	src->sign = !src->sign;
1371da177e4SLinus Torvalds 	return fp_fadd(&FPDATA->temp[1], src);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds struct fp_ext *
fp_ftst(struct fp_ext * dest,struct fp_ext * src)1411da177e4SLinus Torvalds fp_ftst(struct fp_ext *dest, struct fp_ext *src)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	dprint(PINSTR, "ftst\n");
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	(void)dest;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	return src;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds struct fp_ext *
fp_fmul(struct fp_ext * dest,struct fp_ext * src)1511da177e4SLinus Torvalds fp_fmul(struct fp_ext *dest, struct fp_ext *src)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	union fp_mant128 temp;
1541da177e4SLinus Torvalds 	int exp;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	dprint(PINSTR, "fmul\n");
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* calculate the correct sign now, as it's necessary for infinities */
1611da177e4SLinus Torvalds 	dest->sign = src->sign ^ dest->sign;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	/* Handle infinities */
1641da177e4SLinus Torvalds 	if (IS_INF(dest)) {
1651da177e4SLinus Torvalds 		if (IS_ZERO(src))
1661da177e4SLinus Torvalds 			fp_set_nan(dest);
1671da177e4SLinus Torvalds 		return dest;
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	if (IS_INF(src)) {
1701da177e4SLinus Torvalds 		if (IS_ZERO(dest))
1711da177e4SLinus Torvalds 			fp_set_nan(dest);
1721da177e4SLinus Torvalds 		else
1731da177e4SLinus Torvalds 			fp_copy_ext(dest, src);
1741da177e4SLinus Torvalds 		return dest;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* Of course, as we all know, zero * anything = zero.  You may
1781da177e4SLinus Torvalds 	   not have known that it might be a positive or negative
1791da177e4SLinus Torvalds 	   zero... */
1801da177e4SLinus Torvalds 	if (IS_ZERO(dest) || IS_ZERO(src)) {
1811da177e4SLinus Torvalds 		dest->exp = 0;
1821da177e4SLinus Torvalds 		dest->mant.m64 = 0;
1831da177e4SLinus Torvalds 		dest->lowmant = 0;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 		return dest;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	exp = dest->exp + src->exp - 0x3ffe;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/* shift up the mantissa for denormalized numbers,
1911da177e4SLinus Torvalds 	   so that the highest bit is set, this makes the
1921da177e4SLinus Torvalds 	   shift of the result below easier */
1931da177e4SLinus Torvalds 	if ((long)dest->mant.m32[0] >= 0)
1941da177e4SLinus Torvalds 		exp -= fp_overnormalize(dest);
1951da177e4SLinus Torvalds 	if ((long)src->mant.m32[0] >= 0)
1961da177e4SLinus Torvalds 		exp -= fp_overnormalize(src);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* now, do a 64-bit multiply with expansion */
1991da177e4SLinus Torvalds 	fp_multiplymant(&temp, dest, src);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* normalize it back to 64 bits and stuff it back into the
2021da177e4SLinus Torvalds 	   destination struct */
2031da177e4SLinus Torvalds 	if ((long)temp.m32[0] > 0) {
2041da177e4SLinus Torvalds 		exp--;
2051da177e4SLinus Torvalds 		fp_putmant128(dest, &temp, 1);
2061da177e4SLinus Torvalds 	} else
2071da177e4SLinus Torvalds 		fp_putmant128(dest, &temp, 0);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	if (exp >= 0x7fff) {
2101da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
2111da177e4SLinus Torvalds 		return dest;
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 	dest->exp = exp;
2141da177e4SLinus Torvalds 	if (exp < 0) {
2151da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_UNFL);
2161da177e4SLinus Torvalds 		fp_denormalize(dest, -exp);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	return dest;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* fp_fdiv: Implements the "kernel" of the FDIV, FSDIV, FDDIV and
2231da177e4SLinus Torvalds    FSGLDIV instructions.
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds    Note that the order of the operands is counter-intuitive: instead
2261da177e4SLinus Torvalds    of src / dest, the result is actually dest / src. */
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds struct fp_ext *
fp_fdiv(struct fp_ext * dest,struct fp_ext * src)2291da177e4SLinus Torvalds fp_fdiv(struct fp_ext *dest, struct fp_ext *src)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	union fp_mant128 temp;
2321da177e4SLinus Torvalds 	int exp;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	dprint(PINSTR, "fdiv\n");
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* calculate the correct sign now, as it's necessary for infinities */
2391da177e4SLinus Torvalds 	dest->sign = src->sign ^ dest->sign;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* Handle infinities */
2421da177e4SLinus Torvalds 	if (IS_INF(dest)) {
2431da177e4SLinus Torvalds 		/* infinity / infinity = NaN (quiet, as always) */
2441da177e4SLinus Torvalds 		if (IS_INF(src))
2451da177e4SLinus Torvalds 			fp_set_nan(dest);
246*4fbdb1a9SJulia Lawall 		/* infinity / anything else = infinity (with appropriate sign) */
2471da177e4SLinus Torvalds 		return dest;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	if (IS_INF(src)) {
2501da177e4SLinus Torvalds 		/* anything / infinity = zero (with appropriate sign) */
2511da177e4SLinus Torvalds 		dest->exp = 0;
2521da177e4SLinus Torvalds 		dest->mant.m64 = 0;
2531da177e4SLinus Torvalds 		dest->lowmant = 0;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		return dest;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* zeroes */
2591da177e4SLinus Torvalds 	if (IS_ZERO(dest)) {
2601da177e4SLinus Torvalds 		/* zero / zero = NaN */
2611da177e4SLinus Torvalds 		if (IS_ZERO(src))
2621da177e4SLinus Torvalds 			fp_set_nan(dest);
2631da177e4SLinus Torvalds 		/* zero / anything else = zero */
2641da177e4SLinus Torvalds 		return dest;
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 	if (IS_ZERO(src)) {
2671da177e4SLinus Torvalds 		/* anything / zero = infinity (with appropriate sign) */
2681da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_DZ);
2691da177e4SLinus Torvalds 		dest->exp = 0x7fff;
2701da177e4SLinus Torvalds 		dest->mant.m64 = 0;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		return dest;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	exp = dest->exp - src->exp + 0x3fff;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	/* shift up the mantissa for denormalized numbers,
2781da177e4SLinus Torvalds 	   so that the highest bit is set, this makes lots
2791da177e4SLinus Torvalds 	   of things below easier */
2801da177e4SLinus Torvalds 	if ((long)dest->mant.m32[0] >= 0)
2811da177e4SLinus Torvalds 		exp -= fp_overnormalize(dest);
2821da177e4SLinus Torvalds 	if ((long)src->mant.m32[0] >= 0)
2831da177e4SLinus Torvalds 		exp -= fp_overnormalize(src);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* now, do the 64-bit divide */
2861da177e4SLinus Torvalds 	fp_dividemant(&temp, dest, src);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* normalize it back to 64 bits and stuff it back into the
2891da177e4SLinus Torvalds 	   destination struct */
2901da177e4SLinus Torvalds 	if (!temp.m32[0]) {
2911da177e4SLinus Torvalds 		exp--;
2921da177e4SLinus Torvalds 		fp_putmant128(dest, &temp, 32);
2931da177e4SLinus Torvalds 	} else
2941da177e4SLinus Torvalds 		fp_putmant128(dest, &temp, 31);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (exp >= 0x7fff) {
2971da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
2981da177e4SLinus Torvalds 		return dest;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	dest->exp = exp;
3011da177e4SLinus Torvalds 	if (exp < 0) {
3021da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_UNFL);
3031da177e4SLinus Torvalds 		fp_denormalize(dest, -exp);
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	return dest;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds struct fp_ext *
fp_fsglmul(struct fp_ext * dest,struct fp_ext * src)3101da177e4SLinus Torvalds fp_fsglmul(struct fp_ext *dest, struct fp_ext *src)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int exp;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	dprint(PINSTR, "fsglmul\n");
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	/* calculate the correct sign now, as it's necessary for infinities */
3191da177e4SLinus Torvalds 	dest->sign = src->sign ^ dest->sign;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* Handle infinities */
3221da177e4SLinus Torvalds 	if (IS_INF(dest)) {
3231da177e4SLinus Torvalds 		if (IS_ZERO(src))
3241da177e4SLinus Torvalds 			fp_set_nan(dest);
3251da177e4SLinus Torvalds 		return dest;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 	if (IS_INF(src)) {
3281da177e4SLinus Torvalds 		if (IS_ZERO(dest))
3291da177e4SLinus Torvalds 			fp_set_nan(dest);
3301da177e4SLinus Torvalds 		else
3311da177e4SLinus Torvalds 			fp_copy_ext(dest, src);
3321da177e4SLinus Torvalds 		return dest;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Of course, as we all know, zero * anything = zero.  You may
3361da177e4SLinus Torvalds 	   not have known that it might be a positive or negative
3371da177e4SLinus Torvalds 	   zero... */
3381da177e4SLinus Torvalds 	if (IS_ZERO(dest) || IS_ZERO(src)) {
3391da177e4SLinus Torvalds 		dest->exp = 0;
3401da177e4SLinus Torvalds 		dest->mant.m64 = 0;
3411da177e4SLinus Torvalds 		dest->lowmant = 0;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		return dest;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	exp = dest->exp + src->exp - 0x3ffe;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	/* do a 32-bit multiply */
3491da177e4SLinus Torvalds 	fp_mul64(dest->mant.m32[0], dest->mant.m32[1],
3501da177e4SLinus Torvalds 		 dest->mant.m32[0] & 0xffffff00,
3511da177e4SLinus Torvalds 		 src->mant.m32[0] & 0xffffff00);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	if (exp >= 0x7fff) {
3541da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
3551da177e4SLinus Torvalds 		return dest;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 	dest->exp = exp;
3581da177e4SLinus Torvalds 	if (exp < 0) {
3591da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_UNFL);
3601da177e4SLinus Torvalds 		fp_denormalize(dest, -exp);
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	return dest;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds struct fp_ext *
fp_fsgldiv(struct fp_ext * dest,struct fp_ext * src)3671da177e4SLinus Torvalds fp_fsgldiv(struct fp_ext *dest, struct fp_ext *src)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	int exp;
3701da177e4SLinus Torvalds 	unsigned long quot, rem;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	dprint(PINSTR, "fsgldiv\n");
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	/* calculate the correct sign now, as it's necessary for infinities */
3771da177e4SLinus Torvalds 	dest->sign = src->sign ^ dest->sign;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Handle infinities */
3801da177e4SLinus Torvalds 	if (IS_INF(dest)) {
3811da177e4SLinus Torvalds 		/* infinity / infinity = NaN (quiet, as always) */
3821da177e4SLinus Torvalds 		if (IS_INF(src))
3831da177e4SLinus Torvalds 			fp_set_nan(dest);
3841da177e4SLinus Torvalds 		/* infinity / anything else = infinity (with approprate sign) */
3851da177e4SLinus Torvalds 		return dest;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	if (IS_INF(src)) {
3881da177e4SLinus Torvalds 		/* anything / infinity = zero (with appropriate sign) */
3891da177e4SLinus Torvalds 		dest->exp = 0;
3901da177e4SLinus Torvalds 		dest->mant.m64 = 0;
3911da177e4SLinus Torvalds 		dest->lowmant = 0;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		return dest;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	/* zeroes */
3971da177e4SLinus Torvalds 	if (IS_ZERO(dest)) {
3981da177e4SLinus Torvalds 		/* zero / zero = NaN */
3991da177e4SLinus Torvalds 		if (IS_ZERO(src))
4001da177e4SLinus Torvalds 			fp_set_nan(dest);
4011da177e4SLinus Torvalds 		/* zero / anything else = zero */
4021da177e4SLinus Torvalds 		return dest;
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 	if (IS_ZERO(src)) {
4051da177e4SLinus Torvalds 		/* anything / zero = infinity (with appropriate sign) */
4061da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_DZ);
4071da177e4SLinus Torvalds 		dest->exp = 0x7fff;
4081da177e4SLinus Torvalds 		dest->mant.m64 = 0;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		return dest;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	exp = dest->exp - src->exp + 0x3fff;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	dest->mant.m32[0] &= 0xffffff00;
4161da177e4SLinus Torvalds 	src->mant.m32[0] &= 0xffffff00;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/* do the 32-bit divide */
4191da177e4SLinus Torvalds 	if (dest->mant.m32[0] >= src->mant.m32[0]) {
4201da177e4SLinus Torvalds 		fp_sub64(dest->mant, src->mant);
4211da177e4SLinus Torvalds 		fp_div64(quot, rem, dest->mant.m32[0], 0, src->mant.m32[0]);
4221da177e4SLinus Torvalds 		dest->mant.m32[0] = 0x80000000 | (quot >> 1);
4231da177e4SLinus Torvalds 		dest->mant.m32[1] = (quot & 1) | rem;	/* only for rounding */
4241da177e4SLinus Torvalds 	} else {
4251da177e4SLinus Torvalds 		fp_div64(quot, rem, dest->mant.m32[0], 0, src->mant.m32[0]);
4261da177e4SLinus Torvalds 		dest->mant.m32[0] = quot;
4271da177e4SLinus Torvalds 		dest->mant.m32[1] = rem;		/* only for rounding */
4281da177e4SLinus Torvalds 		exp--;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	if (exp >= 0x7fff) {
4321da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
4331da177e4SLinus Torvalds 		return dest;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 	dest->exp = exp;
4361da177e4SLinus Torvalds 	if (exp < 0) {
4371da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_UNFL);
4381da177e4SLinus Torvalds 		fp_denormalize(dest, -exp);
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	return dest;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /* fp_roundint: Internal rounding function for use by several of these
4451da177e4SLinus Torvalds    emulated instructions.
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds    This one rounds off the fractional part using the rounding mode
4481da177e4SLinus Torvalds    specified. */
4491da177e4SLinus Torvalds 
fp_roundint(struct fp_ext * dest,int mode)4501da177e4SLinus Torvalds static void fp_roundint(struct fp_ext *dest, int mode)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	union fp_mant64 oldmant;
4531da177e4SLinus Torvalds 	unsigned long mask;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (!fp_normalize_ext(dest))
4561da177e4SLinus Torvalds 		return;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/* infinities and zeroes */
4591da177e4SLinus Torvalds 	if (IS_INF(dest) || IS_ZERO(dest))
4601da177e4SLinus Torvalds 		return;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	/* first truncate the lower bits */
4631da177e4SLinus Torvalds 	oldmant = dest->mant;
4641da177e4SLinus Torvalds 	switch (dest->exp) {
4651da177e4SLinus Torvalds 	case 0 ... 0x3ffe:
4661da177e4SLinus Torvalds 		dest->mant.m64 = 0;
4671da177e4SLinus Torvalds 		break;
4681da177e4SLinus Torvalds 	case 0x3fff ... 0x401e:
4691da177e4SLinus Torvalds 		dest->mant.m32[0] &= 0xffffffffU << (0x401e - dest->exp);
4701da177e4SLinus Torvalds 		dest->mant.m32[1] = 0;
4711da177e4SLinus Torvalds 		if (oldmant.m64 == dest->mant.m64)
4721da177e4SLinus Torvalds 			return;
4731da177e4SLinus Torvalds 		break;
4741da177e4SLinus Torvalds 	case 0x401f ... 0x403e:
4751da177e4SLinus Torvalds 		dest->mant.m32[1] &= 0xffffffffU << (0x403e - dest->exp);
4761da177e4SLinus Torvalds 		if (oldmant.m32[1] == dest->mant.m32[1])
4771da177e4SLinus Torvalds 			return;
4781da177e4SLinus Torvalds 		break;
4791da177e4SLinus Torvalds 	default:
4801da177e4SLinus Torvalds 		return;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	fp_set_sr(FPSR_EXC_INEX2);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	/* We might want to normalize upwards here... however, since
4851da177e4SLinus Torvalds 	   we know that this is only called on the output of fp_fdiv,
4861da177e4SLinus Torvalds 	   or with the input to fp_fint or fp_fintrz, and the inputs
4871da177e4SLinus Torvalds 	   to all these functions are either normal or denormalized
4881da177e4SLinus Torvalds 	   (no subnormals allowed!), there's really no need.
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	   In the case of fp_fdiv, observe that 0x80000000 / 0xffff =
4911da177e4SLinus Torvalds 	   0xffff8000, and the same holds for 128-bit / 64-bit. (i.e. the
4921da177e4SLinus Torvalds 	   smallest possible normal dividend and the largest possible normal
4931da177e4SLinus Torvalds 	   divisor will still produce a normal quotient, therefore, (normal
4941da177e4SLinus Torvalds 	   << 64) / normal is normal in all cases) */
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	switch (mode) {
4971da177e4SLinus Torvalds 	case FPCR_ROUND_RN:
4981da177e4SLinus Torvalds 		switch (dest->exp) {
4991da177e4SLinus Torvalds 		case 0 ... 0x3ffd:
5001da177e4SLinus Torvalds 			return;
5011da177e4SLinus Torvalds 		case 0x3ffe:
5021da177e4SLinus Torvalds 			/* As noted above, the input is always normal, so the
5031da177e4SLinus Torvalds 			   guard bit (bit 63) is always set.  therefore, the
5041da177e4SLinus Torvalds 			   only case in which we will NOT round to 1.0 is when
5051da177e4SLinus Torvalds 			   the input is exactly 0.5. */
5061da177e4SLinus Torvalds 			if (oldmant.m64 == (1ULL << 63))
5071da177e4SLinus Torvalds 				return;
5081da177e4SLinus Torvalds 			break;
5091da177e4SLinus Torvalds 		case 0x3fff ... 0x401d:
5101da177e4SLinus Torvalds 			mask = 1 << (0x401d - dest->exp);
5111da177e4SLinus Torvalds 			if (!(oldmant.m32[0] & mask))
5121da177e4SLinus Torvalds 				return;
5131da177e4SLinus Torvalds 			if (oldmant.m32[0] & (mask << 1))
5141da177e4SLinus Torvalds 				break;
5151da177e4SLinus Torvalds 			if (!(oldmant.m32[0] << (dest->exp - 0x3ffd)) &&
5161da177e4SLinus Torvalds 					!oldmant.m32[1])
5171da177e4SLinus Torvalds 				return;
5181da177e4SLinus Torvalds 			break;
5191da177e4SLinus Torvalds 		case 0x401e:
520ddc2fc2cSChen Gang 			if (oldmant.m32[1] & 0x80000000)
5211da177e4SLinus Torvalds 				return;
5221da177e4SLinus Torvalds 			if (oldmant.m32[0] & 1)
5231da177e4SLinus Torvalds 				break;
5241da177e4SLinus Torvalds 			if (!(oldmant.m32[1] << 1))
5251da177e4SLinus Torvalds 				return;
5261da177e4SLinus Torvalds 			break;
5271da177e4SLinus Torvalds 		case 0x401f ... 0x403d:
5281da177e4SLinus Torvalds 			mask = 1 << (0x403d - dest->exp);
5291da177e4SLinus Torvalds 			if (!(oldmant.m32[1] & mask))
5301da177e4SLinus Torvalds 				return;
5311da177e4SLinus Torvalds 			if (oldmant.m32[1] & (mask << 1))
5321da177e4SLinus Torvalds 				break;
5331da177e4SLinus Torvalds 			if (!(oldmant.m32[1] << (dest->exp - 0x401d)))
5341da177e4SLinus Torvalds 				return;
5351da177e4SLinus Torvalds 			break;
5361da177e4SLinus Torvalds 		default:
5371da177e4SLinus Torvalds 			return;
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 		break;
5401da177e4SLinus Torvalds 	case FPCR_ROUND_RZ:
5411da177e4SLinus Torvalds 		return;
5421da177e4SLinus Torvalds 	default:
5431da177e4SLinus Torvalds 		if (dest->sign ^ (mode - FPCR_ROUND_RM))
5441da177e4SLinus Torvalds 			break;
5451da177e4SLinus Torvalds 		return;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	switch (dest->exp) {
5491da177e4SLinus Torvalds 	case 0 ... 0x3ffe:
5501da177e4SLinus Torvalds 		dest->exp = 0x3fff;
5511da177e4SLinus Torvalds 		dest->mant.m64 = 1ULL << 63;
5521da177e4SLinus Torvalds 		break;
5531da177e4SLinus Torvalds 	case 0x3fff ... 0x401e:
5541da177e4SLinus Torvalds 		mask = 1 << (0x401e - dest->exp);
5551da177e4SLinus Torvalds 		if (dest->mant.m32[0] += mask)
5561da177e4SLinus Torvalds 			break;
5571da177e4SLinus Torvalds 		dest->mant.m32[0] = 0x80000000;
5581da177e4SLinus Torvalds 		dest->exp++;
5591da177e4SLinus Torvalds 		break;
5601da177e4SLinus Torvalds 	case 0x401f ... 0x403e:
5611da177e4SLinus Torvalds 		mask = 1 << (0x403e - dest->exp);
5621da177e4SLinus Torvalds 		if (dest->mant.m32[1] += mask)
5631da177e4SLinus Torvalds 			break;
5641da177e4SLinus Torvalds 		if (dest->mant.m32[0] += 1)
5651da177e4SLinus Torvalds                         break;
5661da177e4SLinus Torvalds 		dest->mant.m32[0] = 0x80000000;
5671da177e4SLinus Torvalds                 dest->exp++;
5681da177e4SLinus Torvalds 		break;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /* modrem_kernel: Implementation of the FREM and FMOD instructions
5731da177e4SLinus Torvalds    (which are exactly the same, except for the rounding used on the
5741da177e4SLinus Torvalds    intermediate value) */
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds static struct fp_ext *
modrem_kernel(struct fp_ext * dest,struct fp_ext * src,int mode)5771da177e4SLinus Torvalds modrem_kernel(struct fp_ext *dest, struct fp_ext *src, int mode)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct fp_ext tmp;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/* Infinities and zeros */
5841da177e4SLinus Torvalds 	if (IS_INF(dest) || IS_ZERO(src)) {
5851da177e4SLinus Torvalds 		fp_set_nan(dest);
5861da177e4SLinus Torvalds 		return dest;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 	if (IS_ZERO(dest) || IS_INF(src))
5891da177e4SLinus Torvalds 		return dest;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/* FIXME: there is almost certainly a smarter way to do this */
5921da177e4SLinus Torvalds 	fp_copy_ext(&tmp, dest);
5931da177e4SLinus Torvalds 	fp_fdiv(&tmp, src);		/* NOTE: src might be modified */
5941da177e4SLinus Torvalds 	fp_roundint(&tmp, mode);
5951da177e4SLinus Torvalds 	fp_fmul(&tmp, src);
5961da177e4SLinus Torvalds 	fp_fsub(dest, &tmp);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/* set the quotient byte */
5991da177e4SLinus Torvalds 	fp_set_quotient((dest->mant.m64 & 0x7f) | (dest->sign << 7));
6001da177e4SLinus Torvalds 	return dest;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /* fp_fmod: Implements the kernel of the FMOD instruction.
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds    Again, the argument order is backwards.  The result, as defined in
6061da177e4SLinus Torvalds    the Motorola manuals, is:
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds    fmod(src,dest) = (dest - (src * floor(dest / src))) */
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds struct fp_ext *
fp_fmod(struct fp_ext * dest,struct fp_ext * src)6111da177e4SLinus Torvalds fp_fmod(struct fp_ext *dest, struct fp_ext *src)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	dprint(PINSTR, "fmod\n");
6141da177e4SLinus Torvalds 	return modrem_kernel(dest, src, FPCR_ROUND_RZ);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /* fp_frem: Implements the kernel of the FREM instruction.
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds    frem(src,dest) = (dest - (src * round(dest / src)))
6201da177e4SLinus Torvalds  */
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds struct fp_ext *
fp_frem(struct fp_ext * dest,struct fp_ext * src)6231da177e4SLinus Torvalds fp_frem(struct fp_ext *dest, struct fp_ext *src)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	dprint(PINSTR, "frem\n");
6261da177e4SLinus Torvalds 	return modrem_kernel(dest, src, FPCR_ROUND_RN);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds struct fp_ext *
fp_fint(struct fp_ext * dest,struct fp_ext * src)6301da177e4SLinus Torvalds fp_fint(struct fp_ext *dest, struct fp_ext *src)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	dprint(PINSTR, "fint\n");
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	fp_copy_ext(dest, src);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	fp_roundint(dest, FPDATA->rnd);
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	return dest;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds struct fp_ext *
fp_fintrz(struct fp_ext * dest,struct fp_ext * src)6421da177e4SLinus Torvalds fp_fintrz(struct fp_ext *dest, struct fp_ext *src)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	dprint(PINSTR, "fintrz\n");
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	fp_copy_ext(dest, src);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	fp_roundint(dest, FPCR_ROUND_RZ);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	return dest;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds struct fp_ext *
fp_fscale(struct fp_ext * dest,struct fp_ext * src)6541da177e4SLinus Torvalds fp_fscale(struct fp_ext *dest, struct fp_ext *src)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	int scale, oldround;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	dprint(PINSTR, "fscale\n");
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	fp_dyadic_check(dest, src);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/* Infinities */
6631da177e4SLinus Torvalds 	if (IS_INF(src)) {
6641da177e4SLinus Torvalds 		fp_set_nan(dest);
6651da177e4SLinus Torvalds 		return dest;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	if (IS_INF(dest))
6681da177e4SLinus Torvalds 		return dest;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	/* zeroes */
6711da177e4SLinus Torvalds 	if (IS_ZERO(src) || IS_ZERO(dest))
6721da177e4SLinus Torvalds 		return dest;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* Source exponent out of range */
6751da177e4SLinus Torvalds 	if (src->exp >= 0x400c) {
6761da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
6771da177e4SLinus Torvalds 		return dest;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	/* src must be rounded with round to zero. */
6811da177e4SLinus Torvalds 	oldround = FPDATA->rnd;
6821da177e4SLinus Torvalds 	FPDATA->rnd = FPCR_ROUND_RZ;
6831da177e4SLinus Torvalds 	scale = fp_conv_ext2long(src);
6841da177e4SLinus Torvalds 	FPDATA->rnd = oldround;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	/* new exponent */
6871da177e4SLinus Torvalds 	scale += dest->exp;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if (scale >= 0x7fff) {
6901da177e4SLinus Torvalds 		fp_set_ovrflw(dest);
6911da177e4SLinus Torvalds 	} else if (scale <= 0) {
6921da177e4SLinus Torvalds 		fp_set_sr(FPSR_EXC_UNFL);
6931da177e4SLinus Torvalds 		fp_denormalize(dest, -scale);
6941da177e4SLinus Torvalds 	} else
6951da177e4SLinus Torvalds 		dest->exp = scale;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	return dest;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
700