ieee754dp.c 4.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* IEEE754 floating point arithmetic
 * double precision: common utilities
 */
/*
 * MIPS floating point support
 * Copyright (C) 1994-2000 Algorithmics Ltd.
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
19
 *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
L
Linus Torvalds 已提交
20 21
 */

22
#include <linux/compiler.h>
L
Linus Torvalds 已提交
23 24 25

#include "ieee754dp.h"

26
int ieee754dp_class(union ieee754dp x)
L
Linus Torvalds 已提交
27 28 29 30 31 32
{
	COMPXDP;
	EXPLODEXDP;
	return xc;
}

33
int ieee754dp_isnan(union ieee754dp x)
L
Linus Torvalds 已提交
34 35 36 37
{
	return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
}

38
static inline int ieee754dp_issnan(union ieee754dp x)
L
Linus Torvalds 已提交
39 40
{
	assert(ieee754dp_isnan(x));
R
Ralf Baechle 已提交
41
	return (DPMANT(x) & DP_MBIT(DP_FBITS - 1)) == DP_MBIT(DP_FBITS - 1);
L
Linus Torvalds 已提交
42 43 44
}


45
union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r)
L
Linus Torvalds 已提交
46 47 48 49 50 51
{
	assert(ieee754dp_isnan(r));

	if (!ieee754dp_issnan(r))	/* QNAN does not cause invalid op !! */
		return r;

52
	if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
L
Linus Torvalds 已提交
53
		/* not enabled convert to a quiet NaN */
54
		DPMANT(r) &= (~DP_MBIT(DP_FBITS-1));
L
Linus Torvalds 已提交
55 56 57 58 59 60
		if (ieee754dp_isnan(r))
			return r;
		else
			return ieee754dp_indef();
	}

61
	return r;
L
Linus Torvalds 已提交
62 63
}

64
static u64 ieee754dp_get_rounding(int sn, u64 xm)
L
Linus Torvalds 已提交
65 66 67 68 69
{
	/* inexact must round of 3 bits
	 */
	if (xm & (DP_MBIT(3) - 1)) {
		switch (ieee754_csr.rm) {
70
		case FPU_CSR_RZ:
L
Linus Torvalds 已提交
71
			break;
72
		case FPU_CSR_RN:
L
Linus Torvalds 已提交
73 74 75
			xm += 0x3 + ((xm >> 3) & 1);
			/* xm += (xm&0x8)?0x4:0x3 */
			break;
76
		case FPU_CSR_RU:	/* toward +Infinity */
L
Linus Torvalds 已提交
77 78 79
			if (!sn)	/* ?? */
				xm += 0x8;
			break;
80
		case FPU_CSR_RD:	/* toward -Infinity */
R
Ralf Baechle 已提交
81
			if (sn) /* ?? */
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
				xm += 0x8;
			break;
		}
	}
	return xm;
}


/* generate a normal/denormal number with over,under handling
 * sn is sign
 * xe is an unbiased exponent
 * xm is 3bit extended precision value.
 */
95
union ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
L
Linus Torvalds 已提交
96 97 98
{
	assert(xm);		/* we don't gen exact zeros (probably should) */

99
	assert((xm >> (DP_FBITS + 1 + 3)) == 0);	/* no execess */
L
Linus Torvalds 已提交
100 101 102 103 104 105 106
	assert(xm & (DP_HIDDEN_BIT << 3));

	if (xe < DP_EMIN) {
		/* strip lower bits */
		int es = DP_EMIN - xe;

		if (ieee754_csr.nod) {
107 108
			ieee754_setcx(IEEE754_UNDERFLOW);
			ieee754_setcx(IEEE754_INEXACT);
L
Linus Torvalds 已提交
109 110

			switch(ieee754_csr.rm) {
111 112
			case FPU_CSR_RN:
			case FPU_CSR_RZ:
L
Linus Torvalds 已提交
113
				return ieee754dp_zero(sn);
114
			case FPU_CSR_RU:    /* toward +Infinity */
115
				if (sn == 0)
L
Linus Torvalds 已提交
116 117 118
					return ieee754dp_min(0);
				else
					return ieee754dp_zero(1);
119
			case FPU_CSR_RD:    /* toward -Infinity */
120
				if (sn == 0)
L
Linus Torvalds 已提交
121 122 123 124 125 126
					return ieee754dp_zero(0);
				else
					return ieee754dp_min(1);
			}
		}

127 128
		if (xe == DP_EMIN - 1 &&
		    ieee754dp_get_rounding(sn, xm) >> (DP_FBITS + 1 + 3))
L
Linus Torvalds 已提交
129 130
		{
			/* Not tiny after rounding */
131
			ieee754_setcx(IEEE754_INEXACT);
132
			xm = ieee754dp_get_rounding(sn, xm);
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
			xm >>= 1;
			/* Clear grs bits */
			xm &= ~(DP_MBIT(3) - 1);
			xe++;
		}
		else {
			/* sticky right shift es bits
			 */
			xm = XDPSRS(xm, es);
			xe += es;
			assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
			assert(xe == DP_EMIN);
		}
	}
	if (xm & (DP_MBIT(3) - 1)) {
148
		ieee754_setcx(IEEE754_INEXACT);
L
Linus Torvalds 已提交
149
		if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
150
			ieee754_setcx(IEEE754_UNDERFLOW);
L
Linus Torvalds 已提交
151 152 153 154
		}

		/* inexact must round of 3 bits
		 */
155
		xm = ieee754dp_get_rounding(sn, xm);
L
Linus Torvalds 已提交
156 157
		/* adjust exponent for rounding add overflowing
		 */
158
		if (xm >> (DP_FBITS + 3 + 1)) {
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166
			/* add causes mantissa overflow */
			xm >>= 1;
			xe++;
		}
	}
	/* strip grs bits */
	xm >>= 3;

167
	assert((xm >> (DP_FBITS + 1)) == 0);	/* no execess */
L
Linus Torvalds 已提交
168 169 170
	assert(xe >= DP_EMIN);

	if (xe > DP_EMAX) {
171 172
		ieee754_setcx(IEEE754_OVERFLOW);
		ieee754_setcx(IEEE754_INEXACT);
L
Linus Torvalds 已提交
173 174
		/* -O can be table indexed by (rm,sn) */
		switch (ieee754_csr.rm) {
175
		case FPU_CSR_RN:
L
Linus Torvalds 已提交
176
			return ieee754dp_inf(sn);
177
		case FPU_CSR_RZ:
L
Linus Torvalds 已提交
178
			return ieee754dp_max(sn);
179
		case FPU_CSR_RU:	/* toward +Infinity */
L
Linus Torvalds 已提交
180 181 182 183
			if (sn == 0)
				return ieee754dp_inf(0);
			else
				return ieee754dp_max(1);
184
		case FPU_CSR_RD:	/* toward -Infinity */
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196
			if (sn == 0)
				return ieee754dp_max(0);
			else
				return ieee754dp_inf(1);
		}
	}
	/* gen norm/denorm/zero */

	if ((xm & DP_HIDDEN_BIT) == 0) {
		/* we underflow (tiny/zero) */
		assert(xe == DP_EMIN);
		if (ieee754_csr.mx & IEEE754_UNDERFLOW)
197
			ieee754_setcx(IEEE754_UNDERFLOW);
L
Linus Torvalds 已提交
198 199
		return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
	} else {
200
		assert((xm >> (DP_FBITS + 1)) == 0);	/* no execess */
L
Linus Torvalds 已提交
201 202 203 204 205
		assert(xm & DP_HIDDEN_BIT);

		return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
	}
}