2005-04-16 22:20:36 +00:00
|
|
|
/* IEEE754 floating point arithmetic
|
|
|
|
* single precision
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 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.,
|
2014-04-25 23:49:14 +00:00
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-04-15 22:47:59 +00:00
|
|
|
#include <linux/compiler.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include "ieee754sp.h"
|
|
|
|
|
2014-04-15 23:31:11 +00:00
|
|
|
int ieee754sp_class(union ieee754sp x)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
COMPXSP;
|
|
|
|
EXPLODEXSP;
|
|
|
|
return xc;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:25:57 +00:00
|
|
|
static inline int ieee754sp_isnan(union ieee754sp x)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-04-03 22:25:38 +00:00
|
|
|
return ieee754_class_nan(ieee754sp_class(x));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 22:40:42 +00:00
|
|
|
static inline int ieee754sp_issnan(union ieee754sp x)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-11-13 00:47:28 +00:00
|
|
|
int qbit;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
assert(ieee754sp_isnan(x));
|
2015-11-13 00:47:28 +00:00
|
|
|
qbit = (SPMANT(x) & SP_MBIT(SP_FBITS - 1)) == SP_MBIT(SP_FBITS - 1);
|
|
|
|
return ieee754_csr.nan2008 ^ qbit;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-03 22:25:34 +00:00
|
|
|
/*
|
|
|
|
* Raise the Invalid Operation IEEE 754 exception
|
|
|
|
* and convert the signaling NaN supplied to a quiet NaN.
|
|
|
|
*/
|
2014-04-25 01:19:57 +00:00
|
|
|
union ieee754sp __cold ieee754sp_nanxcpt(union ieee754sp r)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-04-03 22:25:34 +00:00
|
|
|
assert(ieee754sp_issnan(r));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-04-03 22:25:34 +00:00
|
|
|
ieee754_setcx(IEEE754_INVALID_OPERATION);
|
2015-11-13 00:48:48 +00:00
|
|
|
if (ieee754_csr.nan2008) {
|
2015-11-13 00:47:28 +00:00
|
|
|
SPMANT(r) |= SP_MBIT(SP_FBITS - 1);
|
2015-11-13 00:48:48 +00:00
|
|
|
} else {
|
|
|
|
SPMANT(r) &= ~SP_MBIT(SP_FBITS - 1);
|
|
|
|
if (!ieee754sp_isnan(r))
|
|
|
|
SPMANT(r) |= SP_MBIT(SP_FBITS - 2);
|
|
|
|
}
|
2015-11-13 00:47:28 +00:00
|
|
|
|
|
|
|
return r;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 08:54:06 +00:00
|
|
|
static unsigned ieee754sp_get_rounding(int sn, unsigned xm)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
/* inexact must round of 3 bits
|
|
|
|
*/
|
|
|
|
if (xm & (SP_MBIT(3) - 1)) {
|
|
|
|
switch (ieee754_csr.rm) {
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RZ:
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RN:
|
2005-04-16 22:20:36 +00:00
|
|
|
xm += 0x3 + ((xm >> 3) & 1);
|
|
|
|
/* xm += (xm&0x8)?0x4:0x3 */
|
|
|
|
break;
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RU: /* toward +Infinity */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!sn) /* ?? */
|
|
|
|
xm += 0x8;
|
|
|
|
break;
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RD: /* toward -Infinity */
|
2013-01-22 11:59:30 +00:00
|
|
|
if (sn) /* ?? */
|
2005-04-16 22:20:36 +00:00
|
|
|
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.
|
|
|
|
*/
|
2014-04-15 23:31:11 +00:00
|
|
|
union ieee754sp ieee754sp_format(int sn, int xe, unsigned xm)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
assert(xm); /* we don't gen exact zeros (probably should) */
|
|
|
|
|
2016-02-25 08:44:58 +00:00
|
|
|
assert((xm >> (SP_FBITS + 1 + 3)) == 0); /* no excess */
|
2005-04-16 22:20:36 +00:00
|
|
|
assert(xm & (SP_HIDDEN_BIT << 3));
|
|
|
|
|
|
|
|
if (xe < SP_EMIN) {
|
|
|
|
/* strip lower bits */
|
|
|
|
int es = SP_EMIN - xe;
|
|
|
|
|
|
|
|
if (ieee754_csr.nod) {
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_UNDERFLOW);
|
|
|
|
ieee754_setcx(IEEE754_INEXACT);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
switch(ieee754_csr.rm) {
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RN:
|
|
|
|
case FPU_CSR_RZ:
|
2005-04-16 22:20:36 +00:00
|
|
|
return ieee754sp_zero(sn);
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RU: /* toward +Infinity */
|
2014-04-16 09:00:12 +00:00
|
|
|
if (sn == 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return ieee754sp_min(0);
|
|
|
|
else
|
|
|
|
return ieee754sp_zero(1);
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RD: /* toward -Infinity */
|
2014-04-16 09:00:12 +00:00
|
|
|
if (sn == 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return ieee754sp_zero(0);
|
|
|
|
else
|
|
|
|
return ieee754sp_min(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 08:54:06 +00:00
|
|
|
if (xe == SP_EMIN - 1 &&
|
|
|
|
ieee754sp_get_rounding(sn, xm) >> (SP_FBITS + 1 + 3))
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
/* Not tiny after rounding */
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_INEXACT);
|
2014-04-25 08:54:06 +00:00
|
|
|
xm = ieee754sp_get_rounding(sn, xm);
|
2005-04-16 22:20:36 +00:00
|
|
|
xm >>= 1;
|
|
|
|
/* Clear grs bits */
|
|
|
|
xm &= ~(SP_MBIT(3) - 1);
|
|
|
|
xe++;
|
2014-04-16 09:00:12 +00:00
|
|
|
} else {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* sticky right shift es bits
|
|
|
|
*/
|
2016-04-21 13:04:54 +00:00
|
|
|
xm = XSPSRS(xm, es);
|
|
|
|
xe += es;
|
2005-04-16 22:20:36 +00:00
|
|
|
assert((xm & (SP_HIDDEN_BIT << 3)) == 0);
|
|
|
|
assert(xe == SP_EMIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xm & (SP_MBIT(3) - 1)) {
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_INEXACT);
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((xm & (SP_HIDDEN_BIT << 3)) == 0) {
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_UNDERFLOW);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* inexact must round of 3 bits
|
|
|
|
*/
|
2014-04-25 08:54:06 +00:00
|
|
|
xm = ieee754sp_get_rounding(sn, xm);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* adjust exponent for rounding add overflowing
|
|
|
|
*/
|
2014-04-22 13:51:55 +00:00
|
|
|
if (xm >> (SP_FBITS + 1 + 3)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* add causes mantissa overflow */
|
|
|
|
xm >>= 1;
|
|
|
|
xe++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* strip grs bits */
|
|
|
|
xm >>= 3;
|
|
|
|
|
2016-02-25 08:44:58 +00:00
|
|
|
assert((xm >> (SP_FBITS + 1)) == 0); /* no excess */
|
2005-04-16 22:20:36 +00:00
|
|
|
assert(xe >= SP_EMIN);
|
|
|
|
|
|
|
|
if (xe > SP_EMAX) {
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_OVERFLOW);
|
|
|
|
ieee754_setcx(IEEE754_INEXACT);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* -O can be table indexed by (rm,sn) */
|
|
|
|
switch (ieee754_csr.rm) {
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RN:
|
2005-04-16 22:20:36 +00:00
|
|
|
return ieee754sp_inf(sn);
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RZ:
|
2005-04-16 22:20:36 +00:00
|
|
|
return ieee754sp_max(sn);
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RU: /* toward +Infinity */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (sn == 0)
|
|
|
|
return ieee754sp_inf(0);
|
|
|
|
else
|
|
|
|
return ieee754sp_max(1);
|
2014-04-30 09:21:55 +00:00
|
|
|
case FPU_CSR_RD: /* toward -Infinity */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (sn == 0)
|
|
|
|
return ieee754sp_max(0);
|
|
|
|
else
|
|
|
|
return ieee754sp_inf(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* gen norm/denorm/zero */
|
|
|
|
|
|
|
|
if ((xm & SP_HIDDEN_BIT) == 0) {
|
|
|
|
/* we underflow (tiny/zero) */
|
|
|
|
assert(xe == SP_EMIN);
|
|
|
|
if (ieee754_csr.mx & IEEE754_UNDERFLOW)
|
2014-04-18 22:36:32 +00:00
|
|
|
ieee754_setcx(IEEE754_UNDERFLOW);
|
2005-04-16 22:20:36 +00:00
|
|
|
return buildsp(sn, SP_EMIN - 1 + SP_EBIAS, xm);
|
|
|
|
} else {
|
2016-02-25 08:44:58 +00:00
|
|
|
assert((xm >> (SP_FBITS + 1)) == 0); /* no excess */
|
2005-04-16 22:20:36 +00:00
|
|
|
assert(xm & SP_HIDDEN_BIT);
|
|
|
|
|
|
|
|
return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT);
|
|
|
|
}
|
|
|
|
}
|